79a35943 |
1 | // Created on: 2014-04-16 |
2 | // Created by: Denis BOGOLEPOV |
3 | // Copyright (c) 2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
fe9b8ff2 |
16 | #if defined(_MSC_VER) |
17 | // supress "std::Equal1" warning suggesting using msvc "Checked Iterators" |
18 | #define _SCL_SECURE_NO_WARNINGS |
19 | #endif |
20 | |
79a35943 |
21 | #include <QANCollection.hxx> |
22 | #include <Draw_Interpretor.hxx> |
23 | |
24 | #include <NCollection_Array1.hxx> |
25 | #include <NCollection_List.hxx> |
26 | #include <NCollection_Sequence.hxx> |
27 | #include <NCollection_Vector.hxx> |
28 | #include <NCollection_Map.hxx> |
29 | #include <NCollection_DataMap.hxx> |
30 | #include <NCollection_IndexedMap.hxx> |
31 | #include <NCollection_IndexedDataMap.hxx> |
32 | #include <Standard_Assert.hxx> |
33 | #include <OSD_Timer.hxx> |
c7b59798 |
34 | #include <OSD_Parallel.hxx> |
79a35943 |
35 | #include <algorithm> |
36 | #include <list> |
37 | #include <set> |
38 | #include <typeinfo> |
39 | #include <vector> |
40 | |
41 | //! Size of test data sets. |
42 | const int THE_TEST_SIZE = 5000; |
43 | |
44 | namespace { |
45 | // Auxiliary class to use in std::random_shuffle() |
46 | struct RandomGenerator { |
47 | RandomGenerator () { srand(1); } |
48 | int operator () (int upper) const { return rand() % upper; } |
49 | }; |
50 | } |
51 | |
52 | template<class CollectionType, class StlType> |
53 | struct CollectionFiller |
54 | { |
55 | static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE) |
56 | { |
57 | *theCollec = new CollectionType(); |
58 | srand(1); |
59 | for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx) |
60 | { |
61 | (*theCollec)->Append (rand()); |
62 | } |
63 | } |
64 | |
65 | static void Perform (StlType** theVector, |
66 | CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE) |
67 | { |
68 | CollectionFiller::Perform (theCollec, theSize); |
69 | |
70 | *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end()); |
71 | } |
72 | }; |
73 | |
74 | template<class T, typename StlType> |
75 | struct CollectionFiller<NCollection_Array1<T>, StlType> |
76 | { |
77 | static void Perform (NCollection_Array1<T>** theCollec, |
78 | Standard_Integer theSize = THE_TEST_SIZE) |
79 | { |
80 | *theCollec = new NCollection_Array1<T> (0, theSize - 1); |
81 | srand (1); |
82 | for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx) |
83 | { |
84 | (*theCollec)->ChangeValue (anIdx) = rand(); |
85 | } |
86 | } |
87 | |
88 | static void Perform (StlType** theVector, |
89 | NCollection_Array1<T>** theCollec, Standard_Integer theSize = THE_TEST_SIZE) |
90 | { |
91 | CollectionFiller<NCollection_Array1<T>, StlType >::Perform (theCollec, theSize); |
92 | |
93 | *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end()); |
94 | } |
95 | }; |
96 | |
97 | template<class CollectionType, class T> |
98 | struct MapFiller |
99 | { |
100 | static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE) |
101 | { |
102 | *theCollec = new CollectionType(); |
103 | srand(1); |
104 | for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx) |
105 | { |
106 | (*theCollec)->Add (rand()); |
107 | } |
108 | } |
109 | }; |
110 | |
111 | template<class T> |
112 | struct MapFiller<NCollection_DataMap<T, T>, T> |
113 | { |
114 | static void Perform (NCollection_DataMap<T, T>** theCollec1, |
115 | NCollection_DataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE) |
116 | { |
117 | *theCollec1 = new NCollection_DataMap<T, T>(); |
118 | |
119 | if (theCollec2 != NULL) |
120 | *theCollec2 = new NCollection_DataMap<T, T>(); |
121 | srand(1); |
122 | for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx) |
123 | { |
124 | const T aVal1 = rand(); |
125 | const T aVal2 = rand(); |
126 | |
127 | (*theCollec1)->Bind (aVal1, aVal2); |
128 | |
129 | if (theCollec2 != NULL) |
130 | (*theCollec2)->Bind (aVal1, aVal2); |
131 | } |
132 | } |
133 | }; |
134 | |
135 | template<class T> |
136 | struct MapFiller<NCollection_IndexedDataMap<T, T>, T> |
137 | { |
138 | static void Perform (NCollection_IndexedDataMap<T, T>** theCollec1, |
139 | NCollection_IndexedDataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE) |
140 | { |
141 | *theCollec1 = new NCollection_IndexedDataMap<T, T>(); |
142 | |
143 | if (theCollec2 != NULL) |
144 | *theCollec2 = new NCollection_IndexedDataMap<T, T>(); |
145 | srand(1); |
146 | for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx) |
147 | { |
148 | const T aVal1 = rand(); |
149 | const T aVal2 = rand(); |
150 | |
151 | (*theCollec1)->Add (aVal1, aVal2); |
152 | |
153 | if (theCollec2 != NULL) |
154 | (*theCollec2)->Add (aVal1, aVal2); |
155 | } |
156 | } |
157 | }; |
158 | |
159 | //======================================================================= |
160 | //function : TestIteration |
161 | //purpose : |
162 | //======================================================================= |
163 | template<class CollectionType, class StlType> |
164 | Standard_Boolean TestIteration() |
165 | { |
166 | StlType* aVector (NULL); |
167 | CollectionType* aCollec (NULL); |
168 | |
169 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
170 | |
171 | typename StlType::iterator aVecIter = aVector->begin(); |
172 | typename CollectionType::iterator aColIter = aCollec->begin(); |
173 | |
174 | Standard_Boolean aResult (Standard_True); |
175 | |
176 | for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter) |
177 | { |
178 | if (*aVecIter != *aColIter) |
179 | aResult = Standard_False; |
180 | } |
181 | |
182 | if (aColIter != aCollec->end()) |
183 | { |
184 | aResult = Standard_False; |
185 | } |
186 | |
187 | delete aVector; |
188 | delete aCollec; |
189 | |
190 | return aResult; |
191 | } |
192 | |
193 | //======================================================================= |
194 | //function : TestMinMax |
195 | //purpose : |
196 | //======================================================================= |
197 | template<class CollectionType, class StlType> |
198 | Standard_Boolean TestMinMax() |
199 | { |
200 | StlType* aVector (NULL); |
201 | CollectionType* aCollec (NULL); |
202 | |
203 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
204 | |
205 | typename StlType::value_type aValue1 = *std::min_element (aVector->begin(), aVector->end()); |
206 | typename CollectionType::value_type aValue2 = *std::min_element (aCollec->begin(), aCollec->end()); |
207 | |
208 | Standard_Boolean aResult (Standard_True); |
209 | |
210 | if (aValue1 != aValue2) |
211 | aResult = Standard_False; |
212 | |
213 | aValue1 = *std::max_element (aVector->begin(), aVector->end()); |
214 | aValue2 = *std::max_element (aCollec->begin(), aCollec->end()); |
215 | |
216 | if (aValue1 != aValue2) |
217 | aResult = Standard_False; |
218 | |
219 | delete aVector; |
220 | delete aCollec; |
221 | |
222 | return aResult; |
223 | } |
224 | |
225 | //======================================================================= |
226 | //function : TestReplace |
227 | //purpose : |
228 | //======================================================================= |
229 | template<class CollectionType, class StlType> |
230 | Standard_Boolean TestReplace() |
231 | { |
232 | StlType* aVector (NULL); |
233 | CollectionType* aCollec (NULL); |
234 | |
235 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
236 | |
237 | const typename StlType::value_type aValue = aVector->back(); |
238 | |
239 | std::replace (aVector->begin(), aVector->end(), aValue, static_cast<typename StlType::value_type> (-1)); |
240 | std::replace (aCollec->begin(), aCollec->end(), aValue, static_cast<typename CollectionType::value_type> (-1)); |
241 | |
242 | typename StlType::iterator aVecIter = aVector->begin(); |
243 | typename CollectionType::iterator aColIter = aCollec->begin(); |
244 | |
245 | Standard_Boolean aResult (Standard_True); |
246 | |
247 | for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter) |
248 | { |
249 | if (*aVecIter != *aColIter) |
250 | aResult = Standard_False; |
251 | } |
252 | |
253 | if (aColIter != aCollec->end()) |
254 | { |
255 | aResult = Standard_False; |
256 | } |
257 | |
258 | delete aVector; |
259 | delete aCollec; |
260 | |
261 | return aResult; |
262 | } |
263 | |
264 | //======================================================================= |
265 | //function : TestReverse |
266 | //purpose : |
267 | //======================================================================= |
268 | template<class CollectionType, class StlType> |
269 | Standard_Boolean TestReverse() |
270 | { |
271 | StlType* aVector (NULL); |
272 | CollectionType* aCollec (NULL); |
273 | |
274 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
275 | |
276 | std::reverse (aVector->begin(), aVector->end()); |
277 | std::reverse (aCollec->begin(), aCollec->end()); |
278 | |
279 | typename StlType::iterator aVecIter = aVector->begin(); |
280 | typename CollectionType::iterator aColIter = aCollec->begin(); |
281 | |
282 | Standard_Boolean aResult (Standard_True); |
283 | |
284 | for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter) |
285 | { |
286 | if (*aVecIter != *aColIter) |
287 | aResult = Standard_False; |
288 | } |
289 | |
290 | if (aColIter != aCollec->end()) |
291 | { |
292 | aResult = Standard_False; |
293 | } |
294 | |
295 | delete aVector; |
296 | delete aCollec; |
297 | |
298 | return aResult; |
299 | } |
300 | |
301 | //======================================================================= |
302 | //function : TestSort |
303 | //purpose : |
304 | //======================================================================= |
305 | template<class CollectionType, class StlType> |
306 | Standard_Boolean TestSort() |
307 | { |
308 | StlType* aVector (NULL); |
309 | CollectionType* aCollec (NULL); |
310 | |
311 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
312 | |
313 | std::sort (aVector->begin(), aVector->end()); |
314 | std::sort (aCollec->begin(), aCollec->end()); |
315 | |
316 | typename StlType::iterator aVecIter = aVector->begin(); |
317 | typename CollectionType::iterator aColIter = aCollec->begin(); |
318 | |
319 | Standard_Boolean aResult (Standard_True); |
320 | |
321 | for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter) |
322 | { |
323 | if (*aVecIter != *aColIter) |
324 | aResult = Standard_False; |
325 | } |
326 | |
327 | if (aColIter != aCollec->end()) |
328 | { |
329 | aResult = Standard_False; |
330 | } |
331 | |
332 | delete aVector; |
333 | delete aCollec; |
334 | |
335 | return aResult; |
336 | } |
337 | |
79a35943 |
338 | template <typename T> |
339 | struct Invoker |
340 | { |
341 | void operator()(T& theValue) const |
342 | { |
343 | theValue *= 2; |
344 | } |
345 | }; |
346 | |
347 | //======================================================================= |
c7b59798 |
348 | //function : TestParallel |
79a35943 |
349 | //purpose : |
350 | //======================================================================= |
351 | template<class CollectionType, class StlType> |
c7b59798 |
352 | Standard_Boolean TestParallel() |
79a35943 |
353 | { |
354 | StlType* aVector (NULL); |
355 | CollectionType* aCollec (NULL); |
356 | |
357 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec); |
358 | |
c7b59798 |
359 | OSD_Parallel::ForEach(aVector->begin(), aVector->end(), Invoker<typename StlType::value_type>()); |
360 | OSD_Parallel::ForEach(aCollec->begin(), aCollec->end(), Invoker<typename CollectionType::value_type>()); |
79a35943 |
361 | |
362 | typename StlType::iterator aVecIter = aVector->begin(); |
363 | typename CollectionType::iterator aColIter = aCollec->begin(); |
364 | |
365 | Standard_Boolean aResult (Standard_True); |
366 | |
367 | for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter) |
368 | { |
369 | if (*aVecIter != *aColIter) |
370 | aResult = Standard_False; |
371 | } |
372 | |
373 | if (aColIter != aCollec->end()) |
374 | { |
375 | aResult = Standard_False; |
376 | } |
377 | |
378 | delete aVector; |
379 | delete aCollec; |
380 | |
381 | return aResult; |
382 | } |
383 | |
384 | //======================================================================= |
c7b59798 |
385 | //function : TestDataMapParallel |
79a35943 |
386 | //purpose : |
387 | //======================================================================= |
388 | template<class CollectionType, class T> |
c7b59798 |
389 | Standard_Boolean TestDataMapParallel() |
79a35943 |
390 | { |
391 | CollectionType* aCollec1 (NULL); |
392 | CollectionType* aCollec2 (NULL); |
393 | |
394 | MapFiller<CollectionType, T>::Perform (&aCollec1, &aCollec2); |
395 | |
c7b59798 |
396 | OSD_Parallel::ForEach(aCollec1->begin(), aCollec1->end(), Invoker<T>()); |
79a35943 |
397 | |
398 | // create OCCT-style iterator |
399 | typename CollectionType::Iterator aOccIter (*aCollec2); |
400 | |
401 | // create STL-compatible iterator |
402 | typename CollectionType::const_iterator aStlIter = aCollec1->cbegin(); |
403 | |
404 | Standard_Boolean aResult (Standard_True); |
405 | |
406 | for (; aStlIter != aCollec1->cend(); ++aStlIter, aOccIter.Next()) |
407 | { |
408 | if (static_cast<T> (2) * aOccIter.Value() != *aStlIter) |
409 | aResult = Standard_False; |
410 | } |
411 | |
412 | if (aOccIter.More()) |
413 | { |
414 | aResult = Standard_False; |
415 | } |
416 | |
417 | delete aCollec1; |
418 | delete aCollec2; |
419 | |
420 | return aResult; |
421 | } |
422 | |
79a35943 |
423 | //======================================================================= |
424 | //function : TestMapIteration |
425 | //purpose : |
426 | //======================================================================= |
427 | template<class CollectionType, class T> |
428 | Standard_Boolean TestMapIteration() |
429 | { |
430 | CollectionType* aCollec (NULL); |
431 | |
432 | MapFiller<CollectionType, T>::Perform (&aCollec); |
433 | |
434 | // create OCCT-style iterator |
435 | typename CollectionType::Iterator aOccIter (*aCollec); |
436 | |
437 | // create STL-compatible iterator |
438 | typename CollectionType::const_iterator aStlIter = aCollec->cbegin(); |
439 | |
440 | Standard_Boolean aResult (Standard_True); |
441 | |
442 | for (; aStlIter != aCollec->cend(); ++aStlIter, aOccIter.Next()) |
443 | { |
444 | if (aOccIter.Value() != *aStlIter) |
445 | aResult = Standard_False; |
446 | } |
447 | |
448 | if (aOccIter.More()) |
449 | { |
450 | aResult = Standard_False; |
451 | } |
452 | |
453 | delete aCollec; |
454 | |
455 | return aResult; |
456 | } |
457 | |
458 | //======================================================================= |
459 | //function : TestForwardIterator |
460 | //purpose : test basic features of iterator (forward) |
461 | //======================================================================= |
462 | template <class CollectionType> |
463 | void TestForwardIterator () |
464 | { |
465 | CollectionType* aCollec (NULL); |
466 | |
467 | CollectionFiller<CollectionType, void>::Perform (&aCollec); |
468 | |
469 | // test non-const iteration |
470 | typename CollectionType::iterator it = aCollec->begin(); // copy construction |
471 | typename CollectionType::iterator it2; // default constructor |
472 | it2 = it; // assignment |
473 | it2 = it++; // postfix increment |
474 | if (it2 == it || ! (it2 != it)) |
475 | std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl; |
476 | it2 = ++it; // prefix increment |
477 | if (it2 != it || ! (it2 == it)) |
478 | std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl; |
479 | |
480 | typename CollectionType::iterator::value_type t = *it; |
481 | *it2 = t; |
482 | *(it2.operator-> ()) = t; |
483 | |
484 | // test const iteration |
485 | typename CollectionType::const_iterator cit = aCollec->cbegin(); // copy construction |
486 | typename CollectionType::const_iterator cit2; // default constructor |
487 | cit2 = cit; // assignment |
488 | cit2 = cit++; // postfix increment |
489 | if (cit2 == cit || ! (cit2 != cit)) |
490 | std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl; |
491 | cit2 = ++cit; // prefix increment |
492 | if (cit2 != it || ! (cit2 == cit)) |
493 | std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl; |
494 | |
495 | typename CollectionType::const_iterator::value_type ct = *cit; |
496 | ct = *cit; |
5b111128 |
497 | (void)ct; |
79a35943 |
498 | // *cit2 = ct; |
499 | // *(cit2.operator-> ()) = t; |
500 | |
501 | delete aCollec; |
502 | } |
503 | |
504 | //======================================================================= |
505 | //function : TestBidirIterator |
506 | //purpose : test features of bidirectional iterator |
507 | //======================================================================= |
508 | template <class CollectionType> |
509 | void TestBidirIterator () |
510 | { |
511 | CollectionType* aCollec (NULL); |
512 | |
513 | CollectionFiller<CollectionType, void>::Perform (&aCollec); |
514 | |
515 | // test non-const iteration |
516 | typename CollectionType::iterator it = aCollec->end(); // copy construction |
517 | typename CollectionType::iterator it2 = it--; // postfix decrement |
518 | if (it2 == it || ! (it2 != it)) |
519 | std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl; |
520 | it2 = --it; // prefix decrement |
521 | if (it2 != it || ! (it2 == it)) |
522 | std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl; |
523 | |
524 | delete aCollec; |
525 | } |
526 | |
527 | //======================================================================= |
528 | //function : TestRandomIterator |
529 | //purpose : test features of random iterator |
530 | //======================================================================= |
531 | template <class CollectionType> |
532 | void TestRandomIterator () |
533 | { |
534 | CollectionType* aCollec (NULL); |
535 | |
536 | CollectionFiller<CollectionType, void>::Perform (&aCollec); |
537 | |
538 | // test non-const iteration |
539 | typename CollectionType::iterator it = aCollec->begin(); // copy construction |
540 | typename CollectionType::iterator it2 = it + 5; |
541 | if ((it2 - it) != 5) |
542 | std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl; |
543 | if (it2 < it || it2 <= it || ! (it2 > it) || ! (it2 >= it)) |
544 | std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl; |
545 | it += 5; |
546 | if (it2 != it) |
547 | std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl; |
548 | it2 = it - 5; |
549 | if ((it2 - it) != -5) |
550 | std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl; |
551 | if (it2 > it || it2 >= it || ! (it2 < it) || ! (it2 <= it)) |
552 | std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl; |
553 | it -= 5; |
554 | if (it2 != it) |
555 | std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl; |
556 | |
557 | typename CollectionType::value_type t = it[5]; // offset dereference |
558 | *it = t; |
559 | |
560 | delete aCollec; |
561 | } |
562 | |
563 | //======================================================================= |
564 | //function : QANListStlIterator |
565 | //purpose : |
566 | //======================================================================= |
567 | static Standard_Integer QANListStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
568 | { |
569 | // compile-time tests |
570 | TestForwardIterator <NCollection_List<Standard_Integer> >(); |
571 | |
572 | // run-time tests |
573 | Standard_Boolean aResult = TestIteration<NCollection_List<int>, std::list<int> >(); |
574 | std::cout << "NCollection_List<int> Iteration: " << |
575 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
576 | |
577 | aResult = TestIteration<NCollection_List<double>, std::list<double> >(); |
578 | std::cout << "NCollection_List<double> Iteration: " << |
579 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
580 | |
581 | aResult = TestMinMax<NCollection_List<int>, std::list<int> >(); |
582 | std::cout << "NCollection_List<int> Min-Max: " << |
583 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
584 | |
585 | aResult = TestMinMax<NCollection_List<double>, std::list<double> >(); |
586 | std::cout << "NCollection_List<double> Min-Max: " << |
587 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
588 | |
589 | aResult = TestReplace<NCollection_List<int>, std::list<int> >(); |
590 | std::cout << "NCollection_List<int> Replace: " << |
591 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
592 | |
593 | aResult = TestReplace<NCollection_List<double>, std::list<double> >(); |
594 | std::cout << "NCollection_List<double> Replace: " << |
595 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
596 | |
c7b59798 |
597 | aResult = TestParallel< NCollection_List<int>, std::list<int> >(); |
598 | std::cout << "NCollection_List<int> Parallel: " << |
79a35943 |
599 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
600 | |
c7b59798 |
601 | aResult = TestParallel<NCollection_List<double>, std::list<double> >(); |
602 | std::cout << "NCollection_List<double> Parallel: " << |
79a35943 |
603 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
604 | |
79a35943 |
605 | return 0; |
606 | } |
607 | |
608 | //======================================================================= |
609 | //function : QANMapStlIterator |
610 | //purpose : |
611 | //======================================================================= |
612 | static Standard_Integer QANMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
613 | { |
614 | // compile-time tests |
615 | // TestForwardIterator <NCollection_Map<Standard_Integer> >(); |
616 | |
617 | // run-time tests |
618 | Standard_Boolean aResult = TestMapIteration<NCollection_Map<Standard_Integer>, Standard_Integer>(); |
619 | std::cout << "NCollection_Map<int> Iteration: " << |
620 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
621 | |
622 | aResult = TestMapIteration<NCollection_Map<Standard_Real>, Standard_Real>(); |
623 | std::cout << "NCollection_Map<double> Iteration: " << |
624 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
625 | |
626 | return 0; |
627 | } |
628 | |
629 | //======================================================================= |
630 | //function : QANIndexedMapStlIterator |
631 | //purpose : |
632 | //======================================================================= |
633 | static Standard_Integer QANIndexedMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
634 | { |
635 | // compile-time tests |
636 | // TestForwardIterator <NCollection_IndexedMap<Standard_Integer> >(); |
637 | // TestBidirIterator <NCollection_IndexedMap<Standard_Integer> >(); |
638 | |
639 | // run-time tests |
640 | Standard_Boolean aResult = TestMapIteration<NCollection_IndexedMap<Standard_Integer>, Standard_Integer>(); |
641 | std::cout << "NCollection_IndexedMap<int> Iteration: " << |
642 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
643 | |
644 | aResult = TestMapIteration<NCollection_IndexedMap<Standard_Real>, Standard_Real>(); |
645 | std::cout << "NCollection_IndexedMap<double> Iteration: " << |
646 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
647 | |
648 | return 0; |
649 | } |
650 | |
651 | //======================================================================= |
652 | //function : QANDataMapStlIterator |
653 | //purpose : |
654 | //======================================================================= |
655 | static Standard_Integer QANDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
656 | { |
657 | // compile-time tests |
658 | // TestForwardIterator <NCollection_DataMap<int, int> >(); |
659 | // TestBidirIterator <NCollection_DataMap<int, int> >(); |
660 | |
661 | // run-time tests |
662 | Standard_Boolean aResult = TestMapIteration<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>(); |
663 | std::cout << "NCollection_DataMap<int> Iteration: " << |
664 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
665 | |
666 | aResult = TestMapIteration<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>(); |
667 | std::cout << "NCollection_DataMap<double> Iteration: " << |
668 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
669 | |
c7b59798 |
670 | aResult = TestDataMapParallel<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>(); |
671 | std::cout << "NCollection_DataMap<int> Parallel: " << |
79a35943 |
672 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
673 | |
c7b59798 |
674 | aResult = TestDataMapParallel<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>(); |
675 | std::cout << "NCollection_DataMap<double> Parallel: " << |
79a35943 |
676 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
677 | |
79a35943 |
678 | return 0; |
679 | } |
680 | |
681 | //======================================================================= |
682 | //function : QANIndexedDataMapStlIterator |
683 | //purpose : |
684 | //======================================================================= |
685 | static Standard_Integer QANIndexedDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
686 | { |
687 | // compile-time tests |
688 | // TestForwardIterator <NCollection_IndexedDataMap<int, int> >(); |
689 | // TestBidirIterator <NCollection_IndexedDataMap<int, int> >(); |
690 | |
691 | // run-time tests |
692 | Standard_Boolean aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>(); |
693 | std::cout << "NCollection_IndexedDataMap<int> Iteration: " << |
694 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
695 | |
696 | aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>(); |
697 | std::cout << "NCollection_IndexedDataMap<double> Iteration: " << |
698 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
699 | |
c7b59798 |
700 | aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>(); |
701 | std::cout << "NCollection_IndexedDataMap<int> Parallel: " << |
79a35943 |
702 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
703 | |
c7b59798 |
704 | aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>(); |
705 | std::cout << "NCollection_IndexedDataMap<double> Parallel: " << |
79a35943 |
706 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
707 | |
79a35943 |
708 | return 0; |
709 | } |
710 | |
711 | //======================================================================= |
712 | //function : QANSequenceStlIterator |
713 | //purpose : |
714 | //======================================================================= |
715 | static Standard_Integer QANSequenceStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
716 | { |
717 | // compile-time tests |
718 | TestForwardIterator <NCollection_Sequence<int> >(); |
719 | TestBidirIterator <NCollection_Sequence<int> >(); |
720 | |
721 | // run-time tests |
722 | Standard_Boolean aResult = TestIteration<NCollection_Sequence<int>, std::list<int> >(); |
723 | std::cout << "NCollection_Sequence<int> Iteration: " << |
724 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
725 | |
726 | aResult = TestIteration<NCollection_Sequence<double>, std::list<double> >(); |
727 | std::cout << "NCollection_Sequence<double> Iteration: " << |
728 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
729 | |
730 | aResult = TestMinMax<NCollection_Sequence<int>, std::list<int> >(); |
731 | std::cout << "NCollection_Sequence<int> Min-Max: " << |
732 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
733 | |
734 | aResult = TestMinMax<NCollection_Sequence<double>, std::list<double> >(); |
735 | std::cout << "NCollection_Sequence<double> Min-Max: " << |
736 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
737 | |
738 | aResult = TestReplace<NCollection_Sequence<int>, std::list<int> >(); |
739 | std::cout << "NCollection_Sequence<int> Replace: " << |
740 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
741 | |
742 | aResult = TestReplace<NCollection_Sequence<double>, std::list<double> >(); |
743 | std::cout << "NCollection_Sequence<double> Replace: " << |
744 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
745 | |
746 | aResult = TestReverse<NCollection_Sequence<int>, std::list<int> >(); |
747 | std::cout << "NCollection_Sequence<int> Reverse: " << |
748 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
749 | |
750 | aResult = TestReverse<NCollection_Sequence<double>, std::list<double> >(); |
751 | std::cout << "NCollection_Sequence<double> Reverse: " << |
752 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
753 | |
c7b59798 |
754 | aResult = TestParallel<NCollection_Sequence<int>, std::list<int> >(); |
755 | std::cout << "NCollection_Sequence<int> Parallel: " << |
79a35943 |
756 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
757 | |
c7b59798 |
758 | aResult = TestParallel<NCollection_Sequence<double>, std::list<double> >(); |
759 | std::cout << "NCollection_Sequence<double> Parallel: " << |
79a35943 |
760 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
761 | |
79a35943 |
762 | return 0; |
763 | } |
764 | |
765 | //======================================================================= |
766 | //function : QANVectorStlIterator |
767 | //purpose : |
768 | //======================================================================= |
769 | static Standard_Integer QANVectorStlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
770 | { |
771 | // compile-time tests |
772 | TestForwardIterator <NCollection_Vector<int> >(); |
773 | TestBidirIterator <NCollection_Vector<int> >(); |
774 | TestRandomIterator <NCollection_Vector<int> >(); |
775 | |
776 | // run-time tests |
777 | Standard_Boolean aResult = TestIteration<NCollection_Vector<int>, std::vector<int> >(); |
778 | std::cout << "NCollection_Vector<int> Iteration: " << |
779 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
780 | |
781 | aResult = TestIteration<NCollection_Vector<double>, std::vector<double> >(); |
782 | std::cout << "NCollection_Vector<double> Iteration: " << |
783 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
784 | |
785 | aResult = TestMinMax<NCollection_Vector<int>, std::vector<int> >(); |
786 | std::cout << "NCollection_Vector<int> Min-Max: " << |
787 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
788 | |
789 | aResult = TestMinMax<NCollection_Vector<double>, std::vector<double> >(); |
790 | std::cout << "NCollection_Vector<double> Min-Max: " << |
791 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
792 | |
793 | aResult = TestReplace<NCollection_Vector<int>, std::vector<int> >(); |
794 | std::cout << "NCollection_Vector<int> Replace: " << |
795 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
796 | |
797 | aResult = TestReplace<NCollection_Vector<double>, std::vector<double> >(); |
798 | std::cout << "NCollection_Vector<double> Replace: " << |
799 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
800 | |
801 | aResult = TestReverse<NCollection_Vector<int>, std::vector<int> >(); |
802 | std::cout << "NCollection_Vector<int> Reverse: " << |
803 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
804 | |
805 | aResult = TestReverse<NCollection_Vector<double>, std::vector<double> >(); |
806 | std::cout << "NCollection_Vector<double> Reverse: " << |
807 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
808 | |
809 | aResult = TestSort<NCollection_Vector<int>, std::vector<int> >(); |
810 | std::cout << "NCollection_Vector<int> Sort: " << |
811 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
812 | |
813 | aResult = TestSort<NCollection_Vector<double>, std::vector<double> >(); |
814 | std::cout << "NCollection_Vector<double> Sort: " << |
815 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
816 | |
c7b59798 |
817 | aResult = TestParallel<NCollection_Vector<int>, std::vector<int> >(); |
818 | std::cout << "NCollection_Vector<int> Parallel: " << |
79a35943 |
819 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
820 | |
c7b59798 |
821 | aResult = TestParallel<NCollection_Vector<double>, std::vector<double> >(); |
822 | std::cout << "NCollection_Vector<double> Parallel: " << |
79a35943 |
823 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
824 | |
0f5ea4b5 |
825 | { |
826 | // Test case for a corner case described in a bug #0027941 |
827 | // when vector length matches the increment. |
828 | // In this case NCollection_Vector::Iterator::Offset() produced mathematically equal |
829 | // but not the same iterator as returned by NCollection_Vector::end() |
830 | // so that their comparison was not equal. |
831 | // As result, std::stable_sort() crashed due to out-of-range access. |
832 | const int THE_INCREMENT = 256; |
833 | NCollection_Vector<int> aVector (THE_INCREMENT); |
834 | for (int anIter = 0; anIter < THE_INCREMENT; ++anIter) |
835 | { |
836 | aVector.Append (THE_INCREMENT - anIter); |
837 | } |
838 | |
839 | NCollection_Vector<int>::iterator aBegin = aVector.begin(); |
840 | NCollection_Vector<int>::iterator anEnd = aVector.end(); |
841 | NCollection_Vector<int>::iterator aShift = aBegin + THE_INCREMENT; |
842 | aResult = (aShift == anEnd); |
843 | std::cout << "NCollection_Vector<int> Offset: " << |
844 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
845 | |
846 | std::stable_sort (aVector.begin(), aVector.end()); |
847 | } |
848 | |
79a35943 |
849 | return 0; |
850 | } |
851 | |
852 | //======================================================================= |
853 | //function : QANArray1StlIterator |
854 | //purpose : |
855 | //======================================================================= |
856 | static Standard_Integer QANArray1StlIterator (Draw_Interpretor&, Standard_Integer, const char**) |
857 | { |
858 | // compile-time tests |
859 | TestForwardIterator <NCollection_Vector<int> >(); |
860 | TestBidirIterator <NCollection_Vector<int> >(); |
861 | TestRandomIterator <NCollection_Vector<int> >(); |
862 | |
863 | // run-time tests |
864 | Standard_Boolean aResult = TestIteration<NCollection_Array1<int>, std::vector<int> >(); |
865 | std::cout << "NCollection_Array1<int> Iteration: " << |
866 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
867 | |
868 | aResult = TestIteration<NCollection_Array1<double>, std::vector<double> >(); |
869 | std::cout << "NCollection_Array1<double> Iteration: " << |
870 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
871 | |
872 | aResult = TestMinMax<NCollection_Array1<int>, std::vector<int> >(); |
873 | std::cout << "NCollection_Array1<int> Min-Max: " << |
874 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
875 | |
876 | aResult = TestMinMax<NCollection_Array1<double>, std::vector<double> >(); |
877 | std::cout << "NCollection_Array1<double> Min-Max: " << |
878 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
879 | |
880 | aResult = TestReplace<NCollection_Array1<int>, std::vector<int> >(); |
881 | std::cout << "NCollection_Array1<int> Replace: " << |
882 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
883 | |
884 | aResult = TestReplace<NCollection_Array1<double>, std::vector<double> >(); |
885 | std::cout << "NCollection_Array1<double> Replace: " << |
886 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
887 | |
888 | aResult = TestReverse<NCollection_Array1<int>, std::vector<int> >(); |
889 | std::cout << "NCollection_Array1<int> Reverse: " << |
890 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
891 | |
892 | aResult = TestReverse<NCollection_Array1<double>, std::vector<double> >(); |
893 | std::cout << "NCollection_Array1<double> Reverse: " << |
894 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
895 | |
896 | aResult = TestSort<NCollection_Array1<int>, std::vector<int> >(); |
897 | std::cout << "NCollection_Array1<int> Sort: " << |
898 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
899 | |
900 | aResult = TestSort<NCollection_Array1<double>, std::vector<double> >(); |
901 | std::cout << "NCollection_Array1<double> Sort: " << |
902 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
903 | |
c7b59798 |
904 | aResult = TestParallel<NCollection_Array1<int>, std::vector<int> >(); |
905 | std::cout << "NCollection_Array1<int> Parallel: " << |
79a35943 |
906 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
907 | |
c7b59798 |
908 | aResult = TestParallel<NCollection_Array1<double>, std::vector<double> >(); |
909 | std::cout << "NCollection_Array1<double> Parallel: " << |
79a35943 |
910 | (aResult ? "SUCCESS" : "FAIL") << std::endl; |
911 | |
79a35943 |
912 | return 0; |
913 | } |
914 | |
915 | //======================================================================= |
916 | //function : QANTestStlIterators |
917 | //purpose : |
918 | //======================================================================= |
919 | static Standard_Integer QANTestStlIterators ( |
920 | Draw_Interpretor& theInterpretor, Standard_Integer, const char**) |
921 | { |
922 | QANListStlIterator (theInterpretor, 0, NULL); |
923 | QANArray1StlIterator (theInterpretor, 0, NULL); |
924 | QANVectorStlIterator (theInterpretor, 0, NULL); |
925 | QANSequenceStlIterator (theInterpretor, 0, NULL); |
926 | QANMapStlIterator (theInterpretor, 0, NULL); |
927 | QANDataMapStlIterator (theInterpretor, 0, NULL); |
928 | QANIndexedMapStlIterator (theInterpretor, 0, NULL); |
929 | QANIndexedDataMapStlIterator (theInterpretor, 0, NULL); |
930 | |
931 | return 0; |
932 | } |
933 | |
934 | //======================================================================= |
935 | //function : TestPerformanceRandomIterator |
936 | //purpose : |
937 | //======================================================================= |
938 | template<class CollectionType, class StlType> |
618617fe |
939 | void TestPerformanceRandomIterator(Draw_Interpretor& di) |
79a35943 |
940 | { |
941 | OSD_Timer aTimer; |
942 | |
943 | StlType* aVector (NULL); |
944 | CollectionType* aCollec (NULL); |
945 | |
946 | for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2) |
947 | { |
948 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize); |
949 | |
950 | aTimer.Reset(); |
951 | aTimer.Start(); |
952 | { |
953 | RandomGenerator aRandomGen; |
954 | for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx) |
955 | { |
956 | std::sort (aVector->begin(), aVector->end()); |
957 | std::random_shuffle (aVector->begin(), aVector->end(), aRandomGen); |
958 | } |
959 | } |
960 | aTimer.Stop(); |
961 | |
962 | Standard_Real aStlTime = aTimer.ElapsedTime(); |
963 | |
964 | aTimer.Reset(); |
965 | aTimer.Start(); |
966 | { |
967 | RandomGenerator aRandomGen; |
968 | for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx) |
969 | { |
970 | std::sort (aCollec->begin(), aCollec->end()); |
971 | std::random_shuffle (aCollec->begin(), aCollec->end(), aRandomGen); |
972 | } |
973 | } |
974 | aTimer.Stop(); |
975 | |
976 | Standard_Real aOccTime = aTimer.ElapsedTime(); |
977 | |
618617fe |
978 | di << aSize << "\t" << aStlTime << "\t" << |
979 | aOccTime << "\t" << aOccTime / aStlTime << "\n"; |
79a35943 |
980 | |
981 | // check that result is the same |
982 | if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) |
586db386 |
983 | di << "Error: sequences are not the same at the end!\n"; |
79a35943 |
984 | |
985 | delete aVector; |
986 | delete aCollec; |
987 | } |
988 | } |
989 | |
990 | //======================================================================= |
991 | //function : TestPerformanceForwardIterator |
992 | //purpose : |
993 | //======================================================================= |
994 | template<class CollectionType, class StlType> |
618617fe |
995 | void TestPerformanceForwardIterator(Draw_Interpretor& di) |
79a35943 |
996 | { |
997 | OSD_Timer aTimer; |
998 | |
999 | StlType* aVector = 0; |
1000 | CollectionType* aCollec = 0; |
1001 | |
1002 | for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2) |
1003 | { |
1004 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize); |
1005 | |
1006 | aTimer.Reset(); |
1007 | aTimer.Start(); |
1008 | { |
1009 | for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx) |
1010 | { |
1011 | std::replace (aVector->begin(), aVector->end(), *aVector->begin(), static_cast<typename StlType::value_type> (anIdx)); |
1012 | } |
1013 | } |
1014 | aTimer.Stop(); |
1015 | |
1016 | Standard_Real aStlTime = aTimer.ElapsedTime(); |
1017 | |
1018 | aTimer.Reset(); |
1019 | aTimer.Start(); |
1020 | { |
1021 | for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx) |
1022 | { |
1023 | std::replace (aCollec->begin(), aCollec->end(), *aCollec->begin(), static_cast<typename CollectionType::value_type> (anIdx)); |
1024 | } |
1025 | } |
1026 | aTimer.Stop(); |
1027 | |
1028 | Standard_Real aOccTime = aTimer.ElapsedTime(); |
1029 | |
618617fe |
1030 | di << aSize << "\t" << aStlTime << "\t" << |
1031 | aOccTime << "\t" << aOccTime / aStlTime << "\n"; |
79a35943 |
1032 | |
1033 | // check that result is the same |
1034 | if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) |
586db386 |
1035 | di << "Error: sequences are not the same at the end!\n"; |
79a35943 |
1036 | |
1037 | delete aVector; |
1038 | delete aCollec; |
1039 | } |
1040 | } |
1041 | |
1042 | //======================================================================= |
1043 | //function : TestPerformanceBidirIterator |
1044 | //purpose : |
1045 | //======================================================================= |
1046 | template<class CollectionType, class StlType> |
618617fe |
1047 | void TestPerformanceBidirIterator(Draw_Interpretor& di) |
79a35943 |
1048 | { |
1049 | OSD_Timer aTimer; |
1050 | |
1051 | StlType* aVector = 0; |
1052 | CollectionType* aCollec = 0; |
1053 | |
1054 | for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2) |
1055 | { |
1056 | CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize); |
1057 | |
1058 | aTimer.Reset(); |
1059 | aTimer.Start(); |
1060 | { |
1061 | for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx) |
1062 | { |
1063 | std::reverse (aVector->begin(), aVector->end()); |
1064 | } |
1065 | } |
1066 | aTimer.Stop(); |
1067 | |
1068 | Standard_Real aStlTime = aTimer.ElapsedTime(); |
1069 | |
1070 | aTimer.Reset(); |
1071 | aTimer.Start(); |
1072 | { |
1073 | for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx) |
1074 | { |
1075 | std::reverse (aCollec->begin(), aCollec->end()); |
1076 | } |
1077 | } |
1078 | aTimer.Stop(); |
1079 | |
1080 | Standard_Real aOccTime = aTimer.ElapsedTime(); |
1081 | |
618617fe |
1082 | di << aSize << "\t" << aStlTime << "\t" << |
1083 | aOccTime << "\t" << aOccTime / aStlTime << "\n"; |
79a35943 |
1084 | |
1085 | // check that result is the same |
1086 | if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) |
586db386 |
1087 | di << "Error: sequences are not the same at the end!\n"; |
79a35943 |
1088 | |
1089 | delete aVector; |
1090 | delete aCollec; |
1091 | } |
1092 | } |
1093 | |
1094 | //======================================================================= |
1095 | //function : TestPerformanceMapAccess |
1096 | //purpose : |
1097 | //======================================================================= |
1098 | template<class CollectionType, class T> |
618617fe |
1099 | void TestPerformanceMapAccess(Draw_Interpretor& di) |
79a35943 |
1100 | { |
1101 | OSD_Timer aTimer; |
1102 | |
1103 | CollectionType* aCollec (NULL); |
1104 | |
1105 | for (Standard_Integer aSize = 100000; aSize <= 3200000; aSize *= 2) |
1106 | { |
1107 | MapFiller<CollectionType, T>::Perform (&aCollec, aSize); |
1108 | |
1109 | std::set<T> aSet (aCollec->cbegin(), aCollec->cend()); |
1110 | std::vector<T> aVec (aCollec->cbegin(), aCollec->cend()); |
1111 | |
1112 | Standard_Boolean aResult = Standard_True; |
1113 | |
1114 | aTimer.Reset(); |
1115 | aTimer.Start(); |
1116 | { |
1117 | for (Standard_Integer anIdx = 0; anIdx < 10000; ++anIdx) |
1118 | { |
1119 | if (aSet.find (aVec[anIdx + 1000]) == aSet.end()) |
1120 | aResult = Standard_False; |
1121 | if (aSet.find (aVec[anIdx + 2000]) == aSet.end()) |
1122 | aResult = Standard_False; |
1123 | if (aSet.find (aVec[anIdx + 3000]) == aSet.end()) |
1124 | aResult = Standard_False; |
1125 | if (aSet.find (aVec[anIdx + 4000]) == aSet.end()) |
1126 | aResult = Standard_False; |
1127 | if (aSet.find (aVec[anIdx + 5000]) == aSet.end()) |
1128 | aResult = Standard_False; |
1129 | } |
1130 | } |
1131 | aTimer.Stop(); |
1132 | |
1133 | Standard_Real aStlTime = aTimer.ElapsedTime(); |
1134 | |
1135 | aTimer.Reset(); |
1136 | aTimer.Start(); |
1137 | { |
1138 | for (Standard_Integer anIdx = 0; anIdx < 10000; ++anIdx) |
1139 | { |
1140 | if (!aCollec->Contains (aVec[anIdx + 1000])) |
1141 | aResult = Standard_False; |
1142 | if (!aCollec->Contains (aVec[anIdx + 2000])) |
1143 | aResult = Standard_False; |
1144 | if (!aCollec->Contains (aVec[anIdx + 3000])) |
1145 | aResult = Standard_False; |
1146 | if (!aCollec->Contains (aVec[anIdx + 4000])) |
1147 | aResult = Standard_False; |
1148 | if (!aCollec->Contains (aVec[anIdx + 5000])) |
1149 | aResult = Standard_False; |
1150 | } |
1151 | } |
1152 | aTimer.Stop(); |
1153 | |
1154 | Standard_Real aOccTime = aTimer.ElapsedTime(); |
1155 | |
1156 | if (aResult) |
1157 | { |
618617fe |
1158 | di << aSize << "\t" << aStlTime << "\t" << |
1159 | aOccTime << "\t" << (aStlTime > 1e-16 ? aOccTime / aStlTime : -1) << "\n"; |
79a35943 |
1160 | } |
1161 | |
1162 | delete aCollec; |
1163 | } |
1164 | } |
1165 | |
1166 | //======================================================================= |
1167 | //function : QANTestNCollectionPerformance |
1168 | //purpose : |
1169 | //======================================================================= |
618617fe |
1170 | static Standard_Integer QANTestNCollectionPerformance (Draw_Interpretor& di, Standard_Integer, const char**) |
79a35943 |
1171 | { |
586db386 |
1172 | di << "Testing performance (Size | STL time | OCCT time | STL/OCCT boost)\n"; |
79a35943 |
1173 | |
586db386 |
1174 | di << "\nstd::vector vs NCollection_Array1 (sort):\n\n"; |
618617fe |
1175 | TestPerformanceRandomIterator<NCollection_Array1<double>, std::vector<double> >(di); |
9e506120 |
1176 | |
586db386 |
1177 | di << "\nstd::vector vs NCollection_Vector (sort):\n\n"; |
618617fe |
1178 | TestPerformanceRandomIterator<NCollection_Vector<double>, std::vector<double> >(di); |
9e506120 |
1179 | |
586db386 |
1180 | di << "\nstd::vector vs NCollection_Array1 (replace):\n\n"; |
618617fe |
1181 | TestPerformanceForwardIterator<NCollection_Array1<double>, std::vector<double> >(di); |
9e506120 |
1182 | |
586db386 |
1183 | di << "\nstd::vector vs NCollection_Vector (replace):\n\n"; |
618617fe |
1184 | TestPerformanceForwardIterator<NCollection_Vector<double>, std::vector<double> >(di); |
79a35943 |
1185 | |
586db386 |
1186 | di << "\nstd::list vs NCollection_List (replace):\n\n"; |
618617fe |
1187 | TestPerformanceForwardIterator<NCollection_List<double>, std::list<double> >(di); |
79a35943 |
1188 | |
586db386 |
1189 | di << "\nstd::list vs NCollection_Sequence (replace):\n\n"; |
618617fe |
1190 | TestPerformanceForwardIterator<NCollection_Sequence<double>, std::list<double> >(di); |
79a35943 |
1191 | |
586db386 |
1192 | di << "\nstd::list vs NCollection_Sequence (reverse):\n\n"; |
618617fe |
1193 | TestPerformanceBidirIterator<NCollection_Sequence<double>, std::list<double> >(di); |
79a35943 |
1194 | |
586db386 |
1195 | di << "\nstd::set vs NCollection_Map (search):\n\n"; |
618617fe |
1196 | TestPerformanceMapAccess<NCollection_Map<int>, int>(di); |
79a35943 |
1197 | |
586db386 |
1198 | di << "\nstd::set vs NCollection_IndexedMap (search):\n\n"; |
618617fe |
1199 | TestPerformanceMapAccess<NCollection_IndexedMap<int>, int>(di); |
9e506120 |
1200 | |
1201 | return 0; |
1202 | } |
1203 | |
1204 | //======================================================================= |
1205 | //function : QANTestNCollectionIndexedMap |
1206 | //purpose : |
1207 | //======================================================================= |
1208 | static Standard_Integer QANTestNCollectionIndexedMap (Draw_Interpretor& di, Standard_Integer, const char**) |
1209 | { |
1210 | OSD_Timer aTimer; |
1211 | |
1212 | std::vector<Standard_Integer> aIndxs; |
1213 | std::vector<Standard_Integer> aItems; |
1214 | NCollection_IndexedMap<Standard_Integer> aIndxMap; |
1215 | |
1216 | const Standard_Integer aNbItems = 1000000; |
1217 | |
1218 | srand (1); |
1219 | for (Standard_Integer anId = 1; anId <= aNbItems; ++anId) |
1220 | { |
1221 | const Standard_Integer aVal = anId * 2; |
1222 | |
1223 | aIndxs.push_back (anId); |
1224 | aItems.push_back (aVal); |
1225 | |
1226 | aIndxMap.Add (aVal); |
1227 | } |
1228 | |
1229 | aTimer.Start(); |
1230 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1231 | { |
1232 | if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId]) |
1233 | { |
1234 | std::cout << "failed FindIndex\n"; |
1235 | } |
1236 | |
1237 | if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId]) |
1238 | { |
1239 | std::cout << "failed FindKey\n"; |
1240 | } |
1241 | } |
1242 | aTimer.Stop(); |
1243 | |
1244 | const Standard_Real aTime1 = aTimer.ElapsedTime(); |
1245 | |
1246 | aTimer.Reset(); |
1247 | aTimer.Start(); |
1248 | for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId) |
1249 | { |
1250 | const Standard_Integer anId2 = Min (aNbItems - 1, |
1251 | static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems)); |
1252 | |
1253 | aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]); |
1254 | |
1255 | std::swap (aIndxs[anId], aIndxs[anId2]); |
1256 | } |
1257 | aTimer.Stop(); |
1258 | |
1259 | const Standard_Real aTime2 = aTimer.ElapsedTime(); |
1260 | |
1261 | aTimer.Reset(); |
1262 | aTimer.Start(); |
1263 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1264 | { |
1265 | if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId]) |
1266 | { |
1267 | std::cout << "failed FindIndex\n"; |
1268 | } |
1269 | |
1270 | if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId]) |
1271 | { |
1272 | std::cout << "failed FindKey\n"; |
1273 | } |
1274 | } |
1275 | aTimer.Stop(); |
1276 | |
1277 | const Standard_Real aTime3 = aTimer.ElapsedTime(); |
1278 | |
1279 | aTimer.Reset(); |
1280 | aTimer.Start(); |
1281 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1282 | { |
1283 | aIndxMap.RemoveLast(); |
1284 | } |
1285 | aTimer.Stop(); |
1286 | |
1287 | const Standard_Real aTime4 = aTimer.ElapsedTime(); |
1288 | |
1289 | di << "Search time 1: " << aTime1 << "\n" |
1290 | << "Swapping time: " << aTime2 << "\n" |
1291 | << "Search time 2: " << aTime3 << "\n" |
1292 | << "Remove time: " << aTime4 << "\n"; |
1293 | |
1294 | return 0; |
1295 | } |
1296 | |
1297 | //======================================================================= |
1298 | //function : QANTestNCollectionIndexedDataMap |
1299 | //purpose : |
1300 | //======================================================================= |
1301 | static Standard_Integer QANTestNCollectionIndexedDataMap (Draw_Interpretor& di, Standard_Integer, const char**) |
1302 | { |
1303 | OSD_Timer aTimer; |
1304 | |
1305 | std::vector<Standard_Integer> aIndxs; |
1306 | std::vector<Standard_Integer> aItems; |
1307 | NCollection_IndexedDataMap<Standard_Integer, Standard_Integer> aIndxMap; |
1308 | |
1309 | const Standard_Integer aNbItems = 1000000; |
1310 | |
1311 | srand (1); |
1312 | for (Standard_Integer anId = 1; anId <= aNbItems; ++anId) |
1313 | { |
1314 | const Standard_Integer aVal = anId * 2; |
1315 | |
1316 | aIndxs.push_back (anId); |
1317 | aItems.push_back (aVal); |
1318 | |
1319 | aIndxMap.Add (aVal, aVal * 2); |
1320 | } |
1321 | |
1322 | aTimer.Start(); |
1323 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1324 | { |
1325 | if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId]) |
1326 | { |
1327 | std::cout << "failed FindIndex\n"; |
1328 | } |
1329 | |
1330 | if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId]) |
1331 | { |
1332 | std::cout << "failed FindKey\n"; |
1333 | } |
1334 | } |
1335 | aTimer.Stop(); |
1336 | |
1337 | const Standard_Real aTime1 = aTimer.ElapsedTime(); |
1338 | |
1339 | aTimer.Reset(); |
1340 | aTimer.Start(); |
1341 | for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId) |
1342 | { |
1343 | const Standard_Integer anId2 = Min (aNbItems - 1, |
1344 | static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems)); |
1345 | |
1346 | aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]); |
1347 | |
1348 | std::swap (aIndxs[anId], aIndxs[anId2]); |
1349 | } |
1350 | aTimer.Stop(); |
1351 | |
1352 | const Standard_Real aTime2 = aTimer.ElapsedTime(); |
1353 | |
1354 | aTimer.Reset(); |
1355 | aTimer.Start(); |
1356 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1357 | { |
1358 | if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId]) |
1359 | { |
1360 | std::cout << "failed FindIndex\n"; |
1361 | } |
1362 | |
1363 | if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId]) |
1364 | { |
1365 | std::cout << "failed FindKey\n"; |
1366 | } |
1367 | } |
1368 | aTimer.Stop(); |
1369 | |
1370 | const Standard_Real aTime3 = aTimer.ElapsedTime(); |
1371 | |
1372 | aTimer.Reset(); |
1373 | aTimer.Start(); |
1374 | for (Standard_Integer anId = 0; anId < aNbItems; ++anId) |
1375 | { |
1376 | aIndxMap.RemoveLast(); |
1377 | } |
1378 | aTimer.Stop(); |
1379 | |
1380 | const Standard_Real aTime4 = aTimer.ElapsedTime(); |
1381 | |
1382 | di << "Search time 1: " << aTime1 << "\n" |
1383 | << "Swapping time: " << aTime2 << "\n" |
1384 | << "Search time 2: " << aTime3 << "\n" |
1385 | << "Remove time: " << aTime4 << "\n"; |
1386 | |
79a35943 |
1387 | return 0; |
1388 | } |
1389 | |
1390 | //======================================================================= |
1391 | //function : CommandsStl |
1392 | //purpose : |
1393 | //======================================================================= |
1394 | void QANCollection::CommandsStl (Draw_Interpretor& theCommands) |
1395 | { |
1396 | const char* aGroup = "QANCollection"; |
1397 | |
1398 | theCommands.Add ("QANArray1StlIterator", |
1399 | "QANArray1StlIterator", |
1400 | __FILE__, |
1401 | QANArray1StlIterator, |
1402 | aGroup); |
1403 | |
1404 | theCommands.Add ("QANListStlIterator", |
1405 | "QANListStlIterator", |
1406 | __FILE__, |
1407 | QANListStlIterator, |
1408 | aGroup); |
1409 | |
1410 | theCommands.Add ("QANSequenceStlIterator", |
1411 | "QANSequenceStlIterator", |
1412 | __FILE__, |
1413 | QANSequenceStlIterator, |
1414 | aGroup); |
1415 | |
1416 | theCommands.Add ("QANVectorStlIterator", |
1417 | "QANVectorStlIterator", |
1418 | __FILE__, |
1419 | QANVectorStlIterator, |
1420 | aGroup); |
1421 | |
1422 | theCommands.Add ("QANMapStlIterator", |
1423 | "QANMapStlIterator", |
1424 | __FILE__, |
1425 | QANMapStlIterator, |
1426 | aGroup); |
1427 | |
1428 | theCommands.Add ("QANDataMapStlIterator", |
1429 | "QANDataMapStlIterator", |
1430 | __FILE__, |
1431 | QANDataMapStlIterator, |
1432 | aGroup); |
1433 | |
1434 | theCommands.Add ("QANIndexedMapStlIterator", |
1435 | "QANIndexedMapStlIterator", |
1436 | __FILE__, |
1437 | QANIndexedMapStlIterator, |
1438 | aGroup); |
1439 | |
1440 | theCommands.Add ("QANIndexedDataMapStlIterator", |
1441 | "QANIndexedDataMapStlIterator", |
1442 | __FILE__, |
1443 | QANIndexedDataMapStlIterator, |
1444 | aGroup); |
1445 | |
1446 | theCommands.Add ("QANTestStlIterators", |
1447 | "QANTestStlIterators", |
1448 | __FILE__, |
1449 | QANTestStlIterators, |
1450 | aGroup); |
1451 | |
1452 | theCommands.Add ("QANTestNCollectionPerformance", |
1453 | "QANTestNCollectionPerformance", |
1454 | __FILE__, |
1455 | QANTestNCollectionPerformance, |
1456 | aGroup); |
1457 | |
9e506120 |
1458 | theCommands.Add ("QANTestNCollectionIndexedMap", |
1459 | "QANTestNCollectionIndexedMap", |
1460 | __FILE__, |
1461 | QANTestNCollectionIndexedMap, |
1462 | aGroup); |
1463 | |
1464 | theCommands.Add ("QANTestNCollectionIndexedDataMap", |
1465 | "QANTestNCollectionIndexedDataMap", |
1466 | __FILE__, |
1467 | QANTestNCollectionIndexedDataMap, |
1468 | aGroup); |
1469 | |
79a35943 |
1470 | return; |
1471 | } |