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