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