docs: fix opening and ending tag mismatch: para
[Samba/gebeck_regimport.git] / lib / util / charset / tests / charset.c
blob70b674187631abc750fd11a9d7117ce93d25b7fe
1 /*
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/>.
21 #include "includes.h"
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");
31 return true;
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");
39 return true;
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");
48 return true;
51 static bool test_strcasecmp_m(struct torture_context *tctx)
53 torture_assert(tctx, strcasecmp_m("foo", "bar") != 0, "different strings");
54 torture_assert(tctx, strcasecmp_m("foo", "foo") == 0, "same case strings");
55 torture_assert(tctx, strcasecmp_m("foo", "Foo") == 0, "different case strings");
56 torture_assert(tctx, strcasecmp_m(NULL, "Foo") != 0, "one NULL");
57 torture_assert(tctx, strcasecmp_m("foo", NULL) != 0, "other NULL");
58 torture_assert(tctx, strcasecmp_m(NULL, NULL) == 0, "both NULL");
59 return true;
63 static bool test_strequal_m(struct torture_context *tctx)
65 torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
66 torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
67 torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
68 torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
69 torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
70 torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
71 return true;
74 static bool test_strcsequal(struct torture_context *tctx)
76 torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
77 torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
78 torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
79 torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
80 torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
81 torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
82 return true;
85 static bool test_string_replace_m(struct torture_context *tctx)
87 char data[6] = "bla";
88 string_replace_m(data, 'b', 'c');
89 torture_assert_str_equal(tctx, data, "cla", "first char replaced");
90 memcpy(data, "bab", 4);
91 string_replace_m(data, 'b', 'c');
92 torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
93 memcpy(data, "bba", 4);
94 string_replace_m(data, 'b', 'c');
95 torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
96 memcpy(data, "blala", 6);
97 string_replace_m(data, 'o', 'c');
98 torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
99 string_replace_m(NULL, 'b', 'c');
100 return true;
103 static bool test_strncasecmp_m(struct torture_context *tctx)
105 torture_assert(tctx, strncasecmp_m("foo", "bar", 3) != 0, "different strings");
106 torture_assert(tctx, strncasecmp_m("foo", "foo", 3) == 0, "same case strings");
107 torture_assert(tctx, strncasecmp_m("foo", "Foo", 3) == 0, "different case strings");
108 torture_assert(tctx, strncasecmp_m("fool", "Foo", 3) == 0, "different case strings");
109 torture_assert(tctx, strncasecmp_m("fool", "Fool", 40) == 0, "over size");
110 torture_assert(tctx, strncasecmp_m("BLA", "Fool", 0) == 0, "empty");
111 torture_assert(tctx, strncasecmp_m(NULL, "Foo", 3) != 0, "one NULL");
112 torture_assert(tctx, strncasecmp_m("foo", NULL, 3) != 0, "other NULL");
113 torture_assert(tctx, strncasecmp_m(NULL, NULL, 3) == 0, "both NULL");
114 return true;
117 static bool test_next_token_null(struct torture_context *tctx)
119 char buf[20];
120 torture_assert(tctx, !next_token(NULL, buf, " ", 20), "null ptr works");
121 return true;
124 static bool test_next_token(struct torture_context *tctx)
126 const char *teststr = "foo bar bla";
127 char buf[20];
128 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
129 torture_assert_str_equal(tctx, buf, "foo", "token matches");
130 torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
132 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
133 torture_assert_str_equal(tctx, buf, "bar", "token matches");
134 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
136 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
137 torture_assert_str_equal(tctx, buf, "bla", "token matches");
138 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
140 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
141 return true;
144 static bool test_next_token_implicit_sep(struct torture_context *tctx)
146 const char *teststr = "foo\tbar\n bla";
147 char buf[20];
148 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
149 torture_assert_str_equal(tctx, buf, "foo", "token matches");
150 torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
152 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
153 torture_assert_str_equal(tctx, buf, "bar", "token matches");
154 torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
156 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
157 torture_assert_str_equal(tctx, buf, "bla", "token matches");
158 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
160 torture_assert(tctx, !next_token(&teststr, buf, NULL, 20), "finding token doesn't work");
161 return true;
164 static bool test_next_token_seps(struct torture_context *tctx)
166 const char *teststr = ",foo bla";
167 char buf[20];
168 torture_assert(tctx, next_token(&teststr, buf, ",", 20), "finding token works");
169 torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
170 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
172 torture_assert(tctx, !next_token(&teststr, buf, ",", 20), "finding token doesn't work");
173 return true;
176 static bool test_next_token_quotes(struct torture_context *tctx)
178 const char *teststr = "\"foo bar\" bla";
179 char buf[20];
180 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
181 torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
182 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
184 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
185 torture_assert_str_equal(tctx, buf, "bla", "token matches");
186 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
188 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
189 return true;
192 static bool test_next_token_quote_wrong(struct torture_context *tctx)
194 const char *teststr = "\"foo bar bla";
195 char buf[20];
196 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
197 torture_assert_str_equal(tctx, buf, "foo bar 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");
201 return true;
204 static bool test_strlen_m(struct torture_context *tctx)
206 torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
207 torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
208 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
209 return true;
212 static bool test_strlen_m_term(struct torture_context *tctx)
214 torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
215 torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
216 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
217 return true;
220 static bool test_strhaslower(struct torture_context *tctx)
222 torture_assert(tctx, strhaslower("a"), "one low char");
223 torture_assert(tctx, strhaslower("aB"), "one low, one up char");
224 torture_assert(tctx, !strhaslower("B"), "one up char");
225 torture_assert(tctx, !strhaslower(""), "empty string");
226 torture_assert(tctx, !strhaslower("3"), "one digit");
227 return true;
230 static bool test_strhasupper(struct torture_context *tctx)
232 torture_assert(tctx, strhasupper("B"), "one up char");
233 torture_assert(tctx, strhasupper("aB"), "one low, one up char");
234 torture_assert(tctx, !strhasupper("a"), "one low char");
235 torture_assert(tctx, !strhasupper(""), "empty string");
236 torture_assert(tctx, !strhasupper("3"), "one digit");
237 return true;
240 static bool test_count_chars_m(struct torture_context *tctx)
242 torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
243 torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
244 torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
245 torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
246 return true;
249 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
251 struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
253 torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
254 torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
255 torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
256 torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
257 torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
258 torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
259 torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
260 torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
261 torture_suite_add_simple_test(suite, "next_token", test_next_token);
262 torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
263 torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
264 torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
265 torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
266 torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
267 torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
268 torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
269 torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
270 torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
271 torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);
273 return suite;