]> git.draconx.ca Git - dxcommon.git/blob - t/packtests64.c
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / t / packtests64.c
1 /*
2  * Copyright © 2015, 2023 Nick Bowler
3  *
4  * Test application to verify 64-bit signed 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 min[9] = {
20         0x80, 0, 0, 0, 0, 0, 0, 0, 0x80
21 };
22
23 static const unsigned char test_pattern[8] = {
24         0xde, 0xad, 0xbe, 0xef, 0xf0, 0x0d, 0xca, 0xfe
25 };
26
27 #define test(func, pattern, expected) do { \
28         long long result__ = (func)(pattern); \
29         if (!tap_result(result__ == (expected), "%s(%s)", #func, #expected)) { \
30                 tap_diag("  expected: %lld", (long long)(expected)); \
31                 tap_diag("  actual:   %lld", result__); \
32         } \
33 } while (0)
34
35 int main(void)
36 {
37 #if PACK_HAVE_64BIT
38         tap_plan(8);
39
40         test(unpack_s64_be, zero,          0);
41         test(unpack_s64_be, minus_one,    -1);
42         test(unpack_s64_be, test_pattern, -2401053088584709378ll);
43         test(unpack_s64_be, min,          -9223372036854775807ll-1);
44
45         test(unpack_s64_le, zero,          0);
46         test(unpack_s64_le, minus_one,    -1);
47         test(unpack_s64_le, test_pattern, -87241914314740258ll);
48         test(unpack_s64_le, min+1,        -9223372036854775807ll-1);
49 #else
50         tap_skip_all("no 64-bit support");
51 #endif
52
53         tap_done();
54 }