LCOV - code coverage report
Current view: top level - test/unitTest/string_utils - testString_utils.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 54 54 100.0 %
Date: 2024-11-15 09:04:49 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       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
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include <cppunit/TestAssert.h>
      19             : #include <cppunit/TestFixture.h>
      20             : #include <cppunit/extensions/HelperMacros.h>
      21             : 
      22             : #include "string_utils.h"
      23             : #include "account.h"
      24             : 
      25             : #include <string>
      26             : #include <string_view>
      27             : 
      28             : #include "../../test_runner.h"
      29             : 
      30             : using namespace std::literals;
      31             : 
      32             : namespace jami {
      33             : namespace test {
      34             : 
      35             : class StringUtilsTest : public CppUnit::TestFixture
      36             : {
      37             : public:
      38           2 :     static std::string name() { return "string_utils"; }
      39             : 
      40             : private:
      41             :     void bool_to_str_test();
      42             :     void to_string_test();
      43             :     void to_number_test();
      44             :     void split_string_test();
      45             :     void version_test();
      46             : 
      47           2 :     CPPUNIT_TEST_SUITE(StringUtilsTest);
      48           1 :     CPPUNIT_TEST(bool_to_str_test);
      49           1 :     CPPUNIT_TEST(to_string_test);
      50           1 :     CPPUNIT_TEST(to_number_test);
      51           1 :     CPPUNIT_TEST(split_string_test);
      52           1 :     CPPUNIT_TEST(version_test);
      53           4 :     CPPUNIT_TEST_SUITE_END();
      54             : 
      55             :     const double DOUBLE = 3.14159265359;
      56             :     const int INT = 42;
      57             :     const std::string PI_DOUBLE = "3.14159265359";
      58             :     const std::string PI_FLOAT = "3.14159265359";
      59             :     const std::string PI_42 = "42";
      60             : };
      61             : 
      62             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(StringUtilsTest, StringUtilsTest::name());
      63             : 
      64             : void
      65           1 : StringUtilsTest::bool_to_str_test()
      66             : {
      67           1 :     CPPUNIT_ASSERT(bool_to_str(true) == TRUE_STR);
      68           1 :     CPPUNIT_ASSERT(bool_to_str(false) == FALSE_STR);
      69           1 : }
      70             : 
      71             : void
      72           1 : StringUtilsTest::to_string_test()
      73             : {
      74             :     // test with double
      75           1 :     CPPUNIT_ASSERT(to_string(DOUBLE) == PI_DOUBLE);
      76             : 
      77             :     // test with float
      78           1 :     float varFloat = 3.14;
      79           1 :     std::string sVarFloat = to_string(varFloat);
      80           1 :     CPPUNIT_ASSERT(sVarFloat.at(0) == '3' && sVarFloat.at(1) == '.' && sVarFloat.at(2) == '1'
      81             :                    && sVarFloat.at(3) == '4');
      82             : 
      83             :     // test with int
      84           1 :     CPPUNIT_ASSERT(std::to_string(INT).compare(PI_42) == 0);
      85             : 
      86           1 :     CPPUNIT_ASSERT_EQUAL("0000000000000010"s, to_hex_string(16));
      87           1 :     CPPUNIT_ASSERT_EQUAL((uint64_t)16, from_hex_string("0000000000000010"s));
      88           1 : }
      89             : 
      90             : void
      91           1 : StringUtilsTest::to_number_test()
      92             : {
      93             :     // test with int
      94           1 :     CPPUNIT_ASSERT(jami::stoi(PI_42) == INT);
      95             : 
      96             :     // test with double
      97           1 :     CPPUNIT_ASSERT(jami::stod(PI_DOUBLE) == DOUBLE);
      98           1 : }
      99             : 
     100             : void
     101           1 : StringUtilsTest::split_string_test()
     102             : {
     103           1 :     auto data = "*fdg454()**{&xcx*"sv;
     104           1 :     auto split_string_result = split_string(data, '*');
     105           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
     106           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
     107             :                    && split_string_result.at(1) == "{&xcx"sv);
     108             : 
     109           1 :     auto split_string_to_unsigned_result = split_string_to_unsigned("/4545497//45454/", '/');
     110           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.size() == 2);
     111           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.at(0) == 4545497
     112             :                    && split_string_to_unsigned_result.at(1) == 45454);
     113             : 
     114           1 :     std::string_view line;
     115           1 :     split_string_result.clear();
     116           3 :     while (jami::getline(data, line, '*')) {
     117           2 :         split_string_result.emplace_back(line);
     118             :     }
     119           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
     120           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
     121             :                    && split_string_result.at(1) == "{&xcx"sv);
     122           1 : }
     123             : 
     124             : void
     125           1 : StringUtilsTest::version_test()
     126             : {
     127           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({}, {}));
     128           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {}));
     129           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3}));
     130           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3, 4}, {1, 2, 3}));
     131           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({2}, {1, 2, 3}));
     132           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion(
     133             :         split_string_to_unsigned("1.2.3.5", '.'),
     134             :         split_string_to_unsigned("1.2.3.4", '.')));
     135             : 
     136           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({}, {1, 2, 3}));
     137           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 2}, {1, 2, 3}));
     138           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3, 4}));
     139           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {2}));
     140           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion(
     141             :         split_string_to_unsigned("1.2.3.4", '.'),
     142             :         split_string_to_unsigned("1.2.3.5", '.')));
     143           1 : }
     144             : 
     145             : } // namespace test
     146             : } // namespace jami
     147             : 
     148           1 : RING_TEST_RUNNER(jami::test::StringUtilsTest::name());

Generated by: LCOV version 1.14