Initial version

This commit is contained in:
Mateusz Pusz
2018-08-22 12:11:19 +02:00
commit 4b376e3c2e
25 changed files with 1927 additions and 0 deletions

54
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,54 @@
# 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.
cmake_minimum_required(VERSION 3.8)
project(units_test)
# set path to custom cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake/common/cmake")
# include common tools and workarounds
include(tools)
# add dependencies
enable_testing()
find_package(GTest MODULE REQUIRED)
if(NOT TARGET mp::units)
find_package(units CONFIG REQUIRED)
endif()
# unit tests
add_executable(unit_tests
test_dimension.cpp
test_quantity.cpp
test_type_list.cpp
test_units.cpp
)
target_link_libraries(unit_tests
PRIVATE
mp::units
GTest::Main
)
add_test(NAME units.unit_tests
COMMAND
unit_tests
)

65
test/test_dimension.cpp Normal file
View File

@@ -0,0 +1,65 @@
// 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/dimension.h"
#include <gtest/gtest.h>
#include <utility>
using namespace units;
namespace {
template<int Id, int Value>
using e = exp<dim_id<Id>, Value>;
}
// make_dimension
static_assert(std::is_same_v<make_dimension_t<e<0, 1>>, dimension<e<0, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>>, dimension<e<0, 1>, e<1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<0, 1>>, dimension<e<0, 1>, e<1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, 1>>, dimension<e<1, 2>>>);
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, -1>>, dimension<detail::scalar>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, 1>, e<1, 1>>, dimension<e<0, 2>, e<1, 2>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, -1>, e<1, -1>, e<0, -1>, e<1, -1>>, dimension<e<0, -2>, e<1, -2>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<1, -1>>, dimension<e<0, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<0, -1>, e<1, 1>>, dimension<e<1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>>, dimension<e<1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>, e<1, -1>>, dimension<detail::scalar>>);
// dimension_multiply
static_assert(
std::is_same_v<dimension_multiply_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, 1>>>);
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<3, 1>>>,
dimension<e<0, 1>, e<1, 1>, e<2, 1>, e<3, 1>>>);
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, 1>>>,
dimension<e<0, 1>, e<1, 2>, e<2, 1>>>);
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, -1>>>,
dimension<e<0, 1>, e<2, 1>>>);
// dimension_divide
static_assert(std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, -1>>>);
static_assert(std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<0, 1>>>, dimension<detail::scalar>>);

173
test/test_quantity.cpp Normal file
View File

@@ -0,0 +1,173 @@
// 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/si/velocity.h"
#include <gtest/gtest.h>
#include <utility>
using namespace units;
using namespace units::literals;
// is_quantity
static_assert(units::detail::is_quantity<millimeters<int>>::value);
// common_type
static_assert(std::is_same_v<std::common_type_t<meters<int>, kilometers<int>>, meters<int>>);
// constructors
constexpr quantity<dimension_length, int> kilometer{1000};
static_assert(kilometer.count() == 1000);
static_assert(length<int>(kilometer).count() == kilometer.count());
static_assert(meters<int>(kilometer).count() == kilometer.count());
static_assert(millimeters<int>(kilometer).count() == kilometer.count() * 1000);
static_assert(quantity_cast<kilometers<int>>(kilometer).count() == kilometer.count() / 1000);
//static_assert(meters<int>(1.0) == 1_m); // should not compile
static_assert(meters<int>(1) == 1_m);
static_assert(meters<float>(1.0) == 1_m);
static_assert(meters<float>(1) == 1_m);
//static_assert(kilometers<int>(1000_m) == 1000_m); // should not compile
static_assert(kilometers<float>(1000_m) == 1000_m);
//static_assert(meters<int>(meters<float>(1)) == 1_m); // should not compile
static_assert(meters<int>(quantity_cast<meters<int>>(meters<float>(1))) == 1_m);
// zero(), min(), max()
static_assert(length<int>::zero().count() == 0);
static_assert(length<int>::min().count() == std::numeric_limits<int>::min());
static_assert(length<int>::max().count() == std::numeric_limits<int>::max());
// unary member operators
static_assert((+1_m).count() == 1);
static_assert((-1_m).count() == -1);
// binary member operators
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
constexpr auto post_inc(quantity<Dimension, Rep, Ratio> v)
{
auto vv = v++;
return std::make_pair(v, vv);
}
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
constexpr auto pre_inc(quantity<Dimension, Rep, Ratio> v)
{
auto vv = ++v;
return std::make_pair(v, vv);
}
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
constexpr auto post_dec(quantity<Dimension, Rep, Ratio> v)
{
auto vv = v--;
return std::make_pair(v, vv);
}
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
constexpr auto pre_dec(quantity<Dimension, Rep, Ratio> v)
{
auto vv = --v;
return std::make_pair(v, vv);
}
constexpr auto r1 = post_inc(1_m);
static_assert(r1.first.count() == 2);
static_assert(r1.second.count() == 1);
constexpr auto r2 = pre_inc(1_m);
static_assert(r2.first.count() == 2);
static_assert(r2.second.count() == 2);
constexpr auto r3 = post_dec(1_m);
static_assert(r3.first.count() == 0);
static_assert(r3.second.count() == 1);
constexpr auto r4 = pre_dec(1_m);
static_assert(r4.first.count() == 0);
static_assert(r4.second.count() == 0);
// compound assignment
static_assert((1_km += 1_km).count() == 2);
static_assert((2_km -= 1_km).count() == 1);
static_assert((1_km *= 2).count() == 2);
static_assert((2_km /= 2).count() == 1);
static_assert((7_km %= 2).count() == 1);
static_assert((7_km %= 2_km).count() == 1);
// non-member arithmetic operators
static_assert((1_km + 1_m).count() == 1001);
static_assert((1_km - 1_m).count() == 999);
static_assert((2_km * 2).count() == 4);
static_assert((3 * 3_km).count() == 9);
static_assert((2_kmph * 2_h).count() == 4);
//static_assert(kilometers<float>(2_kmph * 15_min).count() == 500_m); // should not compile
static_assert(kilometers<float>(2_kmph * 120.0_min).count() == 4);
static_assert(kilometers<float>(2.0_kmph * 120_min).count() == 4);
static_assert((4_km / 2).count() == 2);
static_assert(4_km / 2_km == 2);
static_assert((1_km / 1_s).count() == 1);
//static_assert((1_km / 1_h).count() == 1); // should not compile
static_assert((1.0_km / 1_h).count() == 1);
static_assert((7_km % 2).count() == 1);
static_assert((7_km % 2_km).count() == 1);
// comparators
static_assert(2_km + 1_km == 3_km);
static_assert(!(2_km + 2_km == 3_km));
static_assert(2_km + 2_km != 3_km);
static_assert(!(2_km + 2_km != 4_km));
static_assert(2_km > 1_km);
static_assert(!(1_km > 1_km));
static_assert(1_km < 2_km);
static_assert(!(2_km < 2_km));
static_assert(2_km >= 1_km);
static_assert(2_km >= 2_km);
static_assert(!(2_km >= 3_km));
static_assert(1_km <= 2_km);
static_assert(2_km <= 2_km);
static_assert(!(3_km <= 2_km));
// quantity_cast
static_assert(quantity_cast<kilometers<int>>(2000_m) == 2_km);
// static_assert(quantity_cast<seconds<int>>(2_m) == 2_s); // should not compile

