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-04-25 08:05:53 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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 "account.h"
      26             : 
      27             : #include <string>
      28             : #include <string_view>
      29             : 
      30             : #include "../../test_runner.h"
      31             : 
      32             : using namespace std::literals;
      33             : 
      34             : namespace jami {
      35             : namespace test {
      36             : 
      37             : class StringUtilsTest : public CppUnit::TestFixture
      38             : {
      39             : public:
      40           2 :     static std::string name() { return "string_utils"; }
      41             : 
      42             : private:
      43             :     void bool_to_str_test();
      44             :     void to_string_test();
      45             :     void to_number_test();
      46             :     void split_string_test();
      47             :     void version_test();
      48             : 
      49           2 :     CPPUNIT_TEST_SUITE(StringUtilsTest);
      50           1 :     CPPUNIT_TEST(bool_to_str_test);
      51           1 :     CPPUNIT_TEST(to_string_test);
      52           1 :     CPPUNIT_TEST(to_number_test);
      53           1 :     CPPUNIT_TEST(split_string_test);
      54           1 :     CPPUNIT_TEST(version_test);
      55           4 :     CPPUNIT_TEST_SUITE_END();
      56             : 
      57             :     const double DOUBLE = 3.14159265359;
      58             :     const int INT = 42;
      59             :     const std::string PI_DOUBLE = "3.14159265359";
      60             :     const std::string PI_FLOAT = "3.14159265359";
      61             :     const std::string PI_42 = "42";
      62             : };
      63             : 
      64             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(StringUtilsTest, StringUtilsTest::name());
      65             : 
      66             : void
      67           1 : StringUtilsTest::bool_to_str_test()
      68             : {
      69           1 :     CPPUNIT_ASSERT(bool_to_str(true) == TRUE_STR);
      70           1 :     CPPUNIT_ASSERT(bool_to_str(false) == FALSE_STR);
      71           1 : }
      72             : 
      73             : void
      74           1 : StringUtilsTest::to_string_test()
      75             : {
      76             :     // test with double
      77           1 :     CPPUNIT_ASSERT(to_string(DOUBLE) == PI_DOUBLE);
      78             : 
      79             :     // test with float
      80           1 :     float varFloat = 3.14;
      81           1 :     std::string sVarFloat = to_string(varFloat);
      82           1 :     CPPUNIT_ASSERT(sVarFloat.at(0) == '3' && sVarFloat.at(1) == '.' && sVarFloat.at(2) == '1'
      83             :                    && sVarFloat.at(3) == '4');
      84             : 
      85             :     // test with int
      86           1 :     CPPUNIT_ASSERT(std::to_string(INT).compare(PI_42) == 0);
      87             : 
      88           1 :     CPPUNIT_ASSERT_EQUAL("0000000000000010"s, to_hex_string(16));
      89           1 :     CPPUNIT_ASSERT_EQUAL((uint64_t)16, from_hex_string("0000000000000010"s));
      90           1 : }
      91             : 
      92             : void
      93           1 : StringUtilsTest::to_number_test()
      94             : {
      95             :     // test with int
      96           1 :     CPPUNIT_ASSERT(jami::stoi(PI_42) == INT);
      97             : 
      98             :     // test with double
      99           1 :     CPPUNIT_ASSERT(jami::stod(PI_DOUBLE) == DOUBLE);
     100           1 : }
     101             : 
     102             : void
     103           1 : StringUtilsTest::split_string_test()
     104             : {
     105           1 :     auto data = "*fdg454()**{&xcx*"sv;
     106           1 :     auto split_string_result = split_string(data, '*');
     107           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
     108           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
     109             :                    && split_string_result.at(1) == "{&xcx"sv);
     110             : 
     111           1 :     auto split_string_to_unsigned_result = split_string_to_unsigned("/4545497//45454/", '/');
     112           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.size() == 2);
     113           1 :     CPPUNIT_ASSERT(split_string_to_unsigned_result.at(0) == 4545497
     114             :                    && split_string_to_unsigned_result.at(1) == 45454);
     115             : 
     116           1 :     std::string_view line;
     117           1 :     split_string_result.clear();
     118           3 :     while (jami::getline(data, line, '*')) {
     119           2 :         split_string_result.emplace_back(line);
     120             :     }
     121           1 :     CPPUNIT_ASSERT(split_string_result.size() == 2);
     122           1 :     CPPUNIT_ASSERT(split_string_result.at(0) == "fdg454()"sv
     123             :                    && split_string_result.at(1) == "{&xcx"sv);
     124           1 : }
     125             : 
     126             : void
     127           1 : StringUtilsTest::version_test()
     128             : {
     129           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({}, {}));
     130           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {}));
     131           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3}));
     132           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({1, 2, 3, 4}, {1, 2, 3}));
     133           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion({2}, {1, 2, 3}));
     134           1 :     CPPUNIT_ASSERT(Account::meetMinimumRequiredVersion(
     135             :         split_string_to_unsigned("1.2.3.5", '.'),
     136             :         split_string_to_unsigned("1.2.3.4", '.')));
     137             : 
     138           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({}, {1, 2, 3}));
     139           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 2}, {1, 2, 3}));
     140           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {1, 2, 3, 4}));
     141           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion({1, 2, 3}, {2}));
     142           1 :     CPPUNIT_ASSERT(!Account::meetMinimumRequiredVersion(
     143             :         split_string_to_unsigned("1.2.3.4", '.'),
     144             :         split_string_to_unsigned("1.2.3.5", '.')));
     145           1 : }
     146             : 
     147             : } // namespace test
     148             : } // namespace jami
     149             : 
     150           1 : RING_TEST_RUNNER(jami::test::StringUtilsTest::name());

Generated by: LCOV version 1.14