1 // Created on: 2014-04-16
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #if defined(_MSC_VER) && ! defined(_SCL_SECURE_NO_WARNINGS)
17 // suppress "std::Equal1" warning suggesting using msvc "Checked Iterators"
18 #define _SCL_SECURE_NO_WARNINGS
21 #include <QANCollection.hxx>
22 #include <Draw_Interpretor.hxx>
24 #include <NCollection_List.hxx>
25 #include <NCollection_Sequence.hxx>
26 #include <NCollection_Vector.hxx>
27 #include <NCollection_Map.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <NCollection_IndexedMap.hxx>
30 #include <NCollection_IndexedDataMap.hxx>
31 #include <OSD_Timer.hxx>
32 #include <OSD_Parallel.hxx>
41 //! Size of test data sets.
42 const int THE_TEST_SIZE = 5000;
44 template<class CollectionType, class StlType>
45 struct CollectionFiller
47 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
49 *theCollec = new CollectionType();
51 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
53 (*theCollec)->Append (rand());
57 static void Perform (StlType** theVector,
58 CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
60 CollectionFiller::Perform (theCollec, theSize);
62 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
66 template<class T, typename StlType>
67 struct CollectionFiller<NCollection_Array1<T>, StlType>
69 static void Perform (NCollection_Array1<T>** theCollec,
70 Standard_Integer theSize = THE_TEST_SIZE)
72 *theCollec = new NCollection_Array1<T> (0, theSize - 1);
74 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
76 (*theCollec)->ChangeValue (anIdx) = rand();
80 static void Perform (StlType** theVector,
81 NCollection_Array1<T>** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
83 CollectionFiller<NCollection_Array1<T>, StlType >::Perform (theCollec, theSize);
85 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
89 template<class CollectionType, class T>
92 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
94 *theCollec = new CollectionType();
96 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
98 (*theCollec)->Add (rand());
104 struct MapFiller<NCollection_DataMap<T, T>, T>
106 static void Perform (NCollection_DataMap<T, T>** theCollec1,
107 NCollection_DataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
109 *theCollec1 = new NCollection_DataMap<T, T>();
111 if (theCollec2 != NULL)
112 *theCollec2 = new NCollection_DataMap<T, T>();
114 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
116 const T aVal1 = rand();
117 const T aVal2 = rand();
119 (*theCollec1)->Bind (aVal1, aVal2);
121 if (theCollec2 != NULL)
122 (*theCollec2)->Bind (aVal1, aVal2);
128 struct MapFiller<NCollection_IndexedDataMap<T, T>, T>
130 static void Perform (NCollection_IndexedDataMap<T, T>** theCollec1,
131 NCollection_IndexedDataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
133 *theCollec1 = new NCollection_IndexedDataMap<T, T>();
135 if (theCollec2 != NULL)
136 *theCollec2 = new NCollection_IndexedDataMap<T, T>();
138 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
140 const T aVal1 = rand();
141 const T aVal2 = rand();
143 (*theCollec1)->Add (aVal1, aVal2);
145 if (theCollec2 != NULL)
146 (*theCollec2)->Add (aVal1, aVal2);
151 //=======================================================================
152 //function : TestIteration
154 //=======================================================================
155 template<class CollectionType, class StlType>
156 Standard_Boolean TestIteration()
158 StlType* aVector (NULL);
159 CollectionType* aCollec (NULL);
161 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
163 typename StlType::iterator aVecIter = aVector->begin();
164 typename CollectionType::iterator aColIter = aCollec->begin();
166 Standard_Boolean aResult (Standard_True);
168 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
170 if (*aVecIter != *aColIter)
171 aResult = Standard_False;
174 if (aColIter != aCollec->end())
176 aResult = Standard_False;
185 //=======================================================================
186 //function : TestMinMax
188 //=======================================================================
189 template<class CollectionType, class StlType>
190 Standard_Boolean TestMinMax()
192 StlType* aVector (NULL);
193 CollectionType* aCollec (NULL);
195 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
197 typename StlType::value_type aValue1 = *std::min_element (aVector->begin(), aVector->end());
198 typename CollectionType::value_type aValue2 = *std::min_element (aCollec->begin(), aCollec->end());
200 Standard_Boolean aResult (Standard_True);
202 if (aValue1 != aValue2)
203 aResult = Standard_False;
205 aValue1 = *std::max_element (aVector->begin(), aVector->end());
206 aValue2 = *std::max_element (aCollec->begin(), aCollec->end());
208 if (aValue1 != aValue2)
209 aResult = Standard_False;
217 //=======================================================================
218 //function : TestReplace
220 //=======================================================================
221 template<class CollectionType, class StlType>
222 Standard_Boolean TestReplace()
224 StlType* aVector (NULL);
225 CollectionType* aCollec (NULL);
227 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
229 const typename StlType::value_type aValue = aVector->back();
231 std::replace (aVector->begin(), aVector->end(), aValue, static_cast<typename StlType::value_type> (-1));
232 std::replace (aCollec->begin(), aCollec->end(), aValue, static_cast<typename CollectionType::value_type> (-1));
234 typename StlType::iterator aVecIter = aVector->begin();
235 typename CollectionType::iterator aColIter = aCollec->begin();
237 Standard_Boolean aResult (Standard_True);
239 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
241 if (*aVecIter != *aColIter)
242 aResult = Standard_False;
245 if (aColIter != aCollec->end())
247 aResult = Standard_False;
256 //=======================================================================
257 //function : TestReverse
259 //=======================================================================
260 template<class CollectionType, class StlType>
261 Standard_Boolean TestReverse()
263 StlType* aVector (NULL);
264 CollectionType* aCollec (NULL);
266 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
268 std::reverse (aVector->begin(), aVector->end());
269 std::reverse (aCollec->begin(), aCollec->end());
271 typename StlType::iterator aVecIter = aVector->begin();
272 typename CollectionType::iterator aColIter = aCollec->begin();
274 Standard_Boolean aResult (Standard_True);
276 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
278 if (*aVecIter != *aColIter)
279 aResult = Standard_False;
282 if (aColIter != aCollec->end())
284 aResult = Standard_False;
293 //=======================================================================
294 //function : TestSort
296 //=======================================================================
297 template<class CollectionType, class StlType>
298 Standard_Boolean TestSort()
300 StlType* aVector (NULL);
301 CollectionType* aCollec (NULL);
303 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
305 std::sort (aVector->begin(), aVector->end());
306 std::sort (aCollec->begin(), aCollec->end());
308 typename StlType::iterator aVecIter = aVector->begin();
309 typename CollectionType::iterator aColIter = aCollec->begin();
311 Standard_Boolean aResult (Standard_True);
313 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
315 if (*aVecIter != *aColIter)
316 aResult = Standard_False;
319 if (aColIter != aCollec->end())
321 aResult = Standard_False;
330 template <typename T>
333 void operator()(T& theValue) const
339 //=======================================================================
340 //function : TestParallel
342 //=======================================================================
343 template<class CollectionType, class StlType>
344 Standard_Boolean TestParallel()
346 StlType* aVector (NULL);
347 CollectionType* aCollec (NULL);
349 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
351 OSD_Parallel::ForEach(aVector->begin(), aVector->end(), Invoker<typename StlType::value_type>());
352 OSD_Parallel::ForEach(aCollec->begin(), aCollec->end(), Invoker<typename CollectionType::value_type>());
354 typename StlType::iterator aVecIter = aVector->begin();
355 typename CollectionType::iterator aColIter = aCollec->begin();
357 Standard_Boolean aResult (Standard_True);
359 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
361 if (*aVecIter != *aColIter)
362 aResult = Standard_False;
365 if (aColIter != aCollec->end())
367 aResult = Standard_False;
376 //=======================================================================
377 //function : TestDataMapParallel
379 //=======================================================================
380 template<class CollectionType, class T>
381 Standard_Boolean TestDataMapParallel()
383 CollectionType* aCollec1 (NULL);
384 CollectionType* aCollec2 (NULL);
386 MapFiller<CollectionType, T>::Perform (&aCollec1, &aCollec2);
388 OSD_Parallel::ForEach(aCollec1->begin(), aCollec1->end(), Invoker<T>());
390 // create OCCT-style iterator
391 typename CollectionType::Iterator aOccIter (*aCollec2);
393 // create STL-compatible iterator
394 typename CollectionType::const_iterator aStlIter = aCollec1->cbegin();
396 Standard_Boolean aResult (Standard_True);
398 for (; aStlIter != aCollec1->cend(); ++aStlIter, aOccIter.Next())
400 if (static_cast<T> (2) * aOccIter.Value() != *aStlIter)
401 aResult = Standard_False;
406 aResult = Standard_False;
415 //=======================================================================
416 //function : TestMapIteration
418 //=======================================================================
419 template<class CollectionType, class T>
420 Standard_Boolean TestMapIteration()
422 CollectionType* aCollec (NULL);
424 MapFiller<CollectionType, T>::Perform (&aCollec);
426 // create OCCT-style iterator
427 typename CollectionType::Iterator aOccIter (*aCollec);
429 // create STL-compatible iterator
430 typename CollectionType::const_iterator aStlIter = aCollec->cbegin();
432 Standard_Boolean aResult (Standard_True);
434 for (; aStlIter != aCollec->cend(); ++aStlIter, aOccIter.Next())
436 if (aOccIter.Value() != *aStlIter)
437 aResult = Standard_False;
442 aResult = Standard_False;
450 //=======================================================================
451 //function : TestForwardIterator
452 //purpose : test basic features of iterator (forward)
453 //=======================================================================
454 template <class CollectionType>
455 void TestForwardIterator ()
457 CollectionType* aCollec (NULL);
459 CollectionFiller<CollectionType, void>::Perform (&aCollec);
461 // test non-const iteration
462 typename CollectionType::iterator it = aCollec->begin(); // copy construction
463 typename CollectionType::iterator it2; // default constructor
464 it2 = it; // assignment
465 it2 = it++; // postfix increment
466 if (it2 == it || ! (it2 != it))
467 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
468 it2 = ++it; // prefix increment
469 if (it2 != it || ! (it2 == it))
470 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
472 typename CollectionType::iterator::value_type t = *it;
474 *(it2.operator-> ()) = t;
476 // test const iteration
477 typename CollectionType::const_iterator cit = aCollec->cbegin(); // copy construction
478 typename CollectionType::const_iterator cit2; // default constructor
479 cit2 = cit; // assignment
480 cit2 = cit++; // postfix increment
481 if (cit2 == cit || ! (cit2 != cit))
482 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
483 cit2 = ++cit; // prefix increment
484 if (cit2 != it || ! (cit2 == cit))
485 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
487 typename CollectionType::const_iterator::value_type ct = *cit;
491 // *(cit2.operator-> ()) = t;
496 //=======================================================================
497 //function : TestBidirIterator
498 //purpose : test features of bidirectional iterator
499 //=======================================================================
500 template <class CollectionType>
501 void TestBidirIterator ()
503 CollectionType* aCollec (NULL);
505 CollectionFiller<CollectionType, void>::Perform (&aCollec);
507 // test non-const iteration
508 typename CollectionType::iterator it = aCollec->end(); // copy construction
509 typename CollectionType::iterator it2 = it--; // postfix decrement
510 if (it2 == it || ! (it2 != it))
511 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
512 it2 = --it; // prefix decrement
513 if (it2 != it || ! (it2 == it))
514 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
519 //=======================================================================
520 //function : TestRandomIterator
521 //purpose : test features of random iterator
522 //=======================================================================
523 template <class CollectionType>
524 void TestRandomIterator ()
526 CollectionType* aCollec (NULL);
528 CollectionFiller<CollectionType, void>::Perform (&aCollec);
530 // test non-const iteration
531 typename CollectionType::iterator it = aCollec->begin(); // copy construction
532 typename CollectionType::iterator it2 = it + 5;
534 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
535 if (it2 < it || it2 <= it || ! (it2 > it) || ! (it2 >= it))
536 std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl;
539 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
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;
547 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
549 typename CollectionType::value_type t = it[5]; // offset dereference
555 //=======================================================================
556 //function : QANListStlIterator
558 //=======================================================================
559 static Standard_Integer QANListStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
561 // compile-time tests
562 TestForwardIterator <NCollection_List<Standard_Integer> >();
565 Standard_Boolean aResult = TestIteration<NCollection_List<int>, std::list<int> >();
566 std::cout << "NCollection_List<int> Iteration: " <<
567 (aResult ? "SUCCESS" : "FAIL") << std::endl;
569 aResult = TestIteration<NCollection_List<double>, std::list<double> >();
570 std::cout << "NCollection_List<double> Iteration: " <<
571 (aResult ? "SUCCESS" : "FAIL") << std::endl;
573 aResult = TestMinMax<NCollection_List<int>, std::list<int> >();
574 std::cout << "NCollection_List<int> Min-Max: " <<
575 (aResult ? "SUCCESS" : "FAIL") << std::endl;
577 aResult = TestMinMax<NCollection_List<double>, std::list<double> >();
578 std::cout << "NCollection_List<double> Min-Max: " <<
579 (aResult ? "SUCCESS" : "FAIL") << std::endl;
581 aResult = TestReplace<NCollection_List<int>, std::list<int> >();
582 std::cout << "NCollection_List<int> Replace: " <<
583 (aResult ? "SUCCESS" : "FAIL") << std::endl;
585 aResult = TestReplace<NCollection_List<double>, std::list<double> >();
586 std::cout << "NCollection_List<double> Replace: " <<
587 (aResult ? "SUCCESS" : "FAIL") << std::endl;
589 aResult = TestParallel< NCollection_List<int>, std::list<int> >();
590 std::cout << "NCollection_List<int> Parallel: " <<
591 (aResult ? "SUCCESS" : "FAIL") << std::endl;
593 aResult = TestParallel<NCollection_List<double>, std::list<double> >();
594 std::cout << "NCollection_List<double> Parallel: " <<
595 (aResult ? "SUCCESS" : "FAIL") << std::endl;
600 //=======================================================================
601 //function : QANMapStlIterator
603 //=======================================================================
604 static Standard_Integer QANMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
606 // compile-time tests
607 // TestForwardIterator <NCollection_Map<Standard_Integer> >();
610 Standard_Boolean aResult = TestMapIteration<NCollection_Map<Standard_Integer>, Standard_Integer>();
611 std::cout << "NCollection_Map<int> Iteration: " <<
612 (aResult ? "SUCCESS" : "FAIL") << std::endl;
614 aResult = TestMapIteration<NCollection_Map<Standard_Real>, Standard_Real>();
615 std::cout << "NCollection_Map<double> Iteration: " <<
616 (aResult ? "SUCCESS" : "FAIL") << std::endl;
621 //=======================================================================
622 //function : QANIndexedMapStlIterator
624 //=======================================================================
625 static Standard_Integer QANIndexedMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
627 // compile-time tests
628 // TestForwardIterator <NCollection_IndexedMap<Standard_Integer> >();
629 // TestBidirIterator <NCollection_IndexedMap<Standard_Integer> >();
632 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedMap<Standard_Integer>, Standard_Integer>();
633 std::cout << "NCollection_IndexedMap<int> Iteration: " <<
634 (aResult ? "SUCCESS" : "FAIL") << std::endl;
636 aResult = TestMapIteration<NCollection_IndexedMap<Standard_Real>, Standard_Real>();
637 std::cout << "NCollection_IndexedMap<double> Iteration: " <<
638 (aResult ? "SUCCESS" : "FAIL") << std::endl;
643 //=======================================================================
644 //function : QANDataMapStlIterator
646 //=======================================================================
647 static Standard_Integer QANDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
649 // compile-time tests
650 // TestForwardIterator <NCollection_DataMap<int, int> >();
651 // TestBidirIterator <NCollection_DataMap<int, int> >();
654 Standard_Boolean aResult = TestMapIteration<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
655 std::cout << "NCollection_DataMap<int> Iteration: " <<
656 (aResult ? "SUCCESS" : "FAIL") << std::endl;
658 aResult = TestMapIteration<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
659 std::cout << "NCollection_DataMap<double> Iteration: " <<
660 (aResult ? "SUCCESS" : "FAIL") << std::endl;
662 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
663 std::cout << "NCollection_DataMap<int> Parallel: " <<
664 (aResult ? "SUCCESS" : "FAIL") << std::endl;
666 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
667 std::cout << "NCollection_DataMap<double> Parallel: " <<
668 (aResult ? "SUCCESS" : "FAIL") << std::endl;
673 //=======================================================================
674 //function : QANIndexedDataMapStlIterator
676 //=======================================================================
677 static Standard_Integer QANIndexedDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
679 // compile-time tests
680 // TestForwardIterator <NCollection_IndexedDataMap<int, int> >();
681 // TestBidirIterator <NCollection_IndexedDataMap<int, int> >();
684 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
685 std::cout << "NCollection_IndexedDataMap<int> Iteration: " <<
686 (aResult ? "SUCCESS" : "FAIL") << std::endl;
688 aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
689 std::cout << "NCollection_IndexedDataMap<double> Iteration: " <<
690 (aResult ? "SUCCESS" : "FAIL") << std::endl;
692 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
693 std::cout << "NCollection_IndexedDataMap<int> Parallel: " <<
694 (aResult ? "SUCCESS" : "FAIL") << std::endl;
696 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
697 std::cout << "NCollection_IndexedDataMap<double> Parallel: " <<
698 (aResult ? "SUCCESS" : "FAIL") << std::endl;
703 //=======================================================================
704 //function : QANSequenceStlIterator
706 //=======================================================================
707 static Standard_Integer QANSequenceStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
709 // compile-time tests
710 TestForwardIterator <NCollection_Sequence<int> >();
711 TestBidirIterator <NCollection_Sequence<int> >();
714 Standard_Boolean aResult = TestIteration<NCollection_Sequence<int>, std::list<int> >();
715 std::cout << "NCollection_Sequence<int> Iteration: " <<
716 (aResult ? "SUCCESS" : "FAIL") << std::endl;
718 aResult = TestIteration<NCollection_Sequence<double>, std::list<double> >();
719 std::cout << "NCollection_Sequence<double> Iteration: " <<
720 (aResult ? "SUCCESS" : "FAIL") << std::endl;
722 aResult = TestMinMax<NCollection_Sequence<int>, std::list<int> >();
723 std::cout << "NCollection_Sequence<int> Min-Max: " <<
724 (aResult ? "SUCCESS" : "FAIL") << std::endl;
726 aResult = TestMinMax<NCollection_Sequence<double>, std::list<double> >();
727 std::cout << "NCollection_Sequence<double> Min-Max: " <<
728 (aResult ? "SUCCESS" : "FAIL") << std::endl;
730 aResult = TestReplace<NCollection_Sequence<int>, std::list<int> >();
731 std::cout << "NCollection_Sequence<int> Replace: " <<
732 (aResult ? "SUCCESS" : "FAIL") << std::endl;
734 aResult = TestReplace<NCollection_Sequence<double>, std::list<double> >();
735 std::cout << "NCollection_Sequence<double> Replace: " <<
736 (aResult ? "SUCCESS" : "FAIL") << std::endl;
738 aResult = TestReverse<NCollection_Sequence<int>, std::list<int> >();
739 std::cout << "NCollection_Sequence<int> Reverse: " <<
740 (aResult ? "SUCCESS" : "FAIL") << std::endl;
742 aResult = TestReverse<NCollection_Sequence<double>, std::list<double> >();
743 std::cout << "NCollection_Sequence<double> Reverse: " <<
744 (aResult ? "SUCCESS" : "FAIL") << std::endl;
746 aResult = TestParallel<NCollection_Sequence<int>, std::list<int> >();
747 std::cout << "NCollection_Sequence<int> Parallel: " <<
748 (aResult ? "SUCCESS" : "FAIL") << std::endl;
750 aResult = TestParallel<NCollection_Sequence<double>, std::list<double> >();
751 std::cout << "NCollection_Sequence<double> Parallel: " <<
752 (aResult ? "SUCCESS" : "FAIL") << std::endl;
757 //=======================================================================
758 //function : QANVectorStlIterator
760 //=======================================================================
761 static Standard_Integer QANVectorStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
763 // compile-time tests
764 TestForwardIterator <NCollection_Vector<int> >();
765 TestBidirIterator <NCollection_Vector<int> >();
766 TestRandomIterator <NCollection_Vector<int> >();
769 Standard_Boolean aResult = TestIteration<NCollection_Vector<int>, std::vector<int> >();
770 std::cout << "NCollection_Vector<int> Iteration: " <<
771 (aResult ? "SUCCESS" : "FAIL") << std::endl;
773 aResult = TestIteration<NCollection_Vector<double>, std::vector<double> >();
774 std::cout << "NCollection_Vector<double> Iteration: " <<
775 (aResult ? "SUCCESS" : "FAIL") << std::endl;
777 aResult = TestMinMax<NCollection_Vector<int>, std::vector<int> >();
778 std::cout << "NCollection_Vector<int> Min-Max: " <<
779 (aResult ? "SUCCESS" : "FAIL") << std::endl;
781 aResult = TestMinMax<NCollection_Vector<double>, std::vector<double> >();
782 std::cout << "NCollection_Vector<double> Min-Max: " <<
783 (aResult ? "SUCCESS" : "FAIL") << std::endl;
785 aResult = TestReplace<NCollection_Vector<int>, std::vector<int> >();
786 std::cout << "NCollection_Vector<int> Replace: " <<
787 (aResult ? "SUCCESS" : "FAIL") << std::endl;
789 aResult = TestReplace<NCollection_Vector<double>, std::vector<double> >();
790 std::cout << "NCollection_Vector<double> Replace: " <<
791 (aResult ? "SUCCESS" : "FAIL") << std::endl;
793 aResult = TestReverse<NCollection_Vector<int>, std::vector<int> >();
794 std::cout << "NCollection_Vector<int> Reverse: " <<
795 (aResult ? "SUCCESS" : "FAIL") << std::endl;
797 aResult = TestReverse<NCollection_Vector<double>, std::vector<double> >();
798 std::cout << "NCollection_Vector<double> Reverse: " <<
799 (aResult ? "SUCCESS" : "FAIL") << std::endl;
801 aResult = TestSort<NCollection_Vector<int>, std::vector<int> >();
802 std::cout << "NCollection_Vector<int> Sort: " <<
803 (aResult ? "SUCCESS" : "FAIL") << std::endl;
805 aResult = TestSort<NCollection_Vector<double>, std::vector<double> >();
806 std::cout << "NCollection_Vector<double> Sort: " <<
807 (aResult ? "SUCCESS" : "FAIL") << std::endl;
809 aResult = TestParallel<NCollection_Vector<int>, std::vector<int> >();
810 std::cout << "NCollection_Vector<int> Parallel: " <<
811 (aResult ? "SUCCESS" : "FAIL") << std::endl;
813 aResult = TestParallel<NCollection_Vector<double>, std::vector<double> >();
814 std::cout << "NCollection_Vector<double> Parallel: " <<
815 (aResult ? "SUCCESS" : "FAIL") << std::endl;
818 // Test case for a corner case described in a bug #0027941
819 // when vector length matches the increment.
820 // In this case NCollection_Vector::Iterator::Offset() produced mathematically equal
821 // but not the same iterator as returned by NCollection_Vector::end()
822 // so that their comparison was not equal.
823 // As result, std::stable_sort() crashed due to out-of-range access.
824 const int THE_INCREMENT = 256;
825 NCollection_Vector<int> aVector (THE_INCREMENT);
826 for (int anIter = 0; anIter < THE_INCREMENT; ++anIter)
828 aVector.Append (THE_INCREMENT - anIter);
831 NCollection_Vector<int>::iterator aBegin = aVector.begin();
832 NCollection_Vector<int>::iterator anEnd = aVector.end();
833 NCollection_Vector<int>::iterator aShift = aBegin + THE_INCREMENT;
834 aResult = (aShift == anEnd);
835 std::cout << "NCollection_Vector<int> Offset: " <<
836 (aResult ? "SUCCESS" : "FAIL") << std::endl;
838 std::stable_sort (aVector.begin(), aVector.end());
844 //=======================================================================
845 //function : QANArray1StlIterator
847 //=======================================================================
848 static Standard_Integer QANArray1StlIterator (Draw_Interpretor&, Standard_Integer, const char**)
850 // compile-time tests
851 TestForwardIterator <NCollection_Vector<int> >();
852 TestBidirIterator <NCollection_Vector<int> >();
853 TestRandomIterator <NCollection_Vector<int> >();
856 Standard_Boolean aResult = TestIteration<NCollection_Array1<int>, std::vector<int> >();
857 std::cout << "NCollection_Array1<int> Iteration: " <<
858 (aResult ? "SUCCESS" : "FAIL") << std::endl;
860 aResult = TestIteration<NCollection_Array1<double>, std::vector<double> >();
861 std::cout << "NCollection_Array1<double> Iteration: " <<
862 (aResult ? "SUCCESS" : "FAIL") << std::endl;
864 aResult = TestMinMax<NCollection_Array1<int>, std::vector<int> >();
865 std::cout << "NCollection_Array1<int> Min-Max: " <<
866 (aResult ? "SUCCESS" : "FAIL") << std::endl;
868 aResult = TestMinMax<NCollection_Array1<double>, std::vector<double> >();
869 std::cout << "NCollection_Array1<double> Min-Max: " <<
870 (aResult ? "SUCCESS" : "FAIL") << std::endl;
872 aResult = TestReplace<NCollection_Array1<int>, std::vector<int> >();
873 std::cout << "NCollection_Array1<int> Replace: " <<
874 (aResult ? "SUCCESS" : "FAIL") << std::endl;
876 aResult = TestReplace<NCollection_Array1<double>, std::vector<double> >();
877 std::cout << "NCollection_Array1<double> Replace: " <<
878 (aResult ? "SUCCESS" : "FAIL") << std::endl;
880 aResult = TestReverse<NCollection_Array1<int>, std::vector<int> >();
881 std::cout << "NCollection_Array1<int> Reverse: " <<
882 (aResult ? "SUCCESS" : "FAIL") << std::endl;
884 aResult = TestReverse<NCollection_Array1<double>, std::vector<double> >();
885 std::cout << "NCollection_Array1<double> Reverse: " <<
886 (aResult ? "SUCCESS" : "FAIL") << std::endl;
888 aResult = TestSort<NCollection_Array1<int>, std::vector<int> >();
889 std::cout << "NCollection_Array1<int> Sort: " <<
890 (aResult ? "SUCCESS" : "FAIL") << std::endl;
892 aResult = TestSort<NCollection_Array1<double>, std::vector<double> >();
893 std::cout << "NCollection_Array1<double> Sort: " <<
894 (aResult ? "SUCCESS" : "FAIL") << std::endl;
896 aResult = TestParallel<NCollection_Array1<int>, std::vector<int> >();
897 std::cout << "NCollection_Array1<int> Parallel: " <<
898 (aResult ? "SUCCESS" : "FAIL") << std::endl;
900 aResult = TestParallel<NCollection_Array1<double>, std::vector<double> >();
901 std::cout << "NCollection_Array1<double> Parallel: " <<
902 (aResult ? "SUCCESS" : "FAIL") << std::endl;
907 //=======================================================================
908 //function : QANTestStlIterators
910 //=======================================================================
911 static Standard_Integer QANTestStlIterators (
912 Draw_Interpretor& theInterpretor, Standard_Integer, const char**)
914 QANListStlIterator (theInterpretor, 0, NULL);
915 QANArray1StlIterator (theInterpretor, 0, NULL);
916 QANVectorStlIterator (theInterpretor, 0, NULL);
917 QANSequenceStlIterator (theInterpretor, 0, NULL);
918 QANMapStlIterator (theInterpretor, 0, NULL);
919 QANDataMapStlIterator (theInterpretor, 0, NULL);
920 QANIndexedMapStlIterator (theInterpretor, 0, NULL);
921 QANIndexedDataMapStlIterator (theInterpretor, 0, NULL);
926 //=======================================================================
927 //function : TestPerformanceRandomIterator
929 //=======================================================================
930 template<class CollectionType, class StlType>
931 void TestPerformanceRandomIterator(Draw_Interpretor& di)
935 StlType* aVector (NULL);
936 CollectionType* aCollec (NULL);
938 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
940 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
945 std::random_device ran_dev;
946 std::mt19937 gen(ran_dev());
947 gen.seed(0x03ac38f2);
948 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
950 std::sort (aVector->begin(), aVector->end());
951 std::shuffle (aVector->begin(), aVector->end(), gen);
956 Standard_Real aStlTime = aTimer.ElapsedTime();
961 std::random_device ran_dev;
962 std::mt19937 gen(ran_dev());
963 gen.seed(0x03ac38f2);
964 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
966 std::sort (aCollec->begin(), aCollec->end());
967 std::shuffle (aCollec->begin(), aCollec->end(), gen);
972 Standard_Real aOccTime = aTimer.ElapsedTime();
974 di << aSize << "\t" << aStlTime << "\t" <<
975 aOccTime << "\t" << aOccTime / aStlTime << "\n";
977 // check that result is the same
978 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
979 di << "Error: sequences are not the same at the end (random iterator)!\n";
986 //=======================================================================
987 //function : TestPerformanceForwardIterator
989 //=======================================================================
990 template<class CollectionType, class StlType>
991 void TestPerformanceForwardIterator(Draw_Interpretor& di)
995 StlType* aVector = 0;
996 CollectionType* aCollec = 0;
998 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
1000 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
1005 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1007 std::replace (aVector->begin(), aVector->end(), *aVector->begin(), static_cast<typename StlType::value_type> (anIdx));
1012 Standard_Real aStlTime = aTimer.ElapsedTime();
1017 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1019 std::replace (aCollec->begin(), aCollec->end(), *aCollec->begin(), static_cast<typename CollectionType::value_type> (anIdx));
1024 Standard_Real aOccTime = aTimer.ElapsedTime();
1026 di << aSize << "\t" << aStlTime << "\t" <<
1027 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1029 // check that result is the same
1030 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1031 di << "Error: sequences are not the same at the end (forward iterator)!\n";
1038 //=======================================================================
1039 //function : TestPerformanceBidirIterator
1041 //=======================================================================
1042 template<class CollectionType, class StlType>
1043 void TestPerformanceBidirIterator(Draw_Interpretor& di)
1047 StlType* aVector = 0;
1048 CollectionType* aCollec = 0;
1050 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
1052 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
1057 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1059 std::reverse (aVector->begin(), aVector->end());
1064 Standard_Real aStlTime = aTimer.ElapsedTime();
1069 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1071 std::reverse (aCollec->begin(), aCollec->end());
1076 Standard_Real aOccTime = aTimer.ElapsedTime();
1078 di << aSize << "\t" << aStlTime << "\t" <<
1079 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1081 // check that result is the same
1082 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1083 di << "Error: sequences are not the same at the end (bidir iterator)!\n";
1090 //=======================================================================
1091 //function : TestPerformanceMapAccess
1093 //=======================================================================
1094 template<class CollectionType, class T>
1095 void TestPerformanceMapAccess(Draw_Interpretor& di)
1099 CollectionType* aCollec (NULL);
1101 for (Standard_Integer aSize = 100000; aSize <= 3200000; aSize *= 2)
1103 MapFiller<CollectionType, T>::Perform (&aCollec, aSize);
1105 std::set<T> aSet (aCollec->cbegin(), aCollec->cend());
1106 std::vector<T> aVec (aCollec->cbegin(), aCollec->cend());
1108 Standard_Boolean aResult = Standard_True;
1113 for (size_t anIdx = 0; anIdx < 10000; ++anIdx)
1115 if (aSet.find (aVec[anIdx + 1000]) == aSet.end())
1116 aResult = Standard_False;
1117 if (aSet.find (aVec[anIdx + 2000]) == aSet.end())
1118 aResult = Standard_False;
1119 if (aSet.find (aVec[anIdx + 3000]) == aSet.end())
1120 aResult = Standard_False;
1121 if (aSet.find (aVec[anIdx + 4000]) == aSet.end())
1122 aResult = Standard_False;
1123 if (aSet.find (aVec[anIdx + 5000]) == aSet.end())
1124 aResult = Standard_False;
1129 Standard_Real aStlTime = aTimer.ElapsedTime();
1134 for (size_t anIdx = 0; anIdx < 10000; ++anIdx)
1136 if (!aCollec->Contains (aVec[anIdx + 1000]))
1137 aResult = Standard_False;
1138 if (!aCollec->Contains (aVec[anIdx + 2000]))
1139 aResult = Standard_False;
1140 if (!aCollec->Contains (aVec[anIdx + 3000]))
1141 aResult = Standard_False;
1142 if (!aCollec->Contains (aVec[anIdx + 4000]))
1143 aResult = Standard_False;
1144 if (!aCollec->Contains (aVec[anIdx + 5000]))
1145 aResult = Standard_False;
1150 Standard_Real aOccTime = aTimer.ElapsedTime();
1154 di << aSize << "\t" << aStlTime << "\t" <<
1155 aOccTime << "\t" << (aStlTime > 1e-16 ? aOccTime / aStlTime : -1) << "\n";
1162 //=======================================================================
1163 //function : QANTestNCollectionPerformance
1165 //=======================================================================
1166 static Standard_Integer QANTestNCollectionPerformance (Draw_Interpretor& di, Standard_Integer, const char**)
1168 di << "Testing performance (Size | STL time | OCCT time | STL/OCCT boost)\n";
1170 di << "\nstd::vector vs NCollection_Array1 (sort):\n\n";
1171 TestPerformanceRandomIterator<NCollection_Array1<double>, std::vector<double> >(di);
1173 di << "\nstd::vector vs NCollection_Vector (sort):\n\n";
1174 TestPerformanceRandomIterator<NCollection_Vector<double>, std::vector<double> >(di);
1176 di << "\nstd::vector vs NCollection_Array1 (replace):\n\n";
1177 TestPerformanceForwardIterator<NCollection_Array1<double>, std::vector<double> >(di);
1179 di << "\nstd::vector vs NCollection_Vector (replace):\n\n";
1180 TestPerformanceForwardIterator<NCollection_Vector<double>, std::vector<double> >(di);
1182 di << "\nstd::list vs NCollection_List (replace):\n\n";
1183 TestPerformanceForwardIterator<NCollection_List<double>, std::list<double> >(di);
1185 di << "\nstd::list vs NCollection_Sequence (replace):\n\n";
1186 TestPerformanceForwardIterator<NCollection_Sequence<double>, std::list<double> >(di);
1188 di << "\nstd::list vs NCollection_Sequence (reverse):\n\n";
1189 TestPerformanceBidirIterator<NCollection_Sequence<double>, std::list<double> >(di);
1191 di << "\nstd::set vs NCollection_Map (search):\n\n";
1192 TestPerformanceMapAccess<NCollection_Map<int>, int>(di);
1194 di << "\nstd::set vs NCollection_IndexedMap (search):\n\n";
1195 TestPerformanceMapAccess<NCollection_IndexedMap<int>, int>(di);
1200 //=======================================================================
1201 //function : QANTestNCollectionIndexedMap
1203 //=======================================================================
1204 static Standard_Integer QANTestNCollectionIndexedMap (Draw_Interpretor& di, Standard_Integer, const char**)
1208 std::vector<Standard_Integer> aIndxs;
1209 std::vector<Standard_Integer> aItems;
1210 NCollection_IndexedMap<Standard_Integer> aIndxMap;
1212 const Standard_Integer aNbItems = 1000000;
1215 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1217 const Standard_Integer aVal = anId * 2;
1219 aIndxs.push_back (anId);
1220 aItems.push_back (aVal);
1222 aIndxMap.Add (aVal);
1226 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1228 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1230 std::cout << "failed FindIndex\n";
1233 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1235 std::cout << "failed FindKey\n";
1240 const Standard_Real aTime1 = aTimer.ElapsedTime();
1244 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1246 const Standard_Integer anId2 = Min (aNbItems - 1,
1247 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1249 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1251 std::swap (aIndxs[anId], aIndxs[anId2]);
1255 const Standard_Real aTime2 = aTimer.ElapsedTime();
1259 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1261 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1263 std::cout << "failed FindIndex\n";
1266 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1268 std::cout << "failed FindKey\n";
1273 const Standard_Real aTime3 = aTimer.ElapsedTime();
1277 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1279 aIndxMap.RemoveLast();
1283 const Standard_Real aTime4 = aTimer.ElapsedTime();
1285 di << "Search time 1: " << aTime1 << "\n"
1286 << "Swapping time: " << aTime2 << "\n"
1287 << "Search time 2: " << aTime3 << "\n"
1288 << "Remove time: " << aTime4 << "\n";
1293 //=======================================================================
1294 //function : QANTestNCollectionIndexedDataMap
1296 //=======================================================================
1297 static Standard_Integer QANTestNCollectionIndexedDataMap (Draw_Interpretor& di, Standard_Integer, const char**)
1301 std::vector<Standard_Integer> aIndxs;
1302 std::vector<Standard_Integer> aItems;
1303 NCollection_IndexedDataMap<Standard_Integer, Standard_Integer> aIndxMap;
1305 const Standard_Integer aNbItems = 1000000;
1308 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1310 const Standard_Integer aVal = anId * 2;
1312 aIndxs.push_back (anId);
1313 aItems.push_back (aVal);
1315 aIndxMap.Add (aVal, aVal * 2);
1319 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1321 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1323 std::cout << "failed FindIndex\n";
1326 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1328 std::cout << "failed FindKey\n";
1333 const Standard_Real aTime1 = aTimer.ElapsedTime();
1337 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1339 const Standard_Integer anId2 = Min (aNbItems - 1,
1340 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1342 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1344 std::swap (aIndxs[anId], aIndxs[anId2]);
1348 const Standard_Real aTime2 = aTimer.ElapsedTime();
1352 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1354 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1356 std::cout << "failed FindIndex\n";
1359 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1361 std::cout << "failed FindKey\n";
1366 const Standard_Real aTime3 = aTimer.ElapsedTime();
1370 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1372 aIndxMap.RemoveLast();
1376 const Standard_Real aTime4 = aTimer.ElapsedTime();
1378 di << "Search time 1: " << aTime1 << "\n"
1379 << "Swapping time: " << aTime2 << "\n"
1380 << "Search time 2: " << aTime3 << "\n"
1381 << "Remove time: " << aTime4 << "\n";
1386 //=======================================================================
1387 //function : CommandsStl
1389 //=======================================================================
1390 void QANCollection::CommandsStl (Draw_Interpretor& theCommands)
1392 const char* aGroup = "QANCollection";
1394 theCommands.Add ("QANArray1StlIterator",
1395 "QANArray1StlIterator",
1397 QANArray1StlIterator,
1400 theCommands.Add ("QANListStlIterator",
1401 "QANListStlIterator",
1406 theCommands.Add ("QANSequenceStlIterator",
1407 "QANSequenceStlIterator",
1409 QANSequenceStlIterator,
1412 theCommands.Add ("QANVectorStlIterator",
1413 "QANVectorStlIterator",
1415 QANVectorStlIterator,
1418 theCommands.Add ("QANMapStlIterator",
1419 "QANMapStlIterator",
1424 theCommands.Add ("QANDataMapStlIterator",
1425 "QANDataMapStlIterator",
1427 QANDataMapStlIterator,
1430 theCommands.Add ("QANIndexedMapStlIterator",
1431 "QANIndexedMapStlIterator",
1433 QANIndexedMapStlIterator,
1436 theCommands.Add ("QANIndexedDataMapStlIterator",
1437 "QANIndexedDataMapStlIterator",
1439 QANIndexedDataMapStlIterator,
1442 theCommands.Add ("QANTestStlIterators",
1443 "QANTestStlIterators",
1445 QANTestStlIterators,
1448 theCommands.Add ("QANTestNCollectionPerformance",
1449 "QANTestNCollectionPerformance",
1451 QANTestNCollectionPerformance,
1454 theCommands.Add ("QANTestNCollectionIndexedMap",
1455 "QANTestNCollectionIndexedMap",
1457 QANTestNCollectionIndexedMap,
1460 theCommands.Add ("QANTestNCollectionIndexedDataMap",
1461 "QANTestNCollectionIndexedDataMap",
1463 QANTestNCollectionIndexedDataMap,