-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathTest_TS_Policy_Safe.cpp
218 lines (158 loc) · 5.79 KB
/
Test_TS_Policy_Safe.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <future>
#include <list>
#include "CppUnitTest.h"
#include "Test_Base.hpp"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Nano_Tests
{
TEST_CLASS(Test_TS_Policy_Safe)
{
const int N = 64; // 4 seconds
//const int N = 256; // 4 minutes
using Moo_T = Moo<Observer_TSS>;
using Subject = Signal_Rng_TSS;
TEST_METHOD(Test_Shared_Signal)
{
Subject subject;
auto context = [&]()
{
std::size_t count = 0;
Rng rng;
for (; count < N; ++count)
{
std::list<Moo_T> moo(N);
for (auto& moo_instance : moo)
{
subject.connect<&Moo_T::slot_next_random>(moo_instance);
}
subject.fire(rng);
}
};
std::vector<std::future<void>> future_results;
for (std::size_t i = std::thread::hardware_concurrency(); i > 0; --i)
{
future_results.emplace_back(std::async(std::launch::async, context));
}
for (auto& future_result : future_results)
{
future_result.get();
}
Assert::IsTrue(subject.is_empty(), L"A signal was found not empty.");
}
//----------------------------------------------------------------------
TEST_METHOD(Test_Signal_Move)
{
Moo_T foo1;
Moo_T foo2;
auto rng1 = Rng();
auto rng2 = Rng();
auto sig1 = new Subject();
auto sig2 = new Subject();
auto fun1 = [](Rng& rng) { rng.discard(1); };
sig1->connect<&Moo_T::slot_next_random>(foo1);
sig1->connect(fun1);
sig1->connect<&Moo_T::slot_static_next_random>();
sig1->connect<&slot_next_random_free_function>();
sig2->connect<&Moo_T::slot_next_random>(foo2);
sig2->connect(fun1);
sig2->connect<&Moo_T::slot_static_next_random>();
sig2->connect<&slot_next_random_free_function>();
{
// This should remove all connections from sig2 and add all sig1 connections
*sig2 = std::move(*sig1);
Assert::IsTrue(sig1->is_empty(), L"Signal failed to remove connections during move.");
Assert::IsTrue(foo2.is_empty(), L"Signal failed to remove connections prior to move.");
Assert::IsFalse(foo1.is_empty(), L"Signal failed to move subject connections to target.");
Assert::IsFalse(sig2->is_empty(), L"Signal failed to move signal connections to target.");
// Should have four connections
sig2->fire(rng1);
rng2.discard(4);
Assert::IsTrue(rng1 == rng2, L"Signal failed to move all connections.");
delete sig2;
}
Assert::IsTrue(foo1.is_empty(), L"Signal failed to dispose connections.");
Assert::IsTrue(foo2.is_empty(), L"Signal failed to dispose connections.");
}
//----------------------------------------------------------------------
TEST_METHOD(Test_Fire_Disconnect)
{
Moo_T foo;
Subject signal;
signal.connect<&Moo_T::slot_next_random>(foo);
std::function<void(Rng&)> test;
test = [&](Rng&)
{
signal.disconnect(test);
};
signal.connect(test);
signal.fire(Rng());
}
TEST_METHOD(Test_Fire_Disconnects)
{
Moo_T foo;
Subject signal;
signal.connect<&Moo_T::slot_next_random>(foo);
std::function<void(Rng&)> test;
test = [&](Rng&)
{
signal.disconnect<&Moo_T::slot_next_random>(foo);
signal.disconnect(test);
};
signal.connect(test);
signal.fire(Rng());
Assert::IsTrue(foo.is_empty(), L"An observer was not disconnected.");
Assert::IsTrue(signal.is_empty(), L"A slot was not disconnected.");
}
TEST_METHOD(Test_Fire_Connects)
{
Moo_T foo;
Subject signal;
std::function<void(Rng&)> test;
test = [&](Rng& rng)
{
signal.connect<&Moo_T::slot_next_random>(foo);
foo.slot_next_random(rng);
};
signal.connect(test);
Rng rng_1;
Rng rng_2;
rng_2.discard(3);
signal.fire(rng_1);
signal.fire(rng_1);
Assert::IsTrue(rng_1 == rng_2, L"A slot was not connected.");
}
TEST_METHOD(Test_Fire_Disconnect_All)
{
Moo_T foo;
Subject signal;
signal.connect<&Moo_T::slot_next_random>(foo);
std::function<void(Rng&)> test;
test = [&](Rng&)
{
signal.disconnect_all();
};
signal.connect(test);
signal.fire(Rng());
Assert::IsTrue(foo.is_empty(), L"An observer was not disconnected.");
Assert::IsTrue(signal.is_empty(), L"A slot was not disconnected.");
}
TEST_METHOD(Test_Fire_Fire)
{
Moo_T foo;
Subject signal;
std::function<void(Rng&)> test;
Rng stop;
stop.discard(16);
test = [&](Rng& rng)
{
if (rng != stop)
{
rng.discard(1);
signal.fire(rng);
}
};
signal.connect(test);
signal.fire(Rng());
}
};
}