Branch data Line data Source code
1 : : #include "test_librational_all.h"
2 : : #include "testmocking.h"
3 : :
4 : : /**
5 : : * @brief Convert a struct timeval into integer milliseconds
6 : : *
7 : : * @param value Timeval whose seconds and microseconds are folded into a single ms count
8 : : * @return Milliseconds since the same epoch the input refers to
9 : : */
10 : 2 : static long long int timeval_to_ms(const struct timeval *const value)
11 : : {
12 : 2 : return((long long int)value->tv_sec * 1000LL + (long long int)value->tv_usec / 1000LL);
13 : : }
14 : :
15 : : /**
16 : : * @brief Convert a struct timespec into integer nanoseconds
17 : : *
18 : : * @param value Timespec whose seconds and nanoseconds are folded into a single ns count
19 : : * @return Nanoseconds since the same epoch the input refers to
20 : : */
21 : 2 : static long long int timespec_to_ns(const struct timespec *const value)
22 : : {
23 : 2 : return((long long int)value->tv_sec * 1000000000LL + (long long int)value->tv_nsec);
24 : : }
25 : :
26 : : /**
27 : : * @brief Check that a value falls within a tolerated window around a range
28 : : *
29 : : * @details Returns true when value lies in [lower_bound - tolerance, upper_bound + tolerance].
30 : : * The signed addition is safe for the wall-clock numbers exercised by this suite
31 : : * (microseconds and nanoseconds since the Unix epoch sit far below LLONG_MAX),
32 : : * but the helper is not suitable for arbitrary near-LLONG_MAX inputs
33 : : *
34 : : * @param value Observed sample
35 : : * @param lower_bound Lower edge of the expected range
36 : : * @param upper_bound Upper edge of the expected range
37 : : * @param tolerance Slack added on each side of the range
38 : : * @return `true` when @p value is inside the tolerated window
39 : : */
40 : 3 : static bool inside_tolerated_range(
41 : : const long long int value,
42 : : const long long int lower_bound,
43 : : const long long int upper_bound,
44 : : const long long int tolerance)
45 : : {
46 [ + - + - ]: 3 : return(value + tolerance >= lower_bound && value <= upper_bound + tolerance);
47 : : }
48 : :
49 : : /**
50 : : * @brief Check that cur_time_ms() returns wall-clock milliseconds
51 : : *
52 : : * @details Wraps the call between two gettimeofday() samples and verifies
53 : : * that the observed value falls within the bracketed range with a tolerance
54 : : * suitable for adjustable realtime clocks
55 : : *
56 : : * @return Return describing success or failure
57 : : */
58 : 1 : static Return test_librational_0003_1(void)
59 : : {
60 : 1 : INITTEST;
61 : :
62 : 1 : const long long int tolerance_ms = 1000LL;
63 : 1 : struct timeval before = {0};
64 : 1 : struct timeval after = {0};
65 : :
66 : : /* Compare cur_time_ms() with gettimeofday() around the call */
67 : 1 : ASSERT(0 == gettimeofday(&before,NULL));
68 : 1 : const long long int observed_ms = cur_time_ms();
69 : 1 : ASSERT(0 == gettimeofday(&after,NULL));
70 : :
71 : 1 : const long long int lower_ms = timeval_to_ms(&before);
72 : 1 : const long long int upper_ms = timeval_to_ms(&after);
73 : :
74 : 1 : ASSERT(observed_ms > 0LL);
75 : 1 : ASSERT(inside_tolerated_range(observed_ms,lower_ms,upper_ms,tolerance_ms));
76 : :
77 : 1 : RETURN_STATUS;
78 : : }
79 : :
80 : : /**
81 : : * @brief Check that cur_time_ns() returns wall-clock nanoseconds
82 : : *
83 : : * @details Wraps the call between two clock_gettime(CLOCK_REALTIME) samples
84 : : * and verifies that the observed value falls within the bracketed range with
85 : : * a tolerance suitable for adjustable realtime clocks
86 : : *
87 : : * @return Return describing success or failure
88 : : */
89 : 1 : static Return test_librational_0003_2(void)
90 : : {
91 : 1 : INITTEST;
92 : :
93 : 1 : const long long int tolerance_ns = 1000000000LL;
94 : 1 : struct timespec before = {0};
95 : 1 : struct timespec after = {0};
96 : :
97 : : /* Compare cur_time_ns() with CLOCK_REALTIME around the call */
98 : 1 : ASSERT(0 == clock_gettime(CLOCK_REALTIME,&before));
99 : 1 : const long long int observed_ns = cur_time_ns();
100 : 1 : ASSERT(0 == clock_gettime(CLOCK_REALTIME,&after));
101 : :
102 : 1 : const long long int lower_ns = timespec_to_ns(&before);
103 : 1 : const long long int upper_ns = timespec_to_ns(&after);
104 : :
105 : 1 : ASSERT(observed_ns > 0LL);
106 : 1 : ASSERT(inside_tolerated_range(observed_ns,lower_ns,upper_ns,tolerance_ns));
107 : :
108 : 1 : RETURN_STATUS;
109 : : }
110 : :
111 : : /**
112 : : * @brief Check that cur_time_ms() and cur_time_ns() share the same epoch
113 : : *
114 : : * @details Brackets a cur_time_ns() sample with two cur_time_ms() calls and
115 : : * verifies that the nanosecond reading, converted to milliseconds, lands
116 : : * inside the bracketed range. This protects against silent drift between
117 : : * the two helpers if their clock sources ever diverge
118 : : *
119 : : * @return Return describing success or failure
120 : : */
121 : 1 : static Return test_librational_0003_3(void)
122 : : {
123 : 1 : INITTEST;
124 : :
125 : : /* Both helpers map to CLOCK_REALTIME under the hood, so their numerical drift is
126 : : on the order of nanoseconds. The 1-second tolerance is therefore not a numerical
127 : : slack but a safety margin against a wall-clock adjustment (NTP step, manual set)
128 : : landing exactly between the three samples taken below */
129 : 1 : const long long int tolerance_ms = 1000LL;
130 : :
131 : : /* cur_time_ns() should represent the same wall-clock epoch as cur_time_ms() */
132 : 1 : const long long int before_ms = cur_time_ms();
133 : 1 : const long long int observed_ns = cur_time_ns();
134 : 1 : const long long int after_ms = cur_time_ms();
135 : 1 : const long long int observed_ns_as_ms = observed_ns / 1000000LL;
136 : :
137 : 1 : ASSERT(before_ms > 0LL);
138 : 1 : ASSERT(observed_ns > 0LL);
139 : 1 : ASSERT(after_ms > 0LL);
140 : 1 : ASSERT(inside_tolerated_range(observed_ns_as_ms,before_ms,after_ms,tolerance_ms));
141 : :
142 : 1 : RETURN_STATUS;
143 : : }
144 : :
145 : : /**
146 : : * @brief Check the complete duration decomposition through form_date_r()
147 : : *
148 : : * @details Uses one carefully constructed nanosecond value that exercises every
149 : : * non-zero unit (years, months, weeks, days, hours, minutes, seconds,
150 : : * milliseconds, microseconds, nanoseconds) in a single FULL_VIEW string. Also
151 : : * verifies pointer equality with the caller-provided buffer and that the
152 : : * static-buffer wrapper form_date() yields the same text
153 : : *
154 : : * @return Return describing success or failure
155 : : */
156 : 1 : static Return test_librational_0003_4(void)
157 : : {
158 : 1 : INITTEST;
159 : :
160 : 1 : const long long int full_date_ns = 339800645368118513LL;
161 : 1 : const char expected_full_date[] = "10y 9mon 1w 2d 3h 4min 5s 368ms 118μs 513ns";
162 : 1 : char date_buffer[MAX_CHARACTERS] = {0};
163 : :
164 : : /* Verify a complete decomposition through both public formatting variants */
165 : 1 : const char *formatted_date = form_date_r(
166 : : full_date_ns,
167 : : FULL_VIEW,
168 : : date_buffer,
169 : : sizeof(date_buffer));
170 : :
171 : 1 : ASSERT(formatted_date == date_buffer);
172 : 1 : ASSERT(0 == strcmp(date_buffer,expected_full_date));
173 : 1 : ASSERT(0 == strcmp(form_date(full_date_ns,FULL_VIEW),expected_full_date));
174 : :
175 : 1 : RETURN_STATUS;
176 : : }
177 : :
178 : : /**
179 : : * @brief Check MAJOR_VIEW unit selection across the full unit ladder
180 : : *
181 : : * @details Feeds form_date() exactly one nanosecond count per unit and confirms
182 : : * that MAJOR_VIEW renders only the matching unit. Covers every branch of the
183 : : * if-else chain in form_date_r() so any future regression in unit ordering
184 : : * is caught here rather than at higher-level usage sites
185 : : *
186 : : * @return Return describing success or failure
187 : : */
188 : 1 : static Return test_librational_0003_5(void)
189 : : {
190 : 1 : INITTEST;
191 : :
192 : 1 : const long long int ns_in_year = 31536000000000000LL;
193 : 1 : const long long int ns_in_month = 2628000000000000LL;
194 : 1 : const long long int ns_in_week = 604800000000000LL;
195 : 1 : const long long int ns_in_day = 86400000000000LL;
196 : 1 : const long long int ns_in_hour = 3600000000000LL;
197 : 1 : const long long int ns_in_minute = 60000000000LL;
198 : 1 : const long long int ns_in_second = 1000000000LL;
199 : 1 : const long long int ns_in_millisecond = 1000000LL;
200 : 1 : const long long int ns_in_microsecond = 1000LL;
201 : :
202 : : /* Exercise every branch of the MAJOR_VIEW decision chain so unit ordering regressions surface here */
203 : 1 : ASSERT(0 == strcmp(form_date(ns_in_year,MAJOR_VIEW),"1y"));
204 : 1 : ASSERT(0 == strcmp(form_date(ns_in_month,MAJOR_VIEW),"1mon"));
205 : 1 : ASSERT(0 == strcmp(form_date(ns_in_week,MAJOR_VIEW),"1w"));
206 : 1 : ASSERT(0 == strcmp(form_date(ns_in_day,MAJOR_VIEW),"1d"));
207 : 1 : ASSERT(0 == strcmp(form_date(ns_in_hour,MAJOR_VIEW),"1h"));
208 : 1 : ASSERT(0 == strcmp(form_date(ns_in_minute,MAJOR_VIEW),"1min"));
209 : 1 : ASSERT(0 == strcmp(form_date(ns_in_second,MAJOR_VIEW),"1s"));
210 : 1 : ASSERT(0 == strcmp(form_date(ns_in_millisecond,MAJOR_VIEW),"1ms"));
211 : 1 : ASSERT(0 == strcmp(form_date(ns_in_microsecond,MAJOR_VIEW),"1μs"));
212 : 1 : ASSERT(0 == strcmp(form_date(1LL,MAJOR_VIEW),"1ns"));
213 : :
214 : 1 : RETURN_STATUS;
215 : : }
216 : :
217 : : #ifndef EVIL_EMPIRE_OS
218 : : /**
219 : : * @brief Check that form_date_r() tolerates a failing snprintf
220 : : *
221 : : * @details Arms the link-time snprintf mock to return -1 for the next call,
222 : : * invokes form_date_r() and verifies that the caller buffer is left as an
223 : : * empty terminated string. Evil Empire OS builds exclude this subtest because
224 : : * the mocks rely on GNU ld --wrap
225 : : *
226 : : * @return Return describing success or failure
227 : : */
228 : 1 : static Return test_librational_0003_6(void)
229 : : {
230 : 1 : INITTEST;
231 : :
232 : : /* The link-time snprintf mock used below relies on GNU ld --wrap */
233 : :
234 : 1 : char date_buffer[MAX_CHARACTERS] = {0};
235 : :
236 : : /* A failed snprintf inside catdate_r() leaves the caller buffer empty */
237 : 1 : testmocking_snprintf_fail_next(1);
238 : 1 : const char *formatted_date = form_date_r(
239 : : 1000LL,
240 : : FULL_VIEW,
241 : : date_buffer,
242 : : sizeof(date_buffer));
243 : 1 : testmocking_snprintf_disable();
244 : :
245 : 1 : ASSERT(formatted_date == date_buffer);
246 : 1 : ASSERT(date_buffer[0] == '\0');
247 : :
248 : 1 : RETURN_STATUS;
249 : : }
250 : :
251 : : /**
252 : : * @brief Check that form_date_r() keeps truncated buffers terminated
253 : : *
254 : : * @details Arms the link-time snprintf mock to report truncation for the next
255 : : * call. The mock writes a sentinel 'T' at buffer[0] and reports a length that
256 : : * forces the production code onto the truncation path. The assertions verify
257 : : * that the destination is filled up to its last byte and remains NUL
258 : : * terminated. Evil Empire OS builds exclude this subtest because the mocks
259 : : * rely on GNU ld --wrap
260 : : *
261 : : * @return Return describing success or failure
262 : : */
263 : 1 : static Return test_librational_0003_7(void)
264 : : {
265 : 1 : INITTEST;
266 : :
267 : : /* The link-time snprintf mock used below relies on GNU ld --wrap */
268 : :
269 : 1 : char date_buffer[8] = {0};
270 : :
271 : : /* Truncation marks the destination as full and preserves NUL termination */
272 : 1 : testmocking_snprintf_truncate_next(1);
273 : 1 : const char *formatted_date = form_date_r(
274 : : 1001LL,
275 : : FULL_VIEW,
276 : : date_buffer,
277 : : sizeof(date_buffer));
278 : 1 : testmocking_snprintf_disable();
279 : :
280 : 1 : ASSERT(formatted_date == date_buffer);
281 : 1 : ASSERT(date_buffer[0] == 'T');
282 : 1 : ASSERT(date_buffer[sizeof(date_buffer) - 1U] == '\0');
283 : :
284 : 1 : RETURN_STATUS;
285 : : }
286 : : #endif
287 : :
288 : : /**
289 : : * @brief Check that cur_time_monotonic_ns() returns ordered samples around a measurable interval
290 : : *
291 : : * @details Two calls bracket a short nanosleep(). The interval must be strictly
292 : : * positive (rules out a stuck clock or a wrong unit) and bounded above by a
293 : : * generous ceiling that tolerates loaded CI runners without becoming a flaky
294 : : * test. On platforms without CLOCK_MONOTONIC, cur_time_monotonic_ns() falls
295 : : * back to cur_time_ns(), so this test checks the public helper contract rather
296 : : * than proving that a monotonic clock source exists. Positivity of the absolute
297 : : * values protects against accidental zero initialization
298 : : *
299 : : * @return Return describing success or failure
300 : : */
301 : 1 : static Return test_librational_0003_8(void)
302 : : {
303 : 1 : INITTEST;
304 : :
305 : : /* A measurable sleep between samples lets the test assert a strictly positive interval */
306 : 1 : const long long int before_ns = cur_time_monotonic_ns();
307 : 1 : const struct timespec sleep_request = { .tv_sec = 0,.tv_nsec = 1000000LL };
308 : 1 : (void)nanosleep(&sleep_request,NULL);
309 : 1 : const long long int after_ns = cur_time_monotonic_ns();
310 : :
311 : 1 : ASSERT(before_ns > 0LL);
312 : 1 : ASSERT(after_ns > 0LL);
313 : :
314 : 1 : const long long int delta_ns = after_ns - before_ns;
315 : :
316 : : /* Upper bound is intentionally generous (10 seconds) to keep the test reliable under load */
317 : 1 : ASSERT(delta_ns > 0LL);
318 : 1 : ASSERT(delta_ns < 10000000000LL);
319 : :
320 : 1 : RETURN_STATUS;
321 : : }
322 : :
323 : : /**
324 : : * @brief Check seconds_to_ISOdate() shape, character classes and static-buffer overwrite behavior
325 : : *
326 : : * @details Verifies that the returned text has the fixed
327 : : * "YYYY-MM-DD HH:MM:SS" shape, that every position the format reserves for a
328 : : * decimal digit actually contains a digit, that the separator characters land
329 : : * on the documented positions, that the function returns a stable static buffer
330 : : * (the same pointer on consecutive calls), and that a later call overwrites the
331 : : * same storage with text for the new input
332 : : *
333 : : * @return Return describing success or failure
334 : : */
335 : 1 : static Return test_librational_0003_9(void)
336 : : {
337 : 1 : INITTEST;
338 : :
339 : 1 : const char *iso_date = seconds_to_ISOdate(0);
340 : : char first_iso_date[sizeof "1970-01-01 00:00:00"];
341 : :
342 : : /* seconds_to_ISOdate() returns a stable static buffer in YYYY-MM-DD HH:MM:SS shape */
343 : 1 : ASSERT(iso_date != NULL);
344 : 1 : ASSERT(strlen(iso_date) == strlen("1970-01-01 00:00:00"));
345 : 1 : memcpy(first_iso_date,iso_date,sizeof(first_iso_date));
346 : 1 : ASSERT(iso_date[4] == '-');
347 : 1 : ASSERT(iso_date[7] == '-');
348 : 1 : ASSERT(iso_date[10] == ' ');
349 : 1 : ASSERT(iso_date[13] == ':');
350 : 1 : ASSERT(iso_date[16] == ':');
351 : :
352 : : /* Positions reserved for decimal digits in YYYY-MM-DD HH:MM:SS must hold a digit.
353 : : The indices below are tied to the exact format string used by seconds_to_ISOdate()
354 : : (currently "%Y-%m-%d %H:%M:%S") — any change to that strftime() format must be
355 : : mirrored here, otherwise this assertion will either miss new digit slots or
356 : : wrongly flag separators */
357 : 1 : const size_t digit_positions[] = {0U,1U,2U,3U,5U,6U,8U,9U,11U,12U,14U,15U,17U,18U};
358 : :
359 [ + + ]: 15 : for(size_t i = 0U; i < sizeof(digit_positions) / sizeof(digit_positions[0]); i++)
360 : : {
361 : 14 : const char ch = iso_date[digit_positions[i]];
362 : 14 : ASSERT(ch >= '0' && ch <= '9');
363 : : }
364 : :
365 : : /* The documented stable static buffer must be reused across consecutive calls */
366 : 1 : const char *iso_date_again = seconds_to_ISOdate(86400);
367 : 1 : ASSERT(iso_date_again == iso_date);
368 : 1 : ASSERT(0 != strcmp(iso_date_again,first_iso_date));
369 : :
370 : 1 : RETURN_STATUS;
371 : : }
372 : :
373 : : /**
374 : : * @brief Check that form_date_r() rejects invalid output buffers
375 : : *
376 : : * @details Covers the public validation contract for the reentrant duration
377 : : * formatter: a NULL destination or zero-sized destination is rejected with a
378 : : * NULL return value
379 : : *
380 : : * @return Return describing success or failure
381 : : */
382 : 1 : static Return test_librational_0003_10(void)
383 : : {
384 : 1 : INITTEST;
385 : :
386 : 1 : char date_buffer[MAX_CHARACTERS] = "unchanged";
387 : :
388 : : /* Invalid output buffer arguments must be rejected before formatting starts */
389 : 1 : ASSERT(NULL == form_date_r(1LL,FULL_VIEW,NULL,sizeof(date_buffer)));
390 : 1 : ASSERT(NULL == form_date_r(1LL,FULL_VIEW,date_buffer,0U));
391 : 1 : ASSERT(0 == strcmp(date_buffer,"unchanged"));
392 : :
393 : 1 : RETURN_STATUS;
394 : : }
395 : :
396 : : /**
397 : : * @brief Check that form_date_r() returns an empty string for negative input
398 : : *
399 : : * @details The library does not currently document a meaningful rendering for
400 : : * negative nanosecond counts. asadate() produces non-positive components for
401 : : * negative inputs, catdate_r() skips non-positive numbers, and the early
402 : : * zero-handling branch only matches exactly nanoseconds == 0. The test pins
403 : : * the current behaviour (empty terminated string for both FULL_VIEW and
404 : : * MAJOR_VIEW) so any unintended change is detected immediately
405 : : *
406 : : * @return Return describing success or failure
407 : : */
408 : 1 : static Return test_librational_0003_11(void)
409 : : {
410 : 1 : INITTEST;
411 : :
412 : : char date_buffer[MAX_CHARACTERS];
413 : :
414 : : /* Sentinel fill verifies that form_date_r() actually wrote the terminator on the negative-input path */
415 : 1 : memset(date_buffer,'X',sizeof(date_buffer));
416 : 1 : const char *formatted_full = form_date_r(-1LL,FULL_VIEW,date_buffer,sizeof(date_buffer));
417 : 1 : ASSERT(formatted_full == date_buffer);
418 : 1 : ASSERT(date_buffer[0] == '\0');
419 : :
420 : 1 : memset(date_buffer,'Y',sizeof(date_buffer));
421 : 1 : const char *formatted_major = form_date_r(-1LL,MAJOR_VIEW,date_buffer,sizeof(date_buffer));
422 : 1 : ASSERT(formatted_major == date_buffer);
423 : 1 : ASSERT(date_buffer[0] == '\0');
424 : :
425 : 1 : RETURN_STATUS;
426 : : }
427 : :
428 : : /**
429 : : * @brief Run librational time and duration helper tests
430 : : *
431 : : * The suite verifies the time-related public API of librational. It checks
432 : : * that cur_time_ms() and cur_time_ns() return positive epoch values, use the
433 : : * documented time units, and agree with each other within a tolerance
434 : : * suitable for wall-clock calls; that cur_time_monotonic_ns() or its fallback
435 : : * returns ordered values over a measurable interval; that form_date() and
436 : : * form_date_r() decompose nanoseconds into a human-readable string, select
437 : : * the largest unit in MAJOR_VIEW, reject invalid output buffers, and survive
438 : : * snprintf failures and truncation injected through the link-time mocks; that
439 : : * seconds_to_ISOdate() returns a fixed-width ISO-like timestamp from a stable
440 : : * static buffer and overwrites that buffer on later calls; and that
441 : : * form_date_r() leaves the destination empty for negative input
442 : : *
443 : : * Covered API surface:
444 : : * - cur_time_ms(), cur_time_ns(), cur_time_monotonic_ns()
445 : : * - form_date(), form_date_r()
446 : : * - seconds_to_ISOdate()
447 : : *
448 : : * @return Return describing success or failure
449 : : */
450 : 1 : Return test_librational_0003(void)
451 : : {
452 : 1 : INITTEST;
453 : :
454 : 1 : TEST(test_librational_0003_1,"cur_time_ms() returns current epoch milliseconds");
455 : 1 : TEST(test_librational_0003_2,"cur_time_ns() returns current epoch nanoseconds");
456 : 1 : TEST(test_librational_0003_3,"cur_time_ms() and cur_time_ns() use the same wall-clock epoch");
457 : 1 : TEST(test_librational_0003_4,"form_date_r() formats a complete duration decomposition");
458 : 1 : TEST(test_librational_0003_5,"form_date() selects the largest requested duration unit");
459 : : #ifndef EVIL_EMPIRE_OS
460 : 1 : TEST(test_librational_0003_6,"form_date_r() tolerates snprintf failure inside duration assembly");
461 : 1 : TEST(test_librational_0003_7,"form_date_r() keeps truncated duration buffers terminated");
462 : : #endif
463 : 1 : TEST(test_librational_0003_8,"cur_time_monotonic_ns() returns ordered nanosecond samples");
464 : 1 : TEST(test_librational_0003_9,"seconds_to_ISOdate() returns and overwrites a fixed-width static timestamp");
465 : 1 : TEST(test_librational_0003_10,"form_date_r() rejects invalid output buffers");
466 : 1 : TEST(test_librational_0003_11,"form_date_r() returns an empty string for negative input");
467 : :
468 : 1 : RETURN_STATUS;
469 : : }
|