try to remove stdint.h
[liba.git] / test / a.h
blobd73332b7bd0ae8dca5bbab62725c317f7b677ec1
1 #define MAIN(x) a##x
2 #include "test.h"
3 #include <stdlib.h>
5 static void test_sq(void)
7 A_BUILD_ASSERT(A_SQ(+2) == 4);
8 A_BUILD_ASSERT(A_SQ(-2) == 4);
11 static void test_abs(void)
13 A_BUILD_ASSERT(A_ABS(+1) > 0);
14 A_BUILD_ASSERT(A_ABS(-1) > 0);
15 A_BUILD_ASSERT(A_ABS_(+1, -1) > 0);
16 A_BUILD_ASSERT(A_ABS_(-1, +1) > 0);
19 static void test_min(void)
21 A_BUILD_ASSERT(A_MIN(0, 1) == 0);
22 A_BUILD_ASSERT(A_MIN(1, 0) == 0);
25 static void test_max(void)
27 A_BUILD_ASSERT(A_MAX(0, 1) == 1);
28 A_BUILD_ASSERT(A_MAX(1, 0) == 1);
31 static void test_sgn(void)
33 A_BUILD_ASSERT(A_SGN(0) == 0);
34 A_BUILD_ASSERT(A_SGN(+10) == +1);
35 A_BUILD_ASSERT(A_SGN(-10) == -1);
36 A_BUILD_ASSERT(A_SGN_(0, 0) == 0);
37 A_BUILD_ASSERT(A_SGN_(+1, -1) == +1);
38 A_BUILD_ASSERT(A_SGN_(-1, +1) == -1);
41 static void test_sat(void)
43 A_BUILD_ASSERT(A_SAT(0, -10, +10) == 0);
44 A_BUILD_ASSERT(A_SAT(+100, -10, +10) <= +10);
45 A_BUILD_ASSERT(A_SAT(-100, -10, +10) >= -10);
48 static void test_for(int argc, char *argv[])
50 int *p, *d, *it, *at;
51 unsigned int i, n = 10;
53 if (argc > 1)
55 char *endptr = A_NULL;
56 unsigned long u = strtoul(argv[1], &endptr, 0);
57 if (u > n) { n = a_cast_s(unsigned int, u); }
60 p = a_new(int, A_NULL, n);
61 d = p + n;
63 A_ASSUME(p);
66 A_FORENUM(unsigned int, i, n)
68 p[i] = a_cast_s(int, i);
69 debug("%u ", i);
71 debug("\n");
73 A_FOREACH(int *, it, at, p, n) { debug("%i ", *it); }
74 debug("\n");
76 A_ITERATE(int *, it, at, p, d) { debug("%i ", *it); }
77 debug("\n");
79 A_FORENUM_REVERSE(unsigned int, i, n)
81 p[i] = a_cast_s(int, i);
82 debug("%u ", i);
84 debug("\n");
86 A_FOREACH_REVERSE(int *, it, at, p, n) { debug("%i ", *it); }
87 debug("\n");
89 A_ITERATE_REVERSE(int *, it, at, p, d) { debug("%i ", *it); }
90 debug("\n");
92 a_die(p);
95 static void test_swap(int argc, char *argv[])
97 a_u64 lhs = 0;
98 a_u64 rhs = A_U64_MAX;
99 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
100 a_swap(&lhs, &rhs, 1);
101 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
102 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
103 a_swap(&lhs, &rhs, 2);
104 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
105 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
106 a_swap(&lhs, &rhs, 4);
107 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
108 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
109 a_swap(&lhs, &rhs, 8);
110 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
111 (void)argc;
112 (void)argv;
115 #include "a/hash.h"
117 int main(int argc, char *argv[]) /* NOLINT(misc-definitions-in-headers) */
119 test_sq();
120 test_min();
121 test_max();
122 test_abs();
123 test_sgn();
124 test_sat();
125 if (argc < 2)
127 test_for(argc, argv);
128 test_swap(argc, argv);
129 return 0;
131 switch (a_hash_bkdr(argv[1], 0))
133 case 0x001AEED5: /* for */
134 test_for(argc - 1, argv + 1);
135 break;
136 case 0x0F8837E3: /* swap */
137 test_swap(argc - 1, argv + 1);
138 break;
139 default:
140 debug("for\n");
141 debug("swap\n");
143 return 0;