Use a static header file, not dependent on autoconf.
[libserialport/gsi.git] / test_timing.c
blobc08badbf6a4500f672d0e4e1169d4ec5539e50d5
1 #include "config.h"
2 #include "libserialport.h"
3 #include "libserialport_internal.h"
4 #include <assert.h>
5 #include <unistd.h>
7 int main(int argc, char *argv[])
9 (void) argc;
10 (void) argv;
11 struct time a, b, c;
12 struct timeval tv;
13 struct timeout to;
14 printf("Testing arithmetic\n");
15 time_set_ms(&a, 10050);
16 time_set_ms(&b, 100);
17 assert(time_greater(&a, &b));
18 assert(!time_greater(&b, &a));
19 time_add(&a, &b, &c);
20 assert(time_as_ms(&c) == 10150);
21 time_sub(&a, &b, &c);
22 assert(time_as_ms(&c) == 9950);
23 time_as_timeval(&a, &tv);
24 assert(tv.tv_sec == 10);
25 assert(tv.tv_usec == 50000);
26 time_get(&a);
27 printf("Sleeping for 1s\n");
28 sleep(1);
29 time_get(&b);
30 time_sub(&b, &a, &c);
31 printf("Measured: %ums\n", time_as_ms(&c));
32 assert(time_as_ms(&c) >= 999);
33 assert(time_as_ms(&c) <= 1001);
34 printf("Starting 3s timeout\n");
35 timeout_start(&to, 3000);
36 printf("Time to wait: %dms\n", timeout_remaining_ms(&to));
37 printf("Sleeping for 1s\n");
38 sleep(1);
39 timeout_update(&to);
40 assert(!timeout_check(&to));
41 printf("Sleeping for 1s\n");
42 sleep(1);
43 timeout_update(&to);
44 assert(!timeout_check(&to));
45 printf("Remaining: %ums\n", timeout_remaining_ms(&to));
46 printf("Sleeping for 1s\n");
47 sleep(1);
48 timeout_update(&to);
49 assert(timeout_check(&to));
50 printf("Timeout expired\n");
51 printf("Starting 2s timeout\n");
52 timeout_start(&to, 2000);
53 printf("Limiting steps to 1s\n");
54 timeout_limit(&to, 1000);
55 printf("Time to wait: %ums\n", timeout_remaining_ms(&to));
56 printf("Sleeping for 1s\n");
57 sleep(1);
58 timeout_update(&to);
59 assert(!timeout_check(&to));
60 printf("Remaining: %ums\n", timeout_remaining_ms(&to));
61 printf("Sleeping for 1s\n");
62 sleep(1);
63 timeout_update(&to);
64 assert(timeout_check(&to));
65 printf("Timeout expired\n");
66 return 0;