Greg Hackmann | c3bac8b | 2015-04-15 16:33:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 <stdint.h> |
| 18 | |
| 19 | #define TEST_CUTILS_ENDIAN_H |
| 20 | #include <cutils/endian.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | static const uint16_t host16 = 0x1122; |
| 24 | static const uint32_t host32 = 0x11223344; |
| 25 | static const uint64_t host64 = 0x1122334455667788; |
| 26 | static const uint16_t swapped16 = 0x2211; |
| 27 | static const uint32_t swapped32 = 0x44332211; |
| 28 | static const uint64_t swapped64 = 0x8877665544332211; |
| 29 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 30 | static const uint16_t le16 = swapped16; |
| 31 | static const uint32_t le32 = swapped32; |
| 32 | static const uint64_t le64 = swapped64; |
| 33 | static const uint16_t be16 = native16; |
| 34 | static const uint32_t be32 = native32; |
| 35 | static const uint64_t be64 = native64; |
| 36 | #else |
| 37 | static const uint16_t le16 = host16; |
| 38 | static const uint32_t le32 = host32; |
| 39 | static const uint64_t le64 = host64; |
| 40 | static const uint16_t be16 = swapped16; |
| 41 | static const uint32_t be32 = swapped32; |
| 42 | static const uint64_t be64 = swapped64; |
| 43 | #endif |
| 44 | |
| 45 | TEST(endian, endian) { |
| 46 | EXPECT_EQ(le16, htole16(host16)); |
| 47 | EXPECT_EQ(host16, le16toh(htole16(host16))); |
| 48 | EXPECT_EQ(be16, htobe16(host16)); |
| 49 | EXPECT_EQ(host16, be16toh(htobe16(host16))); |
| 50 | |
| 51 | EXPECT_EQ(le32, htole32(host32)); |
| 52 | EXPECT_EQ(host32, le32toh(htole32(host32))); |
| 53 | EXPECT_EQ(be32, htobe32(host32)); |
| 54 | EXPECT_EQ(host32, be32toh(htobe32(host32))); |
| 55 | |
| 56 | EXPECT_EQ(le64, htole64(host64)); |
| 57 | EXPECT_EQ(host64, le64toh(htole64(host64))); |
| 58 | EXPECT_EQ(be64, htobe64(host64)); |
| 59 | EXPECT_EQ(host64, be64toh(htobe64(host64))); |
| 60 | } |