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