2 Unix SMB/CIFS implementation.
3 test suite for the charcnv functions
5 Copyright (C) Jelmer Vernooij 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "torture/torture.h"
24 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx);
26 static bool test_toupper_m(struct torture_context *tctx)
28 torture_assert_int_equal(tctx, toupper_m('c'), 'C', "c");
29 torture_assert_int_equal(tctx, toupper_m('Z'), 'Z', "z");
30 torture_assert_int_equal(tctx, toupper_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
34 static bool test_tolower_m(struct torture_context *tctx)
36 torture_assert_int_equal(tctx, tolower_m('C'), 'c', "c");
37 torture_assert_int_equal(tctx, tolower_m('z'), 'z', "z");
38 torture_assert_int_equal(tctx, tolower_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
42 static bool test_codepoint_cmpi(struct torture_context *tctx)
44 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'a'), 0, "same char");
45 torture_assert_int_equal(tctx, codepoint_cmpi('A', 'a'), 0, "upcase version");
46 torture_assert_int_equal(tctx, codepoint_cmpi('b', 'a'), 1, "right diff");
47 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'b'), -1, "right diff");
51 static bool test_strcasecmp_m(struct torture_context *tctx)
53 /* file.{accented e} in iso8859-1 */
54 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
55 /* file.{accented e} in utf8 */
56 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
57 torture_assert(tctx, strcasecmp_m("foo", "bar") != 0, "different strings");
58 torture_assert(tctx, strcasecmp_m("foo", "foo") == 0, "same case strings");
59 torture_assert(tctx, strcasecmp_m("foo", "Foo") == 0, "different case strings");
60 torture_assert(tctx, strcasecmp_m(NULL, "Foo") != 0, "one NULL");
61 torture_assert(tctx, strcasecmp_m("foo", NULL) != 0, "other NULL");
62 torture_assert(tctx, strcasecmp_m(NULL, NULL) == 0, "both NULL");
63 torture_assert(tctx, strcasecmp_m(file_iso8859_1, file_utf8) != 0,
64 "file.{accented e} should differ");
69 static bool test_strequal_m(struct torture_context *tctx)
71 torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
72 torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
73 torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
74 torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
75 torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
76 torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
80 static bool test_strcsequal(struct torture_context *tctx)
82 torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
83 torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
84 torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
85 torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
86 torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
87 torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
91 static bool test_string_replace_m(struct torture_context *tctx)
94 string_replace_m(data, 'b', 'c');
95 torture_assert_str_equal(tctx, data, "cla", "first char replaced");
96 memcpy(data, "bab", 4);
97 string_replace_m(data, 'b', 'c');
98 torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
99 memcpy(data, "bba", 4);
100 string_replace_m(data, 'b', 'c');
101 torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
102 memcpy(data, "blala", 6);
103 string_replace_m(data, 'o', 'c');
104 torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
105 string_replace_m(NULL, 'b', 'c');
109 static bool test_strncasecmp_m(struct torture_context *tctx)
111 /* file.{accented e} in iso8859-1 */
112 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
113 /* file.{accented e} in utf8 */
114 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
115 torture_assert(tctx, strncasecmp_m("foo", "bar", 3) != 0, "different strings");
116 torture_assert(tctx, strncasecmp_m("foo", "foo", 3) == 0, "same case strings");
117 torture_assert(tctx, strncasecmp_m("foo", "Foo", 3) == 0, "different case strings");
118 torture_assert(tctx, strncasecmp_m("fool", "Foo", 3) == 0, "different case strings");
119 torture_assert(tctx, strncasecmp_m("fool", "Fool", 40) == 0, "over size");
120 torture_assert(tctx, strncasecmp_m("BLA", "Fool", 0) == 0, "empty");
121 torture_assert(tctx, strncasecmp_m(NULL, "Foo", 3) != 0, "one NULL");
122 torture_assert(tctx, strncasecmp_m("foo", NULL, 3) != 0, "other NULL");
123 torture_assert(tctx, strncasecmp_m(NULL, NULL, 3) == 0, "both NULL");
124 torture_assert(tctx, strncasecmp_m(file_iso8859_1, file_utf8, 6) != 0,
125 "file.{accented e} should differ");
129 static bool test_next_token_null(struct torture_context *tctx)
132 torture_assert(tctx, !next_token(NULL, buf, " ", 20), "null ptr works");
136 static bool test_next_token(struct torture_context *tctx)
138 const char *teststr = "foo bar bla";
140 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
141 torture_assert_str_equal(tctx, buf, "foo", "token matches");
142 torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
144 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
145 torture_assert_str_equal(tctx, buf, "bar", "token matches");
146 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
148 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
149 torture_assert_str_equal(tctx, buf, "bla", "token matches");
150 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
152 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
156 static bool test_next_token_implicit_sep(struct torture_context *tctx)
158 const char *teststr = "foo\tbar\n bla";
160 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
161 torture_assert_str_equal(tctx, buf, "foo", "token matches");
162 torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
164 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
165 torture_assert_str_equal(tctx, buf, "bar", "token matches");
166 torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
168 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
169 torture_assert_str_equal(tctx, buf, "bla", "token matches");
170 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
172 torture_assert(tctx, !next_token(&teststr, buf, NULL, 20), "finding token doesn't work");
176 static bool test_next_token_seps(struct torture_context *tctx)
178 const char *teststr = ",foo bla";
180 torture_assert(tctx, next_token(&teststr, buf, ",", 20), "finding token works");
181 torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
182 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
184 torture_assert(tctx, !next_token(&teststr, buf, ",", 20), "finding token doesn't work");
188 static bool test_next_token_quotes(struct torture_context *tctx)
190 const char *teststr = "\"foo bar\" bla";
192 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
193 torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
194 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
196 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
197 torture_assert_str_equal(tctx, buf, "bla", "token matches");
198 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
200 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
204 static bool test_next_token_quote_wrong(struct torture_context *tctx)
206 const char *teststr = "\"foo bar bla";
208 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
209 torture_assert_str_equal(tctx, buf, "foo bar bla", "token matches");
210 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
212 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
216 static bool test_strlen_m(struct torture_context *tctx)
218 torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
219 torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
220 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
224 static bool test_strlen_m_term(struct torture_context *tctx)
226 torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
227 torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
228 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
232 static bool test_strhaslower(struct torture_context *tctx)
234 torture_assert(tctx, strhaslower("a"), "one low char");
235 torture_assert(tctx, strhaslower("aB"), "one low, one up char");
236 torture_assert(tctx, !strhaslower("B"), "one up char");
237 torture_assert(tctx, !strhaslower(""), "empty string");
238 torture_assert(tctx, !strhaslower("3"), "one digit");
242 static bool test_strhasupper(struct torture_context *tctx)
244 torture_assert(tctx, strhasupper("B"), "one up char");
245 torture_assert(tctx, strhasupper("aB"), "one low, one up char");
246 torture_assert(tctx, !strhasupper("a"), "one low char");
247 torture_assert(tctx, !strhasupper(""), "empty string");
248 torture_assert(tctx, !strhasupper("3"), "one digit");
252 static bool test_count_chars_m(struct torture_context *tctx)
254 torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
255 torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
256 torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
257 torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
261 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
263 struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
265 torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
266 torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
267 torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
268 torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
269 torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
270 torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
271 torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
272 torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
273 torture_suite_add_simple_test(suite, "next_token", test_next_token);
274 torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
275 torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
276 torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
277 torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
278 torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
279 torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
280 torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
281 torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
282 torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
283 torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);