diff --git a/.clang-format b/.clang-format index 14a58e25..ef4261c8 100644 --- a/.clang-format +++ b/.clang-format @@ -62,10 +62,13 @@ DerivePointerAlignment: false # DisableFormat: false # ExperimentalAutoDetectBinPacking: false # FixNamespaceComments: true -# ForEachMacros: -# - foreach -# - Q_FOREACH -# - BOOST_FOREACH +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH + - GIVEN + - WHEN + - THEN IncludeBlocks: Merge IncludeCategories: - Regex: '^".*' diff --git a/conanfile.py b/conanfile.py index 90d07cbb..d2f45af6 100644 --- a/conanfile.py +++ b/conanfile.py @@ -45,7 +45,8 @@ class UnitsConan(ConanFile): exports = ["LICENSE.md"] settings = "os", "compiler", "build_type", "arch" requires = ( - "range-v3/0.9.1@ericniebler/stable" + "range-v3/0.9.1@ericniebler/stable", + "Catch2/2.10.0@catchorg/stable" ) scm = { "type": "git", diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 44eef94a..a68376b5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,5 +20,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -add_subdirectory(unit_test) +add_subdirectory(unit_test/runtime) +add_subdirectory(unit_test/static) add_subdirectory(metabench) diff --git a/test/unit_test/CMakeLists.txt b/test/unit_test/runtime/CMakeLists.txt similarity index 85% rename from test/unit_test/CMakeLists.txt rename to test/unit_test/runtime/CMakeLists.txt index 328cbe37..5242e8e5 100644 --- a/test/unit_test/CMakeLists.txt +++ b/test/unit_test/runtime/CMakeLists.txt @@ -20,16 +20,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -# unit tests -add_library(unit_tests - test_custom_units.cpp - test_dimension.cpp - test_quantity.cpp - test_ratio.cpp - test_type_list.cpp - test_units.cpp +add_executable(unit_tests_runtime + catch_main.cpp + math_test.cpp ) -target_link_libraries(unit_tests +target_link_libraries(unit_tests_runtime PRIVATE mp::units + CONAN_PKG::Catch2 ) diff --git a/test/unit_test/runtime/catch_main.cpp b/test/unit_test/runtime/catch_main.cpp new file mode 100644 index 00000000..4ed06df1 --- /dev/null +++ b/test/unit_test/runtime/catch_main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include diff --git a/test/unit_test/runtime/math_test.cpp b/test/unit_test/runtime/math_test.cpp new file mode 100644 index 00000000..631402d1 --- /dev/null +++ b/test/unit_test/runtime/math_test.cpp @@ -0,0 +1,85 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "units/math.h" +#include "print_helpers.h" +#include "units/dimensions/area.h" +#include + +using namespace units; + +// classical + +TEST_CASE("pow() on quantity changes the value and the dimension accordingly", "[math][pow]") +{ + // CHECK(pow<0>(2m) == 2); + CHECK(pow<1>(2m) == 2m); + CHECK(pow<2>(2m) == 4sq_m); +} + +TEST_CASE("sqrt() on quantity changes the value and the dimension accordingly", "[math][sqrt]") +{ + REQUIRE(sqrt(4sq_m) == 2m); +} + +// BDD style + +SCENARIO("quantities should work with pow()", "[math][pow]") +{ + GIVEN("A quantity q") { + auto q = 2m; + + REQUIRE(q.count() == 2); + + WHEN("pow<1>(q) is called") { + auto res = pow<1>(q); + + THEN("the same quantity is received") { + REQUIRE(res == q); + } + } + WHEN("pow<2>(q) is called") { + auto res = pow<2>(q); + + THEN("both the value and dimension is raised to the exponent 2") { + REQUIRE(res == 4sq_m); + } + } + } +} + +SCENARIO("quantities should work with sqrt()", "[math][sqrt]") +{ + GIVEN("A quantity q") { + auto q = 4sq_m; + + REQUIRE(q.count() == 4); + + WHEN("sqrt(q) is called") { + auto res = sqrt(q); + + THEN("both the value and dimension are square rooted") { + REQUIRE(res == 2m); + } + } + } +} diff --git a/test/unit_test/runtime/print_helpers.h b/test/unit_test/runtime/print_helpers.h new file mode 100644 index 00000000..c2b6a799 --- /dev/null +++ b/test/unit_test/runtime/print_helpers.h @@ -0,0 +1,41 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "units/dimensions/area.h" + +// TODO Remove when a text formatting is implemented + +namespace units { + + template + std::ostream& operator<<(std::ostream& os, const quantity& value) + { + return os << value.count() << "m"; + } + + template + std::ostream& operator<<(std::ostream& os, const quantity& value) + { + return os << value.count() << "m^2"; + } + +} diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt new file mode 100644 index 00000000..94241c39 --- /dev/null +++ b/test/unit_test/static/CMakeLists.txt @@ -0,0 +1,35 @@ +# The MIT License (MIT) +# +# Copyright (c) 2018 Mateusz Pusz +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +add_library(unit_tests_static + custom_unit_test.cpp + dimension_test.cpp + math_test.cpp + quantity_test.cpp + ratio_test.cpp + type_list_test.cpp + unit_test.cpp +) +target_link_libraries(unit_tests_static + PRIVATE + mp::units +) diff --git a/test/unit_test/test_custom_units.cpp b/test/unit_test/static/custom_unit_test.cpp similarity index 100% rename from test/unit_test/test_custom_units.cpp rename to test/unit_test/static/custom_unit_test.cpp diff --git a/test/unit_test/test_dimension.cpp b/test/unit_test/static/dimension_test.cpp similarity index 100% rename from test/unit_test/test_dimension.cpp rename to test/unit_test/static/dimension_test.cpp diff --git a/test/unit_test/static/math_test.cpp b/test/unit_test/static/math_test.cpp new file mode 100644 index 00000000..907a3da1 --- /dev/null +++ b/test/unit_test/static/math_test.cpp @@ -0,0 +1,34 @@ +// The MIT License (MIT) +// +// Copyright (c) 2018 Mateusz Pusz +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "units/dimensions/area.h" +#include "units/math.h" + +using namespace units; + +namespace { + + static_assert(std::is_same_v(2m)), decltype(2m)>); + static_assert(std::is_same_v(2m)), decltype(4sq_m)>); + static_assert(std::is_same_v); + +} // namespace diff --git a/test/unit_test/test_quantity.cpp b/test/unit_test/static/quantity_test.cpp similarity index 100% rename from test/unit_test/test_quantity.cpp rename to test/unit_test/static/quantity_test.cpp diff --git a/test/unit_test/test_ratio.cpp b/test/unit_test/static/ratio_test.cpp similarity index 100% rename from test/unit_test/test_ratio.cpp rename to test/unit_test/static/ratio_test.cpp diff --git a/test/unit_test/test_type_list.cpp b/test/unit_test/static/type_list_test.cpp similarity index 99% rename from test/unit_test/test_type_list.cpp rename to test/unit_test/static/type_list_test.cpp index e47df4e6..491ede6a 100644 --- a/test/unit_test/test_type_list.cpp +++ b/test/unit_test/static/type_list_test.cpp @@ -20,6 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#include "units/bits/type_list.h" #include "units/dimension.h" #include diff --git a/test/unit_test/test_units.cpp b/test/unit_test/static/unit_test.cpp similarity index 99% rename from test/unit_test/test_units.cpp rename to test/unit_test/static/unit_test.cpp index feeca36a..a972bf02 100644 --- a/test/unit_test/test_units.cpp +++ b/test/unit_test/static/unit_test.cpp @@ -20,26 +20,24 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -71,8 +69,6 @@ namespace { static_assert(5in + 8cm == 207mm); - - /* ************** DERIVED DIMENSIONS WITH NAMED UNITS **************** */ // frequency @@ -114,7 +110,6 @@ namespace { static_assert(10C / 10V == 1F); - /* ************** DERIVED DIMENSIONS IN TERMS OF BASE UNITS **************** */ // velocity