File indexing completed on 2025-05-11 08:24:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
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
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};
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
0081
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
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
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
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
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
0134
0135
0136
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
0144
0145
0146
0147 rtems_timespec_set(timespec2,2,400);
0148 result = rtems_timespec_less_than(timespec1,timespec2);
0149 rtems_test_assert(result);
0150
0151
0152
0153
0154 rtems_timespec_set(timespec2,2,300);
0155 result = rtems_timespec_less_than(timespec1,timespec2);
0156 rtems_test_assert(!result);
0157
0158
0159
0160
0161 rtems_timespec_set(timespec2,2,0);
0162 result = rtems_timespec_less_than(timespec1,timespec2);
0163 rtems_test_assert(!result);
0164
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
0174
0175 result=rtems_timespec_is_valid(tpointer);
0176 rtems_test_assert(!result);
0177
0178 tpointer=¬valid;
0179
0180
0181 result=rtems_timespec_is_valid(tpointer);
0182 rtems_test_assert(!result);
0183
0184 notvalid.tv_sec=20;
0185 result=rtems_timespec_is_valid(tpointer);
0186 rtems_test_assert(!result);
0187
0188 notvalid.tv_nsec=TOD_NANOSECONDS_PER_SECOND+10;
0189 result=rtems_timespec_is_valid(tpointer);
0190 rtems_test_assert(!result);
0191
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
0202
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
0211
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
0219
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
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
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
0244
0245
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
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