util/charset/tests: Add tests for UTF‐16 string length functions
[Samba.git] / lib / util / charset / tests / util_unistr.c
blob1a9fcaafdf5a1296ee246899959465dd1ebfde0e
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for the util_unistr utility functions
5 Copyright (C) Catalyst.Net Ltd. 2023
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 #undef strcasecmp
25 #undef strncasecmp
27 struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx);
29 static bool test_utf16_len(struct torture_context *tctx)
31 static const uint16_t empty_string[] = {'\0'};
32 static const uint16_t foo_bar[] = {
33 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
34 static const uint16_t foo_bar_alternative[] = {0xd83c,
35 0xdd75,
36 0xd83c,
37 0xdd7e,
38 0xd83c,
39 0xdd7e,
40 ' ',
41 0xd83c,
42 0xdd31,
43 0xd83c,
44 0xdd30,
45 0xd83c,
46 0xdd41,
47 '\0'};
49 torture_assert_size_equal(tctx,
50 utf16_len(empty_string),
52 "length of empty string");
53 torture_assert_size_equal(tctx,
54 utf16_null_terminated_len(empty_string),
56 "null‐terminated length of empty string");
57 torture_assert_size_equal(tctx,
58 utf16_len(foo_bar),
59 14,
60 "length of “foo bar”");
61 torture_assert_size_equal(tctx,
62 utf16_null_terminated_len(foo_bar),
63 16,
64 "null‐terminated length of “foo bar”");
65 torture_assert_size_equal(tctx,
66 utf16_len(foo_bar_alternative),
67 26,
68 "length of “🅵🅾🅾 🄱🄰🅁”");
69 torture_assert_size_equal(tctx,
70 utf16_null_terminated_len(
71 foo_bar_alternative),
72 28,
73 "null‐terminated length of “🅵🅾🅾 🄱🄰🅁”");
75 return true;
78 static bool test_utf16_len_n(struct torture_context *tctx)
80 static const uint16_t empty_string[] = {'\0'};
81 static const uint16_t foo_bar[] = {'f', 'o', 'o', ' ', 'b', 'a', 'r'};
82 static const uint16_t null_terminated_foo_bar[] = {
83 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
84 static const uint16_t twice_null_terminated_abc[] = {
85 'a', 'b', 'c', '\0', '\0'};
87 torture_assert_size_equal(tctx,
88 utf16_len_n(empty_string, 0),
90 "length of empty string");
91 torture_assert_size_equal(tctx,
92 utf16_null_terminated_len_n(empty_string, 0),
94 "null‐terminated length of empty string");
96 torture_assert_size_equal(tctx,
97 utf16_len_n(empty_string,
98 sizeof empty_string),
100 "length of null‐terminated empty string");
101 torture_assert_size_equal(
102 tctx,
103 utf16_null_terminated_len_n(empty_string, sizeof empty_string),
105 "null‐terminated length of null‐terminated empty string");
107 torture_assert_size_equal(tctx,
108 utf16_len_n(foo_bar, sizeof foo_bar),
110 "length of “foo bar”");
111 torture_assert_size_equal(tctx,
112 utf16_null_terminated_len_n(foo_bar,
113 sizeof foo_bar),
115 "null‐terminated length of “foo bar”");
117 torture_assert_size_equal(tctx,
118 utf16_len_n(null_terminated_foo_bar,
119 sizeof null_terminated_foo_bar),
121 "length of null‐terminated “foo bar”");
122 torture_assert_size_equal(
123 tctx,
124 utf16_null_terminated_len_n(null_terminated_foo_bar,
125 sizeof null_terminated_foo_bar),
127 "null‐terminated length of null‐terminated “foo bar”");
129 torture_assert_size_equal(tctx,
130 utf16_len_n(null_terminated_foo_bar,
131 sizeof null_terminated_foo_bar -
134 "length of “foo bar” minus one byte");
135 torture_assert_size_equal(
136 tctx,
137 utf16_null_terminated_len_n(null_terminated_foo_bar,
138 sizeof null_terminated_foo_bar - 1),
140 "null‐terminated length of “foo bar” minus one byte");
142 torture_assert_size_equal(tctx,
143 utf16_len_n(twice_null_terminated_abc,
144 sizeof twice_null_terminated_abc),
146 "length of twice–null‐terminated “abc”");
147 torture_assert_size_equal(
148 tctx,
149 utf16_null_terminated_len_n(twice_null_terminated_abc,
150 sizeof twice_null_terminated_abc),
152 "null‐terminated length of twice–null‐terminated “abc”");
154 return true;
157 struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx)
159 struct torture_suite *suite = torture_suite_create(mem_ctx,
160 "util_unistr");
162 torture_suite_add_simple_test(suite, "utf16_len", test_utf16_len);
163 torture_suite_add_simple_test(suite, "utf16_len_n", test_utf16_len_n);
165 return suite;