summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/scoped_allocator/1.cc
blob: aed208317bcd2dd154a661512ae5213916b11d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// { dg-options "-std=gnu++11" }

// Copyright (C) 2011-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <memory>
#include <scoped_allocator>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>

using __gnu_test::uneq_allocator;

struct Element
{
  typedef uneq_allocator<Element> allocator_type;

  allocator_type alloc;

  Element(const allocator_type& a = allocator_type()) : alloc(a) { }

  Element(std::allocator_arg_t, const allocator_type& a, int = 0)
  : alloc(a) { }

  Element(std::allocator_arg_t, const allocator_type& a, const Element&)
  : alloc(a) { }

  const allocator_type& get_allocator() const { return alloc; }
};

void test01()
{
  bool test __attribute((unused)) = false;

  typedef std::scoped_allocator_adaptor<Element::allocator_type> alloc1_type;

  typedef std::vector<Element, alloc1_type> EltVec;

  alloc1_type a1(1);
  Element e;
  EltVec ev1(1, e, a1);
  VERIFY( ev1.get_allocator().get_personality() == 1 );
  VERIFY( ev1[0].get_allocator().get_personality() == 1 );
}

void test02()
{
  bool test __attribute((unused)) = false;

  typedef std::scoped_allocator_adaptor<Element::allocator_type> inner_alloc_type;

  typedef std::vector<Element, inner_alloc_type> EltVec;

  typedef std::scoped_allocator_adaptor<Element::allocator_type,
                                        Element::allocator_type> alloc_type;

  typedef std::vector<EltVec, alloc_type> EltVecVec;

  alloc_type a(1, Element::allocator_type(2)); // outer=1, inner=2
  Element e;
  EltVec ev(1, e);
  EltVecVec evv(1, ev, a);

  VERIFY( evv.get_allocator().get_personality() == 1 );
  VERIFY( evv[0].get_allocator().get_personality() == 2 );
  VERIFY( evv[0][0].get_allocator().get_personality() == 2 );

  alloc_type a2(3, Element::allocator_type(4)); // outer=3, inner=4

  EltVecVec evv2(evv, a2); // copy with a different allocator

  VERIFY( evv2.get_allocator().get_personality() == 3 );
  VERIFY( evv2[0].get_allocator().get_personality() == 4 );
  VERIFY( evv2[0][0].get_allocator().get_personality() == 4 );

  EltVecVec evv3(std::move(evv), a2); // move with a different allocator

  VERIFY( evv3.get_allocator().get_personality() == 3 );
  VERIFY( evv3[0].get_allocator().get_personality() == 4 );
  VERIFY( evv3[0][0].get_allocator().get_personality() == 4 );

}


int main()
{
  test01();
  test02();
}