Skip to content

Commit

Permalink
Add unit testing 'framework'
Browse files Browse the repository at this point in the history
- Wrapper around GTest which better unifies static and dynamic tests
  • Loading branch information
John McFarlane committed Jan 31, 2022
1 parent 6d23474 commit f61f66b
Show file tree
Hide file tree
Showing 107 changed files with 188 additions and 401 deletions.
26 changes: 0 additions & 26 deletions include/cnl/_impl/type_traits/identical.h

This file was deleted.

1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if (${GTest_FOUND})
# workaround for github.com/conan-io/conan-center-index/issues/3222
set(CNL_GTEST_MAIN_TARGET GTest::Main CACHE STRING "alternative GTest target name")

add_subdirectory(framework)
add_subdirectory(unit)
else ()
message(STATUS "Google Test is required to build unit tests.")
Expand Down
5 changes: 5 additions & 0 deletions test/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test framework

add_library(cnl_test INTERFACE)
target_include_directories(cnl_test INTERFACE "${CMAKE_CURRENT_LIST_DIR}")
target_link_libraries(cnl_test INTERFACE ${CNL_GTEST_MAIN_TARGET})
25 changes: 25 additions & 0 deletions test/framework/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

// Copyright John McFarlane 2022.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

/// \file
/// \brief definitions of use in tests

#if !defined(CNL_TEST_FRAMEWORK_TEST_H)
#define CNL_TEST_FRAMEWORK_TEST_H

#include <gtest/gtest.h>

#include <type_traits>

// cnl::_impl::identical - compiles iff same type; returns true iff equal
template<class A, class B>
[[nodiscard]] constexpr auto identical(A const& a, B const& b)
{
static_assert(std::is_same<A, B>::value, "different types");
return a == b;
}

