Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
0.strncmp.cpp
Go to the documentation of this file.
1
29#include <rw_valcmp.h> // for rw_strncmp()
30
31#include <stdio.h> // for fprintf()
32
33
35static int
36run_test (int, char*[])
37{
38 int exit_status = 0;
39
40 // rw_strncmp():
41 //
42 // compares up to a maximum number of characters from the two strings
43 // posisbly including any embedded NULs (when the CMP_NULTERM bit is
44 // clear) and returns -1, 0, or +1 if the first string compares less,
45 // equal, or greater, respectively, than the second string, or the
46 // offset of the first mismatched character (when the CMP_RETOFF bit
47 // is set) or an out of bounds value such as -1 when no such character
48 // exists
49 //
50 // rw_strncmp(s1, s2) is equivalent to a call to strcmp(s1, s2) when
51 // the type of s1 and s2 is char*, wcscmp(s1, s2) when the type is
52 // wchar_t*
53 //
54 // when (N != SIZE_MAX) is true,
55 // rw_strncmp(s1, s2, N, CMP_NULTERM) is equivalent to a call to
56 // strncmp(s1, s2, N) or wcsncmp(s1, s2, N), respectively
57 //
58 // rw_strncmp(s1, s2, N) is equivalent to a call to memcmp(s1, s2, N)
59 // or wmemcmp(s1, s2, n), respectively
60
61#undef TEST
62#define TEST(expect, s1, s2, nc, fl) \
63 do { \
64 const int result = (fl) < 0 ? \
65 rw_strncmp (s1, s2, nc) : rw_strncmp (s1, s2, nc, fl); \
66 if (expect != result) { \
67 exit_status = 2; \
68 fprintf (stderr, "line %d: rw_strncmp(%p, %p, %u, %d) == %d, " \
69 "got %d\n", __LINE__, (const void*)s1, \
70 (const void*)s2, unsigned (nc), fl, expect, \
71 result); \
72 } \
73 } while (0)
74
76 printf ("rw_strncmp(const char*, const char*, size_t, int)\n");
77
78 // +------------------ expected result
79 // | +--------------- first string
80 // | | +----------- second string
81 // | | | +------ value of N
82 // | | | | +-- value of flags, or none when -1
83 // | | | | |
84 // v v v v v
85 TEST ( 0, "", "", _RWSTD_SIZE_MAX, -1);
86 TEST (+1, "a", "", _RWSTD_SIZE_MAX, -1);
87 TEST (-1, "", "a", _RWSTD_SIZE_MAX, -1);
88
89 TEST ( 0, "a", "b", 0, -1);
90 TEST ( 0, "a\0b", "a\0b", 3, -1);
91 TEST ( 0, "a\0c", "a\0b", 3, -1);
92 TEST ( 0, "a\0b", "a\0c", 3, -1);
93
94 TEST ( 0, "a\0b", "a\0b", 3, CMP_NULTERM);
95 TEST (+1, "a\0c", "a\0b", 3, 0);
96 TEST (-1, "a\0b", "a\0c", 3, 0);
97
98 TEST (-1, "a\0b", "a\0b", 3, CMP_NULTERM | CMP_RETOFF);
99 TEST (-1, "a\0c", "a\0b", 3, CMP_NULTERM | CMP_RETOFF);
100 TEST (-1, "a\0b", "a\0c", 3, CMP_NULTERM | CMP_RETOFF);
101
102 TEST (-1, "a\0b", "a\0b", 3, CMP_RETOFF);
103 TEST ( 2, "a\0c", "a\0b", 3, CMP_RETOFF);
104 TEST ( 2, "a\0b", "a\0c", 3, CMP_RETOFF);
105
106#ifndef _RWSTD_NO_WCHAR_T
107
109 printf ("rw_strncmp(const wchar_t*, const wchar_t*, size_t, int)\n");
110
111 TEST ( 0, L"", L"", ~0, -1);
112 TEST (+1, L"a", L"", ~0, -1);
113 TEST (-1, L"", L"a", ~0, -1);
114
115 TEST ( 0, L"a", L"b", 0, -1);
116 TEST ( 0, L"a\0b", L"a\0b", 3, -1);
117 TEST ( 0, L"a\0c", L"a\0b", 3, -1);
118 TEST ( 0, L"a\0b", L"a\0c", 3, -1);
119
120 TEST ( 0, L"a\0b", L"a\0b", 3, 0);
121 TEST (+1, L"a\0c", L"a\0b", 3, 0);
122 TEST (-1, L"a\0b", L"a\0c", 3, 0);
123
124 TEST (-1, L"a\0b", L"a\0b", 3, CMP_RETOFF);
125 TEST (+2, L"a\0c", L"a\0b", 3, CMP_RETOFF);
126 TEST (+2, L"a\0b", L"a\0c", 3, CMP_RETOFF);
127
128 TEST (-1, L"a\0b", L"a\0b", 3, CMP_NULTERM | CMP_RETOFF);
129 TEST (-1, L"a\0c", L"a\0b", 3, CMP_NULTERM | CMP_RETOFF);
130 TEST (-1, L"a\0b", L"a\0c", 3, CMP_NULTERM | CMP_RETOFF);
131
133 printf ("rw_strncmp(const wchar_t*, const char*, size_t, int)\n");
134
135 TEST ( 0, L"", "", ~0, -1);
136 TEST (+1, L"a", "", ~0, -1);
137 TEST (-1, L"", "a", ~0, -1);
138
139 TEST ( 0, L"a", "b", 0, -1);
140 TEST ( 0, L"a\0b", "a\0b", 3, -1);
141 TEST ( 0, L"a\0c", "a\0b", 3, -1);
142 TEST ( 0, L"a\0b", "a\0c", 3, -1);
143
144 TEST ( 0, L"a\0b", "a\0b", 3, CMP_NULTERM);
145 TEST ( 0, L"a\0c", "a\0b", 3, CMP_NULTERM);
146 TEST ( 0, L"a\0b", "a\0c", 3, CMP_NULTERM);
147
148 TEST (-1, L"a\0b", "a\0b", 3, CMP_RETOFF);
149 TEST ( 2, L"a\0c", "a\0b", 3, CMP_RETOFF);
150 TEST ( 2, L"a\0b", "a\0c", 3, CMP_RETOFF);
151
152 TEST (-1, L"a\0b", "a\0b", 3, CMP_NULTERM | CMP_RETOFF);
153 TEST (-1, L"a\0c", "a\0b", 3, CMP_NULTERM | CMP_RETOFF);
154 TEST (-1, L"a\0b", "a\0c", 3, CMP_NULTERM | CMP_RETOFF);
155
157 printf ("rw_strncmp(const char*, const wchar_t*, size_t, int)\n");
158
159 TEST ( 0, "", L"", ~0, -1);
160 TEST (+1, "a", L"", ~0, -1);
161 TEST (-1, "", L"a", ~0, -1);
162
163 TEST ( 0, "a", L"b", 0, -1);
164 TEST ( 0, "a\0b", L"a\0b", 3, -1);
165 TEST ( 0, "a\0c", L"a\0b", 3, -1);
166 TEST ( 0, "a\0b", L"a\0c", 3, -1);
167
168 TEST ( 0, "a\0b", L"a\0b", 3, CMP_NULTERM);
169 TEST ( 0, "a\0c", L"a\0b", 3, CMP_NULTERM);
170 TEST ( 0, "a\0b", L"a\0c", 3, CMP_NULTERM);
171
172 TEST (-1, "a\0b", L"a\0b", 3, CMP_RETOFF);
173 TEST ( 2, "a\0c", L"a\0b", 3, CMP_RETOFF);
174 TEST ( 2, "a\0b", L"a\0c", 3, CMP_RETOFF);
175
176 TEST (-1, "a\0b", L"a\0b", 3, CMP_NULTERM | CMP_RETOFF);
177 TEST (-1, "a\0c", L"a\0b", 3, CMP_NULTERM | CMP_RETOFF);
178 TEST (-1, "a\0b", L"a\0c", 3, CMP_NULTERM | CMP_RETOFF);
179
180#endif // _RWSTD_NO_WCHAR_T
181
182 return exit_status;
183}
184
185
187int main (int argc, char *argv[])
188{
189 return run_test (argc, argv);
190}
int exit_status
#define TEST(expect, s1, s2, nc, fl)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
IMPORT_C SInt32 printf(const Char *,...)
#define CMP_NULTERM
Definition rw_valcmp.h:35
#define CMP_RETOFF
Definition rw_valcmp.h:36