1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// #include <memory>
10
11// void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
12
13#include <cassert>
14#include <cstddef>
15#include <memory>
16
17#include "test_macros.h"
18
19int main(int, char**)
20{
21 const unsigned N = 20;
22 char buf[N];
23 void* r;
24 void* p = &buf[0];
25 std::size_t s = N;
26 r = std::align(align: 4, size: 10, ptr&: p, space&: s);
27 assert(p == &buf[0]);
28 assert(r == p);
29 assert(s == N);
30
31 p = &buf[1];
32 s = N;
33 r = std::align(align: 4, size: 10, ptr&: p, space&: s);
34 assert(p == &buf[4]);
35 assert(r == p);
36 assert(s == N-3);
37
38 p = &buf[2];
39 s = N;
40 r = std::align(align: 4, size: 10, ptr&: p, space&: s);
41 assert(p == &buf[4]);
42 assert(r == p);
43 assert(s == N-2);
44
45 p = &buf[3];
46 s = N;
47 r = std::align(align: 4, size: 10, ptr&: p, space&: s);
48 assert(p == &buf[4]);
49 assert(r == p);
50 assert(s == N-1);
51
52 p = &buf[4];
53 s = N;
54 r = std::align(align: 4, size: 10, ptr&: p, space&: s);
55 assert(p == &buf[4]);
56 assert(r == p);
57 assert(s == N);
58
59 p = &buf[0];
60 s = N;
61 r = std::align(align: 4, size: N, ptr&: p, space&: s);
62 assert(p == &buf[0]);
63 assert(r == p);
64 assert(s == N);
65
66 p = &buf[1];
67 s = N-1;
68 r = std::align(align: 4, size: N-4, ptr&: p, space&: s);
69 assert(p == &buf[4]);
70 assert(r == p);
71 assert(s == N-4);
72
73 p = &buf[1];
74 s = N-1;
75 r = std::align(align: 4, size: N-3, ptr&: p, space&: s);
76 assert(p == &buf[1]);
77 assert(r == nullptr);
78 assert(s == N-1);
79
80 p = &buf[0];
81 s = N;
82 r = std::align(align: 1, size: N+1, ptr&: p, space&: s);
83 assert(p == &buf[0]);
84 assert(r == nullptr);
85 assert(s == N);
86
87 return 0;
88}
89

source code of libcxx/test/std/utilities/memory/ptr.align/align.pass.cpp