From 8b14b7cddc0c67cdb9d5564b37f4836deb53ca14 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Thu, 8 Dec 2022 12:25:27 -0800 Subject: [PATCH] Add tests for narrow_cast --- test/Jamfile.v2 | 1 + test/unordered/narrow_cast_tests.cpp | 107 +++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/unordered/narrow_cast_tests.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index c3284098..9a702bc6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -95,6 +95,7 @@ run exception/rehash_exception_tests.cpp ; run exception/swap_exception_tests.cpp : : : BOOST_UNORDERED_SWAP_METHOD=2 ; run exception/merge_exception_tests.cpp ; +run unordered/narrow_cast_tests.cpp ; run quick.cpp ; import ../../config/checks/config : requires ; diff --git a/test/unordered/narrow_cast_tests.cpp b/test/unordered/narrow_cast_tests.cpp new file mode 100644 index 00000000..81d5ee82 --- /dev/null +++ b/test/unordered/narrow_cast_tests.cpp @@ -0,0 +1,107 @@ +// Copyright 2022 Christian Mazakas +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +#include +#include + +// want to prove that for the wider type, the higher bits of the value +// represenation don't affect the results of the narrowing, which in this case +// is masking out the high bits when comapred to the narrow type + +static void signed_integral_narrowing() +{ + // test positive range, fits + // [0, 127] + for (boost::int32_t i = 0x00; i < 0x80; ++i) { + boost::int8_t k = (boost::int8_t)i; + BOOST_TEST_GE(k, 0); + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(i), k); + } + + // test positive range, doesn't fit + // [0xff00, 0xff7f] + for (boost::int32_t i = 0x00; i < 0x80; ++i) { + boost::int32_t j = i + 0xff00; + boost::int8_t k = (boost::int8_t)i; + BOOST_TEST_GE(k, 0); + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(j), k); + } + + // test negative range, fits + // [-128, -1] + for (boost::int32_t i = 0x00; i < 0x80; ++i) { + boost::int32_t j = i + (boost::int32_t)0xffffff80; + boost::int8_t k = (boost::int8_t)j; + BOOST_TEST_LT(j, 0); + BOOST_TEST_LT(k, 0); + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(j), k); + } + + // test negative range, doesn't fit + for (boost::int32_t i = 0x00; i < 0x80; ++i) { + boost::int32_t j = i + (boost::int32_t)0x80000000; + boost::int8_t k = (boost::int8_t)(i); + BOOST_TEST_LT(j, 0); + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(j), k); + } + + for (boost::int32_t i = 0x00; i < 0x100; ++i) { + boost::int32_t j = (boost::int32_t)0x80ff0000 + i; + BOOST_TEST_LT(j, 0); + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(j), + (boost::int8_t)i); + } + + // test special values + { + boost::int32_t x = 0xff; + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(x), -1); + } + + { + boost::int32_t x = (boost::int32_t)0xffffff00; + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(x), + (boost::int8_t)0x00); + } + + { + boost::int32_t x = (boost::int32_t)0xffffff7f; + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(x), + (boost::int8_t)0x7f); + } + + { + boost::int32_t x = (boost::int32_t)0xffffffff; + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(x), + (boost::int8_t)-1); + } +} + +static void unsigned_integral_narrowing() +{ + // test range: [0x00, 0xff] + for (boost::uint32_t i = 0x00; i < 0x100; ++i) { + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(i), + (boost::uint8_t)(i & 0xff)); + } + + // test range: [0xffffff00, 0xffffffff] + boost::uint32_t i = 0xffffff00; + for (; i < 0xffffffff; ++i) { + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(i), + (boost::uint8_t)(i & 0xff)); + } + BOOST_TEST_EQ(boost::unordered::detail::narrow_cast(i), + (boost::uint8_t)(i & 0xff)); +} + +int main() +{ + signed_integral_narrowing(); + unsigned_integral_narrowing(); + + return boost::report_errors(); +}