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 static constexpr uint8_t kLittleEndianRepresentation[4] = {0x01, 0x02, 0x03,
33 0x04};
34 static constexpr uint8_t kBigEndianRepresentation[4] = {0x04, 0x03, 0x02, 0x01};
35
TEST(EndianTest,LittleEndianToBigEndianHost)36 TEST(EndianTest, LittleEndianToBigEndianHost) {
37 uint32_t value;
38 ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
39
40 value = nanoapp_testing::littleEndianToHost(value);
41 EXPECT_EQ(0, ::memcmp(&value, kBigEndianRepresentation, sizeof(value)));
42 }
43
TEST(EndianTest,BigEndianHostToLittleEndian)44 TEST(EndianTest, BigEndianHostToLittleEndian) {
45 uint32_t value;
46 ::memcpy(&value, kBigEndianRepresentation, sizeof(value));
47
48 value = nanoapp_testing::hostToLittleEndian(value);
49 EXPECT_EQ(0, ::memcmp(&value, kLittleEndianRepresentation, sizeof(value)));
50 }
51