blob: 16e4094519a0fe1a1c9fedf4760edc59acc071ea [file] [log] [blame]
Adrien Béraudefe27372023-05-27 18:56:29 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
Adrien Béraudefe27372023-05-27 18:56:29 -04003 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraudefe27372023-05-27 18:56:29 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraudefe27372023-05-27 18:56:29 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraudefe27372023-05-27 18:56:29 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraudefe27372023-05-27 18:56:29 -040016 */
Adrien Béraudefe27372023-05-27 18:56:29 -040017#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
27using namespace std::literals;
28
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040029namespace dhtnet {
Adrien Béraudefe27372023-05-27 18:56:29 -040030namespace test {
31
32class StringUtilsTest : public CppUnit::TestFixture
33{
34public:
35 static std::string name() { return "string_utils"; }
36
37private:
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
57CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(StringUtilsTest, StringUtilsTest::name());
58
59void
60StringUtilsTest::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
66void
67StringUtilsTest::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
85void
86StringUtilsTest::to_number_test()
87{
88 // test with int
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040089 CPPUNIT_ASSERT(dhtnet::stoi(PI_42) == INT);
Adrien Béraudefe27372023-05-27 18:56:29 -040090
91 // test with double
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040092 CPPUNIT_ASSERT(dhtnet::stod(PI_DOUBLE) == DOUBLE);
Adrien Béraudefe27372023-05-27 18:56:29 -040093}
94
95void
96StringUtilsTest::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éraud1ae60aa2023-07-07 09:55:09 -0400111 while (dhtnet::getline(data, line, '*')) {
Adrien Béraudefe27372023-05-27 18:56:29 -0400112 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
Sébastien Blin464bdff2023-07-19 08:02:53 -0400120} // namespace dhtnet
Adrien Béraudefe27372023-05-27 18:56:29 -0400121
Adrien Béraud1ae60aa2023-07-07 09:55:09 -0400122JAMI_TEST_RUNNER(dhtnet::test::StringUtilsTest::name());