Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 20_util / allocator_traits / members / allocate_hint.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2011-2013 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 #include <memory>
21 #include <cstddef>
22 #include <testsuite_hooks.h>
23
24 struct X { };
25
26 template<typename T>
27 struct hintable_allocator
28 {
29   typedef T value_type;
30   struct const_void_pointer { };
31   typedef unsigned size_type;
32
33   hintable_allocator() : called(false) { }
34
35   bool called;
36
37   // this is the overload that should get called:
38   T* allocate(size_type n, const_void_pointer) { called = true; return 0; }
39
40   // none of these should get called:
41   T* allocate(size_type n);
42   T* allocate(size_type n, void*);
43   T* allocate(size_type n, const void*);
44   T* allocate(size_type n, const_void_pointer) const;
45 };
46
47 void test01()
48 {
49   bool test __attribute__((unused)) = true;
50
51   typedef std::allocator_traits<hintable_allocator<X>> traits_type;
52   traits_type::allocator_type a;
53   traits_type::const_void_pointer v;
54   X* p __attribute__((unused)) = traits_type::allocate(a, 1, v);
55   VERIFY( a.called );
56 }
57
58 template<typename T>
59 struct unhintable_allocator
60 {
61   typedef T value_type;
62   typedef unsigned size_type;
63
64   unhintable_allocator() : called(false) { }
65
66   bool called;
67
68   // this is the overload that should get called:
69   T* allocate(size_type n) { called = true; return 0; }
70
71   // this should not get called:
72   T* allocate(size_type n, void*);
73 };
74
75 void test02()
76 {
77   bool test __attribute__((unused)) = true;
78
79   typedef std::allocator_traits<unhintable_allocator<X>> traits_type;
80   traits_type::allocator_type a;
81   traits_type::const_void_pointer v;
82   X* p __attribute__((unused)) = traits_type::allocate(a, 1, v);
83   VERIFY( a.called );
84 }
85
86 int main()
87 {
88   test01();
89   test02();
90 }