Updated unaligned_type constructors and tests

This commit is contained in:
John Wellbelove 2024-12-26 10:58:10 +00:00
parent 71268c4cf3
commit ee95ab4db2
2 changed files with 28 additions and 3 deletions

View File

@ -299,7 +299,7 @@ namespace etl
}
//*************************************************************************
/// Construct from an address and size.
/// Construct from an address and buffer size.
//*************************************************************************
ETL_CONSTEXPR14 unaligned_type(const void* address, size_t buffer_size)
{
@ -307,7 +307,7 @@ namespace etl
etl::copy_n(reinterpret_cast<const char*>(address), sizeof(T), this->storage);
}
//*************************************************************************
/// Copy constructor
//*************************************************************************
@ -362,7 +362,6 @@ namespace etl
return value;
}
//*************************************************************************
/// Unaligned copy
//*************************************************************************

View File

@ -33,6 +33,8 @@ SOFTWARE.
#include "etl/private/diagnostic_useless_cast_push.h"
#include <array>
namespace
{
SUITE(test_unaligned_type)
@ -149,6 +151,30 @@ namespace
CHECK_EQUAL(3.1415927L, be_v3);
}
//*************************************************************************
TEST(test_construction_from_buffer)
{
const std::array<char, 4> buffer = { 0x12, 0x34, 0x56, 0x78 };
const uint32_t le_value = 0x78563412;
const uint32_t be_value = 0x12345678;
etl::le_uint32_t le_v1(buffer.data());
etl::be_uint32_t be_v1(buffer.data());
etl::le_uint32_t le_v2(buffer.data(), buffer.size());
etl::be_uint32_t be_v2(buffer.data(), buffer.size());
CHECK_EQUAL(le_value, le_v1);
CHECK_EQUAL(be_value, be_v1);
CHECK_EQUAL(le_value, le_v2);
CHECK_EQUAL(be_value, be_v2);
CHECK_THROW(etl::le_uint32_t le_v3(buffer.data(), buffer.size() - 1), etl::unaligned_type_buffer_size);
CHECK_THROW(etl::be_uint32_t be_v3(buffer.data(), buffer.size() - 1), etl::unaligned_type_buffer_size);
}
//*************************************************************************
TEST(test_endianness)
{