| 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 |
#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 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 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 |
} |