| 1 |
/* |
| 2 |
* Copyright 2025 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "pan_encoder.h" |
| 6 |
|
| 7 |
struct test { |
| 8 |
const char *label; |
| 9 |
uint32_t offset_id; |
| 10 |
uint32_t divisor; |
| 11 |
}; |
| 12 |
|
| 13 |
#define TEST_DEF(label, offset_id, divisor) {label, offset_id, divisor} |
| 14 |
|
| 15 |
static const struct test attribute_tests[] = { |
| 16 |
TEST_DEF("Sanity check even", 2, 2), |
| 17 |
TEST_DEF("Sanity check odd", 9, 3), |
| 18 |
TEST_DEF("Sanity check random #1", 42, 3), |
| 19 |
TEST_DEF("Sanity check random #2", 999, 3), |
| 20 |
TEST_DEF("Sanity check max #1", UINT32_MAX, UINT32_MAX), |
| 21 |
TEST_DEF("Sanity check max #2", UINT32_MAX, INT32_MAX), |
| 22 |
TEST_DEF("Regression test case #1", 404896682, 444453), |
| 23 |
TEST_DEF("Regression test case #2", 449209605, 980807), |
| 24 |
TEST_DEF("Regression test case #3", 412962763, 103240691), |
| 25 |
TEST_DEF("Regression test case #4", 1269169964, 507465), |
| 26 |
TEST_DEF("Regression test case #5", 1771745081, 6867229), |
| 27 |
}; |
| 28 |
|
| 29 |
static uint32_t |
| 30 |
compute_effective_id(uint64_t offset_id, uint64_t r, uint64_t e, uint64_t d) |
| 31 |
{ |
| 32 |
return (offset_id + e) * (d + (1u << 31)) / ((uint64_t)1 << (32 + r)); |
| 33 |
} |
| 34 |
|
| 35 |
#define ASSERT_EQ(x, y) \ |
| 36 |
do { \ |
| 37 |
if (x == y) { \ |
| 38 |
nr_pass++; \ |
| 39 |
} else { \ |
| 40 |
nr_fail++; \ |
| 41 |
fprintf(stderr, "%s: Assertion failed %s (%x) != %s (%x)\n", T.label, \ |
| 42 |
#x, x, #y, y); \ |
| 43 |
} \ |
| 44 |
} while (0) |
| 45 |
|
| 46 |
int |
| 47 |
main(int argc, const char **argv) |
| 48 |
{ |
| 49 |
unsigned nr_pass = 0, nr_fail = 0; |
| 50 |
|
| 51 |
for (unsigned i = 0; i < ARRAY_SIZE(attribute_tests); ++i) { |
| 52 |
struct test T = attribute_tests[i]; |
| 53 |
|
| 54 |
uint32_t e; |
| 55 |
uint32_t r; |
| 56 |
uint32_t d = pan_compute_npot_divisor(T.divisor, &r, &e); |
| 57 |
|
| 58 |
uint32_t expected_effective_id = T.offset_id / T.divisor; |
| 59 |
uint32_t computed_effective_id = |
| 60 |
compute_effective_id(T.offset_id, r, e, d); |
| 61 |
|
| 62 |
ASSERT_EQ(expected_effective_id, computed_effective_id); |
| 63 |
} |
| 64 |
|
| 65 |
printf("Passed %u/%u\n", nr_pass, nr_pass + nr_fail); |
| 66 |
return nr_fail ? 1 : 0; |
| 67 |
} |