aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/memory_resource/resource_adaptor.cc
blob: 8e639f22380380eafd2667bd732a300694e846ac (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
// { dg-do run { target c++14 } }

// Copyright (C) 2016-2017 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 <experimental/memory_resource>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>

using std::experimental::pmr::memory_resource;
using std::experimental::pmr::resource_adaptor;

template<typename T>
  struct Allocator : __gnu_test::SimpleAllocator<T>
  {
    Allocator(int) { } // not default constructible

    template<typename U>
      Allocator(const Allocator<U>&) { }
  };

template<typename T>
  bool aligned(void* p)
  {
    return (reinterpret_cast<std::uintptr_t>(p) % alignof(T)) == 0;
  }

// resource_adaptor
void
test05()
{
  using std::max_align_t;
  using std::uintptr_t;
  void* p = nullptr;

  Allocator<int> a1(1), a2(2); // minimal interface allocators
  resource_adaptor<decltype(a1)> r1(a1), r2(a2);
  VERIFY( r1 == r1 );
  VERIFY( r1 == r2 );
  p = r1.allocate(1);
  VERIFY( aligned<max_align_t>(p) );
  r1.deallocate(p, 1);
  p = r1.allocate(1, alignof(short));
  VERIFY( aligned<short>(p) );
  r1.deallocate(p, 1, alignof(short));
  p = r1.allocate(1, alignof(long));
  VERIFY( aligned<long>(p) );
  r1.deallocate(p, 1, alignof(long));

  __gnu_test::uneq_allocator<double> a3(3), a4(4); // non-equal allocators
  resource_adaptor<decltype(a3)> r3(a3), r4(a4);
  VERIFY( r3 == r3 );
  VERIFY( r4 == r4 );
  VERIFY( r3 != r4 );
  p = r3.allocate(1);
  VERIFY( aligned<max_align_t>(p) );
  r3.deallocate(p, 1);
  p = r3.allocate(1, alignof(short));
  VERIFY( aligned<short>(p) );
  r3.deallocate(p, 1, alignof(short));
  p = r3.allocate(1, alignof(long));
  VERIFY( aligned<long>(p) );
  r3.deallocate(p, 1, alignof(long));

  // TODO test with an allocator that doesn't use new or malloc, so
  // returns pointers that are not suitably aligned for any type.
}

int main()
{
  test05();
}