tsort: replace with openbsd version
[unleashed.git] / contrib / libjeffpc / test_array.c
blob859daee1ec5361df54f9c882e34f5dcfc43faa1a
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/types.h>
25 #include "test.c"
27 struct foo {
28 int a;
29 char b;
32 union bar {
33 int a;
34 char b;
37 #define TEST_ONE(type, nelem) \
38 do { \
39 type test_array[nelem]; \
40 size_t res = ARRAY_LEN(test_array); \
42 fprintf(stderr, " %-16s test_array[%lu]\t=> %zu...",\
43 #type, nelem, res); \
45 if (res != nelem) \
46 fail("expected %zu, got %zu", nelem, \
47 res); \
49 fprintf(stderr, "ok.\n"); \
50 } while (0)
52 #define TEST_TYPES(nelem) \
53 do { \
54 TEST_ONE(int8_t, nelem); \
55 TEST_ONE(int16_t, nelem); \
56 TEST_ONE(int32_t, nelem); \
57 TEST_ONE(int64_t, nelem); \
58 TEST_ONE(uint8_t, nelem); \
59 TEST_ONE(uint16_t, nelem); \
60 TEST_ONE(uint32_t, nelem); \
61 TEST_ONE(uint64_t, nelem); \
62 TEST_ONE(char, nelem); \
63 TEST_ONE(signed char, nelem); \
64 TEST_ONE(unsigned char, nelem); \
65 TEST_ONE(short, nelem); \
66 TEST_ONE(signed short, nelem); \
67 TEST_ONE(unsigned short, nelem); \
68 TEST_ONE(int, nelem); \
69 TEST_ONE(signed int, nelem); \
70 TEST_ONE(unsigned int, nelem); \
71 TEST_ONE(long, nelem); \
72 TEST_ONE(signed long, nelem); \
73 TEST_ONE(unsigned long, nelem); \
74 TEST_ONE(void *, nelem); \
75 TEST_ONE(intptr_t, nelem); \
76 TEST_ONE(uintptr_t, nelem); \
77 TEST_ONE(ptrdiff_t, nelem); \
78 TEST_ONE(struct foo, nelem); \
79 TEST_ONE(struct foo *, nelem); \
80 TEST_ONE(union bar, nelem); \
81 TEST_ONE(union bar *, nelem); \
82 } while (0)
84 static void test_array_len(void)
86 int i;
88 fprintf(stderr, "testing ARRAY_LEN with various types & lengths\n");
91 * Note:
93 * We try to create arrays up to 2**24 (16777216) elements in size
94 * right on the stack. This can make the process size huge in no
95 * time if we feed TEST_ONE a type that is large to begin with.
97 for (i = 0; i <= 24; i++)
98 TEST_TYPES(1UL << i);
100 fprintf(stderr, "All ARRAY_LEN tests passed.\n");
103 void test(void)
105 test_array_len();