mirror of
https://github.com/boostorg/endian.git
synced 2025-07-31 04:57:21 +02:00
Add endian_buffer stream inserter and extractor, beef up buffer_test a bit.
This commit is contained in:
@ -11,23 +11,158 @@
|
||||
|
||||
#include <boost/endian/detail/disable_warnings.hpp>
|
||||
|
||||
#define BOOST_ENDIAN_LOG
|
||||
//#define BOOST_ENDIAN_LOG
|
||||
#include <boost/endian/buffers.hpp>
|
||||
#include <boost/detail/lightweight_main.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace bel = boost::endian;
|
||||
using namespace boost::endian;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// check_size ------------------------------------------------------------//
|
||||
|
||||
void check_size()
|
||||
{
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_float32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_float64_buf_t), 8);
|
||||
BOOST_TEST_EQ(sizeof(little_float32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_float64_buf_t), 8);
|
||||
BOOST_TEST_EQ(sizeof(native_float32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(native_float64_buf_t), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_float32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_float64_buf_ut), 8);
|
||||
BOOST_TEST_EQ(sizeof(little_float32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_float64_buf_ut), 8);
|
||||
BOOST_TEST_EQ(sizeof(native_float32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(native_float64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_int8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(big_int16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(big_int24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(big_int32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_int40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(big_int48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(big_int56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(big_int64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_uint8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(big_uint16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(big_uint24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(big_uint32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_uint40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(big_uint48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(big_uint56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(big_uint64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(little_int8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(little_int16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(little_int24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(little_int32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_int40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(little_int48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(little_int56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(little_int64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(little_uint8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(little_uint16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(little_uint24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(little_uint32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_uint40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(little_uint48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(little_uint56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(little_uint64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(native_int8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(native_int16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(native_int24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(native_int32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(native_int40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(native_int48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(native_int56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(native_int64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(native_uint8_buf_ut), 1);
|
||||
BOOST_TEST_EQ(sizeof(native_uint16_buf_ut), 2);
|
||||
BOOST_TEST_EQ(sizeof(native_uint24_buf_ut), 3);
|
||||
BOOST_TEST_EQ(sizeof(native_uint32_buf_ut), 4);
|
||||
BOOST_TEST_EQ(sizeof(native_uint40_buf_ut), 5);
|
||||
BOOST_TEST_EQ(sizeof(native_uint48_buf_ut), 6);
|
||||
BOOST_TEST_EQ(sizeof(native_uint56_buf_ut), 7);
|
||||
BOOST_TEST_EQ(sizeof(native_uint64_buf_ut), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_int16_buf_t), 2);
|
||||
BOOST_TEST_EQ(sizeof(big_int32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_int64_buf_t), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(big_uint16_buf_t), 2);
|
||||
BOOST_TEST_EQ(sizeof(big_uint32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(big_uint64_buf_t), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(little_int16_buf_t), 2);
|
||||
BOOST_TEST_EQ(sizeof(little_int32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_int64_buf_t), 8);
|
||||
|
||||
BOOST_TEST_EQ(sizeof(little_uint16_buf_t), 2);
|
||||
BOOST_TEST_EQ(sizeof(little_uint32_buf_t), 4);
|
||||
BOOST_TEST_EQ(sizeof(little_uint64_buf_t), 8);
|
||||
} // check_size
|
||||
|
||||
// test_inserter_and_extractor -----------------------------------------------------//
|
||||
|
||||
void test_inserter_and_extractor()
|
||||
{
|
||||
std::cout << "test inserter and extractor..." << std::endl;
|
||||
|
||||
big_uint64_buf_ut bu64(0x010203040506070ULL);
|
||||
little_uint64_buf_ut lu64(0x010203040506070ULL);
|
||||
|
||||
uint64_t x;
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
ss << bu64;
|
||||
ss >> x;
|
||||
BOOST_TEST_EQ(x, 0x010203040506070ULL);
|
||||
|
||||
ss.clear();
|
||||
ss << lu64;
|
||||
ss >> x;
|
||||
BOOST_TEST_EQ(x, 0x010203040506070ULL);
|
||||
|
||||
ss.clear();
|
||||
ss << 0x010203040506070ULL;
|
||||
big_uint64_buf_ut bu64z(0);
|
||||
ss >> bu64z;
|
||||
BOOST_TEST_EQ(bu64z.value(), bu64.value());
|
||||
|
||||
ss.clear();
|
||||
ss << 0x010203040506070ULL;
|
||||
little_uint64_buf_ut lu64z(0);
|
||||
ss >> lu64z;
|
||||
BOOST_TEST_EQ(lu64z.value(), lu64.value());
|
||||
|
||||
std::cout << "test inserter and extractor complete" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
int cpp_main(int, char *[])
|
||||
{
|
||||
cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
|
||||
|
||||
cout << " construct big endian aligned" << endl;
|
||||
bel::big_int32_buf_t x(1122334455);
|
||||
big_int32_buf_t x(1122334455);
|
||||
|
||||
cout << " assign to buffer from built-in integer" << endl;
|
||||
x = 1234567890;
|
||||
@ -37,7 +172,7 @@ int cpp_main(int, char *[])
|
||||
BOOST_TEST(b1);
|
||||
|
||||
cout << " construct little endian unaligned" << endl;
|
||||
bel::little_int32_buf_ut x2(1122334455);
|
||||
little_int32_buf_ut x2(1122334455);
|
||||
|
||||
cout << " assign to buffer from built-in integer" << endl;
|
||||
x2 = 1234567890;
|
||||
@ -46,6 +181,9 @@ int cpp_main(int, char *[])
|
||||
bool b2(x2.value() == 1234567890);
|
||||
BOOST_TEST(b2);
|
||||
|
||||
check_size();
|
||||
test_inserter_and_extractor();
|
||||
|
||||
cout << " done" << endl;
|
||||
|
||||
return ::boost::report_errors();
|
||||
|
Reference in New Issue
Block a user