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