Debian Codegraph @

test/rist/unit/test_seq_gap.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for rist_seq_gap() - the forward sequence-number gap helper used
4 * by the receiver gap detector (receiver_mark_missing). The key property under
5 * test is that short-sequence (Simple/Main, 16-bit) flows wrap modulo 65536
6 * while full 32-bit (Advanced) flows report the true gap, so a genuine >64k
7 * gap on an Advanced flow is no longer silently truncated to a 16-bit value.
8 * Pure, link-free: mirrors the static-inline definition from rist-private.h. */
9
10 #include <inttypes.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14
15 /* The helper is a static inline in rist-private.h, which drags in the entire
16 * private header graph. Mirror just the definition here (kept byte-identical)
17 * so the test stays a pure, link-free unit. A divergence between the two is a
18 * deliberate review signal: update both. */
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 /* T1 - small in-order gap is identical for both framings. */
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 /* T2 - 16-bit wrap: short flows wrap modulo 65536. */
49 static void test_short_wrap(void)
50 {
51 /* last=65530, current=4 -> 16-bit forward gap is 10. */
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 /* T3 - the regression we are fixing: a real >64k gap on a 32-bit flow.
57 * The old unconditional `& UINT16_MAX` truncated this to a tiny value and the
58 * detector mis-counted the loss; the 32-bit path must report the true gap. */
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 /* Same numbers under short framing DO truncate (70000 & 0xFFFF = 4464),
65 * documenting why the 16-bit path must stay separate. */
66 CHECK(rist_seq_gap(70000u, 0u, true) == (70000u & UINT16_MAX),
67 "short framing truncates to 16 bits by design");
68 }
69
70 /* T4 - 32-bit wrap is well-defined unsigned arithmetic. */
71 static void test_long_wrap(void)
72 {
73 /* last near the top of the 32-bit space, current just past wrap. */
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 }