Line data Source code
1 : // Copyright (c) 2021-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libutf8
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // libutf8
20 : //
21 : #include <libutf8/caseinsensitivestring.h>
22 :
23 :
24 : // unit test
25 : //
26 : #include "catch_main.h"
27 :
28 :
29 : // C++
30 : //
31 : #include <cctype>
32 : #include <iostream>
33 :
34 :
35 : // last include
36 : //
37 : #include <snapdev/poison.h>
38 :
39 :
40 :
41 : namespace
42 : {
43 :
44 :
45 2 : libutf8::case_insensitive_string get_time(std::string & result)
46 : {
47 2 : time_t const now(time(nullptr));
48 2 : struct tm t;
49 2 : localtime_r(&now, &t);
50 2 : char buf[256];
51 2 : strftime(buf, sizeof(buf), "%T", &t);
52 2 : buf[sizeof(buf) - 1] = '\0';
53 2 : result = buf;
54 6 : libutf8::case_insensitive_string r(buf);
55 2 : r += " PST";
56 4 : return r;
57 0 : }
58 :
59 2 : std::string get_date(std::string & result)
60 : {
61 2 : time_t const now(time(nullptr));
62 2 : struct tm t;
63 2 : localtime_r(&now, &t);
64 2 : char buf[256];
65 2 : strftime(buf, sizeof(buf), "%F", &t);
66 2 : buf[sizeof(buf) - 1] = '\0';
67 2 : result = buf;
68 6 : libutf8::case_insensitive_string r(buf);
69 2 : r += " plus a few days";
70 4 : return r;
71 2 : }
72 :
73 :
74 :
75 : }
76 :
77 :
78 :
79 2 : CATCH_TEST_CASE("case_insensitive", "[string],[compare],[insensitive]")
80 : {
81 2 : CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Constructors")
82 : {
83 : {
84 1 : libutf8::case_insensitive_string empty;
85 1 : CATCH_REQUIRE(empty.empty());
86 1 : }
87 :
88 : {
89 1 : std::allocator<char> allocator;
90 1 : libutf8::case_insensitive_string empty(allocator);
91 1 : CATCH_REQUIRE(empty.empty());
92 1 : }
93 :
94 : {
95 3 : libutf8::case_insensitive_string dashes(10, '-');
96 1 : CATCH_REQUIRE(dashes == "----------");
97 1 : }
98 :
99 : {
100 3 : libutf8::case_insensitive_string name("alexis");
101 1 : CATCH_REQUIRE(name == "alexis");
102 1 : }
103 :
104 : {
105 3 : libutf8::case_insensitive_string name("alexis", 4);
106 1 : CATCH_REQUIRE(name == "alex");
107 1 : }
108 :
109 : {
110 3 : libutf8::case_insensitive_string name("alexis");
111 1 : CATCH_REQUIRE(name == "alexis");
112 :
113 3 : libutf8::case_insensitive_string section(name, 2);
114 1 : CATCH_REQUIRE(section == "exis");
115 1 : }
116 :
117 : {
118 3 : libutf8::case_insensitive_string name("alexis");
119 1 : CATCH_REQUIRE(name == "alexis");
120 :
121 3 : libutf8::case_insensitive_string section(name, 2, 2);
122 1 : CATCH_REQUIRE(section == "ex");
123 1 : }
124 :
125 : {
126 3 : std::string name("alexis");
127 1 : CATCH_REQUIRE(name == "alexis");
128 :
129 3 : libutf8::case_insensitive_string section(name, 2);
130 1 : CATCH_REQUIRE(section == "exis");
131 1 : }
132 :
133 : {
134 3 : std::string name("alexis");
135 1 : CATCH_REQUIRE(name == "alexis");
136 :
137 3 : libutf8::case_insensitive_string section(name, 2, 2);
138 1 : CATCH_REQUIRE(section == "ex");
139 1 : }
140 :
141 : {
142 3 : libutf8::case_insensitive_string name("alexis");
143 1 : CATCH_REQUIRE(name == "alexis");
144 :
145 3 : libutf8::case_insensitive_string section(name.begin() + 2, name.end() - 2);
146 1 : CATCH_REQUIRE(section == "ex");
147 1 : }
148 :
149 : {
150 3 : std::string name("alexis");
151 1 : CATCH_REQUIRE(name == "alexis");
152 :
153 1 : libutf8::case_insensitive_string full(name);
154 1 : CATCH_REQUIRE(full == "alexis");
155 1 : }
156 :
157 : {
158 3 : libutf8::case_insensitive_string name("alexis");
159 1 : CATCH_REQUIRE(name == "alexis");
160 :
161 1 : libutf8::case_insensitive_string full(name);
162 1 : CATCH_REQUIRE(full == "alexis");
163 1 : }
164 :
165 : {
166 3 : libutf8::case_insensitive_string name({'a', 'l', 'e', 'x', 'i', 's'});
167 1 : CATCH_REQUIRE(name == "alexis");
168 1 : }
169 :
170 : {
171 3 : std::string expected("not this");
172 1 : libutf8::case_insensitive_string now(get_time(expected));
173 1 : CATCH_REQUIRE(expected + " PST" == now);
174 1 : }
175 :
176 : {
177 1 : std::allocator<char> allocator;
178 3 : std::string expected("not this");
179 1 : libutf8::case_insensitive_string now(get_time(expected), allocator);
180 1 : CATCH_REQUIRE(expected + " PST" == now);
181 1 : }
182 :
183 : {
184 3 : std::string expected("not this");
185 1 : libutf8::case_insensitive_string now(get_date(expected));
186 1 : CATCH_REQUIRE(now == expected + " plus a few days");
187 1 : }
188 :
189 : {
190 1 : std::allocator<char> allocator;
191 3 : std::string expected("not this");
192 1 : libutf8::case_insensitive_string now(get_date(expected), allocator);
193 1 : CATCH_REQUIRE(now == expected + " plus a few days");
194 1 : }
195 : }
196 2 : CATCH_END_SECTION()
197 :
198 2 : CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Comparators")
199 : {
200 : {
201 3 : libutf8::case_insensitive_string name1("Alexis");
202 3 : libutf8::case_insensitive_string name2("alexis");
203 1 : CATCH_REQUIRE(name1 == name2);
204 1 : CATCH_REQUIRE_FALSE(name1 != name2);
205 1 : CATCH_REQUIRE_FALSE(name1 > name2);
206 1 : CATCH_REQUIRE(name1 >= name2);
207 1 : CATCH_REQUIRE_FALSE(name1 < name2);
208 1 : CATCH_REQUIRE(name1 <= name2);
209 1 : }
210 :
211 : {
212 3 : libutf8::case_insensitive_string name1("Alexis");
213 3 : libutf8::case_insensitive_string name2("Wilke");
214 1 : CATCH_REQUIRE_FALSE(name1 == name2);
215 1 : CATCH_REQUIRE(name1 != name2);
216 1 : CATCH_REQUIRE_FALSE(name1 > name2);
217 1 : CATCH_REQUIRE_FALSE(name1 >= name2);
218 1 : CATCH_REQUIRE(name1 < name2);
219 1 : CATCH_REQUIRE(name1 <= name2);
220 1 : }
221 :
222 : {
223 3 : libutf8::case_insensitive_string name1("Alexis");
224 3 : std::string name2("alexis");
225 1 : CATCH_REQUIRE(name1 == name2);
226 1 : CATCH_REQUIRE_FALSE(name1 != name2);
227 1 : CATCH_REQUIRE_FALSE(name1 > name2);
228 1 : CATCH_REQUIRE(name1 >= name2);
229 1 : CATCH_REQUIRE_FALSE(name1 < name2);
230 1 : CATCH_REQUIRE(name1 <= name2);
231 1 : }
232 :
233 : {
234 3 : std::string name1("Alexis");
235 3 : libutf8::case_insensitive_string name2("Wilke");
236 1 : CATCH_REQUIRE_FALSE(name1 == name2);
237 1 : CATCH_REQUIRE(name1 != name2);
238 1 : CATCH_REQUIRE_FALSE(name1 > name2);
239 1 : CATCH_REQUIRE_FALSE(name1 >= name2);
240 1 : CATCH_REQUIRE(name1 < name2);
241 1 : CATCH_REQUIRE(name1 <= name2);
242 1 : }
243 :
244 : {
245 3 : libutf8::case_insensitive_string name1("Alexis");
246 1 : CATCH_REQUIRE(name1 == "alexis");
247 1 : CATCH_REQUIRE_FALSE(name1 != "alexis");
248 1 : CATCH_REQUIRE_FALSE(name1 > "alexis");
249 1 : CATCH_REQUIRE(name1 >= "alexis");
250 1 : CATCH_REQUIRE_FALSE(name1 < "alexis");
251 1 : CATCH_REQUIRE(name1 <= "alexis");
252 1 : }
253 :
254 : {
255 3 : libutf8::case_insensitive_string name2("Wilke");
256 1 : CATCH_REQUIRE_FALSE("Alexis" == name2);
257 1 : CATCH_REQUIRE("Alexis" != name2);
258 1 : CATCH_REQUIRE_FALSE("Alexis" > name2);
259 1 : CATCH_REQUIRE_FALSE("Alexis" >= name2);
260 1 : CATCH_REQUIRE("Alexis" < name2);
261 1 : CATCH_REQUIRE("Alexis" <= name2);
262 1 : }
263 : }
264 2 : CATCH_END_SECTION()
265 2 : }
266 :
267 :
268 : // vim: ts=4 sw=4 et
|