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