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