77
test/test_type_list.cpp Normal file
View File

@@ -0,0 +1,77 @@
// 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/dimension.h"
#include <gtest/gtest.h>
#include <utility>
using namespace mp;
using namespace units;
// type_list_push_front
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int>, type_list<int>>);
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int, long, double>, type_list<int, long, double>>);
static_assert(std::is_same_v<type_list_push_front_t<type_list<double>, int, long>, type_list<int, long, double>>);
// type_list_split_half
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::first_list, type_list<int>>);
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::second_list, type_list<>>);
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::first_list, type_list<int>>);
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::second_list, type_list<long>>);
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::first_list, type_list<int, long>>);
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::second_list, type_list<double>>);
static_assert(
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::first_list, type_list<int, long>>);
static_assert(
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
// type_list_merge_sorted
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<0>>, type_list<dim_id<1>>, dim_id_less>,
type_list<dim_id<0>, dim_id<1>>>);
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<1>>, type_list<dim_id<0>>, dim_id_less>,
type_list<dim_id<0>, dim_id<1>>>);
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<27>, dim_id<38>>,
type_list<dim_id<3>, dim_id<43>>, dim_id_less>,
type_list<dim_id<3>, dim_id<27>, dim_id<38>, dim_id<43>>>);
static_assert(
std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<9>, dim_id<82>>, type_list<dim_id<10>>, dim_id_less>,
type_list<dim_id<9>, dim_id<10>, dim_id<82>>>);
// type_list_sort
static_assert(std::is_same_v<type_list_sort_t<type_list<dim_id<0>>, dim_id_less>, type_list<dim_id<0>>>);
static_assert(
std::is_same_v<type_list_sort_t<type_list<dim_id<0>, dim_id<1>>, dim_id_less>, type_list<dim_id<0>, dim_id<1>>>);
static_assert(
std::is_same_v<type_list_sort_t<type_list<dim_id<1>, dim_id<0>>, dim_id_less>, type_list<dim_id<0>, dim_id<1>>>);
static_assert(
std::is_same_v<
type_list_sort_t<type_list<dim_id<38>, dim_id<27>, dim_id<43>, dim_id<3>, dim_id<9>, dim_id<82>, dim_id<10>>,
dim_id_less>,
type_list<dim_id<3>, dim_id<9>, dim_id<10>, dim_id<27>, dim_id<38>, dim_id<43>, dim_id<82>>>);

70
test/test_units.cpp Normal file
View File

@@ -0,0 +1,70 @@
// 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/si/length.h"
#include "units/si/time.h"
#include "units/si/frequency.h"
#include "units/si/velocity.h"
#include <gtest/gtest.h>
#include <utility>
using namespace units;
using namespace units::literals;
// frequency
static_assert(2 / 1_s == 2_Hz);
static_assert(1000 / 1_s == 1_kHz);
static_assert(3.2_GHz == 3'200'000'000_Hz);
// time
static_assert(1_h == 3600_s);
// length
static_assert(1_km == 1000_m);
static_assert(10_km / 5_km == 2);
static_assert(10_km / 2 == 5_km);
// velocity
static_assert(std::is_same_v<decltype(1_km / 1_s), velocity<long long int, std::ratio<1000, 1>>>);
static_assert(10_m / 5_s == 2_mps);
static_assert(1_km / 1_s == 1000_mps);
//static_assert(1_km / 1_h == 1_kmph); // should not compile
static_assert(1.0_km / 1_h == 1_kmph);
static_assert(1000.0_m / 3600.0_s == 1_kmph);
static_assert(2_kmph * 2_h == 4_km);
//static_assert(2_kmph * 15_min == 500_m); // should not compile
static_assert(2_kmph * 15.0_min == 500_m);
static_assert(2.0_kmph * 15_min == 500_m);
static_assert(2_km / 2_kmph == 1_h);
// static_assert(2000_m / 2_kmph == 1_h); // should not compile
static_assert(quantity_cast<kilometers<int>>(2000_m) / 2_kmph == 1_h);