1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/sys_byteorder.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
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;
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);
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);
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);
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);
54 #error architecture not supported
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);
63 TEST(ByteOrderTest, ByteSwapToLE16) {
64 uint16_t le = base::ByteSwapToLE16(k16BitTestData);
65 #if defined(ARCH_CPU_LITTLE_ENDIAN)
66 EXPECT_EQ(k16BitTestData, le);
68 EXPECT_EQ(k16BitSwappedTestData, le);
72 TEST(ByteOrderTest, ByteSwapToLE32) {
73 uint32_t le = base::ByteSwapToLE32(k32BitTestData);
74 #if defined(ARCH_CPU_LITTLE_ENDIAN)
75 EXPECT_EQ(k32BitTestData, le);
77 EXPECT_EQ(k32BitSwappedTestData, le);
81 TEST(ByteOrderTest, ByteSwapToLE64) {
82 uint64_t le = base::ByteSwapToLE64(k64BitTestData);
83 #if defined(ARCH_CPU_LITTLE_ENDIAN)
84 EXPECT_EQ(k64BitTestData, le);
86 EXPECT_EQ(k64BitSwappedTestData, le);
90 TEST(ByteOrderTest, NetToHost16) {
91 uint16_t h = base::NetToHost16(k16BitTestData);
92 #if defined(ARCH_CPU_LITTLE_ENDIAN)
93 EXPECT_EQ(k16BitSwappedTestData, h);
95 EXPECT_EQ(k16BitTestData, h);
99 TEST(ByteOrderTest, NetToHost32) {
100 uint32_t h = base::NetToHost32(k32BitTestData);
101 #if defined(ARCH_CPU_LITTLE_ENDIAN)
102 EXPECT_EQ(k32BitSwappedTestData, h);
104 EXPECT_EQ(k32BitTestData, h);
108 TEST(ByteOrderTest, NetToHost64) {
109 uint64_t h = base::NetToHost64(k64BitTestData);
110 #if defined(ARCH_CPU_LITTLE_ENDIAN)
111 EXPECT_EQ(k64BitSwappedTestData, h);
113 EXPECT_EQ(k64BitTestData, h);
117 TEST(ByteOrderTest, HostToNet16) {
118 uint16_t n = base::HostToNet16(k16BitTestData);
119 #if defined(ARCH_CPU_LITTLE_ENDIAN)
120 EXPECT_EQ(k16BitSwappedTestData, n);
122 EXPECT_EQ(k16BitTestData, n);
126 TEST(ByteOrderTest, HostToNet32) {
127 uint32_t n = base::HostToNet32(k32BitTestData);
128 #if defined(ARCH_CPU_LITTLE_ENDIAN)
129 EXPECT_EQ(k32BitSwappedTestData, n);
131 EXPECT_EQ(k32BitTestData, n);
135 TEST(ByteOrderTest, HostToNet64) {
136 uint64_t n = base::HostToNet64(k64BitTestData);
137 #if defined(ARCH_CPU_LITTLE_ENDIAN)
138 EXPECT_EQ(k64BitSwappedTestData, n);
140 EXPECT_EQ(k64BitTestData, n);