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_switch_string<"CREATE">(s))
65 : {
66 1 : CATCH_REQUIRE(strcmp(s, "CREATE") == 0);
67 1 : CATCH_REQUIRE(snapdev::compare_upper_switch_string<"Create">(s));
68 : }
69 3 : if(snapdev::compare_switch_string<"CREATED">(s))
70 : {
71 0 : CATCH_REQUIRE(strcmp(s, "CREATED") == 0);
72 : }
73 3 : if(snapdev::compare_switch_string<"CONTEXT">(s))
74 : {
75 1 : CATCH_REQUIRE(strcmp(s, "CONTEXT") == 0);
76 1 : CATCH_REQUIRE_FALSE(snapdev::compare_switch_string<"Context">(s));
77 1 : CATCH_REQUIRE(snapdev::compare_upper_switch_string<"context">(s));
78 : }
79 3 : if(snapdev::compare_switch_string<"COMMENT">(s))
80 : {
81 0 : CATCH_REQUIRE(strcmp(s, "COMMENT") == 0);
82 : }
83 3 : if(snapdev::compare_upper_switch_string<"CONNECT">(s))
84 : {
85 1 : CATCH_REQUIRE(strcmp(s, "Connect") == 0);
86 : }
87 3 : break;
88 :
89 1 : case 'F':
90 1 : if(snapdev::compare_switch_string<"FIRST">(s))
91 : {
92 0 : CATCH_REQUIRE(strcmp(s, "FIRST") == 0);
93 : }
94 1 : if(snapdev::compare_switch_string<"FROM">(s))
95 : {
96 1 : CATCH_REQUIRE(strcmp(s, "FROM") == 0);
97 : }
98 1 : if(snapdev::compare_switch_string<"FORWARD">(s))
99 : {
100 0 : CATCH_REQUIRE(strcmp(s, "FORWARD") == 0);
101 : }
102 1 : break;
103 :
104 1 : case 'I':
105 1 : if(snapdev::compare_switch_string<"IN">(s))
106 : {
107 0 : CATCH_REQUIRE(strcmp(s, "IN") == 0);
108 : }
109 1 : if(snapdev::compare_switch_string<"INDEX">(s))
110 : {
111 1 : CATCH_REQUIRE(strcmp(s, "INDEX") == 0);
112 : }
113 1 : if(snapdev::compare_switch_string<"IMPORT">(s))
114 : {
115 0 : CATCH_REQUIRE(strcmp(s, "IMPORT") == 0);
116 : }
117 1 : break;
118 :
119 1 : case 'O':
120 1 : if(snapdev::compare_switch_string<"OR">(s))
121 : {
122 0 : CATCH_REQUIRE(strcmp(s, "OR") == 0);
123 : }
124 1 : if(snapdev::compare_switch_string<"ORDER">(s))
125 : {
126 1 : CATCH_REQUIRE(strcmp(s, "ORDER") == 0);
127 : }
128 1 : if(snapdev::compare_switch_string<"OLDER">(s))
129 : {
130 0 : CATCH_REQUIRE(strcmp(s, "OLDER") == 0);
131 : }
132 1 : break;
133 :
134 1 : case 'S':
135 1 : if(snapdev::compare_switch_string<"SORT">(s))
136 : {
137 0 : CATCH_REQUIRE(strcmp(s, "SORT") == 0);
138 : }
139 1 : if(snapdev::compare_switch_string<"SELECT">(s))
140 : {
141 1 : CATCH_REQUIRE(strcmp(s, "SELECT") == 0);
142 : }
143 1 : if(snapdev::compare_switch_string<"SET">(s))
144 : {
145 0 : CATCH_REQUIRE(strcmp(s, "SET") == 0);
146 : }
147 1 : if(snapdev::compare_switch_string<"SELL">(s))
148 : {
149 0 : CATCH_REQUIRE(strcmp(s, "SELL") == 0);
150 : }
151 1 : break;
152 :
153 : }
154 : }
155 : }
156 1 : CATCH_END_SECTION()
157 1 : }
158 :
159 :
160 1 : CATCH_TEST_CASE("compare_switch_string_errors", "[string][error]")
161 : {
162 1 : CATCH_START_SECTION("compare_switch_string_errors: check first letter in debug")
163 : {
164 : #ifdef _DEBUG
165 3 : CATCH_REQUIRE_THROWS_MATCHES(
166 : snapdev::compare_switch_string<"QUIT">("KUIT")
167 : , std::runtime_error
168 : , Catch::Matchers::ExceptionMessage("first letter invalid."));
169 :
170 3 : CATCH_REQUIRE_THROWS_MATCHES(
171 : snapdev::compare_upper_switch_string<"QUIT">("KUIT")
172 : , std::runtime_error
173 : , Catch::Matchers::ExceptionMessage("first letter invalid."));
174 : #else
175 : // in release, it thinks the strings are equal (not a bug)
176 : //
177 : CATCH_REQUIRE(snapdev::compare_switch_string<"QUIT">("KUIT"));
178 : CATCH_REQUIRE(snapdev::compare_upper_switch_string<"QUIT">("KUIT"));
179 : #endif
180 : }
181 1 : CATCH_END_SECTION()
182 1 : }
183 :
184 :
185 :
186 : // vim: ts=4 sw=4 et
|