1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <shared/nano_endian.h>
18
19 #include <cstdint>
20 #include <cstring>
21
22 #include <gtest/gtest.h>
23 #include <shared/array_length.h>
24
25 template <size_t kByteCount>
swapByteTest()26 static void swapByteTest() {
27 uint8_t bytes[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
28 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
29 static constexpr uint8_t postSwap[] = {0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B,
30 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05,
31 0x04, 0x03, 0x02, 0x01};
32
33 #ifdef __clang__
34 // This static_assert crashes g++, but it's legit and works in clang.
35 static_assert(arrayLength(bytes) == arrayLength(postSwap),
36 "Mismatched arrays");
37 #endif
38 static_assert((kByteCount > 0) && (kByteCount <= arrayLength(bytes)),
39 "Invalid test");
40
41 constexpr const uint8_t *kExpected =
42 postSwap + (arrayLength(postSwap) - kByteCount);
43 nanoapp_testing::swapBytes(bytes, kByteCount);
44 EXPECT_EQ(0, ::memcmp(bytes, kExpected, kByteCount));
45
46 if (arrayLength(bytes) < kByteCount) {
47 // Confirm that we didn't modify out of bounds.
48 EXPECT_EQ(kByteCount + 1, bytes[kByteCount]);
49 }
50 }
51
TEST(EndianTest,SwapBytes1)52 TEST(EndianTest, SwapBytes1) {
53 swapByteTest<1>();
54 }
55
TEST(EndianTest,SwapBytes2)56 TEST(EndianTest, SwapBytes2) {
57 swapByteTest<2>();
58 }
59
TEST(EndianTest,SwapBytes4)60 TEST(EndianTest, SwapBytes4) {
61 swapByteTest<4>();
62 }
63
TEST(EndianTest,SwapBytes8)64 TEST(EndianTest, SwapBytes8) {
65 swapByteTest<8>();
66 }
67
TEST(EndianTest,SwapBytes16)68 TEST(EndianTest, SwapBytes16) {
69 swapByteTest<16>();
70 }
71
72 // These tests should work regardless of which endian platform this
73 // test happens to be built and running on.
74
75 static constexpr uint32_t kValue = UINT32_C(0x04030201);
76 static constexpr uint8_t kLittleEndianRepresentation[4] = {0x01, 0x02, 0x03,
77 0x04};
78
TEST(EndianTest,LittleEndianToHost)79 TEST(EndianTest, LittleEndianToHost) {
80 uint32_t value;
81 ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
82
83 value = nanoapp_testing::littleEndianToHost(value);
84 EXPECT_EQ(kValue, value);
85 }
86
TEST(EndianTest,HostToLittleEndian)87 TEST(EndianTest, HostToLittleEndian) {
88 uint32_t value = kValue;
89 value = nanoapp_testing::hostToLittleEndian(value);
90
91 const uint8_t *bytes = reinterpret_cast<uint8_t *>(&value);
92 EXPECT_EQ(0, ::memcmp(kLittleEndianRepresentation, bytes,
93 sizeof(kLittleEndianRepresentation)));
94 }
95