#endif // CNL_TEST_FRAMEWORK_TEST_H
15 changes: 2 additions & 13 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,6 @@ set(sample_sources

find_package(Boost 1.71.0)

######################################################################
# add_gtest_dependency

function(add_gtest_dependency target)
target_link_libraries("${target}" ${CNL_GTEST_MAIN_TARGET})
endfunction(add_gtest_dependency)

######################################################################
# add test-unit target

Expand All @@ -155,9 +148,8 @@ add_dependencies(test-all test-unit)
# test plain - the all.cpp project with *no* tests of compiler flags

add_executable(test-unit-plain all.cpp)
target_link_libraries(test-unit-plain Cnl)
target_link_libraries(test-unit-plain Cnl cnl_test)
add_test(test-unit-plain "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-unit-plain")
add_gtest_dependency(test-unit-plain)
add_dependencies(test-unit test-unit-plain)

######################################################################
Expand All @@ -175,12 +167,9 @@ function(make_test source compile_flags)
add_executable("${target}" "${source}")
target_include_directories("${target}" PRIVATE "${CMAKE_CURRENT_LIST_DIR}")
set_target_properties("${target}" PROPERTIES COMPILE_FLAGS "${compile_flags}")
target_link_libraries("${target}" Cnl)
target_link_libraries("${target}" Cnl cnl_test)
add_test("${target}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target}")

# Google Test dependency
add_gtest_dependency("${target}")

# Boost dependency
if(Boost_FOUND)
target_compile_definitions("${target}" PRIVATE "-DCNL_BOOST_ENABLED")
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/charconv/to_chars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

#include <cnl/_impl/charconv/to_chars.h>

#include <cnl/_impl/type_traits/identical.h>

#include <gtest/gtest.h>
#include <test.h>

#include <cstdint>
#include <limits>

using cnl::_impl::identical;

#if !defined(_MSC_VER)
namespace {
static_assert(identical(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/cmath/abs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

#include <cnl/_impl/cmath/abs.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/cstdint.h>

using cnl::_impl::identical;
#include <test.h>

namespace test_abs {
static_assert(identical(cnl::_impl::abs(-302398479), 302398479));
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/cmath/sqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

#include <cnl/_impl/cmath/sqrt.h>

#include <cnl/_impl/type_traits/identical.h>

using cnl::_impl::identical;
#include <test.h>

static_assert(identical(0xffffffffLLU, cnl::sqrt(0xfffffffe00000001LLU)));
static_assert(identical(0xfffffffeLLU, cnl::sqrt(0xfffffffe00000000LLU)));
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/duplex_int/definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@
#include <cnl/_impl/duplex_integer/operators.h>
#include <cnl/cstdint.h>

#include <cnl/_impl/type_traits/identical.h>

#include <gtest/gtest.h>
#include <test.h>

#include <limits>

using cnl::_impl::identical;

namespace {
namespace test_ctor {
static_assert(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/duplex_int/digits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

#include <cnl/_impl/duplex_integer/digits.h>

#include <cnl/_impl/type_traits/identical.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
static_assert(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/duplex_int/from_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#include <cnl/_impl/duplex_integer/from_value.h>

#include <cnl/_impl/duplex_integer.h>
#include <cnl/_impl/type_traits/identical.h>
#include <cnl/cstdint.h>

using cnl::_impl::identical;
#include <test.h>

using duplex_63_int = cnl::_impl::duplex_integer<std::int32_t, std::uint32_t>;
using duplex_127_int = cnl::_impl::duplex_integer<
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/duplex_int/numeric_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
#include <cnl/_impl/duplex_integer/numeric_limits.h>
#include <cnl/_impl/duplex_integer/operators.h>

#include <cnl/_impl/type_traits/identical.h>
#include <test.h>

#include <limits>

using cnl::_impl::identical;

namespace {
namespace test_is_specialize {
static_assert(
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/duplex_int/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
#include <cnl/_impl/duplex_integer/ctors.h>
#include <cnl/_impl/duplex_integer/operators.h>

#include <cnl/_impl/type_traits/identical.h>

#include <gtest/gtest.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
namespace test_not {
Expand Down
3 changes: 1 addition & 2 deletions test/unit/_impl/elastic_int/sqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

#include <cnl/elastic_integer.h>

#include <cnl/_impl/type_traits/identical.h>
#include <test.h>

using cnl::_impl::identical;
using namespace cnl::literals;

static_assert(identical(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/num_traits/max_digits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

#include <cnl/_impl/config.h>
#include <cnl/_impl/num_traits/max_digits.h>
#include <cnl/_impl/type_traits/identical.h>
#include <test.h>

namespace {
using cnl::_impl::identical;

#if defined(CNL_INT128_ENABLED)
static_assert(
identical(cnl::_impl::max_digits<std::uint8_t>, 128), "cnl::_impl::max_digits<>");
Expand Down
2 changes: 1 addition & 1 deletion test/unit/_impl/ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <cnl/_impl/ostream.h>

#include <gtest/gtest.h>
#include <test.h>

#include <limits>

Expand Down
2 changes: 1 addition & 1 deletion test/unit/_impl/overflow/is_overflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// http://www.boost.org/LICENSE_1_0.txt)

#include <cnl/_impl/overflow/is_overflow.h>
#include <cnl/_impl/type_traits/identical.h>
#include <test.h>

#include <limits>

Expand Down
6 changes: 2 additions & 4 deletions test/unit/_impl/rounding/convert_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
/// \brief tests for <cnl/_impl/rounding/convert.h>

#include <cnl/_impl/rounding/convert_operator.h>
#include <cnl/_impl/type_traits/identical.h>
#include <cnl/elastic_scaled_integer.h>
#include <cnl/scaled_integer.h>

using cnl::_impl::identical;
#include <test.h>

namespace test_convert_nearest_rounding_native_datatypes {

Expand Down Expand Up @@ -123,7 +121,7 @@ namespace test_convert_tie_to_pos_inf_rounding_native_datatypes {

namespace test_convert_native_rounding {
static_assert(
cnl::_impl::identical(
identical(
short{3},
cnl::custom_operator<
cnl::_impl::convert_op,
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/wide-integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
#include <cnl/_impl/num_traits/rounding.h>
#include <cnl/_impl/rounding/native_rounding_tag.h>

#include <cnl/_impl/type_traits/identical.h>

#include <gtest/gtest.h>

using cnl::_impl::identical;
#include <test.h>

static_assert(cnl::is_integer_v<cnl::_impl::math::wide_integer::uintwide_t<64>>);

Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/wide_int/definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@

#include <cnl/_impl/wide_integer/definition.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/cstdint.h>
#include <cnl/wide_integer.h>

#include <gtest/gtest.h>
#include <test.h>

#include <type_traits>

using cnl::_impl::identical;

namespace {
namespace test_parameters {
static_assert(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/wide_int/digits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

#include <cnl/_impl/wide_integer/digits.h>

#include <cnl/_impl/type_traits/identical.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
static_assert(
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/wide_int/from_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

#include <cnl/wide_integer.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/_impl/wide_integer/operators.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
static_assert(
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/wide_int/generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@

#include <cnl/_impl/wide_tag/custom_operator.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/cmath.h>
#include <cnl/wide_integer.h>

#include <gtest/gtest.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
TEST(wide_integer, multiply) // NOLINT
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/wide_int/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

#include <cnl/wide_integer.h>

#include <cnl/_impl/type_traits/identical.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
namespace parse {
Expand Down
4 changes: 1 addition & 3 deletions test/unit/_impl/wide_int/make_wide_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

#include <cnl/_impl/wide_integer/make_wide_integer.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/_impl/wide_integer/operators.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
static_assert(
Expand Down
5 changes: 1 addition & 4 deletions test/unit/_impl/wide_int/numeric_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

#include <cnl/_impl/wide_integer/numeric_limits.h>

#include <cnl/_impl/type_traits/identical.h>
#include <cnl/wide_integer.h>

#include <gtest/gtest.h>
#include <test.h>

#include <limits>

using cnl::_impl::identical;

namespace {
static_assert(
std::numeric_limits<cnl::wide_integer<>>::is_specialized,
Expand Down
6 changes: 1 addition & 5 deletions test/unit/_impl/wide_int/scale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
#include <cnl/_impl/num_traits/fixed_width_scale.h>
#include <cnl/wide_integer.h>

#include <cnl/_impl/type_traits/identical.h>

#include <gtest/gtest.h>

using cnl::_impl::identical;
#include <test.h>

namespace {
namespace test_power_value {
Expand Down
Loading

0 comments on commit f61f66b

Please sign in to comment.