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 static bool test_toupper_m(struct torture_context
*tctx
)
26 torture_assert_int_equal(tctx
, toupper_m('c'), 'C', "c");
27 torture_assert_int_equal(tctx
, toupper_m('Z'), 'Z', "z");
28 torture_assert_int_equal(tctx
, toupper_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
32 static bool test_tolower_m(struct torture_context
*tctx
)
34 torture_assert_int_equal(tctx
, tolower_m('C'), 'c', "c");
35 torture_assert_int_equal(tctx
, tolower_m('z'), 'z', "z");
36 torture_assert_int_equal(tctx
, tolower_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
40 static bool test_codepoint_cmpi(struct torture_context
*tctx
)
42 torture_assert_int_equal(tctx
, codepoint_cmpi('a', 'a'), 0, "same char");
43 torture_assert_int_equal(tctx
, codepoint_cmpi('A', 'a'), 0, "upcase version");
44 torture_assert_int_equal(tctx
, codepoint_cmpi('b', 'a'), 1, "right diff");
45 torture_assert_int_equal(tctx
, codepoint_cmpi('a', 'b'), -1, "right diff");
49 static bool test_strcasecmp_m(struct torture_context
*tctx
)
51 torture_assert(tctx
, strcasecmp_m("foo", "bar") != 0, "different strings");
52 torture_assert(tctx
, strcasecmp_m("foo", "foo") == 0, "same case strings");
53 torture_assert(tctx
, strcasecmp_m("foo", "Foo") == 0, "different case strings");
54 torture_assert(tctx
, strcasecmp_m(NULL
, "Foo") != 0, "one NULL");
55 torture_assert(tctx
, strcasecmp_m("foo", NULL
) != 0, "other NULL");
56 torture_assert(tctx
, strcasecmp_m(NULL
, NULL
) == 0, "both NULL");
61 static bool test_strequal_m(struct torture_context
*tctx
)
63 torture_assert(tctx
, !strequal_m("foo", "bar"), "different strings");
64 torture_assert(tctx
, strequal_m("foo", "foo"), "same case strings");
65 torture_assert(tctx
, strequal_m("foo", "Foo"), "different case strings");
66 torture_assert(tctx
, !strequal_m(NULL
, "Foo"), "one NULL");
67 torture_assert(tctx
, !strequal_m("foo", NULL
), "other NULL");
68 torture_assert(tctx
, strequal_m(NULL
, NULL
), "both NULL");
72 static bool test_strcsequal_m(struct torture_context
*tctx
)
74 torture_assert(tctx
, !strcsequal_m("foo", "bar"), "different strings");
75 torture_assert(tctx
, strcsequal_m("foo", "foo"), "same case strings");
76 torture_assert(tctx
, !strcsequal_m("foo", "Foo"), "different case strings");
77 torture_assert(tctx
, !strcsequal_m(NULL
, "Foo"), "one NULL");
78 torture_assert(tctx
, !strcsequal_m("foo", NULL
), "other NULL");
79 torture_assert(tctx
, strcsequal_m(NULL
, NULL
), "both NULL");
83 static bool test_string_replace_m(struct torture_context
*tctx
)
86 string_replace_m(data
, 'b', 'c');
87 torture_assert_str_equal(tctx
, data
, "cla", "first char replaced");
88 memcpy(data
, "bab", 4);
89 string_replace_m(data
, 'b', 'c');
90 torture_assert_str_equal(tctx
, data
, "cac", "other chars replaced");
91 memcpy(data
, "bba", 4);
92 string_replace_m(data
, 'b', 'c');
93 torture_assert_str_equal(tctx
, data
, "cca", "other chars replaced");
94 memcpy(data
, "blala", 6);
95 string_replace_m(data
, 'o', 'c');
96 torture_assert_str_equal(tctx
, data
, "blala", "no chars replaced");
97 string_replace_m(NULL
, 'b', 'c');
101 static bool test_strncasecmp_m(struct torture_context
*tctx
)
103 torture_assert(tctx
, strncasecmp_m("foo", "bar", 3) != 0, "different strings");
104 torture_assert(tctx
, strncasecmp_m("foo", "foo", 3) == 0, "same case strings");
105 torture_assert(tctx
, strncasecmp_m("foo", "Foo", 3) == 0, "different case strings");
106 torture_assert(tctx
, strncasecmp_m("fool", "Foo", 3) == 0, "different case strings");
107 torture_assert(tctx
, strncasecmp_m("fool", "Fool", 40) == 0, "over size");
108 torture_assert(tctx
, strncasecmp_m("BLA", "Fool", 0) == 0, "empty");
109 torture_assert(tctx
, strncasecmp_m(NULL
, "Foo", 3) != 0, "one NULL");
110 torture_assert(tctx
, strncasecmp_m("foo", NULL
, 3) != 0, "other NULL");
111 torture_assert(tctx
, strncasecmp_m(NULL
, NULL
, 3) == 0, "both NULL");
115 static bool test_next_token_null(struct torture_context
*tctx
)
118 torture_assert(tctx
, !next_token(NULL
, buf
, " ", 20), "null ptr works");
122 static bool test_next_token(struct torture_context
*tctx
)
124 const char *teststr
= "foo bar bla";
126 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
127 torture_assert_str_equal(tctx
, buf
, "foo", "token matches");
128 torture_assert_str_equal(tctx
, teststr
, "bar bla", "ptr modified correctly");
130 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
131 torture_assert_str_equal(tctx
, buf
, "bar", "token matches");
132 torture_assert_str_equal(tctx
, teststr
, "bla", "ptr modified correctly");
134 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
135 torture_assert_str_equal(tctx
, buf
, "bla", "token matches");
136 torture_assert_str_equal(tctx
, teststr
, "", "ptr modified correctly");
138 torture_assert(tctx
, !next_token(&teststr
, buf
, " ", 20), "finding token doesn't work");
142 static bool test_next_token_implicit_sep(struct torture_context
*tctx
)
144 const char *teststr
= "foo\tbar\n bla";
146 torture_assert(tctx
, next_token(&teststr
, buf
, NULL
, 20), "finding token works");
147 torture_assert_str_equal(tctx
, buf
, "foo", "token matches");
148 torture_assert_str_equal(tctx
, teststr
, "bar\n bla", "ptr modified correctly");
150 torture_assert(tctx
, next_token(&teststr
, buf
, NULL
, 20), "finding token works");
151 torture_assert_str_equal(tctx
, buf
, "bar", "token matches");
152 torture_assert_str_equal(tctx
, teststr
, " bla", "ptr modified correctly");
154 torture_assert(tctx
, next_token(&teststr
, buf
, NULL
, 20), "finding token works");
155 torture_assert_str_equal(tctx
, buf
, "bla", "token matches");
156 torture_assert_str_equal(tctx
, teststr
, "", "ptr modified correctly");
158 torture_assert(tctx
, !next_token(&teststr
, buf
, NULL
, 20), "finding token doesn't work");
162 static bool test_next_token_seps(struct torture_context
*tctx
)
164 const char *teststr
= ",foo bla";
166 torture_assert(tctx
, next_token(&teststr
, buf
, ",", 20), "finding token works");
167 torture_assert_str_equal(tctx
, buf
, "foo bla", "token matches");
168 torture_assert_str_equal(tctx
, teststr
, "", "ptr modified correctly");
170 torture_assert(tctx
, !next_token(&teststr
, buf
, ",", 20), "finding token doesn't work");
174 static bool test_next_token_quotes(struct torture_context
*tctx
)
176 const char *teststr
= "\"foo bar\" bla";
178 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
179 torture_assert_str_equal(tctx
, buf
, "foo bar", "token matches");
180 torture_assert_str_equal(tctx
, teststr
, "bla", "ptr modified correctly");
182 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
183 torture_assert_str_equal(tctx
, buf
, "bla", "token matches");
184 torture_assert_str_equal(tctx
, teststr
, "", "ptr modified correctly");
186 torture_assert(tctx
, !next_token(&teststr
, buf
, " ", 20), "finding token doesn't work");
190 static bool test_next_token_quote_wrong(struct torture_context
*tctx
)
192 const char *teststr
= "\"foo bar bla";
194 torture_assert(tctx
, next_token(&teststr
, buf
, " ", 20), "finding token works");
195 torture_assert_str_equal(tctx
, buf
, "foo bar bla", "token matches");
196 torture_assert_str_equal(tctx
, teststr
, "", "ptr modified correctly");
198 torture_assert(tctx
, !next_token(&teststr
, buf
, " ", 20), "finding token doesn't work");
202 static bool test_strlen_m(struct torture_context
*tctx
)
204 torture_assert_int_equal(tctx
, strlen_m("foo"), 3, "simple len");
205 torture_assert_int_equal(tctx
, strlen_m("foo\x83l"), 6, "extended len");
206 torture_assert_int_equal(tctx
, strlen_m(NULL
), 0, "NULL");
210 static bool test_strlen_m_term(struct torture_context
*tctx
)
212 torture_assert_int_equal(tctx
, strlen_m_term("foo"), 4, "simple len");
213 torture_assert_int_equal(tctx
, strlen_m_term("foo\x83l"), 7, "extended len");
214 torture_assert_int_equal(tctx
, strlen_m(NULL
), 0, "NULL");
218 static bool test_strhaslower(struct torture_context
*tctx
)
220 torture_assert(tctx
, strhaslower("a"), "one low char");
221 torture_assert(tctx
, strhaslower("aB"), "one low, one up char");
222 torture_assert(tctx
, !strhaslower("B"), "one up char");
223 torture_assert(tctx
, !strhaslower(""), "empty string");
224 torture_assert(tctx
, !strhaslower("3"), "one digit");
228 static bool test_strhasupper(struct torture_context
*tctx
)
230 torture_assert(tctx
, strhasupper("B"), "one up char");
231 torture_assert(tctx
, strhasupper("aB"), "one low, one up char");
232 torture_assert(tctx
, !strhasupper("a"), "one low char");
233 torture_assert(tctx
, !strhasupper(""), "empty string");
234 torture_assert(tctx
, !strhasupper("3"), "one digit");
238 static bool test_count_chars_m(struct torture_context
*tctx
)
240 torture_assert_int_equal(tctx
, count_chars_m("foo", 'o'), 2, "simple");
241 torture_assert_int_equal(tctx
, count_chars_m("", 'o'), 0, "empty");
242 torture_assert_int_equal(tctx
, count_chars_m("bla", 'o'), 0, "none");
243 torture_assert_int_equal(tctx
, count_chars_m("bla", '\0'), 0, "null");
247 struct torture_suite
*torture_local_charset(TALLOC_CTX
*mem_ctx
)
249 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "CHARSET");
251 torture_suite_add_simple_test(suite
, "toupper_m", test_toupper_m
);
252 torture_suite_add_simple_test(suite
, "tolower_m", test_tolower_m
);
253 torture_suite_add_simple_test(suite
, "codepoint_cmpi", test_codepoint_cmpi
);
254 torture_suite_add_simple_test(suite
, "strcasecmp_m", test_strcasecmp_m
);
255 torture_suite_add_simple_test(suite
, "strequal_m", test_strequal_m
);
256 torture_suite_add_simple_test(suite
, "strcsequal_m", test_strcsequal_m
);
257 torture_suite_add_simple_test(suite
, "string_replace_m", test_string_replace_m
);
258 torture_suite_add_simple_test(suite
, "strncasecmp_m", test_strncasecmp_m
);
259 torture_suite_add_simple_test(suite
, "next_token", test_next_token
);
260 torture_suite_add_simple_test(suite
, "next_token_null", test_next_token_null
);
261 torture_suite_add_simple_test(suite
, "next_token_implicit_sep", test_next_token_implicit_sep
);
262 torture_suite_add_simple_test(suite
, "next_token_quotes", test_next_token_quotes
);
263 torture_suite_add_simple_test(suite
, "next_token_seps", test_next_token_seps
);
264 torture_suite_add_simple_test(suite
, "next_token_quote_wrong", test_next_token_quote_wrong
);
265 torture_suite_add_simple_test(suite
, "strlen_m", test_strlen_m
);
266 torture_suite_add_simple_test(suite
, "strlen_m_term", test_strlen_m_term
);
267 torture_suite_add_simple_test(suite
, "strhaslower", test_strhaslower
);
268 torture_suite_add_simple_test(suite
, "strhasupper", test_strhasupper
);
269 torture_suite_add_simple_test(suite
, "count_chars_m", test_count_chars_m
);