Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 <endian.h>
     18 
     19 #include <gtest/gtest.h>
     20 
     21 static constexpr uint16_t le16 = 0x1234;
     22 static constexpr uint32_t le32 = 0x12345678;
     23 static constexpr uint64_t le64 = 0x123456789abcdef0;
     24 
     25 static constexpr uint16_t be16 = 0x3412;
     26 static constexpr uint32_t be32 = 0x78563412;
     27 static constexpr uint64_t be64 = 0xf0debc9a78563412;
     28 
     29 TEST(endian, constants) {
     30   ASSERT_TRUE(__LITTLE_ENDIAN == LITTLE_ENDIAN);
     31   ASSERT_TRUE(__BIG_ENDIAN == BIG_ENDIAN);
     32   ASSERT_TRUE(__BYTE_ORDER == BYTE_ORDER);
     33 
     34 #if defined(__BIONIC__)
     35   ASSERT_TRUE(_LITTLE_ENDIAN == LITTLE_ENDIAN);
     36   ASSERT_TRUE(_BIG_ENDIAN == BIG_ENDIAN);
     37   ASSERT_TRUE(_BYTE_ORDER == BYTE_ORDER);
     38 #endif
     39 
     40   ASSERT_EQ(__LITTLE_ENDIAN, __BYTE_ORDER);
     41 }
     42 
     43 TEST(endian, htons_htonl_htonq_macros) {
     44 #if defined(__BIONIC__)
     45   ASSERT_EQ(be16, htons(le16));
     46   ASSERT_EQ(be32, htonl(le32));
     47   ASSERT_EQ(be64, htonq(le64));
     48 #else
     49   GTEST_LOG_(INFO) << "glibc doesn't have these macros";
     50 #endif
     51 }
     52 
     53 TEST(endian, ntohs_ntohl_ntohq_macros) {
     54 #if defined(__BIONIC__)
     55   ASSERT_EQ(le16, ntohs(be16));
     56   ASSERT_EQ(le32, ntohl(be32));
     57   ASSERT_EQ(le64, ntohq(be64));
     58 #else
     59   GTEST_LOG_(INFO) << "glibc doesn't have these macros";
     60 #endif
     61 }
     62 
     63 TEST(endian, htobe16_htobe32_htobe64) {
     64   ASSERT_EQ(be16, htobe16(le16));
     65   ASSERT_EQ(be32, htobe32(le32));
     66   ASSERT_EQ(be64, htobe64(le64));
     67 }
     68 
     69 TEST(endian, htole16_htole32_htole64) {
     70   ASSERT_EQ(le16, htole16(le16));
     71   ASSERT_EQ(le32, htole32(le32));
     72   ASSERT_EQ(le64, htole64(le64));
     73 }
     74 
     75 TEST(endian, be16toh_be32toh_be64toh) {
     76   ASSERT_EQ(le16, be16toh(be16));
     77   ASSERT_EQ(le32, be32toh(be32));
     78   ASSERT_EQ(le64, be64toh(be64));
     79 }
     80 
     81 TEST(endian, le16toh_le32toh_le64toh) {
     82   ASSERT_EQ(le16, le16toh(le16));
     83   ASSERT_EQ(le32, le32toh(le32));
     84   ASSERT_EQ(le64, le64toh(le64));
     85 }
     86 
     87 TEST(endian, betoh16_betoh32_betoh64) {
     88 #if defined(__BIONIC__)
     89   ASSERT_EQ(le16, betoh16(be16));
     90   ASSERT_EQ(le32, betoh32(be32));
     91   ASSERT_EQ(le64, betoh64(be64));
     92 #else
     93   GTEST_LOG_(INFO) << "glibc doesn't have these macros";
     94 #endif
     95 }
     96 
     97 TEST(endian, letoh16_letoh32_letoh64) {
     98 #if defined(__BIONIC__)
     99   ASSERT_EQ(le16, letoh16(le16));
    100   ASSERT_EQ(le32, letoh32(le32));
    101   ASSERT_EQ(le64, letoh64(le64));
    102 #else
    103   GTEST_LOG_(INFO) << "glibc doesn't have these macros";
    104 #endif
    105 }
    106