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