]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/allocator_traits/members/construct.cc
6d616f1eee88d516674159acf981607befcaf0dc
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / allocator_traits / members / construct.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2011-2016 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 static int counter;
27 X() { }
28 X(const X&) { ++counter; }
29 explicit X(int) { ++counter; }
30 X(int, int) { ++counter; }
31 X(int, int, int) { ++counter; }
32 };
33
34 int X::counter = 0;
35
36 template<typename T>
37 struct fake_allocator
38 {
39 typedef T value_type;
40
41 fake_allocator() : counter() {}
42
43 int counter;
44
45 T* allocate(std::size_t n) { return (T*)new char[n*sizeof(T)]; }
46 void deallocate(T* p, std::size_t) { delete[] (char*)p; }
47
48 // don't actually construct anything when these are called
49 void construct(T* p) { ++counter; }
50 void construct(T* p, int, int) { ++counter; }
51 };
52
53 void test01()
54 {
55 bool test __attribute__((unused)) = true;
56
57 typedef std::allocator_traits<fake_allocator<X>> traits_type;
58 traits_type::allocator_type a;
59 X* p = traits_type::allocate(a, 1);
60 traits_type::construct(a, p);
61 VERIFY( a.counter == 1 );
62 traits_type::construct(a, p, 1);
63 VERIFY( a.counter == 1 );
64 VERIFY( X::counter == 1 );
65 traits_type::destroy(a, p);
66 traits_type::construct(a, p, 1, 1);
67 VERIFY( a.counter == 2 );
68 VERIFY( X::counter == 1 );
69 traits_type::construct(a, p, 1, 1, 1);
70 VERIFY( a.counter == 2 );
71 VERIFY( X::counter == 2 );
72 traits_type::destroy(a, p);
73 traits_type::deallocate(a, p, 1);
74 }
75
76 int main()
77 {
78 test01();
79 }