tsort: replace with openbsd version
[unleashed.git] / contrib / libjeffpc / test_bswap.c
blob9a2b21d679eacdb56907e13cbacaa533ce52d0a9
1 /*
2 * Copyright (c) 2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <jeffpc/int.h>
24 #include <jeffpc/types.h>
26 #include "test.c"
28 struct run {
29 uint64_t in;
30 uint8_t out8;
31 uint16_t out16;
32 uint32_t out32;
33 uint64_t out64;
36 static const struct run runs[] = {
38 .in = 0,
39 .out8 = 0,
40 .out16 = 0,
41 .out32 = 0,
42 .out64 = 0,
45 .in = 0xffffffffffffffff,
46 .out8 = 0xff,
47 .out16 = 0xffff,
48 .out32 = 0xffffffff,
49 .out64 = 0xffffffffffffffff,
52 .in = 0x123456789abcdef0,
53 .out8 = 0xf0,
54 .out16 = 0xf0de,
55 .out32 = 0xf0debc9a,
56 .out64 = 0xf0debc9a78563412,
59 .in = 0x8080808080808080,
60 .out8 = 0x80,
61 .out16 = 0x8080,
62 .out32 = 0x80808080,
63 .out64 = 0x8080808080808080,
67 #define CHECK(iter, size, fmt, in, exp) \
68 do { \
69 uint##size##_t got = bswap_##size(in); \
71 fprintf(stderr, "%d: expected: %#"fmt"\n", iter, exp); \
72 fprintf(stderr, "%d: got: %#"fmt"\n", iter, got); \
74 if (got != exp) \
75 fail("mismatch!"); \
76 } while (0)
78 static void __test(int iter, const struct run *run)
80 fprintf(stderr, "%d: input: %#"PRIx64"\n", iter, run->in);
81 CHECK(iter, 8, PRIx8, run->in & 0xff, run->out8);
82 CHECK(iter, 16, PRIx16, run->in & 0xffff, run->out16);
83 CHECK(iter, 32, PRIx32, run->in & 0xffffffff, run->out32);
84 CHECK(iter, 64, PRIx64, run->in, run->out64);
85 fprintf(stderr, "%d: ok.\n", iter);
88 void test(void)
90 int i;
92 for (i = 0; i < ARRAY_LEN(runs) ; i++)
93 __test(i, &runs[i]);