forked from kp2pml30/shared_ptr_task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_object.cpp
63 lines (52 loc) · 1.39 KB
/
test_object.cpp
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
#include "test_object.h"
#include <gtest/gtest.h>
namespace
{
int transcode(int data, void const* ptr)
{
return data ^ static_cast<int>(reinterpret_cast<std::ptrdiff_t>(ptr) / sizeof(test_object));
}
}
test_object::test_object(int data)
: data(transcode(data, this))
{
auto p = instances.insert(this);
EXPECT_TRUE(p.second);
}
test_object::test_object(test_object const& other)
{
{
EXPECT_TRUE(instances.find(&other) != instances.end());
auto p = instances.insert(this);
EXPECT_TRUE(p.second);
}
data = transcode(transcode(other.data, &other), this);
}
test_object::~test_object()
{
size_t n = instances.erase(this);
EXPECT_EQ(1u, n);
}
test_object& test_object::operator=(test_object const& c)
{
EXPECT_TRUE(instances.find(this) != instances.end());
data = transcode(transcode(c.data, &c), this);
return *this;
}
test_object::operator int() const
{
EXPECT_TRUE(instances.find(this) != instances.end());
return transcode(data, this);
}
std::set<test_object const*> test_object::instances;
test_object::no_new_instances_guard::no_new_instances_guard()
: old_instances(instances)
{}
test_object::no_new_instances_guard::~no_new_instances_guard()
{
EXPECT_TRUE(old_instances == instances);
}
void test_object::no_new_instances_guard::expect_no_instances() const
{
EXPECT_TRUE(old_instances == instances);
}