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 // For this test, we pretend we're a big endian platform, even if we don't
18 // happen to be.
19 #define CHRE_NO_ENDIAN_H 1
20 #define __LITTLE_ENDIAN 0
21 #define __BIG_ENDIAN 1
22 #define __BYTE_ORDER __BIG_ENDIAN
23
24 #include <shared/nano_endian.h>
25
26 #include <cstdint>
27 #include <cstring>
28
29 #include <gtest/gtest.h>
30 #include <shared/array_length.h>
31
32
33 static constexpr uint8_t kLittleEndianRepresentation[4] = {
34 0x01, 0x02, 0x03, 0x04 };
35 static constexpr uint8_t kBigEndianRepresentation[4] = {
36 0x04, 0x03, 0x02, 0x01 };
37
TEST(EndianTest,LittleEndianToBigEndianHost)38 TEST(EndianTest, LittleEndianToBigEndianHost) {
39 alignas(alignof(uint32_t)) uint8_t bytes[4];
40 ::memcpy(bytes, kLittleEndianRepresentation, sizeof(bytes));
41 uint32_t *valuePtr = reinterpret_cast<uint32_t*>(bytes);
42
43 nanoapp_testing::littleEndianToHost(valuePtr);
44 EXPECT_EQ(0, ::memcmp(bytes, kBigEndianRepresentation, sizeof(bytes)));
45 }
46
TEST(EndianTest,BigEndianHostToLittleEndian)47 TEST(EndianTest, BigEndianHostToLittleEndian) {
48 alignas(alignof(uint32_t)) uint8_t bytes[4];
49 ::memcpy(bytes, kBigEndianRepresentation, sizeof(bytes));
50 uint32_t *valuePtr = reinterpret_cast<uint32_t*>(bytes);
51
52 nanoapp_testing::hostToLittleEndian(valuePtr);
53 EXPECT_EQ(0, ::memcmp(bytes, kLittleEndianRepresentation, sizeof(bytes)));
54 }
55