]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/owner_less/cmp.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / owner_less / cmp.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 20.8.13.4 Template class owner_less [util.smartptr.ownerless]
21
22 #include <memory>
23 #include <algorithm>
24 #include <testsuite_hooks.h>
25
26 struct A { };
27
28 struct B { A a[2]; };
29
30 // 20.8.13.4 Template class owner_less [util.smartptr.ownerless]
31
32 int
33 test01()
34 {
35 // test empty shared_ptrs compare equivalent
36 std::owner_less<std::shared_ptr<A>> less;
37 std::owner_less<std::weak_ptr<A>> wless;
38 std::shared_ptr<A> p1;
39 std::shared_ptr<A> p2;
40 VERIFY( !less(p1, p2) && !less(p2, p1) );
41 std::weak_ptr<A> p3;
42 VERIFY( !less(p1, p3) && !less(p3, p1) );
43 VERIFY( !wless(p1, p3) && !wless(p3, p1) );
44 return 0;
45 }
46
47
48 // Construction from pointer
49 int
50 test02()
51 {
52 std::owner_less<std::shared_ptr<A>> less;
53 std::owner_less<std::weak_ptr<A>> wless;
54
55 std::shared_ptr<A> empty;
56
57 std::shared_ptr<A> a1(new A);
58 VERIFY( less(empty, a1) || less(a1, empty) );
59
60 std::shared_ptr<A> a2(new A);
61 VERIFY( less(a1, a2) || less(a2, a1) );
62
63 std::weak_ptr<A> w1(a1);
64 VERIFY( !less(a1, w1) && !less(w1, a1) );
65
66 std::weak_ptr<A> w2(a2);
67 VERIFY( wless(w1, w2) || wless(w2, w1) );
68
69 a1.reset();
70 VERIFY( !less(empty, a1) && !less(a1, empty) );
71 VERIFY( less(a1, w1) || less(w1, a1) );
72
73 a2.reset();
74 VERIFY( !less(a2, a1) && !less(a1, a2) );
75
76 return 0;
77 }
78
79 // aliasing
80 int
81 test03()
82 {
83 std::owner_less<std::shared_ptr<A>> less;
84 std::owner_less<std::weak_ptr<A>> wless;
85
86 std::shared_ptr<B> b(new B);
87 std::shared_ptr<A> a0(b, &b->a[0]);
88 std::shared_ptr<A> a1(b, &b->a[1]);
89 // values are different but owners are equivalent:
90 VERIFY( a0 < a1 && !less(a0, a1) && !less(a1, a0) );
91
92 std::weak_ptr<A> w0(a0);
93 std::weak_ptr<A> w1(a1);
94 VERIFY( !wless(w0, w1) && !wless(w1, w0) );
95 VERIFY( !less(a0, w1) && !less(w1, a0) );
96 VERIFY( !wless(w0, a1) && !wless(a1, w0) );
97
98 return 0;
99 }
100
101 // strict weak ordering
102 int
103 test04()
104 {
105 std::owner_less<std::shared_ptr<A>> less;
106
107 std::shared_ptr<A> a[3];
108 a[0].reset(new A);
109 a[1].reset(new A);
110 a[2].reset(new A);
111 std::sort(a, a+3, less);
112 VERIFY( !less(a[0], a[0]) );
113 VERIFY( less(a[0], a[1]) && !less(a[1], a[0]) );
114 VERIFY( less(a[0], a[1]) && less(a[1], a[2]) && less(a[0], a[2]) );
115
116 return 0;
117 }
118
119 int
120 main()
121 {
122 test01();
123 test02();
124 test03();
125 test04();
126 return 0;
127 }