Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 17 | #include <cppunit/TestAssert.h> |
| 18 | #include <cppunit/TestFixture.h> |
| 19 | #include <cppunit/extensions/HelperMacros.h> |
| 20 | |
| 21 | #include "string_utils.h" |
| 22 | #include "test_runner.h" |
| 23 | |
| 24 | #include <string> |
| 25 | #include <string_view> |
| 26 | |
| 27 | using namespace std::literals; |
| 28 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 29 | namespace dhtnet { |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 30 | namespace test { |
| 31 | |
| 32 | class StringUtilsTest : public CppUnit::TestFixture |
| 33 | { |
| 34 | public: |
| 35 | static std::string name() { return "string_utils"; } |
| 36 | |
| 37 | private: |
| 38 | void bool_to_str_test(); |
| 39 | void to_string_test(); |
| 40 | void to_number_test(); |
| 41 | void split_string_test(); |
| 42 | |
| 43 | CPPUNIT_TEST_SUITE(StringUtilsTest); |
| 44 | CPPUNIT_TEST(bool_to_str_test); |
| 45 | CPPUNIT_TEST(to_string_test); |
| 46 | CPPUNIT_TEST(to_number_test); |
| 47 | CPPUNIT_TEST(split_string_test); |
| 48 | CPPUNIT_TEST_SUITE_END(); |
| 49 | |
| 50 | const double DOUBLE = 3.14159265359; |
| 51 | const int INT = 42; |
| 52 | const std::string PI_DOUBLE = "3.14159265359"; |
| 53 | const std::string PI_FLOAT = "3.14159265359"; |
| 54 | const std::string PI_42 = "42"; |
| 55 | }; |
| 56 | |
| 57 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(StringUtilsTest, StringUtilsTest::name()); |
| 58 | |
| 59 | void |
| 60 | StringUtilsTest::bool_to_str_test() |
| 61 | { |
| 62 | CPPUNIT_ASSERT(bool_to_str(true) == TRUE_STR); |
| 63 | CPPUNIT_ASSERT(bool_to_str(false) == FALSE_STR); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | StringUtilsTest::to_string_test() |
| 68 | { |
| 69 | // test with double |
| 70 | CPPUNIT_ASSERT(to_string(DOUBLE) == PI_DOUBLE); |
| 71 | |
| 72 | // test with float |
| 73 | float varFloat = 3.14; |
| 74 | std::string sVarFloat = to_string(varFloat); |
| 75 | CPPUNIT_ASSERT(sVarFloat.at(0) == '3' && sVarFloat.at(1) == '.' && sVarFloat.at(2) == '1' |
| 76 | && sVarFloat.at(3) == '4'); |
| 77 | |
| 78 | // test with int |
| 79 | CPPUNIT_ASSERT(std::to_string(INT).compare(PI_42) == 0); |
| 80 | |
| 81 | CPPUNIT_ASSERT_EQUAL("0000000000000010"s, to_hex_string(16)); |
| 82 | CPPUNIT_ASSERT_EQUAL((uint64_t)16, from_hex_string("0000000000000010"s)); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | StringUtilsTest::to_number_test() |
| 87 | { |
| 88 | // test with int |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 89 | CPPUNIT_ASSERT(dhtnet::stoi(PI_42) == INT); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 90 | |
| 91 | // test with double |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 92 | CPPUNIT_ASSERT(dhtnet::stod(PI_DOUBLE) == DOUBLE); |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | StringUtilsTest::split_string_test() |
| 97 | { |
| 98 | auto data = "*fdg454()**{&xcx*"sv; |
| 99 | auto split_string_result = split_string(data, '*'); |
| 100 | CPPUNIT_ASSERT(split_string_result.size() == 2); |
| 101 | CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv |
| 102 | && split_string_result.at(1) == "{&xcx"sv); |
| 103 | |
| 104 | auto split_string_to_unsigned_result = split_string_to_unsigned("/4545497//45454/", '/'); |
| 105 | CPPUNIT_ASSERT(split_string_to_unsigned_result.size() == 2); |
| 106 | CPPUNIT_ASSERT(split_string_to_unsigned_result.at(0) == 4545497 |
| 107 | && split_string_to_unsigned_result.at(1) == 45454); |
| 108 | |
| 109 | std::string_view line; |
| 110 | split_string_result.clear(); |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 111 | while (dhtnet::getline(data, line, '*')) { |
Adrien Béraud | efe2737 | 2023-05-27 18:56:29 -0400 | [diff] [blame] | 112 | split_string_result.emplace_back(line); |
| 113 | } |
| 114 | CPPUNIT_ASSERT(split_string_result.size() == 2); |
| 115 | CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv |
| 116 | && split_string_result.at(1) == "{&xcx"sv); |
| 117 | } |
| 118 | |
| 119 | } // namespace test |
| 120 | } // namespace jami |
| 121 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 122 | JAMI_TEST_RUNNER(dhtnet::test::StringUtilsTest::name()); |