test_cstddef.cpp revision 1aa5708e437b96e04f31d9c9f167427411ba5510
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#include <cstddef>
30#ifndef BIONIC_LIBSTDCPP_INCLUDE_CSTDDEF__
31#error "Wrong header file included!!"
32#endif
33
34
35namespace {
36const int kPassed = 0;
37const int kFailed = 1;
38#define FAIL_UNLESS(f) if (!android::f()) return kFailed;
39}  // anonymous namespace
40
41namespace android {
42// Dummy struct used to calculate offset of some of its fields.
43struct Foo
44{
45    char field1;
46    char field2;
47};
48
49// Check various types are declared in the std namespace.
50bool testTypesStd()
51{
52    // size_t should be defined in both namespaces
53    volatile ::size_t size_t_in_top_ns = 0;
54    volatile ::std::size_t size_t_in_std_ns = 0;
55
56    if (sizeof(::size_t) != sizeof(::std::size_t))
57    {
58        return false;
59    }
60
61    // ptrdiff_t should be defined in both namespaces
62    volatile ::ptrdiff_t ptrdiff_t_in_top_ns = 0;
63    volatile ::std::ptrdiff_t ptrdiff_t_in_std_ns = 0;
64
65    if (sizeof(::ptrdiff_t) != sizeof(::std::ptrdiff_t))
66    {
67        return false;
68    }
69    // NULL is only in the top namespace
70    volatile int *null_is_defined = NULL;
71    return true;
72}
73
74bool testOffsetOf()
75{
76#ifndef offsetof
77#error "offsetof is not a macro"
78#endif
79
80    // offsetof is only in the top namespace
81    volatile size_t offset = offsetof(struct Foo, field2);
82    return offset == 1;
83}
84
85bool testNull()
86{
87#ifndef NULL
88#error "NULL is not a macro"
89#endif
90    // If NULL is void* this will issue a warning.
91    volatile int null_is_not_void_star = NULL;
92    return true;
93}
94
95}  // android namespace
96
97int main(int argc, char **argv)
98{
99    FAIL_UNLESS(testTypesStd);
100    FAIL_UNLESS(testOffsetOf);
101    FAIL_UNLESS(testNull);
102    return kPassed;
103}
104