libicmp/tests/utils/test_time.c
lohhiiccc 2e509336c7 feat(utils): add ICMP checksum and time utilities with tests
- Added `icmp_checksum`.
 - Added `icmp_get_time`.
 - Created unit tests for checksum and time.
 - Set `-D_POSIX_C_SOURCE=199309L` in CPPFLAGS for clock_gettime support.
2026-01-24 22:00:24 +01:00

44 lines
1.2 KiB
C

#include <criterion/criterion.h>
#include <stdint.h>
#include <time.h>
#include "internal/icmp_utils.h"
/* Test 1: icmp_get_time() fills the timespec structure */
Test(time, get_time_fills_struct)
{
struct timespec ts = {0};
int ret = icmp_get_time(&ts);
cr_assert_eq(ret, 0, "Expected 0 (success), got %d", ret);
/* Should have non-zero seconds */
cr_assert_neq(ts.tv_sec, 0,
"Expected non-zero timestamp, got tv_sec=%ld", ts.tv_sec);
}
/* Test 2: Time difference with same timestamp should be zero */
Test(time, diff_same_time_is_zero)
{
struct timespec ts;
int ret = icmp_get_time(&ts);
cr_assert_eq(ret, 0, "Expected 0 (success), got %d", ret);
int64_t diff = icmp_time_diff_ns(&ts, &ts);
cr_assert_eq(diff, 0, "Same timestamp should give 0ns diff, got %ld", diff);
}
/* Test 3: Time difference calculation is correct */
Test(time, diff_calculation)
{
struct timespec start = {.tv_sec = 10, .tv_nsec = 500000000}; /* 10.5s */
struct timespec end = {.tv_sec = 12, .tv_nsec = 750000000}; /* 12.75s */
int64_t diff = icmp_time_diff_ns(&start, &end);
/* Expected: (12 - 10) * 1e9 + (750000000 - 500000000)
* = 2000000000 + 250000000 = 2250000000 ns = 2.25s */
cr_assert_eq(diff, 2250000000LL,
"Expected 2250000000ns, got %ld", diff);
}