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: 56 56 100.0 %
Date: 2024-12-21 08:56:24 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 split_string_test();
      44             :     void starts_with_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(split_string_test);
      51           1 :     CPPUNIT_TEST(starts_with_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::split_string_test()
      92             : {
      93           1 :     auto data = "*fdg454()**{&xcx*"sv;
      94           1 :     auto split_string_result = split_string(data, '*');
      95           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
      96           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
      97             :                    && split_string_result.at(1) == "{&xcx"sv);
      98             : 
      99           1 :     auto split_string_to_unsigned_result = split_string_to_unsigned("/4545497//45454/", '/');
     100           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.size() == 2);
     101           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.at(0) == 4545497
     102             :                    && split_string_to_unsigned_result.at(1) == 45454);
     103             : 
     104           1 :     std::string_view line;
     105           1 :     split_string_result.clear();
     106           3 :     while (jami::getline(data, line, '*')) {
     107           2 :         split_string_result.emplace_back(line);
     108             :     }
     109           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
     110           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
     111             :                    && split_string_result.at(1) == "{&xcx"sv);
     112           1 : }
     113             : 
     114             : void
     115           1 : StringUtilsTest::version_test()
     116             : {
     117           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({}, {}));
     118           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {}));
     119           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3}));
     120           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3, 4}, {1, 2, 3}));
     121           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({2}, {1, 2, 3}));
     122           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion(
     123             :         split_string_to_unsigned("1.2.3.5", '.'),
     124             :         split_string_to_unsigned("1.2.3.4", '.')));
     125             : 
     126           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({}, {1, 2, 3}));
     127           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 2}, {1, 2, 3}));
     128           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3, 4}));
     129           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {2}));
     130           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion(
     131             :         split_string_to_unsigned("1.2.3.4", '.'),
     132             :         split_string_to_unsigned("1.2.3.5", '.')));
     133           1 : }
     134             : 
     135             : void
     136           1 : StringUtilsTest::starts_with_test()
     137             : {
     138           1 :     CPPUNIT_ASSERT(starts_with("test", "te"));
     139           1 :     CPPUNIT_ASSERT(starts_with("test", "test"));
     140           1 :     CPPUNIT_ASSERT(!starts_with("test", "testt"));
     141           1 :     CPPUNIT_ASSERT(!starts_with("test", "es"));
     142           1 : }
     143             : 
     144             : } // namespace test
     145             : } // namespace jami
     146             : 
     147           1 : RING_TEST_RUNNER(jami::test::StringUtilsTest::name());

Generated by: LCOV version 1.14