]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte_os/tests/test_time/fosa_test_time.c
9aebc13796ff79f3f295733bd9f0a4687a488285
[frescor/fosa.git] / src_marte_os / tests / test_time / fosa_test_time.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
36 //  FOSA is free software; you can redistribute it and/or modify it
37 //  under terms of the GNU General Public License as published by the
38 //  Free Software Foundation; either version 2, or (at your option) any
39 //  later version.  FOSA is distributed in the hope that it will be
40 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 //  General Public License for more details. You should have received a
43 //  copy of the GNU General Public License along with FOSA; see file
44 //  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 //  Cambridge, MA 02139, USA.
46 //
47 //  As a special exception, including FOSA header files in a file,
48 //  instantiating FOSA generics or templates, or linking other files
49 //  with FOSA objects to produce an executable application, does not
50 //  by itself cause the resulting executable application to be covered
51 //  by the GNU General Public License. This exception does not
52 //  however invalidate any other reasons why the executable file might be
53 //  covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55 #include "fosa.h"
56 #include <assert.h>
57 #include <time.h>   /* for timespec */
58 #include <stdio.h>  /* for printf */
59 #include <string.h> /* for memcmp */
60
61 static int check_exact_abs_msec(fosa_abs_time_t abs_time, long msec);
62 static int check_exact_rel_msec(fosa_rel_time_t rel_time, long msec);
63
64 int main()
65 {
66
67     struct timespec tspec1 = {1, 500000000};
68     struct timespec tspec2 = {0, 400000000};
69     struct timespec tspec_result;
70
71     fosa_rel_time_t rel_time1 = fosa_msec_to_rel_time(1500);
72     fosa_rel_time_t rel_time2 = fosa_msec_to_rel_time(400);
73     fosa_rel_time_t rel_time_aux = fosa_msec_to_rel_time(1500);
74     fosa_rel_time_t rel_time_result;
75
76     fosa_abs_time_t abs_time1 = fosa_msec_to_abs_time(1500);
77     fosa_abs_time_t abs_time2 = fosa_msec_to_abs_time(400);
78     fosa_abs_time_t abs_time_aux = fosa_msec_to_abs_time(1500);
79     fosa_abs_time_t abs_time_result;
80
81     long numeric;
82     bool test;
83
84     /* fosa_abs_time_incr */
85     abs_time_result = fosa_abs_time_incr(abs_time1, rel_time2);
86     assert( check_exact_abs_msec(abs_time_result, 1900) );
87
88     /* fosa_abs_time_decr */
89     abs_time_result = fosa_abs_time_decr(abs_time1, rel_time2);
90     assert( check_exact_abs_msec(abs_time_result, 1100) );
91
92     /* fosa_abs_time_decr (negative) */
93     abs_time_result = fosa_abs_time_decr(abs_time2, rel_time1);
94 //    assert( check_exact_abs_msec(abs_time_result, -1100) );
95
96     /* fosa_abs_time_extract_interval */
97     rel_time_result = fosa_abs_time_extract_interval(abs_time2, abs_time1);
98     assert( check_exact_rel_msec(rel_time_result, 1100) );
99
100     /* fosa_abs_time_extract_interval (negative) */
101     rel_time_result = fosa_abs_time_extract_interval(abs_time1, abs_time2);
102 //    assert( check_exact_rel_msec(rel_time_result, -1100) );
103
104     /* fosa_rel_time_add */
105     rel_time_result = fosa_rel_time_add(rel_time1, rel_time2);
106     assert( check_exact_rel_msec(rel_time_result, 1900) );
107     
108     /* fosa_rel_time_decr */
109     rel_time_result = fosa_rel_time_decr(rel_time1, rel_time2);
110     assert( check_exact_rel_msec(rel_time_result, 1100) );
111     
112     /* fosa_rel_time_decr (negative) */
113     rel_time_result = fosa_rel_time_decr(rel_time2, rel_time1);
114 //    assert( check_exact_rel_msec(rel_time_result, 1100) );
115
116 /*
117     struct timespec t1 = {0, 876352172};
118     fosa_rel_time_t t1_rel = fosa_timespec_to_rel_time(t1);
119     long factor = 573102543;
120     struct timespec product = {502239658, 336773396};
121     fosa_rel_time_t product_rel = fosa_timespec_to_rel_time(product);
122 */
123
124     struct timespec t1 = {0, 876352172};
125     struct timespec product = {50223965,  570771688};
126     long factor = 57310254;
127     struct timespec product_divided_by_17 = {2954350, 915927746};  // inexact division
128     struct timespec product_divided_by_3 = {16741321, 856923896};  // exact division
129
130     fosa_rel_time_t t1_rel = fosa_timespec_to_rel_time(t1);
131     fosa_rel_time_t product_rel = fosa_timespec_to_rel_time(product);
132     fosa_rel_time_t product_17_rel = fosa_timespec_to_rel_time(product_divided_by_17);
133     fosa_rel_time_t product_3_rel = fosa_timespec_to_rel_time(product_divided_by_3);
134
135     /* fosa_rel_time_times_integer */
136     /* fosa_rel_time_divided_by_integer */
137     rel_time_result = fosa_rel_time_times_integer(rel_time2, 4);
138     assert(  check_exact_rel_msec(rel_time_result, 1600) );
139
140     rel_time_result = fosa_rel_time_divided_by_integer(rel_time_result, 4);
141     assert(  check_exact_rel_msec(rel_time_result, 400) );
142
143     rel_time_result = fosa_rel_time_times_integer(t1_rel, factor);
144     assert(  fosa_rel_time_equal(rel_time_result, product_rel) );
145
146     rel_time_result = fosa_rel_time_divided_by_integer(product_rel, factor);
147     assert(  fosa_rel_time_equal(rel_time_result, t1_rel) );
148     
149     rel_time_result = fosa_rel_time_divided_by_integer(product_rel, 17);
150     assert(  fosa_rel_time_equal(rel_time_result, product_17_rel) );
151     
152     rel_time_result = fosa_rel_time_divided_by_integer(product_rel, 3);
153     assert(  fosa_rel_time_equal(rel_time_result, product_3_rel) );
154     
155     /* fosa_abs_time_smaller */
156     test = fosa_abs_time_smaller(abs_time1, abs_time2);
157     assert(test == false);
158
159     test = fosa_abs_time_smaller(abs_time1, abs_time_aux);
160     assert(test == false);
161
162     test = fosa_abs_time_smaller(abs_time2, abs_time1);
163     assert(test);
164
165     /* fosa_rel_time_smaller */
166     test = fosa_rel_time_smaller(rel_time1, rel_time2);
167     assert(test == false);
168
169     test = fosa_rel_time_smaller(rel_time1, rel_time_aux);
170     assert(test == false);
171
172     test = fosa_rel_time_smaller(rel_time2, rel_time1);
173     assert(test);
174
175     /* fosa_abs_time_smaller_or_equal */
176     test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time2);
177     assert(test == false);
178
179     test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time_aux);
180     assert(test);
181
182     test = fosa_abs_time_smaller_or_equal(abs_time2, abs_time1);
183     assert(test);
184
185     /* fosa_rel_time_smaller_or_equal */
186     test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time2);
187     assert(test == false);
188
189     test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time_aux);
190     assert(test);
191
192     test = fosa_rel_time_smaller_or_equal(rel_time2, rel_time1);
193     assert(test);
194
195     /* fosa_msec_to_rel_time */
196     rel_time_result = fosa_msec_to_rel_time(1850);
197     assert( check_exact_rel_msec(rel_time_result, 1850) );
198
199     /* fosa_rel_time_to_msec */
200     numeric = fosa_rel_time_to_msec(rel_time1);
201     assert(numeric == 1500);
202
203     /* fosa_msec_to_abs_time */
204     abs_time_result = fosa_msec_to_rel_time(1320);
205     assert( check_exact_abs_msec(abs_time_result, 1320) );
206
207     /* fosa_abs_time_to_msec */
208     numeric = fosa_abs_time_to_msec(abs_time2);
209     assert(numeric == 400);
210
211     /* fosa_usec_to_rel_time */
212     rel_time_result = fosa_usec_to_rel_time(1850000);
213     assert( check_exact_rel_msec(rel_time_result, 1850) );
214
215     /* fosa_rel_time_to_usec */
216     numeric = fosa_rel_time_to_usec(rel_time1);
217     assert(numeric == 1500000);
218
219     /* fosa_msec_to_abs_time */
220     abs_time_result = fosa_usec_to_rel_time(1320000);
221     assert( check_exact_abs_msec(abs_time_result, 1320) );
222
223     /* fosa_abs_time_to_msec */
224     numeric = fosa_abs_time_to_usec(abs_time2);
225     assert(numeric == 400000);
226
227     /* fosa_timespec_to_rel_time */
228     struct timespec tspec_aux = {1, 750000000};
229     rel_time_result = fosa_timespec_to_rel_time(tspec_aux);
230     assert( check_exact_rel_msec(rel_time_result, 1750) );
231
232     /* fosa_rel_time_to_timespec */
233     tspec_result = fosa_rel_time_to_timespec(rel_time1);
234     test = memcmp(&tspec_result, &tspec1, sizeof(tspec_result) );
235     assert(test == 0);
236
237     /* fosa_timespec_to_abs_time */
238     struct timespec tspec_aux_extra = {1, 200000000};
239     abs_time_result = fosa_timespec_to_abs_time(tspec_aux_extra);
240     assert( check_exact_abs_msec(abs_time_result, 1200) );
241
242     /* fosa_abs_time_to_timespec */
243     tspec_result = fosa_abs_time_to_timespec(abs_time2);
244     test = memcmp(&tspec_result, &tspec2, sizeof(tspec_result) );
245     assert(test == 0);
246
247
248
249     printf("End of test OK!!\n");
250
251     return 0;
252 }
253
254 // -------------------------------------------------------------
255
256 static int check_exact_abs_msec(fosa_abs_time_t abs_time, long msec)
257 {
258     fosa_abs_time_t time_check;
259     int cmp = -1;
260
261     time_check = fosa_msec_to_abs_time(msec);
262     cmp = memcmp(&time_check, &abs_time, sizeof(time_check) );
263
264     if (cmp == 0)
265     {
266         return 1;
267     }
268     else
269     {
270         return 0;
271     }
272 }
273
274 // -------------------------------------------------------------
275
276 static int check_exact_rel_msec(fosa_rel_time_t rel_time, long msec)
277 {
278     fosa_rel_time_t time_check;
279     int cmp = -1;
280
281     time_check = fosa_msec_to_rel_time(msec);
282     cmp = memcmp(&time_check, &rel_time, sizeof(time_check) );
283
284     if (cmp == 0)
285     {
286         return 1;
287     }
288     else
289     {
290         return 0;
291     }
292 }
293