Mercurial > hg > orthanc-stone
comparison OrthancStone/UnitTestsSources/TestStrategy.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | UnitTestsSources/TestStrategy.cpp@30deba7bc8e2 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include <gtest/gtest.h> | |
23 | |
24 #include "../Sources/Loaders/BasicFetchingStrategy.h" | |
25 #include "../Sources/Loaders/BasicFetchingItemsSorter.h" | |
26 | |
27 #include <OrthancException.h> | |
28 | |
29 | |
30 namespace | |
31 { | |
32 class StrategyTester : public boost::noncopyable | |
33 { | |
34 private: | |
35 std::map<unsigned int, unsigned int> qualities_; | |
36 | |
37 public: | |
38 bool IsValidCommand(unsigned int item, | |
39 unsigned int quality) | |
40 { | |
41 if (qualities_.find(item) != qualities_.end() && | |
42 qualities_[item] >= quality) | |
43 { | |
44 return false; | |
45 } | |
46 else | |
47 { | |
48 qualities_[item] = quality; | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 bool HasFinished(OrthancStone::BasicFetchingStrategy& strategy) | |
54 { | |
55 for (unsigned int i = 0; i < strategy.GetItemsCount(); i++) | |
56 { | |
57 if (qualities_.find(i) == qualities_.end() || | |
58 qualities_[i] != strategy.GetMaxQuality()) | |
59 { | |
60 return false; | |
61 } | |
62 } | |
63 | |
64 return true; | |
65 } | |
66 }; | |
67 } | |
68 | |
69 | |
70 TEST(BasicFetchingStrategy, Test1) | |
71 { | |
72 ASSERT_THROW(OrthancStone::BasicFetchingStrategy(NULL, 0), Orthanc::OrthancException); | |
73 ASSERT_THROW(OrthancStone::BasicFetchingStrategy(new OrthancStone::BasicFetchingItemsSorter(0), 0), Orthanc::OrthancException); | |
74 | |
75 { | |
76 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(1), 0); | |
77 unsigned int i, q; | |
78 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(0u, q); | |
79 ASSERT_FALSE(s.GetNext(i, q)); | |
80 } | |
81 | |
82 { | |
83 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(1), 5); | |
84 unsigned int i, q; | |
85 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(5u, q); | |
86 ASSERT_FALSE(s.GetNext(i, q)); | |
87 } | |
88 | |
89 { | |
90 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(2), 2); | |
91 unsigned int i, q; | |
92 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(2u, q); | |
93 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(1u, q); | |
94 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(2u, q); | |
95 ASSERT_FALSE(s.GetNext(i, q)); | |
96 } | |
97 | |
98 { | |
99 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(3), 2); | |
100 unsigned int i, q; | |
101 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(2u, q); | |
102 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(1u, q); | |
103 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(1u, q); | |
104 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(2u, q); | |
105 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(2u, q); | |
106 ASSERT_FALSE(s.GetNext(i, q)); | |
107 } | |
108 | |
109 { | |
110 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(3), 2); | |
111 s.SetBlockSize(1); | |
112 s.SetCurrent(0); | |
113 unsigned int i, q; | |
114 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(2u, q); | |
115 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(1u, q); | |
116 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(2u, q); | |
117 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(0u, q); | |
118 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(1u, q); | |
119 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(2u, q); | |
120 ASSERT_FALSE(s.GetNext(i, q)); | |
121 } | |
122 | |
123 { | |
124 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(5), 0); | |
125 ASSERT_THROW(s.SetCurrent(5), Orthanc::OrthancException); | |
126 s.SetCurrent(2); | |
127 | |
128 unsigned int i, q; | |
129 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(0u, q); | |
130 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(3u, i); ASSERT_EQ(0u, q); | |
131 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(0u, q); | |
132 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(4u, i); ASSERT_EQ(0u, q); | |
133 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(0u, q); | |
134 ASSERT_FALSE(s.GetNext(i, q)); | |
135 } | |
136 | |
137 { | |
138 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(5), 0); | |
139 s.SetCurrent(4); | |
140 | |
141 unsigned int i, q; | |
142 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(4u, i); ASSERT_EQ(0u, q); | |
143 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(3u, i); ASSERT_EQ(0u, q); | |
144 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(2u, i); ASSERT_EQ(0u, q); | |
145 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(1u, i); ASSERT_EQ(0u, q); | |
146 ASSERT_TRUE(s.GetNext(i, q)); ASSERT_EQ(0u, i); ASSERT_EQ(0u, q); | |
147 ASSERT_FALSE(s.GetNext(i, q)); | |
148 } | |
149 } | |
150 | |
151 | |
152 TEST(BasicFetchingStrategy, Test2) | |
153 { | |
154 OrthancStone::BasicFetchingStrategy s(new OrthancStone::BasicFetchingItemsSorter(20), 2); | |
155 ASSERT_EQ(20u, s.GetItemsCount()); | |
156 ASSERT_EQ(2u, s.GetMaxQuality()); | |
157 | |
158 StrategyTester t; | |
159 | |
160 s.SetCurrent(10); | |
161 | |
162 unsigned int i, q; | |
163 while (s.GetNext(i, q)) | |
164 { | |
165 ASSERT_TRUE(t.IsValidCommand(i, q)); | |
166 } | |
167 | |
168 ASSERT_TRUE(t.HasFinished(s)); | |
169 } | |
170 | |
171 | |
172 | |
173 | |
174 TEST(BasicFetchingItemsSorter, Small) | |
175 { | |
176 ASSERT_THROW(OrthancStone::BasicFetchingItemsSorter(0), Orthanc::OrthancException); | |
177 std::vector<unsigned int> v; | |
178 | |
179 { | |
180 OrthancStone::BasicFetchingItemsSorter s(1); | |
181 s.Sort(v, 0); | |
182 ASSERT_EQ(1u, v.size()); | |
183 ASSERT_EQ(0u, v[0]); | |
184 | |
185 ASSERT_THROW(s.Sort(v, 1), Orthanc::OrthancException); | |
186 } | |
187 | |
188 { | |
189 OrthancStone::BasicFetchingItemsSorter s(2); | |
190 s.Sort(v, 0); | |
191 ASSERT_EQ(2u, v.size()); | |
192 ASSERT_EQ(0u, v[0]); | |
193 ASSERT_EQ(1u, v[1]); | |
194 | |
195 s.Sort(v, 1); | |
196 ASSERT_EQ(2u, v.size()); | |
197 ASSERT_EQ(1u, v[0]); | |
198 ASSERT_EQ(0u, v[1]); | |
199 | |
200 ASSERT_THROW(s.Sort(v, 2), Orthanc::OrthancException); | |
201 } | |
202 | |
203 { | |
204 OrthancStone::BasicFetchingItemsSorter s(3); | |
205 s.Sort(v, 0); | |
206 ASSERT_EQ(3u, v.size()); | |
207 ASSERT_EQ(0u, v[0]); | |
208 ASSERT_EQ(1u, v[1]); | |
209 ASSERT_EQ(2u, v[2]); | |
210 | |
211 s.Sort(v, 1); | |
212 ASSERT_EQ(3u, v.size()); | |
213 ASSERT_EQ(1u, v[0]); | |
214 ASSERT_EQ(2u, v[1]); | |
215 ASSERT_EQ(0u, v[2]); | |
216 | |
217 s.Sort(v, 2); | |
218 ASSERT_EQ(3u, v.size()); | |
219 ASSERT_EQ(2u, v[0]); | |
220 ASSERT_EQ(1u, v[1]); | |
221 ASSERT_EQ(0u, v[2]); | |
222 | |
223 ASSERT_THROW(s.Sort(v, 3), Orthanc::OrthancException); | |
224 } | |
225 } | |
226 | |
227 | |
228 TEST(BasicFetchingItemsSorter, Odd) | |
229 { | |
230 OrthancStone::BasicFetchingItemsSorter s(7); | |
231 std::vector<unsigned int> v; | |
232 | |
233 ASSERT_THROW(s.Sort(v, 7), Orthanc::OrthancException); | |
234 | |
235 { | |
236 s.Sort(v, 0); | |
237 ASSERT_EQ(7u, v.size()); | |
238 ASSERT_EQ(0u, v[0]); | |
239 ASSERT_EQ(1u, v[1]); | |
240 ASSERT_EQ(2u, v[2]); | |
241 ASSERT_EQ(3u, v[3]); | |
242 ASSERT_EQ(4u, v[4]); | |
243 ASSERT_EQ(5u, v[5]); | |
244 ASSERT_EQ(6u, v[6]); | |
245 } | |
246 | |
247 { | |
248 s.Sort(v, 1); | |
249 ASSERT_EQ(7u, v.size()); | |
250 ASSERT_EQ(1u, v[0]); | |
251 ASSERT_EQ(2u, v[1]); | |
252 ASSERT_EQ(0u, v[2]); | |
253 ASSERT_EQ(3u, v[3]); | |
254 ASSERT_EQ(4u, v[4]); | |
255 ASSERT_EQ(5u, v[5]); | |
256 ASSERT_EQ(6u, v[6]); | |
257 } | |
258 | |
259 { | |
260 s.Sort(v, 2); | |
261 ASSERT_EQ(7u, v.size()); | |
262 ASSERT_EQ(2u, v[0]); | |
263 ASSERT_EQ(3u, v[1]); | |
264 ASSERT_EQ(1u, v[2]); | |
265 ASSERT_EQ(4u, v[3]); | |
266 ASSERT_EQ(0u, v[4]); | |
267 ASSERT_EQ(5u, v[5]); | |
268 ASSERT_EQ(6u, v[6]); | |
269 } | |
270 | |
271 { | |
272 s.Sort(v, 3); | |
273 ASSERT_EQ(7u, v.size()); | |
274 ASSERT_EQ(3u, v[0]); | |
275 ASSERT_EQ(4u, v[1]); | |
276 ASSERT_EQ(2u, v[2]); | |
277 ASSERT_EQ(5u, v[3]); | |
278 ASSERT_EQ(1u, v[4]); | |
279 ASSERT_EQ(6u, v[5]); | |
280 ASSERT_EQ(0u, v[6]); | |
281 } | |
282 | |
283 { | |
284 s.Sort(v, 4); | |
285 ASSERT_EQ(7u, v.size()); | |
286 ASSERT_EQ(4u, v[0]); | |
287 ASSERT_EQ(5u, v[1]); | |
288 ASSERT_EQ(3u, v[2]); | |
289 ASSERT_EQ(6u, v[3]); | |
290 ASSERT_EQ(2u, v[4]); | |
291 ASSERT_EQ(1u, v[5]); | |
292 ASSERT_EQ(0u, v[6]); | |
293 } | |
294 | |
295 { | |
296 s.Sort(v, 5); | |
297 ASSERT_EQ(7u, v.size()); | |
298 ASSERT_EQ(5u, v[0]); | |
299 ASSERT_EQ(6u, v[1]); | |
300 ASSERT_EQ(4u, v[2]); | |
301 ASSERT_EQ(3u, v[3]); | |
302 ASSERT_EQ(2u, v[4]); | |
303 ASSERT_EQ(1u, v[5]); | |
304 ASSERT_EQ(0u, v[6]); | |
305 } | |
306 | |
307 { | |
308 s.Sort(v, 6); | |
309 ASSERT_EQ(7u, v.size()); | |
310 ASSERT_EQ(6u, v[0]); | |
311 ASSERT_EQ(5u, v[1]); | |
312 ASSERT_EQ(4u, v[2]); | |
313 ASSERT_EQ(3u, v[3]); | |
314 ASSERT_EQ(2u, v[4]); | |
315 ASSERT_EQ(1u, v[5]); | |
316 ASSERT_EQ(0u, v[6]); | |
317 } | |
318 } | |
319 | |
320 | |
321 TEST(BasicFetchingItemsSorter, Even) | |
322 { | |
323 OrthancStone::BasicFetchingItemsSorter s(6); | |
324 std::vector<unsigned int> v; | |
325 | |
326 { | |
327 s.Sort(v, 0); | |
328 ASSERT_EQ(6u, v.size()); | |
329 ASSERT_EQ(0u, v[0]); | |
330 ASSERT_EQ(1u, v[1]); | |
331 ASSERT_EQ(2u, v[2]); | |
332 ASSERT_EQ(3u, v[3]); | |
333 ASSERT_EQ(4u, v[4]); | |
334 ASSERT_EQ(5u, v[5]); | |
335 } | |
336 | |
337 { | |
338 s.Sort(v, 1); | |
339 ASSERT_EQ(6u, v.size()); | |
340 ASSERT_EQ(1u, v[0]); | |
341 ASSERT_EQ(2u, v[1]); | |
342 ASSERT_EQ(0u, v[2]); | |
343 ASSERT_EQ(3u, v[3]); | |
344 ASSERT_EQ(4u, v[4]); | |
345 ASSERT_EQ(5u, v[5]); | |
346 } | |
347 | |
348 { | |
349 s.Sort(v, 2); | |
350 ASSERT_EQ(6u, v.size()); | |
351 ASSERT_EQ(2u, v[0]); | |
352 ASSERT_EQ(3u, v[1]); | |
353 ASSERT_EQ(1u, v[2]); | |
354 ASSERT_EQ(4u, v[3]); | |
355 ASSERT_EQ(0u, v[4]); | |
356 ASSERT_EQ(5u, v[5]); | |
357 } | |
358 | |
359 { | |
360 s.Sort(v, 3); | |
361 ASSERT_EQ(6u, v.size()); | |
362 ASSERT_EQ(3u, v[0]); | |
363 ASSERT_EQ(4u, v[1]); | |
364 ASSERT_EQ(2u, v[2]); | |
365 ASSERT_EQ(5u, v[3]); | |
366 ASSERT_EQ(1u, v[4]); | |
367 ASSERT_EQ(0u, v[5]); | |
368 } | |
369 | |
370 { | |
371 s.Sort(v, 4); | |
372 ASSERT_EQ(6u, v.size()); | |
373 ASSERT_EQ(4u, v[0]); | |
374 ASSERT_EQ(5u, v[1]); | |
375 ASSERT_EQ(3u, v[2]); | |
376 ASSERT_EQ(2u, v[3]); | |
377 ASSERT_EQ(1u, v[4]); | |
378 ASSERT_EQ(0u, v[5]); | |
379 } | |
380 | |
381 { | |
382 s.Sort(v, 5); | |
383 ASSERT_EQ(6u, v.size()); | |
384 ASSERT_EQ(5u, v[0]); | |
385 ASSERT_EQ(4u, v[1]); | |
386 ASSERT_EQ(3u, v[2]); | |
387 ASSERT_EQ(2u, v[3]); | |
388 ASSERT_EQ(1u, v[4]); | |
389 ASSERT_EQ(0u, v[5]); | |
390 } | |
391 } |