1 | /* |
---|
2 | * Copyright (c) 2012. |
---|
3 | * Krzysztof Miesowicz <krzysztof.miesowicz@gmail.com> |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2012. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.org/license/LICENSE. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <tmacros.h> |
---|
14 | #include "test_support.h" |
---|
15 | #include <rtems/timespec.h> |
---|
16 | #include <rtems/score/todimpl.h> |
---|
17 | |
---|
18 | const char rtems_test_name[] = "SPTIMESPEC 1"; |
---|
19 | |
---|
20 | /* forward declarations to avoid warnings */ |
---|
21 | rtems_task Init(rtems_task_argument argument); |
---|
22 | void test_add(void); |
---|
23 | void test_divide(void); |
---|
24 | void test_divide_by_integer(void); |
---|
25 | void test_compare(void); |
---|
26 | void test_validity(void); |
---|
27 | void test_subtract(void); |
---|
28 | void test_convert(void); |
---|
29 | |
---|
30 | struct timespec *timespec1; |
---|
31 | struct timespec *timespec2; |
---|
32 | struct timespec *tpointer; |
---|
33 | struct timespec ts1; |
---|
34 | struct timespec ts2; |
---|
35 | struct timespec notvalid={-20,-50}; /* set very invalid timespec */ |
---|
36 | bool result; |
---|
37 | |
---|
38 | rtems_task Init( |
---|
39 | rtems_task_argument argument |
---|
40 | ) |
---|
41 | { |
---|
42 | timespec1=&ts1; |
---|
43 | timespec2=&ts2; |
---|
44 | |
---|
45 | TEST_BEGIN(); |
---|
46 | |
---|
47 | test_add(); |
---|
48 | test_divide(); |
---|
49 | test_divide_by_integer(); |
---|
50 | test_compare(); |
---|
51 | test_validity(); |
---|
52 | test_subtract(); |
---|
53 | test_convert(); |
---|
54 | |
---|
55 | TEST_END(); |
---|
56 | |
---|
57 | rtems_test_exit(0); |
---|
58 | } |
---|
59 | |
---|
60 | void test_add(){ |
---|
61 | /* Basic add_to test. tv_nsec in result is in range |
---|
62 | * (less than TOD_NANOSECONDS_PER_SECOND) */ |
---|
63 | rtems_timespec_set(timespec1, 13, 300); |
---|
64 | rtems_timespec_set(timespec2, 26, 5000); |
---|
65 | rtems_timespec_add_to(timespec1, timespec2); |
---|
66 | rtems_test_assert( (timespec1->tv_sec==39)&&(timespec1->tv_nsec==5300) ); |
---|
67 | /* Another case. tv_nsec in result is greater than TOD_NANOSECONDS_PER_SECOND */ |
---|
68 | rtems_timespec_set(timespec1, 13, (TOD_NANOSECONDS_PER_SECOND-300)); |
---|
69 | rtems_timespec_set(timespec2, 26, 5000); |
---|
70 | rtems_timespec_add_to(timespec1, timespec2); |
---|
71 | rtems_test_assert( (timespec1->tv_sec==40)&&(timespec1->tv_nsec==4700) ); |
---|
72 | |
---|
73 | puts( "\n rtems_timespec_add_to test PASSED " ); |
---|
74 | } |
---|
75 | |
---|
76 | void test_divide(){ |
---|
77 | /* RTEMS_TIMESPEC_DIVIDE TEST -PART1- divide by non-zero timespec */ |
---|
78 | uint32_t ival_percentage, fval_percentage; |
---|
79 | rtems_timespec_set(timespec1, 20, 5000); |
---|
80 | rtems_timespec_set(timespec2, 61, 5000); |
---|
81 | rtems_timespec_divide( |
---|
82 | timespec1, |
---|
83 | timespec2, |
---|
84 | &ival_percentage, |
---|
85 | &fval_percentage |
---|
86 | ); |
---|
87 | rtems_test_assert( (ival_percentage==32) || (fval_percentage=786) ); |
---|
88 | puts( " rtems_timespace_divide test -part1- divide by non-zero PASSED" ); |
---|
89 | |
---|
90 | |
---|
91 | /* RTEMS_TIMESPEC_DIVIDE TEST -PART2- divide by zero */ |
---|
92 | rtems_timespec_zero(timespec2); |
---|
93 | rtems_timespec_divide( |
---|
94 | timespec1, |
---|
95 | timespec2, |
---|
96 | &ival_percentage, |
---|
97 | &fval_percentage |
---|
98 | ); |
---|
99 | rtems_test_assert( (ival_percentage==0) && (fval_percentage==0) ); |
---|
100 | puts( " rtems_timespace_divide test -part2- divide by zero PASSED" ); |
---|
101 | } |
---|
102 | |
---|
103 | void test_divide_by_integer(){ |
---|
104 | /* RTEMS_TIMESPEC_DIVIDE_BY_INTEGER TEST - simple divide */ |
---|
105 | rtems_timespec_set(timespec1, 40, 4700); |
---|
106 | rtems_timespec_divide_by_integer(timespec1, 30, timespec2); |
---|
107 | rtems_test_assert( ((timespec2->tv_sec)==1) && |
---|
108 | ((timespec2->tv_nsec)==333333490) ); |
---|
109 | puts( " rtems_timespec_divide_by_integer test PASSED" ); |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | void test_compare(){ |
---|
114 | /* Each case hit another single branch in timespeclessthan.c file */ |
---|
115 | /* |
---|
116 | * Basic check if timespec1 is less than timespec2. Timespec1 has lower number |
---|
117 | * of either seconds and nanoseconds. |
---|
118 | */ |
---|
119 | rtems_timespec_set(timespec1,2,300); |
---|
120 | rtems_timespec_set(timespec2,3,400); |
---|
121 | result = rtems_timespec_less_than(timespec1,timespec2); |
---|
122 | rtems_test_assert(result); |
---|
123 | /* |
---|
124 | * Another check if timespec1 is less. Now number of seconds are equal in both |
---|
125 | * timespec1 and timespec2. It hits another branch in rtems_timespec_less_than |
---|
126 | * method. |
---|
127 | */ |
---|
128 | rtems_timespec_set(timespec2,2,400); |
---|
129 | result = rtems_timespec_less_than(timespec1,timespec2); |
---|
130 | rtems_test_assert(result); |
---|
131 | /* |
---|
132 | * Another check if timespec1 is less. In this case timespecs are equal so it |
---|
133 | * should return false. |
---|
134 | */ |
---|
135 | rtems_timespec_set(timespec2,2,300); |
---|
136 | result = rtems_timespec_less_than(timespec1,timespec2); |
---|
137 | rtems_test_assert(!result); |
---|
138 | /* |
---|
139 | * Check if timespec1 is less. Both timespecs are equal on seconds, but |
---|
140 | * timespec2 has smaller number of nanoseconds. |
---|
141 | */ |
---|
142 | rtems_timespec_set(timespec2,2,0); |
---|
143 | result = rtems_timespec_less_than(timespec1,timespec2); |
---|
144 | rtems_test_assert(!result); |
---|
145 | /* In this case timespec2 is smaller in both seconds and nanoseconds. */ |
---|
146 | rtems_timespec_set(timespec2,1,400); |
---|
147 | result = rtems_timespec_less_than(timespec1,timespec2); |
---|
148 | rtems_test_assert(!result); |
---|
149 | |
---|
150 | puts( " rtems_timespec_less_than PASSED "); |
---|
151 | } |
---|
152 | |
---|
153 | void test_validity(){ |
---|
154 | /* Each case hits another branch in timespecisvalid.c src file */ |
---|
155 | /* #1 INVALID - pointer to timespec is null */ |
---|
156 | result=rtems_timespec_is_valid(tpointer); |
---|
157 | rtems_test_assert(!result); |
---|
158 | |
---|
159 | tpointer=¬valid; /* set pointer to invalid timespec */ |
---|
160 | |
---|
161 | /* #2 INVALID- tv_sec of timespec is less than 0 */ |
---|
162 | result=rtems_timespec_is_valid(tpointer); |
---|
163 | rtems_test_assert(!result); |
---|
164 | /* #3 INVALID- tv_nsec of timespec is less than 0 */ |
---|
165 | notvalid.tv_sec=20; /* correct seconds */ |
---|
166 | result=rtems_timespec_is_valid(tpointer); |
---|
167 | rtems_test_assert(!result); |
---|
168 | /* #4 INVALID- tv_nsec is not in the range - positive but too large */ |
---|
169 | notvalid.tv_nsec=TOD_NANOSECONDS_PER_SECOND+10; |
---|
170 | result=rtems_timespec_is_valid(tpointer); |
---|
171 | rtems_test_assert(!result); |
---|
172 | /* #5 VALID- valid timespec */ |
---|
173 | rtems_timespec_set(tpointer,13, 500); |
---|
174 | result=rtems_timespec_is_valid(tpointer); |
---|
175 | rtems_test_assert(result); |
---|
176 | |
---|
177 | puts(" rtems_timespec_is_valid test PASSED"); |
---|
178 | } |
---|
179 | |
---|
180 | void test_subtract(){ |
---|
181 | /* |
---|
182 | * Simple subtraction, number of nanoseconds in timespec2 is less than in |
---|
183 | * timespec1. |
---|
184 | */ |
---|
185 | rtems_timespec_set(timespec1,10, 800); |
---|
186 | rtems_timespec_set(timespec2,13, 500); |
---|
187 | rtems_timespec_subtract(timespec1,timespec2,tpointer); |
---|
188 | rtems_test_assert( (tpointer->tv_sec==2) && |
---|
189 | (tpointer->tv_nsec==(TOD_NANOSECONDS_PER_SECOND-300)) ); |
---|
190 | /* |
---|
191 | * Simple subtraction, number of nanoseconds in timespec2 is greater than in |
---|
192 | * timespec1. It hits another branch. |
---|
193 | */ |
---|
194 | rtems_timespec_set(timespec1,10,200); |
---|
195 | rtems_timespec_subtract(timespec1,timespec2,tpointer); |
---|
196 | rtems_test_assert( (tpointer->tv_sec==3) && |
---|
197 | (tpointer->tv_nsec==300) ); |
---|
198 | /* |
---|
199 | * Case when timespec2 (end) is less than timespec1 (start). It produce |
---|
200 | * negative result. |
---|
201 | */ |
---|
202 | rtems_timespec_set(timespec1,13,600); |
---|
203 | rtems_timespec_subtract(timespec1,timespec2,tpointer); |
---|
204 | rtems_test_assert( (tpointer->tv_sec==(-1)) && \ |
---|
205 | (tpointer->tv_nsec==999999900) ); |
---|
206 | puts(" rtems_timespec_subtract test PASSED"); |
---|
207 | } |
---|
208 | |
---|
209 | void test_convert(){ |
---|
210 | /* RTEMS_TIMESPEC_FROM_TICKS TEST - basic conversions */ |
---|
211 | uint32_t ticks=0; |
---|
212 | rtems_timespec_from_ticks(ticks, timespec1); |
---|
213 | rtems_test_assert( ((timespec1->tv_sec)==0) && ((timespec1->tv_nsec)==0) ); |
---|
214 | ticks=1008; |
---|
215 | rtems_timespec_from_ticks(ticks, timespec2); |
---|
216 | puts( " rtems_timespec_from_ticks PASSED "); |
---|
217 | |
---|
218 | /* RTEMS_TIMESPEC_TO_TICKS TEST - basic conversion - inverse to above ones */ |
---|
219 | ticks = rtems_timespec_to_ticks(timespec1); |
---|
220 | rtems_test_assert(ticks==0); |
---|
221 | ticks = rtems_timespec_to_ticks(timespec2); |
---|
222 | rtems_test_assert(ticks==1008); |
---|
223 | /* |
---|
224 | * RTEMS_TIMESPEC_TO_TICKS TEST - test case when tv_nsec of timespec isn't |
---|
225 | * multiplicity of nanoseconds_per_tick. Due to that, calculated number of ticks |
---|
226 | * should be increased by one. |
---|
227 | */ |
---|
228 | uint32_t nanoseconds_per_tick; |
---|
229 | uint32_t nanoseconds; |
---|
230 | nanoseconds_per_tick = rtems_configuration_get_nanoseconds_per_tick(); |
---|
231 | nanoseconds = timespec2->tv_nsec; |
---|
232 | if( (nanoseconds % nanoseconds_per_tick)==0 ) |
---|
233 | nanoseconds += 1; |
---|
234 | rtems_timespec_set(timespec1,timespec2->tv_sec,nanoseconds); |
---|
235 | ticks = rtems_timespec_to_ticks(timespec1); |
---|
236 | rtems_test_assert(ticks==1009); |
---|
237 | puts( " rtems_timespec_from_ticks and rtems_timespec_to_ticks test PASSED"); |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /* configuration information */ |
---|
242 | |
---|
243 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
244 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
245 | |
---|
246 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
247 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
248 | |
---|
249 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
250 | |
---|
251 | #define CONFIGURE_INIT |
---|
252 | |
---|
253 | #include <rtems/confdefs.h> |
---|
254 | /* end of file */ |
---|