Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:49

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Copyright (c) 2012.
0005  *  Krzysztof Miesowicz <krzysztof.miesowicz@gmail.com>
0006  *  
0007  *  COPYRIGHT (c) 1989-2012.
0008  *  On-Line Applications Research Corporation (OAR).
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  */
0031 
0032 #include <tmacros.h>
0033 #include "test_support.h"
0034 #include <rtems/timespec.h>
0035 #include <rtems/score/todimpl.h>
0036 
0037 const char rtems_test_name[] = "SPTIMESPEC 1";
0038 
0039 /* forward declarations to avoid warnings */
0040 rtems_task Init(rtems_task_argument argument);
0041 void test_add(void);
0042 void test_divide(void);
0043 void test_divide_by_integer(void);
0044 void test_compare(void);
0045 void test_validity(void);
0046 void test_subtract(void);
0047 void test_convert(void);
0048 
0049 struct timespec *timespec1;
0050 struct timespec *timespec2;
0051 struct timespec *tpointer;
0052 struct timespec ts1;
0053 struct timespec ts2;
0054 struct timespec notvalid={-20,-50}; /* set very invalid timespec */
0055 bool result;
0056 
0057 rtems_task Init(
0058   rtems_task_argument argument
0059 )
0060 {
0061   timespec1=&ts1;
0062   timespec2=&ts2;
0063 
0064   TEST_BEGIN();
0065 
0066   test_add();
0067   test_divide();
0068   test_divide_by_integer();
0069   test_compare();
0070   test_validity();
0071   test_subtract();
0072   test_convert();
0073 
0074   TEST_END();
0075 
0076   rtems_test_exit(0);
0077 }
0078 
0079 void test_add(){
0080 /* Basic add_to test. tv_nsec in result is in range 
0081  * (less than TOD_NANOSECONDS_PER_SECOND) */
0082   rtems_timespec_set(timespec1, 13, 300);
0083   rtems_timespec_set(timespec2, 26, 5000);
0084   rtems_timespec_add_to(timespec1, timespec2);
0085   rtems_test_assert( (timespec1->tv_sec==39)&&(timespec1->tv_nsec==5300) );
0086 /* Another case. tv_nsec in result is greater than TOD_NANOSECONDS_PER_SECOND */
0087   rtems_timespec_set(timespec1, 13, (TOD_NANOSECONDS_PER_SECOND-300));
0088   rtems_timespec_set(timespec2, 26, 5000);
0089   rtems_timespec_add_to(timespec1, timespec2);
0090   rtems_test_assert( (timespec1->tv_sec==40)&&(timespec1->tv_nsec==4700) );
0091 
0092   puts( "\n rtems_timespec_add_to test PASSED " );
0093 }
0094 
0095 void test_divide(){
0096 /* RTEMS_TIMESPEC_DIVIDE TEST -PART1- divide by non-zero timespec */
0097   uint32_t ival_percentage, fval_percentage;
0098   rtems_timespec_set(timespec1, 20, 5000);
0099   rtems_timespec_set(timespec2, 61, 5000);
0100   rtems_timespec_divide(
0101     timespec1,
0102     timespec2,
0103     &ival_percentage,
0104     &fval_percentage
0105   );
0106   rtems_test_assert( (ival_percentage==32) || (fval_percentage=786) );
0107   puts( " rtems_timespace_divide test -part1- divide by non-zero PASSED" );
0108 
0109 
0110 /* RTEMS_TIMESPEC_DIVIDE TEST -PART2- divide by zero */
0111   rtems_timespec_zero(timespec2);
0112   rtems_timespec_divide(
0113     timespec1,
0114     timespec2, 
0115     &ival_percentage, 
0116     &fval_percentage
0117   );
0118   rtems_test_assert( (ival_percentage==0) && (fval_percentage==0) );
0119   puts( " rtems_timespace_divide test -part2- divide by zero PASSED" );
0120 }
0121 
0122 void test_divide_by_integer(){
0123 /* RTEMS_TIMESPEC_DIVIDE_BY_INTEGER TEST - simple divide */
0124   rtems_timespec_set(timespec1, 40, 4700);
0125   rtems_timespec_divide_by_integer(timespec1, 30, timespec2);
0126   rtems_test_assert( ((timespec2->tv_sec)==1) &&
0127             ((timespec2->tv_nsec)==333333490) );
0128   puts( " rtems_timespec_divide_by_integer test PASSED" );
0129 
0130 }
0131 
0132 void test_compare(){
0133 /* Each case hit another single branch in timespeclessthan.c file */
0134 /* 
0135  * Basic check if timespec1 is less than timespec2. Timespec1 has lower number
0136  * of either seconds and nanoseconds.
0137  */
0138   rtems_timespec_set(timespec1,2,300);
0139   rtems_timespec_set(timespec2,3,400);
0140   result = rtems_timespec_less_than(timespec1,timespec2);
0141   rtems_test_assert(result);
0142 /*
0143  * Another check if timespec1 is less. Now number of seconds are equal in both
0144  * timespec1 and timespec2. It hits another branch in rtems_timespec_less_than
0145  * method.
0146  */
0147   rtems_timespec_set(timespec2,2,400);
0148   result = rtems_timespec_less_than(timespec1,timespec2);
0149   rtems_test_assert(result);
0150 /*
0151  * Another check if timespec1 is less. In this case timespecs are equal so it
0152  * should return false.
0153  */
0154   rtems_timespec_set(timespec2,2,300);
0155   result = rtems_timespec_less_than(timespec1,timespec2);
0156   rtems_test_assert(!result);
0157 /*
0158  * Check if timespec1 is less. Both timespecs are equal on seconds, but 
0159  * timespec2 has smaller number of nanoseconds.
0160  */
0161   rtems_timespec_set(timespec2,2,0);
0162   result = rtems_timespec_less_than(timespec1,timespec2);
0163   rtems_test_assert(!result);
0164 /* In this case timespec2 is smaller in both seconds and nanoseconds. */
0165   rtems_timespec_set(timespec2,1,400);
0166   result = rtems_timespec_less_than(timespec1,timespec2);
0167   rtems_test_assert(!result);
0168 
0169   puts( " rtems_timespec_less_than PASSED ");
0170 }
0171 
0172 void test_validity(){
0173 /* Each case hits another branch in timespecisvalid.c src file */
0174   /* #1 INVALID - pointer to timespec is null */
0175   result=rtems_timespec_is_valid(tpointer); 
0176   rtems_test_assert(!result);
0177 
0178   tpointer=&notvalid; /* set pointer to invalid timespec */
0179 
0180   /* #2 INVALID- tv_sec of timespec is less than 0 */
0181   result=rtems_timespec_is_valid(tpointer);
0182   rtems_test_assert(!result);
0183   /* #3 INVALID- tv_nsec of timespec is less than 0 */
0184   notvalid.tv_sec=20; /* correct seconds */
0185   result=rtems_timespec_is_valid(tpointer);
0186   rtems_test_assert(!result);
0187   /* #4 INVALID- tv_nsec is not in the range - positive but too large */
0188   notvalid.tv_nsec=TOD_NANOSECONDS_PER_SECOND+10; 
0189   result=rtems_timespec_is_valid(tpointer);
0190   rtems_test_assert(!result);
0191   /* #5 VALID- valid timespec */
0192   rtems_timespec_set(tpointer,13, 500);
0193   result=rtems_timespec_is_valid(tpointer);
0194   rtems_test_assert(result);
0195 
0196   puts(" rtems_timespec_is_valid test PASSED");
0197 }
0198 
0199 void test_subtract(){
0200 /* 
0201  * Simple subtraction, number of nanoseconds in timespec2 is less than in 
0202  * timespec1.
0203  */
0204   rtems_timespec_set(timespec1,10, 800);
0205   rtems_timespec_set(timespec2,13, 500);
0206   rtems_timespec_subtract(timespec1,timespec2,tpointer);
0207   rtems_test_assert( (tpointer->tv_sec==2) && 
0208       (tpointer->tv_nsec==(TOD_NANOSECONDS_PER_SECOND-300)) );
0209 /* 
0210  * Simple subtraction, number of nanoseconds in timespec2 is greater than in 
0211  * timespec1. It hits another branch.
0212  */ 
0213   rtems_timespec_set(timespec1,10,200);
0214   rtems_timespec_subtract(timespec1,timespec2,tpointer);
0215   rtems_test_assert( (tpointer->tv_sec==3) && 
0216       (tpointer->tv_nsec==300) );
0217 /* 
0218  * Case when timespec2 (end) is less than timespec1 (start). It produce
0219  * negative result.
0220  */
0221   rtems_timespec_set(timespec1,13,600);
0222   rtems_timespec_subtract(timespec1,timespec2,tpointer);
0223   rtems_test_assert( (tpointer->tv_sec==(-1)) && \
0224       (tpointer->tv_nsec==999999900) );
0225   puts(" rtems_timespec_subtract test PASSED");
0226 }
0227 
0228 void test_convert(){
0229 /* RTEMS_TIMESPEC_FROM_TICKS TEST - basic conversions */
0230   uint32_t ticks=0;
0231   rtems_timespec_from_ticks(ticks, timespec1);
0232   rtems_test_assert( ((timespec1->tv_sec)==0) && ((timespec1->tv_nsec)==0) );
0233   ticks=1008;
0234   rtems_timespec_from_ticks(ticks, timespec2);
0235   puts( " rtems_timespec_from_ticks PASSED ");
0236 
0237 /* RTEMS_TIMESPEC_TO_TICKS TEST - basic conversion - inverse to above ones */
0238   ticks = rtems_timespec_to_ticks(timespec1);
0239   rtems_test_assert(ticks==0);
0240   ticks = rtems_timespec_to_ticks(timespec2);
0241   rtems_test_assert(ticks==1008);
0242 /* 
0243  * RTEMS_TIMESPEC_TO_TICKS TEST - test case when tv_nsec of timespec isn't 
0244  * multiplicity of nanoseconds_per_tick. Due to that, calculated number of ticks
0245  * should be increased by one.
0246  */
0247   uint32_t nanoseconds_per_tick;
0248   uint32_t nanoseconds;
0249   nanoseconds_per_tick  = rtems_configuration_get_nanoseconds_per_tick();
0250   nanoseconds = timespec2->tv_nsec;
0251   if( (nanoseconds % nanoseconds_per_tick)==0 )
0252     nanoseconds += 1;
0253   rtems_timespec_set(timespec1,timespec2->tv_sec,nanoseconds);
0254   ticks = rtems_timespec_to_ticks(timespec1);
0255   rtems_test_assert(ticks==1009);
0256   puts( " rtems_timespec_from_ticks and rtems_timespec_to_ticks test PASSED");
0257 }
0258 
0259 
0260 /* configuration information */
0261 
0262 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0263 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0264 
0265 #define CONFIGURE_MAXIMUM_TASKS             1
0266 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0267 
0268 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0269 
0270 #define CONFIGURE_INIT
0271 
0272 #include <rtems/confdefs.h>
0273 /* end of file */