test_cstddef.cpp revision 84011c7599ab8a4463cefda5e8ce8a59987640d8
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29// Test that including cstddef works. This should be the only include in this 30// file to make sure that we don't pick definitions without us knowing. 31#include <cstddef> 32 33namespace { 34const int kPassed = 0; 35const int kFailed = 1; 36#define FAIL_UNLESS(f) if (!android::f()) return kFailed; 37} // anonymous namespace 38 39namespace android { 40// Dummy struct used to calculate offset of some of its fields. 41struct Foo 42{ 43 char field1; 44 char field2; 45}; 46 47// Check various types are declared in the std namespace. 48bool testTypesStd() 49{ 50 // size_t should be defined in both namespaces 51 volatile ::size_t size_t_in_top_ns = 0; 52 volatile ::std::size_t size_t_in_std_ns = 0; 53 54 if (sizeof(::size_t) != sizeof(::std::size_t)) 55 { 56 return false; 57 } 58 59 // ptrdiff_t should be defined in both namespaces 60 volatile ::ptrdiff_t ptrdiff_t_in_top_ns = 0; 61 volatile ::std::ptrdiff_t ptrdiff_t_in_std_ns = 0; 62 63 if (sizeof(::ptrdiff_t) != sizeof(::std::ptrdiff_t)) 64 { 65 return false; 66 } 67 // NULL is only in the top namespace 68 volatile int *null_is_defined = NULL; 69 return true; 70} 71 72bool testOffsetOf() 73{ 74#ifndef offsetof 75#error "offsetof is not a macro" 76#endif 77 78 // offsetof is only in the top namespace 79 volatile size_t offset = offsetof(struct Foo, field2); 80 return offset == 1; 81} 82 83bool testNull() 84{ 85#ifndef NULL 86#error "NULL is not a macro" 87#endif 88 // If NULL is void* this will issue a warning. 89 volatile int null_is_not_void_star = NULL; 90 return true; 91} 92 93} // android namespace 94 95int main(int argc, char **argv) 96{ 97 FAIL_UNLESS(testTypesStd); 98 FAIL_UNLESS(testOffsetOf); 99 FAIL_UNLESS(testNull); 100 return kPassed; 101} 102