2021-12-31 12:18:41 +00:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
2023-02-04 00:22:49 +00:00
|
|
|
static const int32_t INT_DATA[] = {0, 1, -1, 32, 64, 256, -512, 12345, -123456, 1234567, 12345678, 123456789, 2147483647, (-2147483647 - 1)};
|
|
|
|
static const uint32_t UINT_DATA[] = {0u, 1u, 2u, 32u, 64u, 256u, 512u, 12345u, 123456u, 1234567u, 12345678u, 123456789u, 2147483647u, 2147483648u, 4294967295u};
|
2021-12-31 12:18:41 +00:00
|
|
|
|
|
|
|
TEST(BytePacking, RoundtripInt)
|
|
|
|
{
|
2022-07-10 19:09:58 +00:00
|
|
|
for(auto i : INT_DATA)
|
2021-12-31 12:18:41 +00:00
|
|
|
{
|
2023-02-04 00:22:49 +00:00
|
|
|
unsigned char aPacked[sizeof(int32_t)];
|
2023-02-04 00:02:11 +00:00
|
|
|
uint_to_bytes_be(aPacked, i);
|
|
|
|
EXPECT_EQ(bytes_be_to_uint(aPacked), i);
|
2021-12-31 12:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BytePacking, RoundtripUnsigned)
|
|
|
|
{
|
2022-07-10 19:09:58 +00:00
|
|
|
for(auto u : UINT_DATA)
|
2021-12-31 12:18:41 +00:00
|
|
|
{
|
2023-02-04 00:22:49 +00:00
|
|
|
unsigned char aPacked[sizeof(uint32_t)];
|
2022-07-10 19:09:58 +00:00
|
|
|
uint_to_bytes_be(aPacked, u);
|
|
|
|
EXPECT_EQ(bytes_be_to_uint(aPacked), u);
|
2021-12-31 12:18:41 +00:00
|
|
|
}
|
|
|
|
}
|