add test to expose undefined behaviors endian_buffer when used with signed integers

This commit is contained in:
arvidn
2017-11-06 13:18:47 +01:00
parent b5a79243a2
commit 674adbd592

View File

@ -18,6 +18,7 @@
#include <boost/cstdint.hpp>
#include <iostream>
#include <sstream>
#include <limits>
using namespace boost::endian;
using std::cout;
@ -198,6 +199,23 @@ namespace
std::cout << "test construction and assignment" << std::endl;
}
template <typename Int>
void test_boundary_values()
{
endian_buffer<order::big, Int, sizeof(Int) * CHAR_BIT, align::no> bmax(
std::numeric_limits<Int>::max());
endian_buffer<order::big, Int, sizeof(Int) * CHAR_BIT, align::no> bmin(
std::numeric_limits<Int>::min());
endian_buffer<order::little, Int, sizeof(Int) * CHAR_BIT, align::no> lmax(
std::numeric_limits<Int>::max());
endian_buffer<order::little, Int, sizeof(Int) * CHAR_BIT, align::no> lmin(
std::numeric_limits<Int>::min());
BOOST_TEST(bmax.value() == std::numeric_limits<Int>::max());
BOOST_TEST(bmin.value() == std::numeric_limits<Int>::min());
BOOST_TEST(lmax.value() == std::numeric_limits<Int>::max());
BOOST_TEST(lmin.value() == std::numeric_limits<Int>::min());
}
} // unnamed namespace
//--------------------------------------------------------------------------------------//
@ -230,6 +248,13 @@ int cpp_main(int, char *[])
test_inserter_and_extractor();
test_construction_and_assignment();
test_boundary_values<signed int>();
test_boundary_values<unsigned int>();
test_boundary_values<signed short>();
test_boundary_values<unsigned short>();
test_boundary_values<signed char>();
test_boundary_values<unsigned char>();
cout << " done" << endl;
return ::boost::report_errors();