henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/basictypes.h" |
| 12 | |
| 13 | #include "webrtc/base/gunit.h" |
| 14 | |
| 15 | namespace rtc { |
| 16 | |
kjellander | 979e0b3 | 2015-06-16 14:13:35 | [diff] [blame] | 17 | static_assert(sizeof(int8) == 1, "Unexpected size"); |
| 18 | static_assert(sizeof(uint8) == 1, "Unexpected size"); |
| 19 | static_assert(sizeof(int16) == 2, "Unexpected size"); |
| 20 | static_assert(sizeof(uint16) == 2, "Unexpected size"); |
| 21 | static_assert(sizeof(int32) == 4, "Unexpected size"); |
| 22 | static_assert(sizeof(uint32) == 4, "Unexpected size"); |
| 23 | static_assert(sizeof(int64) == 8, "Unexpected size"); |
| 24 | static_assert(sizeof(uint64) == 8, "Unexpected size"); |
| 25 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 26 | TEST(BasicTypesTest, Endian) { |
| 27 | uint16 v16 = 0x1234u; |
| 28 | uint8 first_byte = *reinterpret_cast<uint8*>(&v16); |
henrikg | 71df77b | 2015-09-18 08:48:34 | [diff] [blame^] | 29 | #if defined(RTC_ARCH_CPU_LITTLE_ENDIAN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 30 | EXPECT_EQ(0x34u, first_byte); |
henrikg | 71df77b | 2015-09-18 08:48:34 | [diff] [blame^] | 31 | #elif defined(RTC_ARCH_CPU_BIG_ENDIAN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 32 | EXPECT_EQ(0x12u, first_byte); |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | TEST(BasicTypesTest, SizeOfTypes) { |
| 37 | int8 i8 = -1; |
| 38 | uint8 u8 = 1u; |
| 39 | int16 i16 = -1; |
| 40 | uint16 u16 = 1u; |
| 41 | int32 i32 = -1; |
| 42 | uint32 u32 = 1u; |
| 43 | int64 i64 = -1; |
| 44 | uint64 u64 = 1u; |
| 45 | EXPECT_EQ(1u, sizeof(i8)); |
| 46 | EXPECT_EQ(1u, sizeof(u8)); |
| 47 | EXPECT_EQ(2u, sizeof(i16)); |
| 48 | EXPECT_EQ(2u, sizeof(u16)); |
| 49 | EXPECT_EQ(4u, sizeof(i32)); |
| 50 | EXPECT_EQ(4u, sizeof(u32)); |
| 51 | EXPECT_EQ(8u, sizeof(i64)); |
| 52 | EXPECT_EQ(8u, sizeof(u64)); |
| 53 | EXPECT_GT(0, i8); |
| 54 | EXPECT_LT(0u, u8); |
| 55 | EXPECT_GT(0, i16); |
| 56 | EXPECT_LT(0u, u16); |
| 57 | EXPECT_GT(0, i32); |
| 58 | EXPECT_LT(0u, u32); |
| 59 | EXPECT_GT(0, i64); |
| 60 | EXPECT_LT(0u, u64); |
| 61 | } |
| 62 | |
| 63 | TEST(BasicTypesTest, SizeOfConstants) { |
| 64 | EXPECT_EQ(8u, sizeof(INT64_C(0))); |
| 65 | EXPECT_EQ(8u, sizeof(UINT64_C(0))); |
| 66 | EXPECT_EQ(8u, sizeof(INT64_C(0x1234567887654321))); |
| 67 | EXPECT_EQ(8u, sizeof(UINT64_C(0x8765432112345678))); |
| 68 | } |
| 69 | |
| 70 | // Test CPU_ macros |
| 71 | #if !defined(CPU_ARM) && defined(__arm__) |
| 72 | #error expected CPU_ARM to be defined. |
| 73 | #endif |
| 74 | #if !defined(CPU_X86) && (defined(WEBRTC_WIN) || defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)) |
| 75 | #error expected CPU_X86 to be defined. |
| 76 | #endif |
henrikg | 71df77b | 2015-09-18 08:48:34 | [diff] [blame^] | 77 | #if !defined(RTC_ARCH_CPU_LITTLE_ENDIAN) && \ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 78 | (defined(WEBRTC_WIN) || defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) || defined(CPU_X86)) |
henrikg | 71df77b | 2015-09-18 08:48:34 | [diff] [blame^] | 79 | #error expected RTC_ARCH_CPU_LITTLE_ENDIAN to be defined. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | // TODO(fbarchard): Test all macros in basictypes.h |
| 83 | |
| 84 | } // namespace rtc |