summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/scoped_allocator/propagation.cc
blob: 032984fbeccb92fb9731b8aebe371ffa49366f02 (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
104
105
106
107
108
// { dg-do compile { target c++11 } }

// Copyright (C) 2011-2019 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/>.

// test that propagate_on_container_xxx is true iff it is true for
// any of the outer or inner allocators

#include <scoped_allocator>

using std::scoped_allocator_adaptor;

typedef short test_type;

template<typename T>
  struct minimal_allocator
  {
    typedef T value_type;
    minimal_allocator();
    template <typename U>
      minimal_allocator(const minimal_allocator<U>&);
    T* allocate(std::size_t);
    void deallocate(T*, std::size_t);
  };

template<typename T, bool copy, bool move, bool swap>
  struct test_allocator : minimal_allocator<T>
  {
    template<typename U>
      struct rebind
      { using other = test_allocator<U, copy, move, swap>; };

    struct propagate_on_container_copy_assignment
    : std::integral_constant<bool, copy> { };

    struct propagate_on_container_move_assignment
    : std::integral_constant<bool, move> { };

    struct propagate_on_container_swap
    : std::integral_constant<bool, swap> { };
  };

template<typename A>
  constexpr bool prop_on_copy()
  {
    typedef typename A::propagate_on_container_copy_assignment type;
    return type::value;
  }

template<typename A>
  constexpr bool prop_on_move()
  {
    typedef typename A::propagate_on_container_move_assignment type;
    return type::value;
  }

template<typename A>
  constexpr bool prop_on_swap()
  {
    typedef typename A::propagate_on_container_swap type;
    return type::value;
  }

template<typename A, bool C, bool M, bool S>
  constexpr bool test1()
  {
    static_assert( prop_on_copy<A>() == C, "copy" );
    static_assert( prop_on_move<A>() == M, "move" );
    static_assert( prop_on_swap<A>() == S, "swap" );
    return true;
  }

template<bool C, bool M, bool S>
  constexpr bool test2()
  {
    typedef minimal_allocator<test_type>       base_alloc;
    typedef test_allocator<test_type, C, M, S> test_alloc;
    typedef scoped_allocator_adaptor<base_alloc, test_alloc> scoped1;
    typedef scoped_allocator_adaptor<test_alloc, base_alloc> scoped2;
    typedef scoped_allocator_adaptor<test_alloc, test_alloc> scoped3;
    return test1<scoped1, C, M, S>()
        && test1<scoped2, C, M, S>()
        && test1<scoped3, C, M, S>();
  }

static_assert( test2<false, false, false>(), "never propagate" );
static_assert( test2<true, false, false>(), "propagate on copy" );
static_assert( test2<false, true, false>(), "propagate on move" );
static_assert( test2<false, false, true>(), "propagate on swap" );
static_assert( test2<true, true, false>(), "propagate on copy & move" );
static_assert( test2<true, false, true>(), "propagate on copy & swap" );
static_assert( test2<false, true, true>(), "propagate on move & swap" );
static_assert( test2<true, true, true>(), "always propagate" );