]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_time/fosa_test_time.c
c335402037ee1bb3fa8add37f4ce5bcca6bfb76b
[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     /* fosa_abs_time_smaller */
114     test = fosa_abs_time_smaller(abs_time1, abs_time2);
115     assert(test == false);
116
117     test = fosa_abs_time_smaller(abs_time1, abs_time_aux);
118     assert(test == false);
119
120     test = fosa_abs_time_smaller(abs_time2, abs_time1);
121     assert(test);
122
123     /* fosa_rel_time_smaller */
124     test = fosa_rel_time_smaller(rel_time1, rel_time2);
125     assert(test == false);
126
127     test = fosa_rel_time_smaller(rel_time1, rel_time_aux);
128     assert(test == false);
129
130     test = fosa_rel_time_smaller(rel_time2, rel_time1);
131     assert(test);
132
133     /* fosa_abs_time_smaller_or_equal */
134     test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time2);
135     assert(test == false);
136
137     test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time_aux);
138     assert(test);
139
140     test = fosa_abs_time_smaller_or_equal(abs_time2, abs_time1);
141     assert(test);
142
143     /* fosa_rel_time_smaller_or_equal */
144     test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time2);
145     assert(test == false);
146
147     test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time_aux);
148     assert(test);
149
150     test = fosa_rel_time_smaller_or_equal(rel_time2, rel_time1);
151     assert(test);
152
153     /* fosa_msec_to_rel_time */
154     rel_time_result = fosa_msec_to_rel_time(1850);
155     assert( check_exact_rel_msec(rel_time_result, 1850) );
156
157     /* fosa_rel_time_to_msec */
158     numeric = fosa_rel_time_to_msec(rel_time1);
159     assert(numeric == 1500);
160
161     /* fosa_msec_to_abs_time */
162     abs_time_result = fosa_msec_to_rel_time(1320);
163     assert( check_exact_abs_msec(abs_time_result, 1320) );
164
165     /* fosa_abs_time_to_msec */
166     numeric = fosa_abs_time_to_msec(abs_time2);
167     assert(numeric == 400);
168
169     /* fosa_usec_to_rel_time */
170     rel_time_result = fosa_usec_to_rel_time(1850000);
171     assert( check_exact_rel_msec(rel_time_result, 1850) );
172
173     /* fosa_rel_time_to_usec */
174     numeric = fosa_rel_time_to_usec(rel_time1);
175     assert(numeric == 1500000);
176
177     /* fosa_msec_to_abs_time */
178     abs_time_result = fosa_usec_to_rel_time(1320000);
179     assert( check_exact_abs_msec(abs_time_result, 1320) );
180
181     /* fosa_abs_time_to_msec */
182     numeric = fosa_abs_time_to_usec(abs_time2);
183     assert(numeric == 400000);
184
185     /* fosa_timespec_to_rel_time */
186     struct timespec tspec_aux = {1, 750000000};
187     rel_time_result = fosa_timespec_to_rel_time(tspec_aux);
188     assert( check_exact_rel_msec(rel_time_result, 1750) );
189
190     /* fosa_rel_time_to_timespec */
191     tspec_result = fosa_rel_time_to_timespec(rel_time1);
192     test = memcmp(&tspec_result, &tspec1, sizeof(tspec_result) );
193     assert(test == 0);
194
195     /* fosa_timespec_to_abs_time */
196     struct timespec tspec_aux_extra = {1, 200000000};
197     abs_time_result = fosa_timespec_to_abs_time(tspec_aux_extra);
198     assert( check_exact_abs_msec(abs_time_result, 1200) );
199
200     /* fosa_abs_time_to_timespec */
201     tspec_result = fosa_abs_time_to_timespec(abs_time2);
202     test = memcmp(&tspec_result, &tspec2, sizeof(tspec_result) );
203     assert(test == 0);
204
205     printf("End of test OK!!\n");
206
207     return 0;
208 }
209
210 // -------------------------------------------------------------
211
212 static int check_exact_abs_msec(fosa_abs_time_t abs_time, long msec)
213 {
214     fosa_abs_time_t time_check;
215     int cmp = -1;
216
217     time_check = fosa_msec_to_abs_time(msec);
218     cmp = memcmp(&time_check, &abs_time, sizeof(time_check) );
219
220     if (cmp == 0)
221     {
222         return 1;
223     }
224     else
225     {
226         return 0;
227     }
228 }
229
230 // -------------------------------------------------------------
231
232 static int check_exact_rel_msec(fosa_rel_time_t rel_time, long msec)
233 {
234     fosa_rel_time_t time_check;
235     int cmp = -1;
236
237     time_check = fosa_msec_to_rel_time(msec);
238     cmp = memcmp(&time_check, &rel_time, sizeof(time_check) );
239
240     if (cmp == 0)
241     {
242         return 1;
243     }
244     else
245     {
246         return 0;
247     }
248 }
249