Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CNL_ASSERT_EQ #1028

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cnl/_impl/charconv/to_chars.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace cnl {
}

using native_rounding_type = set_rounding_t<decltype(value), native_rounding_tag>;
auto const& native_rounding_value = static_cast<native_rounding_type>(value);
auto const& native_rounding_value = static_cast<native_rounding_type const&>(value);

return _impl::to_chars_non_zero<native_rounding_type>(
first, last, native_rounding_value, base);
Expand Down
22 changes: 0 additions & 22 deletions include/cnl/_impl/type_traits/assert_same.h

This file was deleted.

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})
29 changes: 29 additions & 0 deletions test/framework/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

// 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>

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define CNL_ASSERT_EQ(a, b) \
ASSERT_EQ(a, b);

// 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
30 changes: 13 additions & 17 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 All @@ -25,30 +21,30 @@ namespace {

TEST(charconv_to_chars, int32_42) // NOLINT
{
auto const expected{cnl::to_chars_static_result<11>{{'4', '2'}, 2}};
auto const actual{cnl::to_chars_static(std::int32_t{42})};
ASSERT_EQ(expected, actual);
constexpr auto expected{cnl::to_chars_static_result<11>{{'4', '2'}, 2}};
constexpr auto actual{cnl::to_chars_static(std::int32_t{42})};
CNL_ASSERT_EQ(expected, actual);
}

TEST(charconv_to_chars, int32_min) // NOLINT
{
auto const expected{cnl::to_chars_static_result<11>{
constexpr auto expected{cnl::to_chars_static_result<11>{
{'-', '2', '1', '4', '7', '4', '8', '3', '6', '4', '7'}, 11}};
auto const actual{cnl::to_chars_static(std::numeric_limits<std::int32_t>::min() + 1)};
ASSERT_EQ(expected, actual);
constexpr auto actual{cnl::to_chars_static(std::numeric_limits<std::int32_t>::min() + 1)};
CNL_ASSERT_EQ(expected, actual);
}

TEST(charconv_to_chars, uint16_max) // NOLINT
{
auto const expected{cnl::to_chars_static_result<5>{
constexpr auto expected{cnl::to_chars_static_result<5>{
{'6', '5', '5', '3', '5'}, 5}};
auto const actual{cnl::to_chars_static(std::numeric_limits<std::uint16_t>::max())};
ASSERT_EQ(expected, actual);
constexpr auto actual{cnl::to_chars_static(std::numeric_limits<std::uint16_t>::max())};
CNL_ASSERT_EQ(expected, actual);
}

TEST(charconv_to_chars, int64_zero) // NOLINT
{
auto const expected{cnl::to_chars_static_result<20>{{'0'}, 1}};
auto const actual{cnl::to_chars_static(std::int64_t{0})};
ASSERT_EQ(expected, actual);
constexpr auto expected{cnl::to_chars_static_result<20>{{'0'}, 1}};
constexpr auto actual{cnl::to_chars_static(std::int64_t{0})};
CNL_ASSERT_EQ(expected, actual);
}
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
38 changes: 17 additions & 21 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 All @@ -35,7 +31,7 @@ namespace {
constexpr auto expected =
cnl::_impl::duplex_integer<std::uint32_t, std::uint32_t>{static_cast<std::uint64_t>(1.23456e15)};
auto const actual = cnl::_impl::duplex_integer<std::uint32_t, std::uint32_t>{1.23456e15};
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}
}

Expand Down Expand Up @@ -68,42 +64,42 @@ namespace {
{
auto expected = std::int8_t{0};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_int8_zero) // NOLINT
{
auto expected = std::int8_t{0};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{0}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_int8_650) // NOLINT
{
auto expected = std::int8_t{0};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{650}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_int8_minus_650) // NOLINT
{
auto expected = std::int8_t{-1};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{-650}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_int8_123) // NOLINT
{
auto expected = std::int8_t{0};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{123}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_int8_minus_1) // NOLINT
{
auto expected = std::int8_t{-1};
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{-1}.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, upper_nested) // NOLINT
Expand All @@ -114,7 +110,7 @@ namespace {
cnl::_impl::duplex_integer<int, unsigned int>,
cnl::_impl::duplex_integer<unsigned int, unsigned int>>{0x600000000LL}
.upper();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}
}

Expand Down Expand Up @@ -145,42 +141,42 @@ namespace {
{
auto expected = 0U;
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_int8_zero) // NOLINT
{
auto expected = 0U;
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{0}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_int8_650) // NOLINT
{
auto expected = 650U;
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{650}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_int8_minus_650) // NOLINT
{
auto expected = static_cast<unsigned>(-650);
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{-650}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_int8_123) // NOLINT
{
auto expected = 123U;
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{123}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_int8_minus_1) // NOLINT
{
auto expected = 0xFFFFFFFFU;
auto actual = cnl::_impl::duplex_integer<std::int8_t, unsigned>{-1}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

TEST(duplex_integer, lower_nested) // NOLINT
Expand All @@ -191,7 +187,7 @@ namespace {
cnl::_impl::duplex_integer<int, unsigned int>,
cnl::_impl::duplex_integer<unsigned int, unsigned int>>{0x600000000LL}
.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}
}

Expand Down Expand Up @@ -223,7 +219,7 @@ namespace {
auto expected = std::uint16_t{0x5678};
auto actual =
cnl::_impl::duplex_integer<signed short, unsigned short>{0x12345678}.lower();
ASSERT_EQ(expected, actual);
CNL_ASSERT_EQ(expected, actual);
}

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
Loading