| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "../../../src/rist-nack-select.h" |
| 9 |
|
| 10 |
#include <stdint.h> |
| 11 |
#include <stdio.h> |
| 12 |
|
| 13 |
struct cand { |
| 14 |
uint32_t priority; |
| 15 |
uint64_t rtt; |
| 16 |
}; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
static int select_index(const struct cand *c, int n) |
| 21 |
{ |
| 22 |
int chosen = -1; |
| 23 |
uint32_t best_priority = 0; |
| 24 |
uint64_t best_rtt = UINT64_MAX; |
| 25 |
for (int i = 0; i < n; i++) { |
| 26 |
if (rist_nack_peer_preferred(c[i].priority, c[i].rtt, |
| 27 |
best_priority, best_rtt)) { |
| 28 |
chosen = i; |
| 29 |
best_priority = c[i].priority; |
| 30 |
best_rtt = c[i].rtt; |
| 31 |
} |
| 32 |
} |
| 33 |
return chosen; |
| 34 |
} |
| 35 |
|
| 36 |
static int check(const char *name, const struct cand *c, int n, int want) |
| 37 |
{ |
| 38 |
int got = select_index(c, n); |
| 39 |
if (got != want) { |
| 40 |
fprintf(stderr, "FAIL: %s -> index %d (want %d)\n", |
| 41 |
name, got, want); |
| 42 |
return 1; |
| 43 |
} |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
int main(void) |
| 48 |
{ |
| 49 |
int failures = 0; |
| 50 |
|
| 51 |
|
| 52 |
struct cand all_zero[] = { |
| 53 |
{0, 50}, {0, 20}, {0, 80}, |
| 54 |
}; |
| 55 |
failures += check("all_zero_lowest_rtt", all_zero, 3, 1); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct cand prio_over_rtt[] = { |
| 61 |
{0, 1}, |
| 62 |
{1000, 9000}, |
| 63 |
}; |
| 64 |
failures += check("priority_beats_low_rtt", prio_over_rtt, 2, 1); |
| 65 |
|
| 66 |
|
| 67 |
struct cand tie_break[] = { |
| 68 |
{5, 100}, {5, 40}, {5, 70}, {1, 1}, |
| 69 |
}; |
| 70 |
failures += check("tie_break_lowest_rtt", tie_break, 4, 1); |
| 71 |
|
| 72 |
|
| 73 |
struct cand single[] = { {0, 1000} }; |
| 74 |
failures += check("single_candidate", single, 1, 0); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
struct cand unmeasured_zero[] = { {0, UINT64_MAX} }; |
| 80 |
failures += check("unmeasured_rtt_priority_zero_skipped", |
| 81 |
unmeasured_zero, 1, -1); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct cand unmeasured_high[] = { {1000, UINT64_MAX} }; |
| 87 |
failures += check("unmeasured_rtt_high_priority_selected", |
| 88 |
unmeasured_high, 1, 0); |
| 89 |
|
| 90 |
|
| 91 |
failures += check("empty", NULL, 0, -1); |
| 92 |
|
| 93 |
|
| 94 |
struct cand full_tie[] = { {3, 30}, {3, 30} }; |
| 95 |
failures += check("full_tie_first_wins", full_tie, 2, 0); |
| 96 |
|
| 97 |
if (failures == 0) { |
| 98 |
printf("OK\n"); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
fprintf(stderr, "Total failures: %d\n", failures); |
| 102 |
return 1; |
| 103 |
} |