OSDN Git Service

2008-11-01 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / comparison / less.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2008 Free Software Foundation
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 2, 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 COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // 20.8.13.2 Template class shared_ptr [util.smartptr.shared]
22
23 #include <memory>
24 #include <testsuite_hooks.h>
25
26 struct A { };
27
28 namespace std
29 {
30   template<>
31     struct less<A*> : binary_function<A*,A*,bool>
32     {
33       static int count;
34       bool operator()(A* l, A* r) { ++count; return l < r; }
35     };
36   int less<A*>::count = 0;
37 }
38
39 // 20.8.13.2.7 shared_ptr comparison [util.smartptr.shared.cmp]
40
41
42 int
43 test01()
44 {
45   std::less<std::shared_ptr<A>> less;
46   // test empty shared_ptrs compare equivalent
47   std::shared_ptr<A> p1;
48   std::shared_ptr<A> p2;
49   VERIFY( !less(p1, p2) && !less(p2, p1) );
50   VERIFY( std::less<A*>::count == 2 );
51   return 0;
52 }
53
54
55 // Construction from pointer
56 int
57 test02()
58 {
59   std::less<std::shared_ptr<A>> less;
60
61   std::shared_ptr<A> empty;
62   std::shared_ptr<A> p1(new A);
63   std::shared_ptr<A> p2(new A);
64
65   VERIFY( less(p1, p2) || less(p2, p1) );
66   VERIFY( !(less(p1, p2) && less(p2, p1)) );
67
68   p1.reset();
69   VERIFY( !less(p1, empty) && !less(empty, p1) );
70
71   p2.reset();
72   VERIFY( !less(p1, p2) && !less(p2, p1) );
73
74   return 0;
75 }
76
77 // Aliasing
78 int
79 test03()
80 {
81   std::less<std::shared_ptr<A>> less;
82
83   A a;
84   std::shared_ptr<A> p1(new A);
85   std::shared_ptr<A> p2(p1, &a);
86   VERIFY( less(p1, p2) || less(p2, p1) );
87
88   return 0;
89 }
90 int 
91 main()
92 {
93   test01();
94   test02();
95   test03();
96   return 0;
97 }