Home | History | Annotate | Download | only in shared
      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 
     38 TEST(EndianTest, LittleEndianToBigEndianHost) {
     39   uint32_t value;
     40   ::memcpy(&value, kLittleEndianRepresentation, sizeof(value));
     41 
     42   value = nanoapp_testing::littleEndianToHost(value);
     43   EXPECT_EQ(0, ::memcmp(&value, kBigEndianRepresentation, sizeof(value)));
     44 }
     45 
     46 TEST(EndianTest, BigEndianHostToLittleEndian) {
     47   uint32_t value;
     48   ::memcpy(&value, kBigEndianRepresentation, sizeof(value));
     49 
     50   value = nanoapp_testing::hostToLittleEndian(value);
     51   EXPECT_EQ(0, ::memcmp(&value, kLittleEndianRepresentation, sizeof(value)));
     52 }
     53