summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/align/1.cc
blob: 2d22d866746f93abe3ae4ff5c021c5c2137e7a3d (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
// { dg-do run { target c++11 } }

// 2014-04-16 RĂ¼diger Sonderfeld  <ruediger@c-plusplus.de>

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

// C++11 [ptr.align] (20.6.5): std::align

// { dg-require-cstdint "" }

#include <memory>
#include <cstdint>
#include <testsuite_hooks.h>

void
test01()
{
  size_t space = 100;
  void* ptr = new char[space];
  char* const orig_ptr = static_cast<char*>(ptr);
  char* old_ptr = orig_ptr;
  const size_t orig_space = space;
  size_t old_space = space;
  const size_t alignment = 16;
  const size_t size = 10;
  while( void* const r = std::align(alignment, size, ptr, space) )
    {
      VERIFY( r == ptr );
      uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
      VERIFY( p % alignment == 0 );
      char* const x = static_cast<char*>(ptr);
      VERIFY( x - old_ptr == old_space - space );
      VERIFY( (void*)x < (void*)(orig_ptr + orig_space) );
      VERIFY( (void*)(x + size) < (void*)(orig_ptr + orig_space) );
      ptr = x + size;
      old_ptr = x;
      old_space = space;
      space -= size;
    }
  delete [] orig_ptr;
}

int main()
{
  test01();
}