Debian Codegraph @

libstdc++-v3/testsuite/20_util/allocator_traits/members/construct.cc

raw
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2011-2025 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 typedef std::allocator_traits<fake_allocator<X>> traits_type;
56 traits_type::allocator_type a;
57 X* p = traits_type::allocate(a, 1);
58 traits_type::construct(a, p);
59 VERIFY( a.counter == 1 );
60 traits_type::construct(a, p, 1);
61 VERIFY( a.counter == 1 );
62 VERIFY( X::counter == 1 );
63 traits_type::destroy(a, p);
64 traits_type::construct(a, p, 1, 1);
65 VERIFY( a.counter == 2 );
66 VERIFY( X::counter == 1 );
67 traits_type::construct(a, p, 1, 1, 1);
68 VERIFY( a.counter == 2 );
69 VERIFY( X::counter == 2 );
70 traits_type::destroy(a, p);
71 traits_type::deallocate(a, p, 1);
72 }
73
74 int main()
75 {
76 test01();
77 }