Line data Source code
1 : // Copyright (c) 2026 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
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 : /** \file
20 : * \brief Verify that the hexadecimal convertions work.
21 : *
22 : * This file implements tests for the hexadecimal to binary and vice
23 : * versa functions.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/compare_switch_string.h>
29 :
30 : #include "catch_main.h"
31 :
32 :
33 :
34 : // C++
35 : //
36 : #include <iomanip>
37 : //#include <set>
38 :
39 :
40 : // last include
41 : //
42 : #include <snapdev/poison.h>
43 :
44 :
45 :
46 1 : CATCH_TEST_CASE("compare_switch_string", "[string]")
47 : {
48 1 : CATCH_START_SECTION("compare_switch_string: verify compare works as expected")
49 : {
50 1 : char const * strings[] = {
51 : "CREATE",
52 : "CONTEXT",
53 : "FROM",
54 : "SELECT",
55 : "ORDER",
56 : "INDEX",
57 : "Connect",
58 : };
59 8 : for(auto const & s : strings)
60 : {
61 7 : switch(*s)
62 : {
63 3 : case 'C':
64 3 : if(snapdev::compare_exact_string<"CREAT">(s))
65 : {
66 0 : CATCH_FAIL("no CREAT in our table above; exact compare did not fail as expected.");
67 : }
68 3 : if(snapdev::compare_exact_switch_string<"CREAT">(s))
69 : {
70 0 : CATCH_FAIL("no CREAT in our table above; switch compare did not fail as expected.");
71 : }
72 3 : if(snapdev::compare_exact_switch_string<"CREATE">(s))
73 : {
74 1 : CATCH_REQUIRE(strcmp(s, "CREATE") == 0);
75 1 : CATCH_REQUIRE(snapdev::compare_upper_exact_switch_string<"Create">(s));
76 :
77 1 : CATCH_REQUIRE(snapdev::compare_starts_with_switch_string<"CREAT">(s));
78 1 : CATCH_REQUIRE(snapdev::compare_upper_starts_with_switch_string<"creat">(s));
79 1 : CATCH_REQUIRE(snapdev::compare_starts_with_switch_string<"CREATE">(s));
80 1 : CATCH_REQUIRE(snapdev::compare_upper_starts_with_switch_string<"create">(s));
81 : }
82 3 : if(snapdev::compare_exact_switch_string<"CREATED">(s))
83 : {
84 0 : CATCH_FAIL("no CREATED in our table above; full compare did not fail as expected.");
85 : }
86 3 : if(snapdev::compare_exact_switch_string<"CONTEXT">(s))
87 : {
88 1 : CATCH_REQUIRE(strcmp(s, "CONTEXT") == 0);
89 1 : CATCH_REQUIRE_FALSE(snapdev::compare_exact_switch_string<"Context">(s));
90 1 : CATCH_REQUIRE(snapdev::compare_upper_exact_switch_string<"context">(s));
91 :
92 1 : CATCH_REQUIRE(snapdev::compare_perfect_prefix_switch_string<"CONT">(s));
93 1 : CATCH_REQUIRE(snapdev::compare_upper_perfect_prefix_switch_string<"CONT">(s));
94 1 : CATCH_REQUIRE_FALSE(snapdev::compare_perfect_prefix_switch_string<"CONTEXT">(s));
95 1 : CATCH_REQUIRE_FALSE(snapdev::compare_upper_perfect_prefix_switch_string<"CONTEXT">(s));
96 : }
97 3 : if(snapdev::compare_exact_switch_string<"COMMENT">(s))
98 : {
99 0 : CATCH_REQUIRE(strcmp(s, "COMMENT") == 0);
100 : }
101 3 : if(snapdev::compare_upper_exact_switch_string<"CONNECT">(s))
102 : {
103 1 : CATCH_REQUIRE(strcmp(s, "Connect") == 0);
104 : }
105 3 : break;
106 :
107 1 : case 'F':
108 1 : if(snapdev::compare_exact_switch_string<"FIRST">(s))
109 : {
110 0 : CATCH_REQUIRE(strcmp(s, "FIRST") == 0);
111 : }
112 1 : if(snapdev::compare_exact_switch_string<"FROM">(s))
113 : {
114 1 : CATCH_REQUIRE(strcmp(s, "FROM") == 0);
115 : }
116 1 : if(snapdev::compare_exact_switch_string<"FORWARD">(s))
117 : {
118 0 : CATCH_REQUIRE(strcmp(s, "FORWARD") == 0);
119 : }
120 1 : break;
121 :
122 1 : case 'I':
123 1 : if(snapdev::compare_exact_switch_string<"IN">(s))
124 : {
125 0 : CATCH_REQUIRE(strcmp(s, "IN") == 0);
126 : }
127 1 : if(snapdev::compare_exact_switch_string<"INDEX">(s))
128 : {
129 1 : CATCH_REQUIRE(strcmp(s, "INDEX") == 0);
130 : }
131 1 : if(snapdev::compare_exact_switch_string<"IMPORT">(s))
132 : {
133 0 : CATCH_REQUIRE(strcmp(s, "IMPORT") == 0);
134 : }
135 1 : break;
136 :
137 1 : case 'O':
138 1 : if(snapdev::compare_exact_switch_string<"OR">(s))
139 : {
140 0 : CATCH_REQUIRE(strcmp(s, "OR") == 0);
141 : }
142 1 : if(snapdev::compare_exact_switch_string<"ORDER">(s))
143 : {
144 1 : CATCH_REQUIRE(strcmp(s, "ORDER") == 0);
145 : }
146 1 : if(snapdev::compare_exact_switch_string<"OLDER">(s))
147 : {
148 0 : CATCH_REQUIRE(strcmp(s, "OLDER") == 0);
149 : }
150 1 : break;
151 :
152 1 : case 'S':
153 1 : if(snapdev::compare_exact_switch_string<"SORT">(s))
154 : {
155 0 : CATCH_REQUIRE(strcmp(s, "SORT") == 0);
156 : }
157 1 : if(snapdev::compare_exact_switch_string<"SELECT">(s))
158 : {
159 1 : CATCH_REQUIRE(strcmp(s, "SELECT") == 0);
160 : }
161 1 : if(snapdev::compare_exact_switch_string<"SET">(s))
162 : {
163 0 : CATCH_REQUIRE(strcmp(s, "SET") == 0);
164 : }
165 1 : if(snapdev::compare_exact_switch_string<"SELL">(s))
166 : {
167 0 : CATCH_REQUIRE(strcmp(s, "SELL") == 0);
168 : }
169 1 : break;
170 :
171 : }
172 : }
173 : }
174 1 : CATCH_END_SECTION()
175 1 : }
176 :
177 :
178 1 : CATCH_TEST_CASE("compare_switch_string_errors", "[string][error]")
179 : {
180 1 : CATCH_START_SECTION("compare_switch_string_errors: check first letter in debug")
181 : {
182 : #ifdef _DEBUG
183 3 : CATCH_REQUIRE_THROWS_MATCHES(
184 : snapdev::compare_exact_switch_string<"QUIT">("KUIT")
185 : , std::runtime_error
186 : , Catch::Matchers::ExceptionMessage("first letter invalid: Q <-> KUIT."));
187 :
188 3 : CATCH_REQUIRE_THROWS_MATCHES(
189 : snapdev::compare_upper_exact_switch_string<"QUIT">("KUIT")
190 : , std::runtime_error
191 : , Catch::Matchers::ExceptionMessage("first letter invalid: Q <-> KUIT."));
192 : #else
193 : // in release, it thinks the strings are equal (not a bug)
194 : //
195 : CATCH_REQUIRE(snapdev::compare_exact_switch_string<"QUIT">("KUIT"));
196 : CATCH_REQUIRE(snapdev::compare_upper_exact_switch_string<"QUIT">("KUIT"));
197 : #endif
198 : }
199 1 : CATCH_END_SECTION()
200 1 : }
201 :
202 :
203 :
204 : // vim: ts=4 sw=4 et
|