blob: 4e243fdcae3584d87bd19f029ee806ae74c77387 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
15namespace rtc {
16
kjellander979e0b32015-06-16 14:13:3517static_assert(sizeof(int8) == 1, "Unexpected size");
18static_assert(sizeof(uint8) == 1, "Unexpected size");
19static_assert(sizeof(int16) == 2, "Unexpected size");
20static_assert(sizeof(uint16) == 2, "Unexpected size");
21static_assert(sizeof(int32) == 4, "Unexpected size");
22static_assert(sizeof(uint32) == 4, "Unexpected size");
23static_assert(sizeof(int64) == 8, "Unexpected size");
24static_assert(sizeof(uint64) == 8, "Unexpected size");
25
henrike@webrtc.orgf0488722014-05-13 18:00:2626TEST(BasicTypesTest, Endian) {
27 uint16 v16 = 0x1234u;
28 uint8 first_byte = *reinterpret_cast<uint8*>(&v16);
henrikg71df77b2015-09-18 08:48:3429#if defined(RTC_ARCH_CPU_LITTLE_ENDIAN)
henrike@webrtc.orgf0488722014-05-13 18:00:2630 EXPECT_EQ(0x34u, first_byte);
henrikg71df77b2015-09-18 08:48:3431#elif defined(RTC_ARCH_CPU_BIG_ENDIAN)
henrike@webrtc.orgf0488722014-05-13 18:00:2632 EXPECT_EQ(0x12u, first_byte);
33#endif
34}
35
36TEST(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
63TEST(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
henrikg71df77b2015-09-18 08:48:3477#if !defined(RTC_ARCH_CPU_LITTLE_ENDIAN) && \
henrike@webrtc.orgf0488722014-05-13 18:00:2678 (defined(WEBRTC_WIN) || defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) || defined(CPU_X86))
henrikg71df77b2015-09-18 08:48:3479#error expected RTC_ARCH_CPU_LITTLE_ENDIAN to be defined.
henrike@webrtc.orgf0488722014-05-13 18:00:2680#endif
81
82// TODO(fbarchard): Test all macros in basictypes.h
83
84} // namespace rtc