Skip to content

Commit

Permalink
added missing test class ConvertTest and started implementation of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zussel committed May 9, 2024
1 parent 7eb9899 commit 92b7c3c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ SET (TEST_TOOLS_SOURCES
utils/VersionTest.cpp
utils/VersionTest.hpp
utils/ConvertTest.cpp
utils/ConvertTest.hpp)
utils/ConvertTest.hpp
utils/ConvertTest.cpp
utils/ConvertTest.hpp)

SET (TEST_JSON_SOURCES
json/JsonTestUnit.cpp
Expand Down
1 change: 1 addition & 0 deletions test/test_matador.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ int main(int argc, char *argv[])

suite.init(argc, argv);

suite.register_unit(new ConvertTest);
suite.register_unit(new Base64Test);
suite.register_unit(new BufferViewTest);
suite.register_unit(new DateTestUnit);
Expand Down
63 changes: 63 additions & 0 deletions test/utils/ConvertTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "ConvertTest.hpp"

#include "matador/utils/convert.hpp"
#include "matador/utils/date.hpp"
#include "matador/utils/time.hpp"
#include "matador/utils/types.hpp"

ConvertTest::ConvertTest()
: unit_test("convert", "convert test")
{
add_test("convert", [this] { test_convert_integral(); }, "convert test");
}

using namespace matador::utils;

template<typename From, typename To>
void ConvertTest::validate_conversion(From from)
{
To to{};
convert(to, from);

UNIT_ASSERT_EQUAL(static_cast<To>(from), to);
}

template<typename From, typename To>
void ConvertTest::validate_conversion(From from, To expected_to)
{
To to{};
convert(to, from);

UNIT_ASSERT_EQUAL(to, expected_to);
}

template<typename From>
void ConvertTest::validate_integral_conversion(From from)
{
validate_conversion<From, char>(from);
validate_conversion<From, short>(from);
validate_conversion<From, int>(from);
validate_conversion<From, long>(from);
validate_conversion<From, long long>(from);
}

void ConvertTest::test_convert_integral()
{
validate_integral_conversion<char>(-56);
validate_integral_conversion<short>(-127);
validate_integral_conversion<int>(-1234567);
validate_integral_conversion<long>(-9876543);
validate_integral_conversion<long long>(-123456790);

validate_integral_conversion<unsigned char>(56);
validate_integral_conversion<unsigned short>(127);
validate_integral_conversion<unsigned int>(1234567);
validate_integral_conversion<unsigned long>(9876543);
validate_integral_conversion<unsigned long long>(123456790);

validate_conversion<short, char>(513, 1);
validate_conversion<int, char>(514, 2);
validate_conversion<long, char>(515, 3);
validate_conversion<long long, char>(516, 4);

}
23 changes: 23 additions & 0 deletions test/utils/ConvertTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MATADOR_CONVERTTEST_HPP
#define MATADOR_CONVERTTEST_HPP

#include "matador/unit/unit_test.hpp"

class ConvertTest : public matador::unit_test
{
public:
ConvertTest();

void test_convert_integral();

private:
template<typename From, typename To>
void validate_conversion(From from);
template<typename From, typename To>
void validate_conversion(From from, To expected_to);
template<typename From>
void validate_integral_conversion(From from);
};


#endif //MATADOR_CONVERTTEST_HPP

0 comments on commit 92b7c3c

Please sign in to comment.