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
109
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/log/log.h>
LOG_SETUP("new_test");
void cmp(const void *a, const void *b) {
EXPECT_EQUAL(a, b);
}
void cmp(const void *base, size_t offset, const void *p) {
cmp((static_cast<const char *>(base) + offset), p);
}
template <typename S>
void veryfy_aligned(S * p) {
EXPECT_TRUE((uintptr_t(p) % alignof(S)) == 0);
memset(p, 0, sizeof(S));
}
TEST("verify new with normal alignment") {
struct S {
int a;
long b;
int c;
};
static_assert(sizeof(S) == 24);
static_assert(alignof(S) == 8);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 8, &s->b);
cmp(s.get(), 16, &s->c);
LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c);
}
TEST("verify new with alignment = 16") {
struct S {
int a;
alignas(16) long b;
int c;
};
static_assert(sizeof(S) == 32);
static_assert(alignof(S) == 16);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 16, &s->b);
cmp(s.get(), 24, &s->c);
LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c);
}
TEST("verify new with alignment = 32") {
struct S {
int a;
alignas(32) long b;
int c;
};
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 32);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 32, &s->b);
cmp(s.get(), 40, &s->c);
LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c);
}
TEST("verify new with alignment = 64") {
struct S {
int a;
alignas(64) long b;
int c;
};
static_assert(sizeof(S) == 128);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 64, &s->b);
cmp(s.get(), 72, &s->c);
LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c);
}
TEST("verify new with alignment = 64 with single element") {
struct S {
alignas(64) long a;
};
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
LOG(info, "&s=%p", s.get());
}
TEST("verify new with alignment = 64 with single element") {
struct alignas(64) S {
long a;
};
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
veryfy_aligned(s.get());
cmp(s.get(), &s->a);
LOG(info, "&s=%p", s.get());
}
TEST_MAIN() { TEST_RUN_ALL(); }
|