1 // Copyright 2019 The PDFium 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 "core/fxcrt/byteorder.h"
6
7 #include "core/fxcrt/fx_system.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 constexpr uint32_t kTestValues32[] = {
13 0x0, 0x1, 0x2, 0x3, 0x4, 0xfe,
14 0xff, 0x100, 0x101, 0xffff, 0x10000, 0x123456,
15 0x345167, 0x2f3e4a5b, 0xff000000, 0xfffffffe, 0xffffffff};
16
17 } // namespace
18
19 namespace fxcrt {
20
TEST(ByteOrder,ByteSwapToLE16)21 TEST(ByteOrder, ByteSwapToLE16) {
22 // Since there are so few values, test them all.
23 for (uint32_t v = 0; v < 0x10000; ++v) {
24 const uint16_t v16 = v;
25 uint16_t expected =
26 FXSYS_UINT16_GET_LSBFIRST(reinterpret_cast<const uint8_t*>(&v16));
27 EXPECT_EQ(expected, ByteSwapToLE16(v16)) << v;
28
29 // Check safety against unexpectedly signed bytes.
30 expected = FXSYS_UINT16_GET_LSBFIRST(reinterpret_cast<const int8_t*>(&v16));
31 EXPECT_EQ(expected, ByteSwapToLE16(v16)) << v;
32 }
33 }
34
TEST(ByteOrder,ByteSwapToLE32)35 TEST(ByteOrder, ByteSwapToLE32) {
36 for (uint32_t v : kTestValues32) {
37 uint32_t expected =
38 FXSYS_UINT32_GET_LSBFIRST(reinterpret_cast<const uint8_t*>(&v));
39 EXPECT_EQ(expected, ByteSwapToLE32(v)) << v;
40
41 // Check safety against unexpectedly signed bytes.
42 expected = FXSYS_UINT32_GET_LSBFIRST(reinterpret_cast<const int8_t*>(&v));
43 EXPECT_EQ(expected, ByteSwapToLE32(v)) << v;
44 }
45 }
46
TEST(ByteOrder,ByteSwapToBE16)47 TEST(ByteOrder, ByteSwapToBE16) {
48 // Since there are so few values, test them all.
49 for (uint32_t v = 0; v < 0x10000; ++v) {
50 const uint16_t v16 = v;
51 uint16_t expected =
52 FXSYS_UINT16_GET_MSBFIRST(reinterpret_cast<const uint8_t*>(&v16));
53 EXPECT_EQ(expected, ByteSwapToBE16(v16)) << v;
54
55 // Check safety against unexpectedly signed bytes.
56 expected = FXSYS_UINT16_GET_MSBFIRST(reinterpret_cast<const int8_t*>(&v16));
57 EXPECT_EQ(expected, ByteSwapToBE16(v16)) << v;
58 }
59 }
60
TEST(ByteOrder,ByteSwapToBE32)61 TEST(ByteOrder, ByteSwapToBE32) {
62 for (uint32_t v : kTestValues32) {
63 uint32_t expected =
64 FXSYS_UINT32_GET_MSBFIRST(reinterpret_cast<const uint8_t*>(&v));
65 EXPECT_EQ(expected, ByteSwapToBE32(v)) << v;
66
67 // Check safety against unexpectedly signed bytes.
68 expected = FXSYS_UINT32_GET_MSBFIRST(reinterpret_cast<const int8_t*>(&v));
69 EXPECT_EQ(expected, ByteSwapToBE32(v)) << v;
70 }
71 }
72
73 } // namespace fxcrt
74