]> git.draconx.ca Git - dxcommon.git/blob - t/packtestu.c
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / t / packtestu.c
1 /*
2  * Copyright © 2015 Nick Bowler
3  *
4  * Test application to verify 16 and 32-bit unsigned unpacking functions.
5  *
6  * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
7  * This is free software: you are free to do what the fuck you want to.
8  * There is NO WARRANTY, to the extent permitted by law.
9  */
10
11 #include "pack.h"
12 #include "tap.h"
13
14 static const unsigned char zero[8];
15 static const unsigned char minus_one[8] = {
16         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
17 };
18
19 static const unsigned char test_pattern[8] = {
20         0xde, 0xad, 0xbe, 0xef, 0xf0, 0x0d, 0xca, 0xfe
21 };
22
23 #define test(func, pattern, expected) do { \
24         unsigned long result__ = (func)(pattern); \
25         if (!tap_result(result__ == (expected), "%s(%s)", #func, #expected)) { \
26                 tap_diag("  expected: %lu", (unsigned long)(expected)); \
27                 tap_diag("  actual:   %lu", result__); \
28         } \
29 } while (0)
30
31 int main(void)
32 {
33         tap_plan(12);
34
35         test(unpack_16_be, zero,         0);
36         test(unpack_16_be, minus_one,    0xffff);
37         test(unpack_16_be, test_pattern, 0xdead);
38         test(unpack_32_be, zero,         0);
39         test(unpack_32_be, minus_one,    0xffffffff);
40         test(unpack_32_be, test_pattern, 0xdeadbeef);
41
42         test(unpack_16_le, zero,         0);
43         test(unpack_16_le, minus_one,    0xffff);
44         test(unpack_16_le, test_pattern, 0xadde);
45         test(unpack_32_le, zero,         0);
46         test(unpack_32_le, minus_one,    0xffffffff);
47         test(unpack_32_le, test_pattern, 0xefbeadde);
48
49         tap_done();
50 }