• 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 // For this test, we pretend we're a little 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 __LITTLE_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 
TEST(EndianTest,LittleEndianToLittleEndianHost)35 TEST(EndianTest, LittleEndianToLittleEndianHost) {
36   uint32_t value;
37   ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
38 
39   value = nanoapp_testing::littleEndianToHost(value);
40   EXPECT_EQ(0, ::memcmp(&value, kLittleEndianRepresentation, sizeof(value)));
41 }
42 
TEST(EndianTest,LittleEndianHostToLittleEndian)43 TEST(EndianTest, LittleEndianHostToLittleEndian) {
44   uint32_t value;
45   ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
46 
47   value = nanoapp_testing::hostToLittleEndian(value);
48   EXPECT_EQ(0, ::memcmp(&value, kLittleEndianRepresentation, sizeof(value)));
49 }
50