1/* 2 * libjingle 3 * Copyright 2012 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "talk/base/basictypes.h" 29 30#include "talk/base/gunit.h" 31 32namespace talk_base { 33 34TEST(BasicTypesTest, Endian) { 35 uint16 v16 = 0x1234u; 36 uint8 first_byte = *reinterpret_cast<uint8*>(&v16); 37#if defined(ARCH_CPU_LITTLE_ENDIAN) 38 EXPECT_EQ(0x34u, first_byte); 39#elif defined(ARCH_CPU_BIG_ENDIAN) 40 EXPECT_EQ(0x12u, first_byte); 41#endif 42} 43 44TEST(BasicTypesTest, SizeOfTypes) { 45 int8 i8 = -1; 46 uint8 u8 = 1u; 47 int16 i16 = -1; 48 uint16 u16 = 1u; 49 int32 i32 = -1; 50 uint32 u32 = 1u; 51 int64 i64 = -1; 52 uint64 u64 = 1u; 53 EXPECT_EQ(1u, sizeof(i8)); 54 EXPECT_EQ(1u, sizeof(u8)); 55 EXPECT_EQ(2u, sizeof(i16)); 56 EXPECT_EQ(2u, sizeof(u16)); 57 EXPECT_EQ(4u, sizeof(i32)); 58 EXPECT_EQ(4u, sizeof(u32)); 59 EXPECT_EQ(8u, sizeof(i64)); 60 EXPECT_EQ(8u, sizeof(u64)); 61 EXPECT_GT(0, i8); 62 EXPECT_LT(0u, u8); 63 EXPECT_GT(0, i16); 64 EXPECT_LT(0u, u16); 65 EXPECT_GT(0, i32); 66 EXPECT_LT(0u, u32); 67 EXPECT_GT(0, i64); 68 EXPECT_LT(0u, u64); 69} 70 71TEST(BasicTypesTest, SizeOfConstants) { 72 EXPECT_EQ(8u, sizeof(INT64_C(0))); 73 EXPECT_EQ(8u, sizeof(UINT64_C(0))); 74 EXPECT_EQ(8u, sizeof(INT64_C(0x1234567887654321))); 75 EXPECT_EQ(8u, sizeof(UINT64_C(0x8765432112345678))); 76} 77 78// Test CPU_ macros 79#if !defined(CPU_ARM) && defined(__arm__) 80#error expected CPU_ARM to be defined. 81#endif 82#if !defined(CPU_X86) && (defined(WIN32) || defined(OSX)) 83#error expected CPU_X86 to be defined. 84#endif 85#if !defined(ARCH_CPU_LITTLE_ENDIAN) && \ 86 (defined(WIN32) || defined(OSX) || defined(CPU_X86)) 87#error expected ARCH_CPU_LITTLE_ENDIAN to be defined. 88#endif 89 90// TODO(fbarchard): Test all macros in basictypes.h 91 92} // namespace talk_base 93