Catch2 support added (resolves #8)

This commit is contained in:
Mateusz Pusz
2019-10-14 10:53:02 +02:00
parent 44067dfd19
commit 3475f5193e
15 changed files with 230 additions and 36 deletions

View File

@ -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: '^".*'

View File

@ -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",

View File

@ -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)

View File

@ -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
)

View File

@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

View File

@ -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 <catch2/catch.hpp>
using namespace units;
// classical
TEST_CASE("pow<N>() 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<N>()", "[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);
}
}
}
}

View File

@ -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<typename Rep>
std::ostream& operator<<(std::ostream& os, const quantity<metre, Rep>& value)
{
return os << value.count() << "m";
}
template<typename Rep>
std::ostream& operator<<(std::ostream& os, const quantity<square_metre, Rep>& value)
{
return os << value.count() << "m^2";
}
}

View File

@ -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
)

View File

@ -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<decltype(pow<1>(2m)), decltype(2m)>);
static_assert(std::is_same_v<decltype(pow<2>(2m)), decltype(4sq_m)>);
static_assert(std::is_same_v<decltype(sqrt(4sq_m)), decltype(2m)>);
} // namespace

View File

@ -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 <utility>

View File

@ -20,26 +20,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/dimensions/time.h>
#include <units/dimensions/length.h>
#include <units/dimensions/mass.h>
#include <units/dimensions/current.h>
#include <units/dimensions/temperature.h>
#include <units/dimensions/substance.h>
#include <units/dimensions/luminous_intensity.h>
#include <units/dimensions/frequency.h>
#include <units/dimensions/force.h>
#include <units/dimensions/pressure.h>
#include <units/dimensions/energy.h>
#include <units/dimensions/power.h>
#include <units/dimensions/electric_charge.h>
#include <units/dimensions/voltage.h>
#include <units/dimensions/capacitance.h>
#include <units/dimensions/velocity.h>
#include <units/dimensions/acceleration.h>
#include <units/dimensions/area.h>
#include <units/dimensions/capacitance.h>
#include <units/dimensions/current.h>
#include <units/dimensions/electric_charge.h>
#include <units/dimensions/energy.h>
#include <units/dimensions/force.h>
#include <units/dimensions/frequency.h>
#include <units/dimensions/length.h>
#include <units/dimensions/luminous_intensity.h>
#include <units/dimensions/mass.h>
#include <units/dimensions/power.h>
#include <units/dimensions/pressure.h>
#include <units/dimensions/substance.h>
#include <units/dimensions/temperature.h>
#include <units/dimensions/time.h>
#include <units/dimensions/velocity.h>
#include <units/dimensions/voltage.h>
#include <units/dimensions/volume.h>
#include <utility>
@ -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