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
110
111
112
113
|
#include "tensorflow/core/lib/gtl/manual_constructor.h"
#include <stdint.h>
#include "tensorflow/core/platform/logging.h"
#include <gtest/gtest.h>
namespace tensorflow {
namespace {
static int constructor_count_ = 0;
template <int kSize>
struct TestN {
TestN() { ++constructor_count_; }
~TestN() { --constructor_count_; }
char a[kSize];
};
typedef TestN<1> Test1;
typedef TestN<2> Test2;
typedef TestN<3> Test3;
typedef TestN<4> Test4;
typedef TestN<5> Test5;
typedef TestN<9> Test9;
typedef TestN<15> Test15;
} // namespace
namespace {
TEST(ManualConstructorTest, Sizeof) {
CHECK_EQ(sizeof(ManualConstructor<Test1>), sizeof(Test1));
CHECK_EQ(sizeof(ManualConstructor<Test2>), sizeof(Test2));
CHECK_EQ(sizeof(ManualConstructor<Test3>), sizeof(Test3));
CHECK_EQ(sizeof(ManualConstructor<Test4>), sizeof(Test4));
CHECK_EQ(sizeof(ManualConstructor<Test5>), sizeof(Test5));
CHECK_EQ(sizeof(ManualConstructor<Test9>), sizeof(Test9));
CHECK_EQ(sizeof(ManualConstructor<Test15>), sizeof(Test15));
CHECK_EQ(constructor_count_, 0);
ManualConstructor<Test1> mt[4];
CHECK_EQ(sizeof(mt), 4);
CHECK_EQ(constructor_count_, 0);
mt[0].Init();
CHECK_EQ(constructor_count_, 1);
mt[0].Destroy();
}
TEST(ManualConstructorTest, Alignment) {
// We want to make sure that ManualConstructor aligns its memory properly
// on a word barrier. Otherwise, it might be unexpectedly slow, since
// memory access will be unaligned.
struct {
char a;
ManualConstructor<void*> b;
} test1;
struct {
char a;
void* b;
} control1;
// TODO(bww): Make these tests more direct with C++11 alignment_of<T>::value.
EXPECT_EQ(reinterpret_cast<char*>(test1.b.get()) - &test1.a,
reinterpret_cast<char*>(&control1.b) - &control1.a);
EXPECT_EQ(reinterpret_cast<intptr_t>(test1.b.get()) % sizeof(control1.b), 0);
struct {
char a;
ManualConstructor<long double> b;
} test2;
struct {
char a;
long double b;
} control2;
EXPECT_EQ(reinterpret_cast<char*>(test2.b.get()) - &test2.a,
reinterpret_cast<char*>(&control2.b) - &control2.a);
#ifdef ARCH_K8
EXPECT_EQ(reinterpret_cast<intptr_t>(test2.b.get()) % 16, 0);
#endif
#ifdef ARCH_PIII
EXPECT_EQ(reinterpret_cast<intptr_t>(test2.b.get()) % 4, 0);
#endif
}
TEST(ManualConstructorTest, DefaultInitialize) {
struct X {
X() : x(123) {}
int x;
};
union {
ManualConstructor<X> x;
ManualConstructor<int> y;
} u;
*u.y = -1;
u.x.Init(); // should default-initialize u.x
EXPECT_EQ(123, u.x->x);
}
TEST(ManualConstructorTest, ZeroInitializePOD) {
union {
ManualConstructor<int> x;
ManualConstructor<int> y;
} u;
*u.y = -1;
u.x.Init(); // should not zero-initialize u.x
EXPECT_EQ(-1, *u.y);
}
} // namespace
} // namespace tensorflow
|