Debian Codegraph @

test/rist/unit/test_seq_next.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for rist_seq_next() - the "expected next sequence number" helper
4 * used by the receiver out-of-order / late guard (receiver_enqueue). The
5 * regression under test: the short_seq successor must wrap at 16 bits with
6 * `& UINT16_MAX` (0xFFFF), not the historical `& (UINT16_MAX - 1)` (0xFFFE)
7 * which forced the value even and mis-computed every odd successor.
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 #ifndef UINT16_MAX
16 #define UINT16_MAX 0xFFFFu
17 #endif
18 static inline uint32_t rist_seq_next(uint32_t last, bool short_seq)
19 {
20 uint32_t next = last + 1;
21 return short_seq ? (next & UINT16_MAX) : next;
22 }
23
24 static int failures = 0;
25
26 #define CHECK(cond, ...) \
27 do { \
28 if (!(cond)) { \
29 fprintf(stderr, "FAIL %s:%d: " #cond, __FILE__, __LINE__); \
30 fprintf(stderr, " - "); fprintf(stderr, __VA_ARGS__); \
31 fprintf(stderr, "\n"); \
32 failures++; \
33 } \
34 } while (0)
35
36 /* T1 - the regression: an odd successor must be returned exactly. Under the
37 * old 0xFFFE mask, rist_seq_next(100, true) would have returned 100 (even). */
38 static void test_odd_successor(void)
39 {
40 CHECK(rist_seq_next(100u, true) == 101u,
41 "short odd successor: got %u want 101", rist_seq_next(100u, true));
42 CHECK(rist_seq_next(101u, true) == 102u,
43 "short even successor: got %u want 102", rist_seq_next(101u, true));
44 }
45
46 /* T2 - 16-bit wrap: short flows wrap modulo 65536. */
47 static void test_short_wrap(void)
48 {
49 CHECK(rist_seq_next(65535u, true) == 0u,
50 "short wrap: got %u want 0", rist_seq_next(65535u, true));
51 CHECK(rist_seq_next(65534u, true) == 65535u,
52 "short top: got %u want 65535", rist_seq_next(65534u, true));
53 }
54
55 /* T3 - 32-bit flows do not wrap at 16 bits and wrap correctly at 32. */
56 static void test_long(void)
57 {
58 CHECK(rist_seq_next(65535u, false) == 65536u,
59 "long no 16-bit wrap: got %u want 65536", rist_seq_next(65535u, false));
60 CHECK(rist_seq_next(0xFFFFFFFFu, false) == 0u,
61 "long 32-bit wrap: got %u want 0", rist_seq_next(0xFFFFFFFFu, false));
62 }
63
64 int main(void)
65 {
66 test_odd_successor();
67 test_short_wrap();
68 test_long();
69
70 if (failures > 0) {
71 fprintf(stderr, "[test_seq_next] %d FAILURES\n", failures);
72 return 1;
73 }
74 fprintf(stderr, "[test_seq_next] all tests passed\n");
75 return 0;
76 }