Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
0.valcmp.cpp
Go to the documentation of this file.
1
30#include <rw_valcmp.h>
31
32#include <stdio.h> // for fprintf, size_t
33
34
35// the exit status of the whole test
36static int exit_status;
37
38
39template <class T, class U>
40void
41test_case (const T* /* dummy */, const U* /* dummy */,
42 const char *tname, const char *uname,
43 int line, int expect,
44 const char *str1, const char *str2, size_t nelems, int flags)
45{
46 T buf1 [256] = { T () };
47 U buf2 [256] = { U () };
48
49 typedef unsigned char UChar;
50
51 for (size_t i = 0; i != nelems; ++i) {
52 buf1 [i] = T (_RWSTD_STATIC_CAST (UChar, str1 [i]));
53 buf2 [i] = U (_RWSTD_STATIC_CAST (UChar, str2 [i]));
54 }
55
56 const int result = rw_valcmp (buf1, buf2, nelems, flags);
57
58 if (result != expect) {
59 fprintf (stderr,
60 "line %d: %d == rw_valcmp(const %s* = %p, const %s* = %p, "
61 "%u, %d), got %d\n",
62 line, expect, tname, (void*)buf1, uname, (void*)buf2,
63 unsigned (nelems), flags, result);
64 exit_status = 1;
65 }
66}
67
68
69struct Case {
70 int line;
71 int expect;
72 const char *str1;
73 const char *str2;
74 size_t nelems;
75 int flags;
76};
77
78extern const Case cases [] = {
79
80#define CASE(expect, str1, str2, nelems, flags) \
81 { __LINE__, expect, str1, str2, nelems, flags }
82
83 CASE ( 0, 0, 0, 0, 0),
84
85 CASE ( 0, "", "", 0, 0),
86 CASE ( 0, "", "", 1, 0),
87
88
89 CASE ( 0, "a", "\0", 0, 0),
90 CASE (+1, "a", "\0", 1, 0),
91 CASE (+1, "a", "\0", 2, 0),
92
93 CASE ( 0, "\0", "a", 0, 0),
94 CASE (-1, "\0", "a", 1, 0),
95 CASE (-1, "\0", "a", 2, 0),
96
97
98 CASE ( 0, "ab", "\0\0", 0, 0),
99 CASE (+1, "ab", "\0\0", 1, 0),
100 CASE (+1, "ab", "\0\0", 2, 0),
101 CASE (+1, "ab", "\0\0", 3, 0),
102
103 CASE ( 0, "\0\0", "ab", 0, 0),
104 CASE (-1, "\0\0", "ab", 1, 0),
105 CASE (-1, "\0\0", "ab", 2, 0),
106 CASE (-1, "\0\0", "ab", 3, 0),
107
108
109 CASE ( 0, "ab", "a\0", 0, 0),
110 CASE ( 0, "ab", "a\0", 1, 0),
111 CASE (+1, "ab", "a\0", 2, 0),
112 CASE (+1, "ab", "a\0", 3, 0),
113
114 CASE ( 0, "a\0", "ab", 0, 0),
115 CASE ( 0, "a\0", "ab", 1, 0),
116 CASE (-1, "a\0", "ab", 2, 0),
117 CASE (-1, "a\0", "ab", 3, 0),
118
119
120 CASE ( 0, "\0ab", "\0a\0", 0, 0),
121 CASE ( 0, "\0ab", "\0a\0", 1, 0),
122 CASE ( 0, "\0ab", "\0a\0", 2, 0),
123 CASE (+1, "\0ab", "\0a\0", 3, 0),
124 CASE (+1, "\0ab", "\0a\0", 4, 0),
125
126 CASE ( 0, "\0a\0", "\0ab", 0, 0),
127 CASE ( 0, "\0a\0", "\0ab", 1, 0),
128 CASE ( 0, "\0a\0", "\0ab", 2, 0),
129 CASE (-1, "\0a\0", "\0ab", 3, 0),
130 CASE (-1, "\0a\0", "\0ab", 4, 0),
131
132
133 CASE ( 0, "abc", "aB\0", 0, 0),
134 CASE ( 0, "abc", "aB\0", 1, 0),
135 CASE (+1, "abc", "aB\0", 2, 0),
136 CASE (+1, "abc", "aB\0", 3, 0),
137 CASE (+1, "abc", "aB\0", 4, 0),
138
139 CASE ( 0, "aB\0", "abc", 0, 0),
140 CASE ( 0, "aB\0", "abc", 1, 0),
141 CASE (-1, "aB\0", "abc", 2, 0),
142 CASE (-1, "aB\0", "abc", 3, 0),
143 CASE (-1, "aB\0", "abc", 4, 0),
144
145
146 CASE ( 0, "a\0b\0c\0d", "a\0b\0c\0R", 0, 0),
147 CASE ( 0, "a\0b\0c\0e", "a\0b\0c\0S", 1, 0),
148 CASE ( 0, "a\0b\0c\0f", "a\0b\0c\0T", 2, 0),
149 CASE ( 0, "a\0b\0c\0g", "a\0b\0c\0U", 3, 0),
150 CASE ( 0, "a\0b\0c\0h", "a\0b\0c\0V", 4, 0),
151 CASE ( 0, "a\0b\0c\0i", "a\0b\0c\0W", 5, 0),
152 CASE ( 0, "a\0b\0c\0j", "a\0b\0c\0X", 6, 0),
153 CASE (+1, "a\0b\0c\0k", "a\0b\0c\0Y", 7, 0),
154 CASE (+1, "a\0b\0c\0l", "a\0b\0c\0Z", 8, 0),
155
156 CASE ( 0, "a\0b\0c\0R", "a\0b\0c\0d", 0, 0),
157 CASE ( 0, "a\0b\0c\0S", "a\0b\0c\0d", 1, 0),
158 CASE ( 0, "a\0b\0c\0T", "a\0b\0c\0d", 2, 0),
159 CASE ( 0, "a\0b\0c\0U", "a\0b\0c\0d", 3, 0),
160 CASE ( 0, "a\0b\0c\0V", "a\0b\0c\0d", 4, 0),
161 CASE ( 0, "a\0b\0c\0W", "a\0b\0c\0d", 5, 0),
162 CASE ( 0, "a\0b\0c\0X", "a\0b\0c\0d", 6, 0),
163 CASE (-1, "a\0b\0c\0Y", "a\0b\0c\0d", 7, 0),
164 CASE (-1, "a\0b\0c\0Z", "a\0b\0c\0d", 8, 0),
165
166
167 CASE ( 0, "a\0b\0c\0R", "a\0b\0c\0d", 0, CMP_NOCASE),
168 CASE ( 0, "a\0b\0c\0S", "a\0b\0c\0e", 1, CMP_NOCASE),
169 CASE ( 0, "a\0b\0c\0T", "a\0b\0c\0f", 2, CMP_NOCASE),
170 CASE ( 0, "a\0b\0c\0U", "a\0b\0c\0g", 3, CMP_NOCASE),
171 CASE ( 0, "a\0b\0c\0V", "a\0b\0c\0h", 4, CMP_NOCASE),
172 CASE ( 0, "a\0b\0c\0W", "a\0b\0c\0i", 5, CMP_NOCASE),
173 CASE ( 0, "a\0b\0c\0X", "a\0b\0c\0j", 6, CMP_NOCASE),
174 CASE (+1, "a\0b\0c\0Y", "a\0b\0c\0k", 7, CMP_NOCASE),
175 CASE (+1, "a\0b\0c\0Z", "a\0b\0c\0l", 8, CMP_NOCASE),
176
177 CASE ( 0, "A\0b\0c\0d", "a\0B\0c\0R", 0, CMP_NOCASE),
178 CASE ( 0, "A\0b\0c\0e", "a\0B\0c\0S", 1, CMP_NOCASE),
179 CASE ( 0, "A\0b\0c\0f", "a\0B\0c\0T", 2, CMP_NOCASE),
180 CASE ( 0, "A\0b\0c\0g", "a\0B\0c\0U", 3, CMP_NOCASE),
181 CASE ( 0, "A\0b\0c\0h", "a\0B\0c\0V", 4, CMP_NOCASE),
182 CASE ( 0, "A\0b\0c\0i", "a\0B\0c\0W", 5, CMP_NOCASE),
183 CASE ( 0, "A\0b\0c\0j", "a\0B\0c\0X", 6, CMP_NOCASE),
184 CASE (-1, "A\0b\0c\0k", "a\0B\0c\0Y", 7, CMP_NOCASE),
185 CASE (-1, "A\0b\0c\0l", "a\0B\0c\0Z", 8, CMP_NOCASE),
186
187
188 CASE ( 0, "Abc", "aB\0", 0, CMP_NOCASE),
189 CASE ( 0, "Abc", "aB\0", 1, CMP_NOCASE),
190 CASE ( 0, "Abc", "aB\0", 2, CMP_NOCASE),
191 CASE (+1, "Abc", "aB\0", 3, CMP_NOCASE),
192 CASE (+1, "Abc", "aB\0", 4, CMP_NOCASE),
193
194 CASE ( 0, "aB\0", "Abc", 0, CMP_NOCASE),
195 CASE ( 0, "aB\0", "Abc", 1, CMP_NOCASE),
196 CASE ( 0, "aB\0", "Abc", 2, CMP_NOCASE),
197 CASE (-1, "aB\0", "Abc", 3, CMP_NOCASE),
198 CASE (-1, "aB\0", "Abc", 4, CMP_NOCASE),
199
200
201 CASE ( 0, "abcd", "abc\0", 0, CMP_NULTERM),
202 CASE ( 0, "abcd", "abc\0", 1, CMP_NULTERM),
203 CASE ( 0, "abcd", "abc\0", 2, CMP_NULTERM),
204 CASE ( 0, "abcd", "abc\0", 3, CMP_NULTERM),
205 CASE (+1, "abcd", "abc\0", 4, CMP_NULTERM),
206 CASE (+1, "abcd", "abc\0", 5, CMP_NULTERM),
207
208 CASE ( 0, "abc\0", "abcd", 0, CMP_NULTERM),
209 CASE ( 0, "abc\0", "abcd", 1, CMP_NULTERM),
210 CASE ( 0, "abc\0", "abcd", 2, CMP_NULTERM),
211 CASE ( 0, "abc\0", "abcd", 3, CMP_NULTERM),
212 CASE (-1, "abc\0", "abcd", 4, CMP_NULTERM),
213 CASE (-1, "abc\0", "abcd", 5, CMP_NULTERM)
214};
215
216
217template <class T, class U>
218void
219test_all_cases (const T* /* dummy */, const U* /* dummy */,
220 const char *tname, const char *uname)
221{
222 // print out the name of the specialization being tested
223 static const int one_time_per_specialization =
224 printf ("rw_valcmp(const %s*, const %s*, size_t, int)\n",
225 tname, uname);
226
227 _RWSTD_UNUSED (one_time_per_specialization);
228
229 for (size_t i = 0; i != sizeof cases / sizeof cases; ++i) {
230 test_case ((T*)0, (U*)0,
231 tname, uname,
232 cases [i].line,
233 cases [i].expect,
234 cases [i].str1,
235 cases [i].str2,
236 cases [i].nelems,
237 cases [i].flags);
238 }
239}
240
241
242#define STR(x) #x
243
244
245template <class T>
246void
247run_test (const T* /* dummy */, const char *tname)
248{
249#define TEST_ALL_CASES(U) test_all_cases ((T*)0, (U*)0, tname, STR (U))
250
251 // run all test cases for all integral types except bool
252 TEST_ALL_CASES (char);
253
254 TEST_ALL_CASES (signed char);
255 TEST_ALL_CASES (unsigned char);
256 TEST_ALL_CASES (short);
257 TEST_ALL_CASES (unsigned short);
258 TEST_ALL_CASES (int);
259 TEST_ALL_CASES (unsigned int);
260 TEST_ALL_CASES (long);
261 TEST_ALL_CASES (unsigned long);
262
263#ifdef _RWSTD_LONG_LONG
264
267
268#endif // _RWSTD_LONG_LONG
269
270#ifndef _RWSTD_NO_WCHAR_T
271
272 TEST_ALL_CASES (wchar_t);
273
274#endif // _RWSTD_NO_WCHAR_T
275}
276
277
278int main ()
279{
280#define RUN_TEST(T) run_test ((T*)0, STR (T))
281
282 // run the for all integral types except bool
283 RUN_TEST (char);
284 RUN_TEST (signed char);
285 RUN_TEST (unsigned char);
286 RUN_TEST (short);
287 RUN_TEST (unsigned short);
288 RUN_TEST (int);
289 RUN_TEST (unsigned int);
290 RUN_TEST (long);
291 RUN_TEST (unsigned long);
292
293#ifdef _RWSTD_LONG_LONG
294
296 RUN_TEST (unsigned _RWSTD_LONG_LONG);
297
298#endif // _RWSTD_LONG_LONG
299
300#ifndef _RWSTD_NO_WCHAR_T
301
302 RUN_TEST (wchar_t);
303
304#endif // _RWSTD_NO_WCHAR_T
305
306 return exit_status;
307}
int exit_status
void test_case(const T *, const U *, const char *tname, const char *uname, int line, int expect, const char *str1, const char *str2, size_t nelems, int flags)
Definition 0.valcmp.cpp:41
#define RUN_TEST(T)
void test_all_cases(const T *, const U *, const char *tname, const char *uname)
Definition 0.valcmp.cpp:219
const Case cases[]
void run_test(const T *, const char *tname)
Definition 0.valcmp.cpp:247
#define TEST_ALL_CASES(U)
#define CASE(expect, str1, str2, nelems, flags)
int main()
Definition 0.valcmp.cpp:278
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define line
Definition 17.names.cpp:490
#define U
Definition 17.names.cpp:164
volatile int result
#define flags
IMPORT_C SInt32 printf(const Char *,...)
#define _RWSTD_LONG_LONG
#define stderr
#define _RWSTD_UNUSED(arg)
Definition _defs.h:972
#define _RWSTD_STATIC_CAST(T, x)
Definition _defs.h:534
_TEST_EXPORT int rw_valcmp(const void *, const void *, _RWSTD_SIZE_T, _RWSTD_SIZE_T, _RWSTD_SIZE_T, int)
#define CMP_NOCASE
Definition rw_valcmp.h:37
#define CMP_NULTERM
Definition rw_valcmp.h:35
unsigned char UChar
Definition collate.cpp:122
size_t nelems
Definition 0.valcmp.cpp:74
const char * str1
Definition 0.valcmp.cpp:72
int line
Definition 0.valcmp.cpp:70
const char * str2
Definition 0.valcmp.cpp:73
int flags
Definition 0.valcmp.cpp:75
int expect
Definition 0.valcmp.cpp:71