| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <inttypes.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef UINT16_MAX |
| 20 |
#define UINT16_MAX 0xFFFFu |
| 21 |
#endif |
| 22 |
static inline uint32_t rist_seq_gap(uint32_t current, uint32_t last, |
| 23 |
bool short_seq) |
| 24 |
{ |
| 25 |
uint32_t gap = current - last; |
| 26 |
return short_seq ? (gap & UINT16_MAX) : gap; |
| 27 |
} |
| 28 |
|
| 29 |
static int failures = 0; |
| 30 |
|
| 31 |
#define CHECK(cond, ...) \ |
| 32 |
do { \ |
| 33 |
if (!(cond)) { \ |
| 34 |
fprintf(stderr, "FAIL %s:%d: " #cond, __FILE__, __LINE__); \ |
| 35 |
fprintf(stderr, " - "); fprintf(stderr, __VA_ARGS__); \ |
| 36 |
fprintf(stderr, "\n"); \ |
| 37 |
failures++; \ |
| 38 |
} \ |
| 39 |
} while (0) |
| 40 |
|
| 41 |
|
| 42 |
static void test_small_gap(void) |
| 43 |
{ |
| 44 |
CHECK(rist_seq_gap(105, 100, true) == 5, "short small gap"); |
| 45 |
CHECK(rist_seq_gap(105, 100, false) == 5, "long small gap"); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static void test_short_wrap(void) |
| 50 |
{ |
| 51 |
|
| 52 |
CHECK(rist_seq_gap(4u, 65530u, true) == 10u, |
| 53 |
"short wrap gap: got %u want 10", rist_seq_gap(4u, 65530u, true)); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static void test_long_gap_not_truncated(void) |
| 60 |
{ |
| 61 |
uint32_t gap = rist_seq_gap(70000u, 0u, false); |
| 62 |
CHECK(gap == 70000u, "long >64k gap must be exact: got %u want 70000", gap); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
CHECK(rist_seq_gap(70000u, 0u, true) == (70000u & UINT16_MAX), |
| 67 |
"short framing truncates to 16 bits by design"); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static void test_long_wrap(void) |
| 72 |
{ |
| 73 |
|
| 74 |
CHECK(rist_seq_gap(5u, 0xFFFFFFFBu, false) == 10u, |
| 75 |
"long 32-bit wrap gap: got %u want 10", |
| 76 |
rist_seq_gap(5u, 0xFFFFFFFBu, false)); |
| 77 |
} |
| 78 |
|
| 79 |
int main(void) |
| 80 |
{ |
| 81 |
test_small_gap(); |
| 82 |
test_short_wrap(); |
| 83 |
test_long_gap_not_truncated(); |
| 84 |
test_long_wrap(); |
| 85 |
|
| 86 |
if (failures > 0) { |
| 87 |
fprintf(stderr, "[test_seq_gap] %d FAILURES\n", failures); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
fprintf(stderr, "[test_seq_gap] all tests passed\n"); |
| 91 |
return 0; |
| 92 |
} |