1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/sys_byteorder.h"
6
7 #include <stdint.h>
8
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 const uint16_t k16BitTestData = 0xaabb;
15 const uint16_t k16BitSwappedTestData = 0xbbaa;
16 const uint32_t k32BitTestData = 0xaabbccdd;
17 const uint32_t k32BitSwappedTestData = 0xddccbbaa;
18 const uint64_t k64BitTestData = 0xaabbccdd44332211;
19 const uint64_t k64BitSwappedTestData = 0x11223344ddccbbaa;
20
21 } // namespace
22
TEST(ByteOrderTest,ByteSwap16)23 TEST(ByteOrderTest, ByteSwap16) {
24 uint16_t swapped = base::ByteSwap(k16BitTestData);
25 EXPECT_EQ(k16BitSwappedTestData, swapped);
26 uint16_t reswapped = base::ByteSwap(swapped);
27 EXPECT_EQ(k16BitTestData, reswapped);
28 }
29
TEST(ByteOrderTest,ByteSwap32)30 TEST(ByteOrderTest, ByteSwap32) {
31 uint32_t swapped = base::ByteSwap(k32BitTestData);
32 EXPECT_EQ(k32BitSwappedTestData, swapped);
33 uint32_t reswapped = base::ByteSwap(swapped);
34 EXPECT_EQ(k32BitTestData, reswapped);
35 }
36
TEST(ByteOrderTest,ByteSwap64)37 TEST(ByteOrderTest, ByteSwap64) {
38 uint64_t swapped = base::ByteSwap(k64BitTestData);
39 EXPECT_EQ(k64BitSwappedTestData, swapped);
40 uint64_t reswapped = base::ByteSwap(swapped);
41 EXPECT_EQ(k64BitTestData, reswapped);
42 }
43
TEST(ByteOrderTest,ByteSwapUintPtrT)44 TEST(ByteOrderTest, ByteSwapUintPtrT) {
45 #if defined(ARCH_CPU_64_BITS)
46 const uintptr_t test_data = static_cast<uintptr_t>(k64BitTestData);
47 const uintptr_t swapped_test_data =
48 static_cast<uintptr_t>(k64BitSwappedTestData);
49 #elif defined(ARCH_CPU_32_BITS)
50 const uintptr_t test_data = static_cast<uintptr_t>(k32BitTestData);
51 const uintptr_t swapped_test_data =
52 static_cast<uintptr_t>(k32BitSwappedTestData);
53 #else
54 #error architecture not supported
55 #endif
56
57 uintptr_t swapped = base::ByteSwapUintPtrT(test_data);
58 EXPECT_EQ(swapped_test_data, swapped);
59 uintptr_t reswapped = base::ByteSwapUintPtrT(swapped);
60 EXPECT_EQ(test_data, reswapped);
61 }
62
TEST(ByteOrderTest,ByteSwapToLE16)63 TEST(ByteOrderTest, ByteSwapToLE16) {
64 uint16_t le = base::ByteSwapToLE16(k16BitTestData);
65 #if defined(ARCH_CPU_LITTLE_ENDIAN)
66 EXPECT_EQ(k16BitTestData, le);
67 #else
68 EXPECT_EQ(k16BitSwappedTestData, le);
69 #endif
70 }
71
TEST(ByteOrderTest,ByteSwapToLE32)72 TEST(ByteOrderTest, ByteSwapToLE32) {
73 uint32_t le = base::ByteSwapToLE32(k32BitTestData);
74 #if defined(ARCH_CPU_LITTLE_ENDIAN)
75 EXPECT_EQ(k32BitTestData, le);
76 #else
77 EXPECT_EQ(k32BitSwappedTestData, le);
78 #endif
79 }
80
TEST(ByteOrderTest,ByteSwapToLE64)81 TEST(ByteOrderTest, ByteSwapToLE64) {
82 uint64_t le = base::ByteSwapToLE64(k64BitTestData);
83 #if defined(ARCH_CPU_LITTLE_ENDIAN)
84 EXPECT_EQ(k64BitTestData, le);
85 #else
86 EXPECT_EQ(k64BitSwappedTestData, le);
87 #endif
88 }
89
TEST(ByteOrderTest,NetToHost16)90 TEST(ByteOrderTest, NetToHost16) {
91 uint16_t h = base::NetToHost16(k16BitTestData);
92 #if defined(ARCH_CPU_LITTLE_ENDIAN)
93 EXPECT_EQ(k16BitSwappedTestData, h);
94 #else
95 EXPECT_EQ(k16BitTestData, h);
96 #endif
97 }
98
TEST(ByteOrderTest,NetToHost32)99 TEST(ByteOrderTest, NetToHost32) {
100 uint32_t h = base::NetToHost32(k32BitTestData);
101 #if defined(ARCH_CPU_LITTLE_ENDIAN)
102 EXPECT_EQ(k32BitSwappedTestData, h);
103 #else
104 EXPECT_EQ(k32BitTestData, h);
105 #endif
106 }
107
TEST(ByteOrderTest,NetToHost64)108 TEST(ByteOrderTest, NetToHost64) {
109 uint64_t h = base::NetToHost64(k64BitTestData);
110 #if defined(ARCH_CPU_LITTLE_ENDIAN)
111 EXPECT_EQ(k64BitSwappedTestData, h);
112 #else
113 EXPECT_EQ(k64BitTestData, h);
114 #endif
115 }
116
TEST(ByteOrderTest,HostToNet16)117 TEST(ByteOrderTest, HostToNet16) {
118 uint16_t n = base::HostToNet16(k16BitTestData);
119 #if defined(ARCH_CPU_LITTLE_ENDIAN)
120 EXPECT_EQ(k16BitSwappedTestData, n);
121 #else
122 EXPECT_EQ(k16BitTestData, n);
123 #endif
124 }
125
TEST(ByteOrderTest,HostToNet32)126 TEST(ByteOrderTest, HostToNet32) {
127 uint32_t n = base::HostToNet32(k32BitTestData);
128 #if defined(ARCH_CPU_LITTLE_ENDIAN)
129 EXPECT_EQ(k32BitSwappedTestData, n);
130 #else
131 EXPECT_EQ(k32BitTestData, n);
132 #endif
133 }
134
TEST(ByteOrderTest,HostToNet64)135 TEST(ByteOrderTest, HostToNet64) {
136 uint64_t n = base::HostToNet64(k64BitTestData);
137 #if defined(ARCH_CPU_LITTLE_ENDIAN)
138 EXPECT_EQ(k64BitSwappedTestData, n);
139 #else
140 EXPECT_EQ(k64BitTestData, n);
141 #endif
142 }
143