-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_atomic.cpp
More file actions
142 lines (111 loc) · 3.36 KB
/
Copy pathstd_atomic.cpp
File metadata and controls
142 lines (111 loc) · 3.36 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "common.hpp"
#define MAKE_TEST_HELPER_FUNCS(t) \
bool operator==(t const & l, t const & r) \
{ return l.x == r.x; } \
inline std::ostream& operator<<(std::ostream&os, t const & s) \
{ \
os << "[x: " << s.x << "]"; \
return os; \
}
struct TrivialISER {
int x;
template <class Archive>
void serialize(Archive& ar) { ar(x); }
};
MAKE_TEST_HELPER_FUNCS(TrivialISER)
struct TrivialISPL {
int x;
template <class Archive>
void load(Archive& ar) { ar(x); }
template <class Archive>
void save(Archive& ar) const { ar(x); }
};
MAKE_TEST_HELPER_FUNCS(TrivialISPL)
struct TrivialESER {
int x;
};
template <class Archive>
void serialize(Archive& ar, TrivialESER& t) { ar(t.x); }
MAKE_TEST_HELPER_FUNCS(TrivialESER)
struct TrivialESPL {
int x;
};
template <class Archive>
void load(Archive& ar, TrivialESPL& t) { ar(t.x); }
template <class Archive>
void save(Archive& ar, const TrivialESPL& t) { ar(t.x); }
MAKE_TEST_HELPER_FUNCS(TrivialESPL)
#undef MAKE_TEST_HELPER_FUNCS
template <class IArchive, class OArchive>
void test_atomic() {
std::random_device rd;
std::mt19937 gen(rd());
for (int ii = 0; ii < 100; ++ii) {
std::atomic<bool> o_ab(random_value<uint8_t>(gen) % 2 ? true : false);
std::atomic<signed char> o_asc(random_value<signed char>(gen));
std::atomic<unsigned short> o_aus(random_value<unsigned short>(gen));
std::atomic<int> o_asi(random_value<int>(gen));
std::atomic<long> o_asl(random_value<long>(gen));
std::atomic<unsigned long long> o_aull(random_value<unsigned long long>(gen));
std::atomic<double> o_ad(random_value<double>(gen));
std::atomic<TrivialISER> o_iser{{random_value<int>(gen)}};
std::atomic<TrivialISPL> o_ispl{{random_value<int>(gen)}};
std::atomic<TrivialESER> o_eser{{random_value<int>(gen)}};
std::atomic<TrivialESPL> o_espl{{random_value<int>(gen)}};
std::ostringstream os; {
OArchive oar(os);
oar(o_ab);
oar(o_asc);
oar(o_aus);
oar(o_asi);
oar(o_asl);
oar(o_aull);
oar(o_ad);
oar(o_iser);
oar(o_ispl);
oar(o_eser);
oar(o_espl);
}
decltype(o_ab) i_ab;
decltype(o_asc) i_asc;
decltype(o_aus) i_aus;
decltype(o_asi) i_asi;
decltype(o_asl) i_asl;
decltype(o_aull) i_aull;
decltype(o_ad) i_ad;
decltype(o_iser) i_iser;
decltype(o_ispl) i_ispl;
decltype(o_eser) i_eser;
decltype(o_espl) i_espl;
std::istringstream is(os.str()); {
IArchive iar(is);
iar(i_ab);
iar(i_asc);
iar(i_aus);
iar(i_asi);
iar(i_asl);
iar(i_aull);
iar(i_ad);
iar(i_iser);
iar(i_ispl);
iar(i_eser);
iar(i_espl);
}
CHECK_EQ(o_ab.load(), i_ab.load());
CHECK_EQ(o_asc.load(), i_asc.load());
CHECK_EQ(o_aus.load(), i_aus.load());
CHECK_EQ(o_asi.load(), i_asi.load());
CHECK_EQ(o_asl.load(), i_asl.load());
CHECK_EQ(o_aull.load(), i_aull.load());
CHECK_EQ(o_ad.load(), doctest::Approx(i_ad.load()).epsilon(1e-5L));
CHECK_EQ(i_iser.load(), o_iser.load());
CHECK_EQ(i_ispl.load(), o_ispl.load());
CHECK_EQ(i_eser.load(), o_eser.load());
CHECK_EQ(i_espl.load(), o_espl.load());
}
}
TEST_SUITE_BEGIN("atomic");
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("atomic", test_atomic)
TEST_SUITE_END();