Line data Source code
1 : // Copyright (c) 2012-2024 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
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 : // this diagnostic has to be turned off "globally" so the catch2 does not
20 : // generate the warning on the floating point == operator
21 : //
22 : #pragma GCC diagnostic ignored "-Wfloat-equal"
23 :
24 : // self
25 : //
26 : #include "catch_main.h"
27 :
28 :
29 : // reporter
30 : //
31 : #include <eventdispatcher/reporter/token.h>
32 :
33 :
34 : // eventdispatcher
35 : //
36 : #include <eventdispatcher/exception.h>
37 :
38 :
39 : // last include
40 : //
41 : #include <snapdev/poison.h>
42 :
43 :
44 :
45 : namespace
46 : {
47 :
48 :
49 : constexpr SNAP_CATCH2_NAMESPACE::reporter::token_t const g_all_tokens[] =
50 : {
51 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_EOF,
52 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_ERROR,
53 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER,
54 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_FLOATING_POINT,
55 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_INTEGER,
56 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_TIMESPEC,
57 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_ADDRESS,
58 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_VARIABLE,
59 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_OPEN_PARENTHESIS,
60 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_CLOSE_PARENTHESIS,
61 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_OPEN_CURLY_BRACE,
62 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_CLOSE_CURLY_BRACE,
63 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_COMMA,
64 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_COLON,
65 : //SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_EQUAL,
66 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_DOUBLE_STRING,
67 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_SINGLE_STRING,
68 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_PLUS,
69 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_MINUS,
70 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_MULTIPLY,
71 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_DIVIDE,
72 : SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_MODULO,
73 : };
74 :
75 :
76 : } // no name namespace
77 :
78 :
79 :
80 6 : CATCH_TEST_CASE("reporter_token", "[token][reporter]")
81 : {
82 6 : CATCH_START_SECTION("reporter_token: set/get token")
83 : {
84 22 : for(auto const tok : g_all_tokens)
85 : {
86 21 : SNAP_CATCH2_NAMESPACE::reporter::token t;
87 21 : CATCH_REQUIRE(t.get_token() == SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_EOF);
88 21 : t.set_token(tok);
89 21 : CATCH_REQUIRE(t.get_token() == tok);
90 21 : }
91 : }
92 6 : CATCH_END_SECTION()
93 :
94 6 : CATCH_START_SECTION("reporter_token: set/get line")
95 : {
96 1 : SNAP_CATCH2_NAMESPACE::reporter::token t;
97 1 : CATCH_REQUIRE(t.get_line() == 0);
98 1 : std::uint32_t line(rand());
99 1 : while(line == 0)
100 : {
101 0 : line = rand();
102 : }
103 1 : t.set_line(line);
104 1 : CATCH_REQUIRE(t.get_line() == line);
105 1 : }
106 6 : CATCH_END_SECTION()
107 :
108 6 : CATCH_START_SECTION("reporter_token: set/get column")
109 : {
110 1 : SNAP_CATCH2_NAMESPACE::reporter::token t;
111 1 : CATCH_REQUIRE(t.get_column() == 0);
112 1 : std::uint32_t column(rand());
113 1 : while(column == 0)
114 : {
115 0 : column = rand();
116 : }
117 1 : t.set_column(column);
118 1 : CATCH_REQUIRE(t.get_column() == column);
119 1 : }
120 6 : CATCH_END_SECTION()
121 :
122 6 : CATCH_START_SECTION("reporter_token: set/get integer")
123 : {
124 101 : for(int count(0); count < 100; ++count)
125 : {
126 100 : SNAP_CATCH2_NAMESPACE::reporter::token t;
127 100 : CATCH_REQUIRE(t.get_integer() == 0);
128 : #pragma GCC diagnostic push
129 : #pragma GCC diagnostic ignored "-Wpedantic"
130 100 : __int128 value(0);
131 100 : SNAP_CATCH2_NAMESPACE::random(value);
132 100 : t.set_integer(value);
133 100 : CATCH_REQUIRE(t.get_integer() == value);
134 : #pragma GCC diagnostic pop
135 100 : }
136 : }
137 6 : CATCH_END_SECTION()
138 :
139 6 : CATCH_START_SECTION("reporter_token: set/get floating point")
140 : {
141 101 : for(int count(0); count < 100; ++count)
142 : {
143 100 : SNAP_CATCH2_NAMESPACE::reporter::token t;
144 100 : CATCH_REQUIRE(t.get_floating_point() == 0.0);
145 100 : std::int64_t n(0);
146 100 : SNAP_CATCH2_NAMESPACE::random(n);
147 100 : n >>= 9;
148 100 : std::int64_t d(0);
149 100 : SNAP_CATCH2_NAMESPACE::random(d);
150 100 : d >>= 9;
151 100 : double const nominator(n);
152 100 : double denominator(d);
153 100 : if(denominator == 0.0)
154 : {
155 0 : denominator = 1.0;
156 : }
157 100 : double const value(nominator / denominator);
158 100 : t.set_floating_point(value);
159 100 : CATCH_REQUIRE(t.get_floating_point() == value);
160 100 : }
161 : }
162 6 : CATCH_END_SECTION()
163 :
164 6 : CATCH_START_SECTION("reporter_token: set/get string")
165 : {
166 101 : for(int count(0); count < 100; ++count)
167 : {
168 100 : SNAP_CATCH2_NAMESPACE::reporter::token t;
169 100 : CATCH_REQUIRE(t.get_string() == "");
170 100 : std::string const str(SNAP_CATCH2_NAMESPACE::random_string(1, 25));
171 100 : t.set_string(str);
172 100 : CATCH_REQUIRE(t.get_string() == str);
173 100 : }
174 : }
175 6 : CATCH_END_SECTION()
176 6 : }
177 :
178 :
179 3 : CATCH_TEST_CASE("reporter_token_error", "[token][reporter][error]")
180 : {
181 3 : CATCH_START_SECTION("reporter_token_error: set token twice")
182 : {
183 22 : for(auto const tok : g_all_tokens)
184 : {
185 21 : SNAP_CATCH2_NAMESPACE::reporter::token t;
186 21 : CATCH_REQUIRE(t.get_token() == SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_EOF);
187 21 : t.set_token(tok);
188 21 : CATCH_REQUIRE(t.get_token() == tok);
189 :
190 21 : if(tok == SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_EOF
191 20 : || tok == SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_ERROR)
192 : {
193 : // allowed since it's still EOF
194 : //
195 2 : t.set_token(tok);
196 2 : CATCH_REQUIRE(t.get_token() == tok);
197 2 : }
198 : else
199 : {
200 : // not allowed
201 : //
202 57 : CATCH_REQUIRE_THROWS_MATCHES(
203 : t.set_token(tok)
204 : , ed::implementation_error
205 : , Catch::Matchers::ExceptionMessage(
206 : "implementation_error: trying to modify token type to something other than an error."));
207 : }
208 :
209 : // switching to an error is always allowed
210 : //
211 21 : t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_ERROR);
212 21 : CATCH_REQUIRE(t.get_token() == SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_ERROR);
213 21 : }
214 : }
215 3 : CATCH_END_SECTION()
216 :
217 3 : CATCH_START_SECTION("reporter_token_error: set line twice")
218 : {
219 1 : SNAP_CATCH2_NAMESPACE::reporter::token t;
220 1 : t.set_line(1);
221 1 : CATCH_REQUIRE(t.get_line() == 1);
222 3 : CATCH_REQUIRE_THROWS_MATCHES(
223 : t.set_line(2)
224 : , ed::implementation_error
225 : , Catch::Matchers::ExceptionMessage(
226 : "implementation_error: trying to modify line number, not allowed anymore."));
227 1 : }
228 3 : CATCH_END_SECTION()
229 :
230 3 : CATCH_START_SECTION("reporter_token_error: set column twice")
231 : {
232 1 : SNAP_CATCH2_NAMESPACE::reporter::token t;
233 1 : t.set_column(1);
234 1 : CATCH_REQUIRE(t.get_column() == 1);
235 3 : CATCH_REQUIRE_THROWS_MATCHES(
236 : t.set_column(2)
237 : , ed::implementation_error
238 : , Catch::Matchers::ExceptionMessage(
239 : "implementation_error: trying to modify column number, not allowed anymore."));
240 1 : }
241 3 : CATCH_END_SECTION()
242 3 : }
243 :
244 :
245 : // vim: ts=4 sw=4 et
|