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