forked from sorokin/year2018-list-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounted.cpp
78 lines (65 loc) · 1.67 KB
/
counted.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "counted.h"
#include <gtest/gtest.h>
#include "fault_injection.h"
namespace
{
int transcode(int data, void const* ptr)
{
return data ^ static_cast<int>(reinterpret_cast<std::ptrdiff_t>(ptr) / sizeof(counted));
}
}
counted::counted(int data)
: data(transcode(data, this))
{
fault_injection_point();
fault_injection_disable fd;
auto p = instances.insert(this);
EXPECT_TRUE(p.second);
}
counted::counted(counted const& other)
{
fault_injection_point();
{
fault_injection_disable fd;
EXPECT_TRUE(instances.find(&other) != instances.end());
auto p = instances.insert(this);
EXPECT_TRUE(p.second);
}
data = transcode(transcode(other.data, &other), this);
}
counted::~counted()
{
fault_injection_disable fd;
size_t n = instances.erase(this);
EXPECT_EQ(1u, n);
}
counted& counted::operator=(counted const& c)
{
fault_injection_point();
{
fault_injection_disable fd;
EXPECT_TRUE(instances.find(this) != instances.end());
}
data = transcode(transcode(c.data, &c), this);
return *this;
}
counted::operator int() const
{
fault_injection_disable fd;
EXPECT_TRUE(instances.find(this) != instances.end());
return transcode(data, this);
}
std::set<counted const*> counted::instances;
counted::no_new_instances_guard::no_new_instances_guard()
: old_instances(instances)
{}
counted::no_new_instances_guard::~no_new_instances_guard()
{
fault_injection_disable fd;
EXPECT_TRUE(old_instances == instances);
}
void counted::no_new_instances_guard::expect_no_instances()
{
fault_injection_disable fd;
EXPECT_TRUE(old_instances == instances);
}