GCC Code Coverage Report


Directory: ./
File: tests/unit/00_support/mytime_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 103 103 100.0%
Functions: 1 1 100.0%
Branches: 73 146 50.0%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "support/mytime.h"
21
22 #include <string.h>
23 #include <unistd.h>
24 #include <assert.h>
25 #include <stdlib.h>
26
27 1 int main(int argc, char **argv) {
28
29 struct timeval tv0, tv1;
30 struct timespec result, time0, time1, time2;
31 1 reset(&time0);
32 1 get_time_coarse(&time1);
33 1 get_time(&time2);
34
35
36 /* diff_time */
37 1 time0 = (struct timespec){.tv_sec=1, .tv_nsec=1};
38 1 time1 = (struct timespec){.tv_sec=0, .tv_nsec=0};
39
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == -1 );
40
41 1 time0 = (struct timespec){.tv_sec=1, .tv_nsec=1};
42 1 time1 = (struct timespec){.tv_sec=0, .tv_nsec=0};
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == -1 );
44
45 1 time0 = (struct timespec){.tv_sec=1, .tv_nsec=1};
46 1 time1 = (struct timespec){.tv_sec=1, .tv_nsec=0};
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == -1 );
48
49 1 time0 = (struct timespec){.tv_sec=1, .tv_nsec=0};
50 1 time1 = (struct timespec){.tv_sec=1, .tv_nsec=0};
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
52
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 0 && result.tv_nsec == 0 );
53
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > -0.1 && to_secs(result) < 0.1 );
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 0 );
55
56 1 time0 = (struct timespec){.tv_sec=1, .tv_nsec=0};
57 1 time1 = (struct timespec){.tv_sec=1, .tv_nsec=1};
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
59
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 0 && result.tv_nsec == 1 );
60
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > 0.0 && to_secs(result) < 0.2 );
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 1 );
62
63 1 time0 = (struct timespec){.tv_sec=42, .tv_nsec=0};
64 1 time1 = (struct timespec){.tv_sec=52, .tv_nsec=0};
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
66
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 10 && result.tv_nsec == 0 );
67
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > 9.9 && to_secs(result) < 10.1 );
68
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 10000000000LL );
69
70 1 time0 = (struct timespec){.tv_sec=42, .tv_nsec=500};
71 1 time1 = (struct timespec){.tv_sec=52, .tv_nsec=500};
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
73
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 10 && result.tv_nsec == 0 );
74
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > 9.9 && to_secs(result) < 10.1 );
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 10000000000LL );
76
77 1 time0 = (struct timespec){.tv_sec=42, .tv_nsec=5000};
78 1 time1 = (struct timespec){.tv_sec=52, .tv_nsec=500};
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
80
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 9 && result.tv_nsec == 999995500L );
81
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > 9.9 && to_secs(result) < 10.1 );
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 9999995500LL );
83
84 1 time0 = (struct timespec){.tv_sec=50042, .tv_nsec=5000};
85 1 time1 = (struct timespec){.tv_sec=50052, .tv_nsec=500};
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( diff_time(time0, time1, &result) == 0 );
87
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( to_secs(result) > 9.9 && to_secs(result) < 10.1 );
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( to_nsecs(&result) == 9999995500LL );
89
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 9 && result.tv_nsec == 999995500L );
90
91 /* add_time */
92 1 time0 = (struct timespec){.tv_sec=0, .tv_nsec=0};
93 1 add_time(time0, time0, &result);
94
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 0 && result.tv_nsec == 0 );
95
96 1 time0 = (struct timespec){.tv_sec=0, .tv_nsec=500000000};
97 1 add_time(time0, time0, &result);
98
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 1 && result.tv_nsec == 0 );
99
100 /* mult_time */
101 1 time0 = (struct timespec){.tv_sec=0, .tv_nsec=0};
102 1 mult_time(time0, 2, &result);
103
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 0 && result.tv_nsec == 0 );
104
105 1 time0 = (struct timespec){.tv_sec=5, .tv_nsec=555555555};
106 1 mult_time(time0, 5, &result);
107
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 27 && result.tv_nsec == 777777775 );
108
109 /* timeval_diff */
110 1 tv0 = (struct timeval){.tv_sec=0, .tv_usec=0};
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( timeval_diff(&tv0, &tv0) == 0 );
112
113 1 tv0 = (struct timeval){.tv_sec=0, .tv_usec=500};
114 1 tv1 = (struct timeval){.tv_sec=5, .tv_usec=50};
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( timeval_diff(&tv0, &tv1) == 4999550 );
116
117 /* add_tv_to_ts */
118 1 tv0 = (struct timeval){.tv_sec=0, .tv_usec=10};
119 1 add_tv_to_ts(&tv0, &tv0, &result);
120
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 0 && result.tv_nsec == 20000 );
121
122 1 tv0 = (struct timeval){.tv_sec=1, .tv_usec=999999};
123 1 add_tv_to_ts(&tv0, &tv0, &result);
124
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert( result.tv_sec == 3 && result.tv_nsec == 999998000 );
125
126 /* ns_to_human */
127 1 char buf[16] = {0};
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(NULL, 42, 1); assert( strcmp(buf, "") == 0);
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 0, 1); assert( strcmp(buf, "") == 0);
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1); assert( strcmp(buf, "1 ns") == 0 );
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 11); assert( strcmp(buf, "11 ns") == 0 );
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 999); assert( strcmp(buf, "999 ns") == 0 );
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1000); assert( strcmp(buf, "1 us") == 0 );
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1009); assert( strcmp(buf, "1 us") == 0 );
135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1099); assert( strcmp(buf, "1.09 us") == 0 );
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 551099); assert( strcmp(buf, "551.09 us") == 0 );
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 999999); assert( strcmp(buf, "999.99 us") == 0 );
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1000000); assert( strcmp(buf, "1 ms") == 0 );
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 55200000); assert( strcmp(buf, "55.20 ms") == 0 );
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 555200000); assert( strcmp(buf, "555.20 ms") == 0 );
141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 999999999); assert( strcmp(buf, "999.99 ms") == 0 );
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1000000000); assert( strcmp(buf, "1 s") == 0 );
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1009999999); assert( strcmp(buf, "1 s") == 0 );
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1010000000); assert( strcmp(buf, "1.01 s") == 0 );
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, 1010000000); assert( strcmp(buf, "1.01 s") == 0 );
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ns_to_human(buf, 16, INT64_MAX); assert( strcmp(buf, "9223372036.85 s") == 0 );
147
148
149 /* formatted string tests*/
150
151 struct tm tm;
152
153 /* fill in values for 2024-07-15 05:14:13 */
154 1 tm.tm_year = 2024 - 1900;
155 1 tm.tm_mon = 7 - 1;
156 1 tm.tm_mday = 15;
157 1 tm.tm_hour = 5;
158 1 tm.tm_min = 14;
159 1 tm.tm_sec = 13;
160 1 tm.tm_isdst = -1;
161 1 char* iso8601 = get_iso_8601_string(&tm);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( strcmp(iso8601, "2024-07-15T05:14:13") == 0 );
163 1 free(iso8601);
164
165 1 return 0;
166 }
167