Add testcase of PR c++/92542, already fixed.
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / new_aligned.cc
blobe6f26197d8b60704562c406c20b2bc594e08c9f4
1 // Copyright (C) 2018-2020 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
21 #include <new>
22 #include <memory>
23 #include <testsuite_hooks.h>
25 struct Test
27 Test(std::size_t size, std::size_t a)
28 : size(size), alignment(std::align_val_t{a}),
29 p(::operator new(size, alignment))
30 { }
32 ~Test() { ::operator delete(p, size, alignment); }
34 std::size_t size;
35 std::align_val_t alignment;
36 void* p;
38 bool valid() const { return p != nullptr; }
40 bool aligned() const
42 auto ptr = p;
43 auto space = size;
44 return std::align((std::size_t)alignment, size, ptr, space) == p;
48 // operator new(size_t size, align_val_t alignment) has
49 // undefined behaviour if the alignment argument is not
50 // a valid alignment value (i.e. not a power of two).
52 // Unlike posix_memalign there is no requirement that
53 // alignment >= sizeof(void*).
55 // Unlike aligned_alloc there is no requirement that
56 // size is an integer multiple of alignment.
58 void
59 test01()
61 // Test small values that would not be valid for
62 // posix_memalign or aligned_alloc.
64 Test t11{1, 1};
65 VERIFY( t11.valid() );
66 VERIFY( t11.aligned() );
68 Test t21{2, 1};
69 VERIFY( t21.valid() );
70 VERIFY( t21.aligned() );
72 Test t12{1, 2};
73 VERIFY( t12.valid() );
74 VERIFY( t12.aligned() );
76 Test t22{2, 2};
77 VERIFY( t22.valid() );
78 VERIFY( t22.aligned() );
80 Test t32{3, 2};
81 VERIFY( t32.valid() );
82 VERIFY( t32.aligned() );
84 Test t42{4, 2};
85 VERIFY( t42.valid() );
86 VERIFY( t42.aligned() );
88 Test t24{2, 4};
89 VERIFY( t24.valid() );
90 VERIFY( t24.aligned() );
92 Test t34{3, 4};
93 VERIFY( t34.valid() );
94 VERIFY( t34.aligned() );
96 Test t44{4, 4};
97 VERIFY( t44.valid() );
98 VERIFY( t44.aligned() );
100 // Test some larger values.
102 Test t128_16{128, 16};
103 VERIFY( t128_16.valid() );
104 VERIFY( t128_16.aligned() );
106 Test t128_64{128, 64};
107 VERIFY( t128_64.valid() );
108 VERIFY( t128_64.aligned() );
110 Test t64_128{64, 128};
111 VERIFY( t64_128.valid() );
112 VERIFY( t64_128.aligned() );
116 main()
118 test01();