]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/owner_less/cmp.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / owner_less / cmp.cc
CommitLineData
8868286e 1// { dg-options "-std=gnu++11" }
3b7d65ec 2
f1717362 3// Copyright (C) 2008-2016 Free Software Foundation, Inc.
3b7d65ec 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
6bc9506f 8// Free Software Foundation; either version 3, or (at your option)
3b7d65ec 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
6bc9506f 17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
3b7d65ec 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
26struct A { };
27
28struct B { A a[2]; };
29
30// 20.8.13.4 Template class owner_less [util.smartptr.ownerless]
31
32int
33test01()
34{
9ad39aef 35 bool test __attribute__((unused)) = true;
36
37 // test empty shared_ptrs compare equivalent
38 std::owner_less<std::shared_ptr<A>> less;
39 std::owner_less<std::weak_ptr<A>> wless;
40 std::shared_ptr<A> p1;
41 std::shared_ptr<A> p2;
42 VERIFY( !less(p1, p2) && !less(p2, p1) );
43 std::weak_ptr<A> p3;
44 VERIFY( !less(p1, p3) && !less(p3, p1) );
45 VERIFY( !wless(p1, p3) && !wless(p3, p1) );
46 return 0;
3b7d65ec 47}
48
49
50// Construction from pointer
51int
52test02()
53{
9ad39aef 54 bool test __attribute__((unused)) = true;
55
3b7d65ec 56 std::owner_less<std::shared_ptr<A>> less;
57 std::owner_less<std::weak_ptr<A>> wless;
58
59 std::shared_ptr<A> empty;
60
61 std::shared_ptr<A> a1(new A);
62 VERIFY( less(empty, a1) || less(a1, empty) );
63
64 std::shared_ptr<A> a2(new A);
65 VERIFY( less(a1, a2) || less(a2, a1) );
66
67 std::weak_ptr<A> w1(a1);
68 VERIFY( !less(a1, w1) && !less(w1, a1) );
69
70 std::weak_ptr<A> w2(a2);
71 VERIFY( wless(w1, w2) || wless(w2, w1) );
72
73 a1.reset();
74 VERIFY( !less(empty, a1) && !less(a1, empty) );
75 VERIFY( less(a1, w1) || less(w1, a1) );
76
77 a2.reset();
78 VERIFY( !less(a2, a1) && !less(a1, a2) );
79
80 return 0;
81}
82
83// aliasing
84int
85test03()
86{
9ad39aef 87 bool test __attribute__((unused)) = true;
88
3b7d65ec 89 std::owner_less<std::shared_ptr<A>> less;
90 std::owner_less<std::weak_ptr<A>> wless;
91
92 std::shared_ptr<B> b(new B);
93 std::shared_ptr<A> a0(b, &b->a[0]);
94 std::shared_ptr<A> a1(b, &b->a[1]);
95 // values are different but owners are equivalent:
96 VERIFY( a0 < a1 && !less(a0, a1) && !less(a1, a0) );
97
98 std::weak_ptr<A> w0(a0);
99 std::weak_ptr<A> w1(a1);
100 VERIFY( !wless(w0, w1) && !wless(w1, w0) );
101 VERIFY( !less(a0, w1) && !less(w1, a0) );
102 VERIFY( !wless(w0, a1) && !wless(a1, w0) );
103
104 return 0;
105}
106
107// strict weak ordering
108int
109test04()
110{
9ad39aef 111 bool test __attribute__((unused)) = true;
112
3b7d65ec 113 std::owner_less<std::shared_ptr<A>> less;
114
115 std::shared_ptr<A> a[3];
116 a[0].reset(new A);
117 a[1].reset(new A);
118 a[2].reset(new A);
119 std::sort(a, a+3, less);
120 VERIFY( !less(a[0], a[0]) );
121 VERIFY( less(a[0], a[1]) && !less(a[1], a[0]) );
122 VERIFY( less(a[0], a[1]) && less(a[1], a[2]) && less(a[0], a[2]) );
123
124 return 0;
125}
126
127int
128main()
129{
130 test01();
131 test02();
132 test03();
133 test04();
134 return 0;
135}