]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.types/nullptr_t.pass.cpp
Vendor import of libc++ release_39 branch r276489:
[FreeBSD/FreeBSD.git] / test / std / language.support / support.types / nullptr_t.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <cstddef>
11 #include <type_traits>
12 #include <cassert>
13
14 // typedef decltype(nullptr) nullptr_t;
15
16 struct A
17 {
18     A(std::nullptr_t) {}
19 };
20
21 template <class T>
22 void test_conversions()
23 {
24     {
25         T p = 0;
26         assert(p == nullptr);
27     }
28     {
29         T p = nullptr;
30         assert(p == nullptr);
31         assert(nullptr == p);
32         assert(!(p != nullptr));
33         assert(!(nullptr != p));
34     }
35 }
36
37 template <class T>
38 void test_comparisons()
39 {
40     T p = nullptr;
41     assert(p == nullptr);
42     assert(p <= nullptr);
43     assert(p >= nullptr);
44     assert(!(p != nullptr));
45     assert(!(p < nullptr));
46     assert(!(p > nullptr));
47     assert(nullptr == p);
48     assert(nullptr <= p);
49     assert(nullptr >= p);
50     assert(!(nullptr != p));
51     assert(!(nullptr < p));
52     assert(!(nullptr > p));
53 }
54
55 #if defined(__clang__)
56 #pragma clang diagnostic push
57 #pragma clang diagnostic ignored "-Wnull-conversion"
58 #endif
59 void test_nullptr_conversions() {
60 // GCC does not accept this due to CWG Defect #1423
61 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1423
62 #if defined(__clang__)
63     {
64         bool b = nullptr;
65         assert(!b);
66     }
67 #endif
68     {
69         bool b(nullptr);
70         assert(!b);
71     }
72 }
73 #if defined(__clang__)
74 #pragma clang diagnostic pop
75 #endif
76
77
78 int main()
79 {
80     static_assert(sizeof(std::nullptr_t) == sizeof(void*),
81                   "sizeof(std::nullptr_t) == sizeof(void*)");
82
83     {
84         test_conversions<std::nullptr_t>();
85         test_conversions<void*>();
86         test_conversions<A*>();
87         test_conversions<void(*)()>();
88         test_conversions<void(A::*)()>();
89         test_conversions<int A::*>();
90     }
91     {
92         test_comparisons<std::nullptr_t>();
93         test_comparisons<void*>();
94         test_comparisons<A*>();
95         test_comparisons<void(*)()>();
96     }
97     test_nullptr_conversions();
98 }