Line data Source code
1 : // Copyright (c) 2021-2022 Made to Order Software Corporation
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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // libutf8
21 : //
22 : #include <libutf8/caseinsensitivestring.h>
23 :
24 :
25 : // unit test
26 : //
27 : #include "catch_main.h"
28 :
29 :
30 : // C++
31 : //
32 : #include <cctype>
33 : #include <iostream>
34 :
35 :
36 : // last include
37 : //
38 : #include <snapdev/poison.h>
39 :
40 :
41 :
42 : namespace
43 : {
44 :
45 :
46 2 : libutf8::case_insensitive_string get_time(std::string & result)
47 : {
48 2 : time_t const now(time(NULL));
49 2 : struct tm t;
50 2 : localtime_r(&now, &t);
51 2 : char buf[256];
52 2 : strftime(buf, sizeof(buf), "%T", &t);
53 2 : buf[sizeof(buf) - 1] = '\0';
54 2 : result = buf;
55 2 : libutf8::case_insensitive_string r(buf);
56 2 : r += " PST";
57 2 : return r;
58 : }
59 :
60 2 : std::string get_date(std::string & result)
61 : {
62 2 : time_t const now(time(NULL));
63 2 : struct tm t;
64 2 : localtime_r(&now, &t);
65 2 : char buf[256];
66 2 : strftime(buf, sizeof(buf), "%F", &t);
67 2 : buf[sizeof(buf) - 1] = '\0';
68 2 : result = buf;
69 4 : libutf8::case_insensitive_string r(buf);
70 2 : r += " plus a few days";
71 4 : return r;
72 : }
73 :
74 :
75 :
76 : }
77 :
78 :
79 :
80 4 : CATCH_TEST_CASE("case_insensitive", "[string],[compare],[insensitive]")
81 : {
82 4 : CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Constructors")
83 : {
84 : {
85 2 : libutf8::case_insensitive_string empty;
86 1 : CATCH_REQUIRE(empty.empty());
87 : }
88 :
89 : {
90 2 : std::allocator<char> allocator;
91 2 : libutf8::case_insensitive_string empty(allocator);
92 1 : CATCH_REQUIRE(empty.empty());
93 : }
94 :
95 : {
96 2 : libutf8::case_insensitive_string dashes(10, '-');
97 1 : CATCH_REQUIRE(dashes == "----------");
98 : }
99 :
100 : {
101 2 : libutf8::case_insensitive_string name("alexis");
102 1 : CATCH_REQUIRE(name == "alexis");
103 : }
104 :
105 : {
106 2 : libutf8::case_insensitive_string name("alexis", 4);
107 1 : CATCH_REQUIRE(name == "alex");
108 : }
109 :
110 : {
111 2 : libutf8::case_insensitive_string name("alexis");
112 1 : CATCH_REQUIRE(name == "alexis");
113 :
114 2 : libutf8::case_insensitive_string section(name, 2);
115 1 : CATCH_REQUIRE(section == "exis");
116 : }
117 :
118 : {
119 2 : libutf8::case_insensitive_string name("alexis");
120 1 : CATCH_REQUIRE(name == "alexis");
121 :
122 2 : libutf8::case_insensitive_string section(name, 2, 2);
123 1 : CATCH_REQUIRE(section == "ex");
124 : }
125 :
126 : {
127 2 : std::string name("alexis");
128 1 : CATCH_REQUIRE(name == "alexis");
129 :
130 2 : libutf8::case_insensitive_string section(name, 2);
131 1 : CATCH_REQUIRE(section == "exis");
132 : }
133 :
134 : {
135 2 : std::string name("alexis");
136 1 : CATCH_REQUIRE(name == "alexis");
137 :
138 2 : libutf8::case_insensitive_string section(name, 2, 2);
139 1 : CATCH_REQUIRE(section == "ex");
140 : }
141 :
142 : {
143 2 : libutf8::case_insensitive_string name("alexis");
144 1 : CATCH_REQUIRE(name == "alexis");
145 :
146 2 : libutf8::case_insensitive_string section(name.begin() + 2, name.end() - 2);
147 1 : CATCH_REQUIRE(section == "ex");
148 : }
149 :
150 : {
151 2 : std::string name("alexis");
152 1 : CATCH_REQUIRE(name == "alexis");
153 :
154 2 : libutf8::case_insensitive_string full(name);
155 1 : CATCH_REQUIRE(full == "alexis");
156 : }
157 :
158 : {
159 2 : libutf8::case_insensitive_string name("alexis");
160 1 : CATCH_REQUIRE(name == "alexis");
161 :
162 2 : libutf8::case_insensitive_string full(name);
163 1 : CATCH_REQUIRE(full == "alexis");
164 : }
165 :
166 : {
167 2 : libutf8::case_insensitive_string name({'a', 'l', 'e', 'x', 'i', 's'});
168 1 : CATCH_REQUIRE(name == "alexis");
169 : }
170 :
171 : {
172 2 : std::string expected("not this");
173 2 : libutf8::case_insensitive_string now(get_time(expected));
174 1 : CATCH_REQUIRE(expected + " PST" == now);
175 : }
176 :
177 : {
178 2 : std::allocator<char> allocator;
179 2 : std::string expected("not this");
180 2 : libutf8::case_insensitive_string now(get_time(expected), allocator);
181 1 : CATCH_REQUIRE(expected + " PST" == now);
182 : }
183 :
184 : {
185 2 : std::string expected("not this");
186 2 : libutf8::case_insensitive_string now(get_date(expected));
187 1 : CATCH_REQUIRE(now == expected + " plus a few days");
188 : }
189 :
190 : {
191 2 : std::allocator<char> allocator;
192 2 : std::string expected("not this");
193 2 : libutf8::case_insensitive_string now(get_date(expected), allocator);
194 1 : CATCH_REQUIRE(now == expected + " plus a few days");
195 : }
196 : }
197 : CATCH_END_SECTION()
198 :
199 4 : CATCH_START_SECTION("case_insensitive: Verify Case Insensitive String Comparators")
200 : {
201 : {
202 2 : libutf8::case_insensitive_string name1("Alexis");
203 2 : libutf8::case_insensitive_string name2("alexis");
204 1 : CATCH_REQUIRE(name1 == name2);
205 1 : CATCH_REQUIRE_FALSE(name1 != name2);
206 1 : CATCH_REQUIRE_FALSE(name1 > name2);
207 1 : CATCH_REQUIRE(name1 >= name2);
208 1 : CATCH_REQUIRE_FALSE(name1 < name2);
209 1 : CATCH_REQUIRE(name1 <= name2);
210 : }
211 :
212 : {
213 2 : libutf8::case_insensitive_string name1("Alexis");
214 2 : libutf8::case_insensitive_string name2("Wilke");
215 1 : CATCH_REQUIRE_FALSE(name1 == name2);
216 1 : CATCH_REQUIRE(name1 != name2);
217 1 : CATCH_REQUIRE_FALSE(name1 > name2);
218 1 : CATCH_REQUIRE_FALSE(name1 >= name2);
219 1 : CATCH_REQUIRE(name1 < name2);
220 1 : CATCH_REQUIRE(name1 <= name2);
221 : }
222 :
223 : {
224 2 : libutf8::case_insensitive_string name1("Alexis");
225 2 : std::string name2("alexis");
226 1 : CATCH_REQUIRE(name1 == name2);
227 1 : CATCH_REQUIRE_FALSE(name1 != name2);
228 1 : CATCH_REQUIRE_FALSE(name1 > name2);
229 1 : CATCH_REQUIRE(name1 >= name2);
230 1 : CATCH_REQUIRE_FALSE(name1 < name2);
231 1 : CATCH_REQUIRE(name1 <= name2);
232 : }
233 :
234 : {
235 2 : std::string name1("Alexis");
236 2 : libutf8::case_insensitive_string name2("Wilke");
237 1 : CATCH_REQUIRE_FALSE(name1 == name2);
238 1 : CATCH_REQUIRE(name1 != name2);
239 1 : CATCH_REQUIRE_FALSE(name1 > name2);
240 1 : CATCH_REQUIRE_FALSE(name1 >= name2);
241 1 : CATCH_REQUIRE(name1 < name2);
242 1 : CATCH_REQUIRE(name1 <= name2);
243 : }
244 :
245 : {
246 2 : libutf8::case_insensitive_string name1("Alexis");
247 1 : CATCH_REQUIRE(name1 == "alexis");
248 1 : CATCH_REQUIRE_FALSE(name1 != "alexis");
249 1 : CATCH_REQUIRE_FALSE(name1 > "alexis");
250 1 : CATCH_REQUIRE(name1 >= "alexis");
251 1 : CATCH_REQUIRE_FALSE(name1 < "alexis");
252 1 : CATCH_REQUIRE(name1 <= "alexis");
253 : }
254 :
255 : {
256 2 : libutf8::case_insensitive_string name2("Wilke");
257 1 : CATCH_REQUIRE_FALSE("Alexis" == name2);
258 1 : CATCH_REQUIRE("Alexis" != name2);
259 1 : CATCH_REQUIRE_FALSE("Alexis" > name2);
260 1 : CATCH_REQUIRE_FALSE("Alexis" >= name2);
261 1 : CATCH_REQUIRE("Alexis" < name2);
262 1 : CATCH_REQUIRE("Alexis" <= name2);
263 : }
264 : }
265 : CATCH_END_SECTION()
266 8 : }
267 :
268 :
269 : // vim: ts=4 sw=4 et
|