From 92b7c3cd69928c1f530027ca60a97d7ce1ce768d Mon Sep 17 00:00:00 2001 From: Sascha Kuehl Date: Thu, 9 May 2024 12:41:38 +0200 Subject: [PATCH] added missing test class ConvertTest and started implementation of tests --- test/CMakeLists.txt | 4 ++- test/test_matador.cpp | 1 + test/utils/ConvertTest.cpp | 63 ++++++++++++++++++++++++++++++++++++++ test/utils/ConvertTest.hpp | 23 ++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/utils/ConvertTest.cpp create mode 100644 test/utils/ConvertTest.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d4289c700..75543f8e8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/test_matador.cpp b/test/test_matador.cpp index 69be06dde..84bb3ed86 100644 --- a/test/test_matador.cpp +++ b/test/test_matador.cpp @@ -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); diff --git a/test/utils/ConvertTest.cpp b/test/utils/ConvertTest.cpp new file mode 100644 index 000000000..ea12805c5 --- /dev/null +++ b/test/utils/ConvertTest.cpp @@ -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 +void ConvertTest::validate_conversion(From from) +{ + To to{}; + convert(to, from); + + UNIT_ASSERT_EQUAL(static_cast(from), to); +} + +template +void ConvertTest::validate_conversion(From from, To expected_to) +{ + To to{}; + convert(to, from); + + UNIT_ASSERT_EQUAL(to, expected_to); +} + +template +void ConvertTest::validate_integral_conversion(From from) +{ + validate_conversion(from); + validate_conversion(from); + validate_conversion(from); + validate_conversion(from); + validate_conversion(from); +} + +void ConvertTest::test_convert_integral() +{ + validate_integral_conversion(-56); + validate_integral_conversion(-127); + validate_integral_conversion(-1234567); + validate_integral_conversion(-9876543); + validate_integral_conversion(-123456790); + + validate_integral_conversion(56); + validate_integral_conversion(127); + validate_integral_conversion(1234567); + validate_integral_conversion(9876543); + validate_integral_conversion(123456790); + + validate_conversion(513, 1); + validate_conversion(514, 2); + validate_conversion(515, 3); + validate_conversion(516, 4); + +} \ No newline at end of file diff --git a/test/utils/ConvertTest.hpp b/test/utils/ConvertTest.hpp new file mode 100644 index 000000000..9a410c141 --- /dev/null +++ b/test/utils/ConvertTest.hpp @@ -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 + void validate_conversion(From from); + template + void validate_conversion(From from, To expected_to); + template + void validate_integral_conversion(From from); +}; + + +#endif //MATADOR_CONVERTTEST_HPP