1#include "test.h"
2#include <gtest/gtest.h>
3
4#include <base/system.h>
5
6#include <array>
7
8TEST(SwapEndian, Generic)
9{
10 const std::array<char, 12> aOriginalData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
11 const std::array<char, 12> aExpectedDataInt16 = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11};
12 const std::array<char, 12> aExpectedDataInt32 = {4, 3, 2, 1, 8, 7, 6, 5, 12, 11, 10, 9};
13
14 std::array<char, 12> aData = aOriginalData;
15 swap_endian(data: aData.data(), elem_size: sizeof(char), num: aData.size() / sizeof(char));
16 EXPECT_EQ(aData, aOriginalData);
17
18 aData = aOriginalData;
19 swap_endian(data: aData.data(), elem_size: sizeof(int16_t), num: aData.size() / sizeof(int16_t));
20 EXPECT_EQ(aData, aExpectedDataInt16);
21
22 aData = aOriginalData;
23 swap_endian(data: aData.data(), elem_size: sizeof(int32_t), num: aData.size() / sizeof(int32_t));
24 EXPECT_EQ(aData, aExpectedDataInt32);
25}
26
27static int SwapEndianInt(int Number)
28{
29 swap_endian(data: &Number, elem_size: sizeof(Number), num: 1);
30 return Number;
31}
32
33TEST(SwapEndian, Int)
34{
35 EXPECT_EQ(SwapEndianInt(0x00000000), 0x00000000);
36 EXPECT_EQ(SwapEndianInt(0xFFFFFFFF), 0xFFFFFFFF);
37 EXPECT_EQ(SwapEndianInt(0x7FFFFFFF), 0xFFFFFF7F);
38 EXPECT_EQ(SwapEndianInt(0xFFFFFF7F), 0x7FFFFFFF);
39 EXPECT_EQ(SwapEndianInt(0x80000000), 0x00000080);
40 EXPECT_EQ(SwapEndianInt(0x00000080), 0x80000000);
41 EXPECT_EQ(SwapEndianInt(0x12345678), 0x78563412);
42 EXPECT_EQ(SwapEndianInt(0x78563412), 0x12345678);
43 EXPECT_EQ(SwapEndianInt(0x87654321), 0x21436587);
44 EXPECT_EQ(SwapEndianInt(0x21436587), 0x87654321);
45}
46