1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "cx/test.h"
30 #include <limits.h>
31 #include <float.h>
32
33 #include "cx/compare.h"
34
35 #define test_compare_gen_subroutine(
T, max_number, signed_type) \
36 static CX_TEST_SUBROUTINE( \
37 test_sub_cmp_##T, \
38 cx_compare_func fnc \
39 ) { \
40 T m = max_number /
512; \
41 T x, y; \
42 x = (signed_type ?
-3 :
3) * m; \
43 y =
5 * m; \
44 CX_TEST_ASSERT(fnc(&x, &y) <
0); \
45 CX_TEST_ASSERT(fnc(&y, &x) >
0); \
46 x =
120 * m; \
47 y =
348 * m; \
48 CX_TEST_ASSERT(fnc(&x, &y) <
0); \
49 CX_TEST_ASSERT(fnc(&y, &x) >
0); \
50 if (signed_type) { \
51 x =
-120 * m; \
52 y =
-348 * m; \
53 CX_TEST_ASSERT(fnc(&x, &y) >
0); \
54 CX_TEST_ASSERT(fnc(&y, &x) <
0); \
55 } \
56 x = y; \
57 CX_TEST_ASSERT(fnc(&x, &y) ==
0); \
58 CX_TEST_ASSERT(fnc(&y, &x) ==
0); \
59 }
60
61
62
63 typedef long long cx_longlong;
64 typedef unsigned long cx_ulong;
65 typedef unsigned long long cx_ulonglong;
66
67
68 test_compare_gen_subroutine(
int,
INT_MAX, true)
69 test_compare_gen_subroutine(
long,
LONG_MAX, true)
70 test_compare_gen_subroutine(cx_longlong,
LLONG_MAX, true)
71 test_compare_gen_subroutine(
int16_t,
INT16_MAX, true)
72 test_compare_gen_subroutine(
int32_t,
INT32_MAX, true)
73 test_compare_gen_subroutine(
int64_t,
INT64_MAX, true)
74 test_compare_gen_subroutine(
unsigned,
UINT_MAX, false)
75 test_compare_gen_subroutine(cx_ulong,
ULONG_MAX, false)
76 test_compare_gen_subroutine(cx_ulonglong,
ULLONG_MAX, false)
77 test_compare_gen_subroutine(
size_t,
SIZE_MAX, false)
78 test_compare_gen_subroutine(
uint16_t,
UINT16_MAX, false)
79 test_compare_gen_subroutine(
uint32_t,
UINT32_MAX, false)
80 test_compare_gen_subroutine(
uint64_t,
UINT64_MAX, false)
81 test_compare_gen_subroutine(
float,
FLT_MAX, true)
82 test_compare_gen_subroutine(
double,
DBL_MAX, true)
83 test_compare_gen_subroutine(
intptr_t,
INTPTR_MAX, true)
84 test_compare_gen_subroutine(
uintptr_t,
UINTPTR_MAX, false)
85
86 CX_TEST(test_compare_int) {
87 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
88 }
89
90 CX_TEST(test_compare_long) {
91 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
92 }
93
94 CX_TEST(test_compare_longlong) {
95 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
96 }
97
98 CX_TEST(
test_compare_int16_t) {
99 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int16_t, cx_cmp_int16);
100 }
101
102 CX_TEST(
test_compare_int32_t) {
103 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int32_t, cx_cmp_int32);
104 }
105
106 CX_TEST(
test_compare_int64_t) {
107 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_int64_t, cx_cmp_int64);
108 }
109
110 CX_TEST(test_compare_unsigned) {
111 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
112 }
113
114 CX_TEST(test_compare_ulong) {
115 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
116 }
117
118 CX_TEST(test_compare_ulonglong) {
119 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
120 }
121
122 CX_TEST(
test_compare_size_t) {
123 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_size_t, cx_cmp_size);
124 }
125
126 CX_TEST(
test_compare_uint16_t) {
127 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint16_t, cx_cmp_uint16);
128 }
129
130 CX_TEST(
test_compare_uint32_t) {
131 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint32_t, cx_cmp_uint32);
132 }
133
134 CX_TEST(
test_compare_uint64_t) {
135 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uint64_t, cx_cmp_uint64);
136 }
137
138 CX_TEST(test_compare_float) {
139 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
140 }
141
142 CX_TEST(test_compare_double) {
143 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
144 }
145
146 CX_TEST(
test_compare_intptr_t) {
147 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_intptr_t, cx_cmp_intptr);
148 }
149
150 CX_TEST(
test_compare_uintptr_t) {
151 CX_TEST_DO CX_TEST_CALL_SUBROUTINE(
test_sub_cmp_uintptr_t, cx_cmp_uintptr);
152 }
153
154 CX_TEST(test_compare_ptr) {
155 int data[
3];
156 CX_TEST_DO {
157 CX_TEST_ASSERT(
0 == cx_cmp_ptr(data, data));
158 CX_TEST_ASSERT(
-1 == cx_cmp_ptr(&data[
0], &data[
1]));
159 CX_TEST_ASSERT(
-1 == cx_cmp_ptr(&data[
1], &data[
2]));
160 CX_TEST_ASSERT(
1 == cx_cmp_ptr(&data[
2], &data[
1]));
161 CX_TEST_ASSERT(
1 == cx_cmp_ptr(&data[
1], data));
162 CX_TEST_ASSERT(
0 == cx_cmp_ptr(&data[
1], &data[
1]));
163 }
164 }
165
166 CxTestSuite *cx_test_suite_compare(
void) {
167 CxTestSuite *suite = cx_test_suite_new(
"compare");
168
169 cx_test_register(suite, test_compare_int);
170 cx_test_register(suite, test_compare_long);
171 cx_test_register(suite, test_compare_longlong);
172 cx_test_register(suite,
test_compare_int16_t);
173 cx_test_register(suite,
test_compare_int32_t);
174 cx_test_register(suite,
test_compare_int64_t);
175 cx_test_register(suite, test_compare_unsigned);
176 cx_test_register(suite, test_compare_ulong);
177 cx_test_register(suite, test_compare_ulonglong);
178 cx_test_register(suite,
test_compare_size_t);
179 cx_test_register(suite,
test_compare_uint16_t);
180 cx_test_register(suite,
test_compare_uint32_t);
181 cx_test_register(suite,
test_compare_uint64_t);
182 cx_test_register(suite, test_compare_float);
183 cx_test_register(suite, test_compare_double);
184 cx_test_register(suite,
test_compare_intptr_t);
185 cx_test_register(suite,
test_compare_uintptr_t);
186 cx_test_register(suite, test_compare_ptr);
187
188 return suite;
189 }
190