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_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>
34 #include <OSD_Parallel.hxx>
42 //! Size of test data sets.
43 const int THE_TEST_SIZE = 5000;
46 // Auxiliary class to use in std::random_shuffle()
47 struct RandomGenerator {
48 RandomGenerator () { srand(1); }
49 ptrdiff_t operator () (ptrdiff_t upper) const { return rand() % upper; }
53 template<class CollectionType, class StlType>
54 struct CollectionFiller
56 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
58 *theCollec = new CollectionType();
60 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
62 (*theCollec)->Append (rand());
66 static void Perform (StlType** theVector,
67 CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
69 CollectionFiller::Perform (theCollec, theSize);
71 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
75 template<class T, typename StlType>
76 struct CollectionFiller<NCollection_Array1<T>, StlType>
78 static void Perform (NCollection_Array1<T>** theCollec,
79 Standard_Integer theSize = THE_TEST_SIZE)
81 *theCollec = new NCollection_Array1<T> (0, theSize - 1);
83 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
85 (*theCollec)->ChangeValue (anIdx) = rand();
89 static void Perform (StlType** theVector,
90 NCollection_Array1<T>** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
92 CollectionFiller<NCollection_Array1<T>, StlType >::Perform (theCollec, theSize);
94 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
98 template<class CollectionType, class T>
101 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
103 *theCollec = new CollectionType();
105 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
107 (*theCollec)->Add (rand());
113 struct MapFiller<NCollection_DataMap<T, T>, T>
115 static void Perform (NCollection_DataMap<T, T>** theCollec1,
116 NCollection_DataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
118 *theCollec1 = new NCollection_DataMap<T, T>();
120 if (theCollec2 != NULL)
121 *theCollec2 = new NCollection_DataMap<T, T>();
123 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
125 const T aVal1 = rand();
126 const T aVal2 = rand();
128 (*theCollec1)->Bind (aVal1, aVal2);
130 if (theCollec2 != NULL)
131 (*theCollec2)->Bind (aVal1, aVal2);
137 struct MapFiller<NCollection_IndexedDataMap<T, T>, T>
139 static void Perform (NCollection_IndexedDataMap<T, T>** theCollec1,
140 NCollection_IndexedDataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
142 *theCollec1 = new NCollection_IndexedDataMap<T, T>();
144 if (theCollec2 != NULL)
145 *theCollec2 = new NCollection_IndexedDataMap<T, T>();
147 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
149 const T aVal1 = rand();
150 const T aVal2 = rand();
152 (*theCollec1)->Add (aVal1, aVal2);
154 if (theCollec2 != NULL)
155 (*theCollec2)->Add (aVal1, aVal2);
160 //=======================================================================
161 //function : TestIteration
163 //=======================================================================
164 template<class CollectionType, class StlType>
165 Standard_Boolean TestIteration()
167 StlType* aVector (NULL);
168 CollectionType* aCollec (NULL);
170 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
172 typename StlType::iterator aVecIter = aVector->begin();
173 typename CollectionType::iterator aColIter = aCollec->begin();
175 Standard_Boolean aResult (Standard_True);
177 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
179 if (*aVecIter != *aColIter)
180 aResult = Standard_False;
183 if (aColIter != aCollec->end())
185 aResult = Standard_False;
194 //=======================================================================
195 //function : TestMinMax
197 //=======================================================================
198 template<class CollectionType, class StlType>
199 Standard_Boolean TestMinMax()
201 StlType* aVector (NULL);
202 CollectionType* aCollec (NULL);
204 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
206 typename StlType::value_type aValue1 = *std::min_element (aVector->begin(), aVector->end());
207 typename CollectionType::value_type aValue2 = *std::min_element (aCollec->begin(), aCollec->end());
209 Standard_Boolean aResult (Standard_True);
211 if (aValue1 != aValue2)
212 aResult = Standard_False;
214 aValue1 = *std::max_element (aVector->begin(), aVector->end());
215 aValue2 = *std::max_element (aCollec->begin(), aCollec->end());
217 if (aValue1 != aValue2)
218 aResult = Standard_False;
226 //=======================================================================
227 //function : TestReplace
229 //=======================================================================
230 template<class CollectionType, class StlType>
231 Standard_Boolean TestReplace()
233 StlType* aVector (NULL);
234 CollectionType* aCollec (NULL);
236 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
238 const typename StlType::value_type aValue = aVector->back();
240 std::replace (aVector->begin(), aVector->end(), aValue, static_cast<typename StlType::value_type> (-1));
241 std::replace (aCollec->begin(), aCollec->end(), aValue, static_cast<typename CollectionType::value_type> (-1));
243 typename StlType::iterator aVecIter = aVector->begin();
244 typename CollectionType::iterator aColIter = aCollec->begin();
246 Standard_Boolean aResult (Standard_True);
248 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
250 if (*aVecIter != *aColIter)
251 aResult = Standard_False;
254 if (aColIter != aCollec->end())
256 aResult = Standard_False;
265 //=======================================================================
266 //function : TestReverse
268 //=======================================================================
269 template<class CollectionType, class StlType>
270 Standard_Boolean TestReverse()
272 StlType* aVector (NULL);
273 CollectionType* aCollec (NULL);
275 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
277 std::reverse (aVector->begin(), aVector->end());
278 std::reverse (aCollec->begin(), aCollec->end());
280 typename StlType::iterator aVecIter = aVector->begin();
281 typename CollectionType::iterator aColIter = aCollec->begin();
283 Standard_Boolean aResult (Standard_True);
285 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
287 if (*aVecIter != *aColIter)
288 aResult = Standard_False;
291 if (aColIter != aCollec->end())
293 aResult = Standard_False;
302 //=======================================================================
303 //function : TestSort
305 //=======================================================================
306 template<class CollectionType, class StlType>
307 Standard_Boolean TestSort()
309 StlType* aVector (NULL);
310 CollectionType* aCollec (NULL);
312 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
314 std::sort (aVector->begin(), aVector->end());
315 std::sort (aCollec->begin(), aCollec->end());
317 typename StlType::iterator aVecIter = aVector->begin();
318 typename CollectionType::iterator aColIter = aCollec->begin();
320 Standard_Boolean aResult (Standard_True);
322 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
324 if (*aVecIter != *aColIter)
325 aResult = Standard_False;
328 if (aColIter != aCollec->end())
330 aResult = Standard_False;
339 template <typename T>
342 void operator()(T& theValue) const
348 //=======================================================================
349 //function : TestParallel
351 //=======================================================================
352 template<class CollectionType, class StlType>
353 Standard_Boolean TestParallel()
355 StlType* aVector (NULL);
356 CollectionType* aCollec (NULL);
358 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
360 OSD_Parallel::ForEach(aVector->begin(), aVector->end(), Invoker<typename StlType::value_type>());
361 OSD_Parallel::ForEach(aCollec->begin(), aCollec->end(), Invoker<typename CollectionType::value_type>());
363 typename StlType::iterator aVecIter = aVector->begin();
364 typename CollectionType::iterator aColIter = aCollec->begin();
366 Standard_Boolean aResult (Standard_True);
368 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
370 if (*aVecIter != *aColIter)
371 aResult = Standard_False;
374 if (aColIter != aCollec->end())
376 aResult = Standard_False;
385 //=======================================================================
386 //function : TestDataMapParallel
388 //=======================================================================
389 template<class CollectionType, class T>
390 Standard_Boolean TestDataMapParallel()
392 CollectionType* aCollec1 (NULL);
393 CollectionType* aCollec2 (NULL);
395 MapFiller<CollectionType, T>::Perform (&aCollec1, &aCollec2);
397 OSD_Parallel::ForEach(aCollec1->begin(), aCollec1->end(), Invoker<T>());
399 // create OCCT-style iterator
400 typename CollectionType::Iterator aOccIter (*aCollec2);
402 // create STL-compatible iterator
403 typename CollectionType::const_iterator aStlIter = aCollec1->cbegin();
405 Standard_Boolean aResult (Standard_True);
407 for (; aStlIter != aCollec1->cend(); ++aStlIter, aOccIter.Next())
409 if (static_cast<T> (2) * aOccIter.Value() != *aStlIter)
410 aResult = Standard_False;
415 aResult = Standard_False;
424 //=======================================================================
425 //function : TestMapIteration
427 //=======================================================================
428 template<class CollectionType, class T>
429 Standard_Boolean TestMapIteration()
431 CollectionType* aCollec (NULL);
433 MapFiller<CollectionType, T>::Perform (&aCollec);
435 // create OCCT-style iterator
436 typename CollectionType::Iterator aOccIter (*aCollec);
438 // create STL-compatible iterator
439 typename CollectionType::const_iterator aStlIter = aCollec->cbegin();
441 Standard_Boolean aResult (Standard_True);
443 for (; aStlIter != aCollec->cend(); ++aStlIter, aOccIter.Next())
445 if (aOccIter.Value() != *aStlIter)
446 aResult = Standard_False;
451 aResult = Standard_False;
459 //=======================================================================
460 //function : TestForwardIterator
461 //purpose : test basic features of iterator (forward)
462 //=======================================================================
463 template <class CollectionType>
464 void TestForwardIterator ()
466 CollectionType* aCollec (NULL);
468 CollectionFiller<CollectionType, void>::Perform (&aCollec);
470 // test non-const iteration
471 typename CollectionType::iterator it = aCollec->begin(); // copy construction
472 typename CollectionType::iterator it2; // default constructor
473 it2 = it; // assignment
474 it2 = it++; // postfix increment
475 if (it2 == it || ! (it2 != it))
476 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
477 it2 = ++it; // prefix increment
478 if (it2 != it || ! (it2 == it))
479 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
481 typename CollectionType::iterator::value_type t = *it;
483 *(it2.operator-> ()) = t;
485 // test const iteration
486 typename CollectionType::const_iterator cit = aCollec->cbegin(); // copy construction
487 typename CollectionType::const_iterator cit2; // default constructor
488 cit2 = cit; // assignment
489 cit2 = cit++; // postfix increment
490 if (cit2 == cit || ! (cit2 != cit))
491 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
492 cit2 = ++cit; // prefix increment
493 if (cit2 != it || ! (cit2 == cit))
494 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
496 typename CollectionType::const_iterator::value_type ct = *cit;
500 // *(cit2.operator-> ()) = t;
505 //=======================================================================
506 //function : TestBidirIterator
507 //purpose : test features of bidirectional iterator
508 //=======================================================================
509 template <class CollectionType>
510 void TestBidirIterator ()
512 CollectionType* aCollec (NULL);
514 CollectionFiller<CollectionType, void>::Perform (&aCollec);
516 // test non-const iteration
517 typename CollectionType::iterator it = aCollec->end(); // copy construction
518 typename CollectionType::iterator it2 = it--; // postfix decrement
519 if (it2 == it || ! (it2 != it))
520 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
521 it2 = --it; // prefix decrement
522 if (it2 != it || ! (it2 == it))
523 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
528 //=======================================================================
529 //function : TestRandomIterator
530 //purpose : test features of random iterator
531 //=======================================================================
532 template <class CollectionType>
533 void TestRandomIterator ()
535 CollectionType* aCollec (NULL);
537 CollectionFiller<CollectionType, void>::Perform (&aCollec);
539 // test non-const iteration
540 typename CollectionType::iterator it = aCollec->begin(); // copy construction
541 typename CollectionType::iterator it2 = it + 5;
543 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
544 if (it2 < it || it2 <= it || ! (it2 > it) || ! (it2 >= it))
545 std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl;
548 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
550 if ((it2 - it) != -5)
551 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
552 if (it2 > it || it2 >= it || ! (it2 < it) || ! (it2 <= it))
553 std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl;
556 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
558 typename CollectionType::value_type t = it[5]; // offset dereference
564 //=======================================================================
565 //function : QANListStlIterator
567 //=======================================================================
568 static Standard_Integer QANListStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
570 // compile-time tests
571 TestForwardIterator <NCollection_List<Standard_Integer> >();
574 Standard_Boolean aResult = TestIteration<NCollection_List<int>, std::list<int> >();
575 std::cout << "NCollection_List<int> Iteration: " <<
576 (aResult ? "SUCCESS" : "FAIL") << std::endl;
578 aResult = TestIteration<NCollection_List<double>, std::list<double> >();
579 std::cout << "NCollection_List<double> Iteration: " <<
580 (aResult ? "SUCCESS" : "FAIL") << std::endl;
582 aResult = TestMinMax<NCollection_List<int>, std::list<int> >();
583 std::cout << "NCollection_List<int> Min-Max: " <<
584 (aResult ? "SUCCESS" : "FAIL") << std::endl;
586 aResult = TestMinMax<NCollection_List<double>, std::list<double> >();
587 std::cout << "NCollection_List<double> Min-Max: " <<
588 (aResult ? "SUCCESS" : "FAIL") << std::endl;
590 aResult = TestReplace<NCollection_List<int>, std::list<int> >();
591 std::cout << "NCollection_List<int> Replace: " <<
592 (aResult ? "SUCCESS" : "FAIL") << std::endl;
594 aResult = TestReplace<NCollection_List<double>, std::list<double> >();
595 std::cout << "NCollection_List<double> Replace: " <<
596 (aResult ? "SUCCESS" : "FAIL") << std::endl;
598 aResult = TestParallel< NCollection_List<int>, std::list<int> >();
599 std::cout << "NCollection_List<int> Parallel: " <<
600 (aResult ? "SUCCESS" : "FAIL") << std::endl;
602 aResult = TestParallel<NCollection_List<double>, std::list<double> >();
603 std::cout << "NCollection_List<double> Parallel: " <<
604 (aResult ? "SUCCESS" : "FAIL") << std::endl;
609 //=======================================================================
610 //function : QANMapStlIterator
612 //=======================================================================
613 static Standard_Integer QANMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
615 // compile-time tests
616 // TestForwardIterator <NCollection_Map<Standard_Integer> >();
619 Standard_Boolean aResult = TestMapIteration<NCollection_Map<Standard_Integer>, Standard_Integer>();
620 std::cout << "NCollection_Map<int> Iteration: " <<
621 (aResult ? "SUCCESS" : "FAIL") << std::endl;
623 aResult = TestMapIteration<NCollection_Map<Standard_Real>, Standard_Real>();
624 std::cout << "NCollection_Map<double> Iteration: " <<
625 (aResult ? "SUCCESS" : "FAIL") << std::endl;
630 //=======================================================================
631 //function : QANIndexedMapStlIterator
633 //=======================================================================
634 static Standard_Integer QANIndexedMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
636 // compile-time tests
637 // TestForwardIterator <NCollection_IndexedMap<Standard_Integer> >();
638 // TestBidirIterator <NCollection_IndexedMap<Standard_Integer> >();
641 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedMap<Standard_Integer>, Standard_Integer>();
642 std::cout << "NCollection_IndexedMap<int> Iteration: " <<
643 (aResult ? "SUCCESS" : "FAIL") << std::endl;
645 aResult = TestMapIteration<NCollection_IndexedMap<Standard_Real>, Standard_Real>();
646 std::cout << "NCollection_IndexedMap<double> Iteration: " <<
647 (aResult ? "SUCCESS" : "FAIL") << std::endl;
652 //=======================================================================
653 //function : QANDataMapStlIterator
655 //=======================================================================
656 static Standard_Integer QANDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
658 // compile-time tests
659 // TestForwardIterator <NCollection_DataMap<int, int> >();
660 // TestBidirIterator <NCollection_DataMap<int, int> >();
663 Standard_Boolean aResult = TestMapIteration<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
664 std::cout << "NCollection_DataMap<int> Iteration: " <<
665 (aResult ? "SUCCESS" : "FAIL") << std::endl;
667 aResult = TestMapIteration<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
668 std::cout << "NCollection_DataMap<double> Iteration: " <<
669 (aResult ? "SUCCESS" : "FAIL") << std::endl;
671 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
672 std::cout << "NCollection_DataMap<int> Parallel: " <<
673 (aResult ? "SUCCESS" : "FAIL") << std::endl;
675 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
676 std::cout << "NCollection_DataMap<double> Parallel: " <<
677 (aResult ? "SUCCESS" : "FAIL") << std::endl;
682 //=======================================================================
683 //function : QANIndexedDataMapStlIterator
685 //=======================================================================
686 static Standard_Integer QANIndexedDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
688 // compile-time tests
689 // TestForwardIterator <NCollection_IndexedDataMap<int, int> >();
690 // TestBidirIterator <NCollection_IndexedDataMap<int, int> >();
693 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
694 std::cout << "NCollection_IndexedDataMap<int> Iteration: " <<
695 (aResult ? "SUCCESS" : "FAIL") << std::endl;
697 aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
698 std::cout << "NCollection_IndexedDataMap<double> Iteration: " <<
699 (aResult ? "SUCCESS" : "FAIL") << std::endl;
701 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
702 std::cout << "NCollection_IndexedDataMap<int> Parallel: " <<
703 (aResult ? "SUCCESS" : "FAIL") << std::endl;
705 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
706 std::cout << "NCollection_IndexedDataMap<double> Parallel: " <<
707 (aResult ? "SUCCESS" : "FAIL") << std::endl;
712 //=======================================================================
713 //function : QANSequenceStlIterator
715 //=======================================================================
716 static Standard_Integer QANSequenceStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
718 // compile-time tests
719 TestForwardIterator <NCollection_Sequence<int> >();
720 TestBidirIterator <NCollection_Sequence<int> >();
723 Standard_Boolean aResult = TestIteration<NCollection_Sequence<int>, std::list<int> >();
724 std::cout << "NCollection_Sequence<int> Iteration: " <<
725 (aResult ? "SUCCESS" : "FAIL") << std::endl;
727 aResult = TestIteration<NCollection_Sequence<double>, std::list<double> >();
728 std::cout << "NCollection_Sequence<double> Iteration: " <<
729 (aResult ? "SUCCESS" : "FAIL") << std::endl;
731 aResult = TestMinMax<NCollection_Sequence<int>, std::list<int> >();
732 std::cout << "NCollection_Sequence<int> Min-Max: " <<
733 (aResult ? "SUCCESS" : "FAIL") << std::endl;
735 aResult = TestMinMax<NCollection_Sequence<double>, std::list<double> >();
736 std::cout << "NCollection_Sequence<double> Min-Max: " <<
737 (aResult ? "SUCCESS" : "FAIL") << std::endl;
739 aResult = TestReplace<NCollection_Sequence<int>, std::list<int> >();
740 std::cout << "NCollection_Sequence<int> Replace: " <<
741 (aResult ? "SUCCESS" : "FAIL") << std::endl;
743 aResult = TestReplace<NCollection_Sequence<double>, std::list<double> >();
744 std::cout << "NCollection_Sequence<double> Replace: " <<
745 (aResult ? "SUCCESS" : "FAIL") << std::endl;
747 aResult = TestReverse<NCollection_Sequence<int>, std::list<int> >();
748 std::cout << "NCollection_Sequence<int> Reverse: " <<
749 (aResult ? "SUCCESS" : "FAIL") << std::endl;
751 aResult = TestReverse<NCollection_Sequence<double>, std::list<double> >();
752 std::cout << "NCollection_Sequence<double> Reverse: " <<
753 (aResult ? "SUCCESS" : "FAIL") << std::endl;
755 aResult = TestParallel<NCollection_Sequence<int>, std::list<int> >();
756 std::cout << "NCollection_Sequence<int> Parallel: " <<
757 (aResult ? "SUCCESS" : "FAIL") << std::endl;
759 aResult = TestParallel<NCollection_Sequence<double>, std::list<double> >();
760 std::cout << "NCollection_Sequence<double> Parallel: " <<
761 (aResult ? "SUCCESS" : "FAIL") << std::endl;
766 //=======================================================================
767 //function : QANVectorStlIterator
769 //=======================================================================
770 static Standard_Integer QANVectorStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
772 // compile-time tests
773 TestForwardIterator <NCollection_Vector<int> >();
774 TestBidirIterator <NCollection_Vector<int> >();
775 TestRandomIterator <NCollection_Vector<int> >();
778 Standard_Boolean aResult = TestIteration<NCollection_Vector<int>, std::vector<int> >();
779 std::cout << "NCollection_Vector<int> Iteration: " <<
780 (aResult ? "SUCCESS" : "FAIL") << std::endl;
782 aResult = TestIteration<NCollection_Vector<double>, std::vector<double> >();
783 std::cout << "NCollection_Vector<double> Iteration: " <<
784 (aResult ? "SUCCESS" : "FAIL") << std::endl;
786 aResult = TestMinMax<NCollection_Vector<int>, std::vector<int> >();
787 std::cout << "NCollection_Vector<int> Min-Max: " <<
788 (aResult ? "SUCCESS" : "FAIL") << std::endl;
790 aResult = TestMinMax<NCollection_Vector<double>, std::vector<double> >();
791 std::cout << "NCollection_Vector<double> Min-Max: " <<
792 (aResult ? "SUCCESS" : "FAIL") << std::endl;
794 aResult = TestReplace<NCollection_Vector<int>, std::vector<int> >();
795 std::cout << "NCollection_Vector<int> Replace: " <<
796 (aResult ? "SUCCESS" : "FAIL") << std::endl;
798 aResult = TestReplace<NCollection_Vector<double>, std::vector<double> >();
799 std::cout << "NCollection_Vector<double> Replace: " <<
800 (aResult ? "SUCCESS" : "FAIL") << std::endl;
802 aResult = TestReverse<NCollection_Vector<int>, std::vector<int> >();
803 std::cout << "NCollection_Vector<int> Reverse: " <<
804 (aResult ? "SUCCESS" : "FAIL") << std::endl;
806 aResult = TestReverse<NCollection_Vector<double>, std::vector<double> >();
807 std::cout << "NCollection_Vector<double> Reverse: " <<
808 (aResult ? "SUCCESS" : "FAIL") << std::endl;
810 aResult = TestSort<NCollection_Vector<int>, std::vector<int> >();
811 std::cout << "NCollection_Vector<int> Sort: " <<
812 (aResult ? "SUCCESS" : "FAIL") << std::endl;
814 aResult = TestSort<NCollection_Vector<double>, std::vector<double> >();
815 std::cout << "NCollection_Vector<double> Sort: " <<
816 (aResult ? "SUCCESS" : "FAIL") << std::endl;
818 aResult = TestParallel<NCollection_Vector<int>, std::vector<int> >();
819 std::cout << "NCollection_Vector<int> Parallel: " <<
820 (aResult ? "SUCCESS" : "FAIL") << std::endl;
822 aResult = TestParallel<NCollection_Vector<double>, std::vector<double> >();
823 std::cout << "NCollection_Vector<double> Parallel: " <<
824 (aResult ? "SUCCESS" : "FAIL") << std::endl;
827 // Test case for a corner case described in a bug #0027941
828 // when vector length matches the increment.
829 // In this case NCollection_Vector::Iterator::Offset() produced mathematically equal
830 // but not the same iterator as returned by NCollection_Vector::end()
831 // so that their comparison was not equal.
832 // As result, std::stable_sort() crashed due to out-of-range access.
833 const int THE_INCREMENT = 256;
834 NCollection_Vector<int> aVector (THE_INCREMENT);
835 for (int anIter = 0; anIter < THE_INCREMENT; ++anIter)
837 aVector.Append (THE_INCREMENT - anIter);
840 NCollection_Vector<int>::iterator aBegin = aVector.begin();
841 NCollection_Vector<int>::iterator anEnd = aVector.end();
842 NCollection_Vector<int>::iterator aShift = aBegin + THE_INCREMENT;
843 aResult = (aShift == anEnd);
844 std::cout << "NCollection_Vector<int> Offset: " <<
845 (aResult ? "SUCCESS" : "FAIL") << std::endl;
847 std::stable_sort (aVector.begin(), aVector.end());
853 //=======================================================================
854 //function : QANArray1StlIterator
856 //=======================================================================
857 static Standard_Integer QANArray1StlIterator (Draw_Interpretor&, Standard_Integer, const char**)
859 // compile-time tests
860 TestForwardIterator <NCollection_Vector<int> >();
861 TestBidirIterator <NCollection_Vector<int> >();
862 TestRandomIterator <NCollection_Vector<int> >();
865 Standard_Boolean aResult = TestIteration<NCollection_Array1<int>, std::vector<int> >();
866 std::cout << "NCollection_Array1<int> Iteration: " <<
867 (aResult ? "SUCCESS" : "FAIL") << std::endl;
869 aResult = TestIteration<NCollection_Array1<double>, std::vector<double> >();
870 std::cout << "NCollection_Array1<double> Iteration: " <<
871 (aResult ? "SUCCESS" : "FAIL") << std::endl;
873 aResult = TestMinMax<NCollection_Array1<int>, std::vector<int> >();
874 std::cout << "NCollection_Array1<int> Min-Max: " <<
875 (aResult ? "SUCCESS" : "FAIL") << std::endl;
877 aResult = TestMinMax<NCollection_Array1<double>, std::vector<double> >();
878 std::cout << "NCollection_Array1<double> Min-Max: " <<
879 (aResult ? "SUCCESS" : "FAIL") << std::endl;
881 aResult = TestReplace<NCollection_Array1<int>, std::vector<int> >();
882 std::cout << "NCollection_Array1<int> Replace: " <<
883 (aResult ? "SUCCESS" : "FAIL") << std::endl;
885 aResult = TestReplace<NCollection_Array1<double>, std::vector<double> >();
886 std::cout << "NCollection_Array1<double> Replace: " <<
887 (aResult ? "SUCCESS" : "FAIL") << std::endl;
889 aResult = TestReverse<NCollection_Array1<int>, std::vector<int> >();
890 std::cout << "NCollection_Array1<int> Reverse: " <<
891 (aResult ? "SUCCESS" : "FAIL") << std::endl;
893 aResult = TestReverse<NCollection_Array1<double>, std::vector<double> >();
894 std::cout << "NCollection_Array1<double> Reverse: " <<
895 (aResult ? "SUCCESS" : "FAIL") << std::endl;
897 aResult = TestSort<NCollection_Array1<int>, std::vector<int> >();
898 std::cout << "NCollection_Array1<int> Sort: " <<
899 (aResult ? "SUCCESS" : "FAIL") << std::endl;
901 aResult = TestSort<NCollection_Array1<double>, std::vector<double> >();
902 std::cout << "NCollection_Array1<double> Sort: " <<
903 (aResult ? "SUCCESS" : "FAIL") << std::endl;
905 aResult = TestParallel<NCollection_Array1<int>, std::vector<int> >();
906 std::cout << "NCollection_Array1<int> Parallel: " <<
907 (aResult ? "SUCCESS" : "FAIL") << std::endl;
909 aResult = TestParallel<NCollection_Array1<double>, std::vector<double> >();
910 std::cout << "NCollection_Array1<double> Parallel: " <<
911 (aResult ? "SUCCESS" : "FAIL") << std::endl;
916 //=======================================================================
917 //function : QANTestStlIterators
919 //=======================================================================
920 static Standard_Integer QANTestStlIterators (
921 Draw_Interpretor& theInterpretor, Standard_Integer, const char**)
923 QANListStlIterator (theInterpretor, 0, NULL);
924 QANArray1StlIterator (theInterpretor, 0, NULL);
925 QANVectorStlIterator (theInterpretor, 0, NULL);
926 QANSequenceStlIterator (theInterpretor, 0, NULL);
927 QANMapStlIterator (theInterpretor, 0, NULL);
928 QANDataMapStlIterator (theInterpretor, 0, NULL);
929 QANIndexedMapStlIterator (theInterpretor, 0, NULL);
930 QANIndexedDataMapStlIterator (theInterpretor, 0, NULL);
935 //=======================================================================
936 //function : TestPerformanceRandomIterator
938 //=======================================================================
939 template<class CollectionType, class StlType>
940 void TestPerformanceRandomIterator(Draw_Interpretor& di)
944 StlType* aVector (NULL);
945 CollectionType* aCollec (NULL);
947 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
949 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
954 RandomGenerator aRandomGen;
955 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
957 std::sort (aVector->begin(), aVector->end());
958 std::random_shuffle (aVector->begin(), aVector->end(), aRandomGen);
963 Standard_Real aStlTime = aTimer.ElapsedTime();
968 RandomGenerator aRandomGen;
969 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
971 std::sort (aCollec->begin(), aCollec->end());
972 std::random_shuffle (aCollec->begin(), aCollec->end(), aRandomGen);
977 Standard_Real aOccTime = aTimer.ElapsedTime();
979 di << aSize << "\t" << aStlTime << "\t" <<
980 aOccTime << "\t" << aOccTime / aStlTime << "\n";
982 // check that result is the same
983 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
984 di << "Error: sequences are not the same at the end!\n";
991 //=======================================================================
992 //function : TestPerformanceForwardIterator
994 //=======================================================================
995 template<class CollectionType, class StlType>
996 void TestPerformanceForwardIterator(Draw_Interpretor& di)
1000 StlType* aVector = 0;
1001 CollectionType* aCollec = 0;
1003 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
1005 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
1010 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1012 std::replace (aVector->begin(), aVector->end(), *aVector->begin(), static_cast<typename StlType::value_type> (anIdx));
1017 Standard_Real aStlTime = aTimer.ElapsedTime();
1022 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1024 std::replace (aCollec->begin(), aCollec->end(), *aCollec->begin(), static_cast<typename CollectionType::value_type> (anIdx));
1029 Standard_Real aOccTime = aTimer.ElapsedTime();
1031 di << aSize << "\t" << aStlTime << "\t" <<
1032 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1034 // check that result is the same
1035 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1036 di << "Error: sequences are not the same at the end!\n";
1043 //=======================================================================
1044 //function : TestPerformanceBidirIterator
1046 //=======================================================================
1047 template<class CollectionType, class StlType>
1048 void TestPerformanceBidirIterator(Draw_Interpretor& di)
1052 StlType* aVector = 0;
1053 CollectionType* aCollec = 0;
1055 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
1057 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
1062 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1064 std::reverse (aVector->begin(), aVector->end());
1069 Standard_Real aStlTime = aTimer.ElapsedTime();
1074 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1076 std::reverse (aCollec->begin(), aCollec->end());
1081 Standard_Real aOccTime = aTimer.ElapsedTime();
1083 di << aSize << "\t" << aStlTime << "\t" <<
1084 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1086 // check that result is the same
1087 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1088 di << "Error: sequences are not the same at the end!\n";
1095 //=======================================================================
1096 //function : TestPerformanceMapAccess
1098 //=======================================================================
1099 template<class CollectionType, class T>
1100 void TestPerformanceMapAccess(Draw_Interpretor& di)
1104 CollectionType* aCollec (NULL);
1106 for (Standard_Integer aSize = 100000; aSize <= 3200000; aSize *= 2)
1108 MapFiller<CollectionType, T>::Perform (&aCollec, aSize);
1110 std::set<T> aSet (aCollec->cbegin(), aCollec->cend());
1111 std::vector<T> aVec (aCollec->cbegin(), aCollec->cend());
1113 Standard_Boolean aResult = Standard_True;
1118 for (size_t anIdx = 0; anIdx < 10000; ++anIdx)
1120 if (aSet.find (aVec[anIdx + 1000]) == aSet.end())
1121 aResult = Standard_False;
1122 if (aSet.find (aVec[anIdx + 2000]) == aSet.end())
1123 aResult = Standard_False;
1124 if (aSet.find (aVec[anIdx + 3000]) == aSet.end())
1125 aResult = Standard_False;
1126 if (aSet.find (aVec[anIdx + 4000]) == aSet.end())
1127 aResult = Standard_False;
1128 if (aSet.find (aVec[anIdx + 5000]) == aSet.end())
1129 aResult = Standard_False;
1134 Standard_Real aStlTime = aTimer.ElapsedTime();
1139 for (size_t anIdx = 0; anIdx < 10000; ++anIdx)
1141 if (!aCollec->Contains (aVec[anIdx + 1000]))
1142 aResult = Standard_False;
1143 if (!aCollec->Contains (aVec[anIdx + 2000]))
1144 aResult = Standard_False;
1145 if (!aCollec->Contains (aVec[anIdx + 3000]))
1146 aResult = Standard_False;
1147 if (!aCollec->Contains (aVec[anIdx + 4000]))
1148 aResult = Standard_False;
1149 if (!aCollec->Contains (aVec[anIdx + 5000]))
1150 aResult = Standard_False;
1155 Standard_Real aOccTime = aTimer.ElapsedTime();
1159 di << aSize << "\t" << aStlTime << "\t" <<
1160 aOccTime << "\t" << (aStlTime > 1e-16 ? aOccTime / aStlTime : -1) << "\n";
1167 //=======================================================================
1168 //function : QANTestNCollectionPerformance
1170 //=======================================================================
1171 static Standard_Integer QANTestNCollectionPerformance (Draw_Interpretor& di, Standard_Integer, const char**)
1173 di << "Testing performance (Size | STL time | OCCT time | STL/OCCT boost)\n";
1175 di << "\nstd::vector vs NCollection_Array1 (sort):\n\n";
1176 TestPerformanceRandomIterator<NCollection_Array1<double>, std::vector<double> >(di);
1178 di << "\nstd::vector vs NCollection_Vector (sort):\n\n";
1179 TestPerformanceRandomIterator<NCollection_Vector<double>, std::vector<double> >(di);
1181 di << "\nstd::vector vs NCollection_Array1 (replace):\n\n";
1182 TestPerformanceForwardIterator<NCollection_Array1<double>, std::vector<double> >(di);
1184 di << "\nstd::vector vs NCollection_Vector (replace):\n\n";
1185 TestPerformanceForwardIterator<NCollection_Vector<double>, std::vector<double> >(di);
1187 di << "\nstd::list vs NCollection_List (replace):\n\n";
1188 TestPerformanceForwardIterator<NCollection_List<double>, std::list<double> >(di);
1190 di << "\nstd::list vs NCollection_Sequence (replace):\n\n";
1191 TestPerformanceForwardIterator<NCollection_Sequence<double>, std::list<double> >(di);
1193 di << "\nstd::list vs NCollection_Sequence (reverse):\n\n";
1194 TestPerformanceBidirIterator<NCollection_Sequence<double>, std::list<double> >(di);
1196 di << "\nstd::set vs NCollection_Map (search):\n\n";
1197 TestPerformanceMapAccess<NCollection_Map<int>, int>(di);
1199 di << "\nstd::set vs NCollection_IndexedMap (search):\n\n";
1200 TestPerformanceMapAccess<NCollection_IndexedMap<int>, int>(di);
1205 //=======================================================================
1206 //function : QANTestNCollectionIndexedMap
1208 //=======================================================================
1209 static Standard_Integer QANTestNCollectionIndexedMap (Draw_Interpretor& di, Standard_Integer, const char**)
1213 std::vector<Standard_Integer> aIndxs;
1214 std::vector<Standard_Integer> aItems;
1215 NCollection_IndexedMap<Standard_Integer> aIndxMap;
1217 const Standard_Integer aNbItems = 1000000;
1220 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1222 const Standard_Integer aVal = anId * 2;
1224 aIndxs.push_back (anId);
1225 aItems.push_back (aVal);
1227 aIndxMap.Add (aVal);
1231 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1233 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1235 std::cout << "failed FindIndex\n";
1238 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1240 std::cout << "failed FindKey\n";
1245 const Standard_Real aTime1 = aTimer.ElapsedTime();
1249 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1251 const Standard_Integer anId2 = Min (aNbItems - 1,
1252 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1254 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1256 std::swap (aIndxs[anId], aIndxs[anId2]);
1260 const Standard_Real aTime2 = aTimer.ElapsedTime();
1264 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1266 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1268 std::cout << "failed FindIndex\n";
1271 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1273 std::cout << "failed FindKey\n";
1278 const Standard_Real aTime3 = aTimer.ElapsedTime();
1282 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1284 aIndxMap.RemoveLast();
1288 const Standard_Real aTime4 = aTimer.ElapsedTime();
1290 di << "Search time 1: " << aTime1 << "\n"
1291 << "Swapping time: " << aTime2 << "\n"
1292 << "Search time 2: " << aTime3 << "\n"
1293 << "Remove time: " << aTime4 << "\n";
1298 //=======================================================================
1299 //function : QANTestNCollectionIndexedDataMap
1301 //=======================================================================
1302 static Standard_Integer QANTestNCollectionIndexedDataMap (Draw_Interpretor& di, Standard_Integer, const char**)
1306 std::vector<Standard_Integer> aIndxs;
1307 std::vector<Standard_Integer> aItems;
1308 NCollection_IndexedDataMap<Standard_Integer, Standard_Integer> aIndxMap;
1310 const Standard_Integer aNbItems = 1000000;
1313 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1315 const Standard_Integer aVal = anId * 2;
1317 aIndxs.push_back (anId);
1318 aItems.push_back (aVal);
1320 aIndxMap.Add (aVal, aVal * 2);
1324 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1326 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1328 std::cout << "failed FindIndex\n";
1331 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1333 std::cout << "failed FindKey\n";
1338 const Standard_Real aTime1 = aTimer.ElapsedTime();
1342 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1344 const Standard_Integer anId2 = Min (aNbItems - 1,
1345 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1347 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1349 std::swap (aIndxs[anId], aIndxs[anId2]);
1353 const Standard_Real aTime2 = aTimer.ElapsedTime();
1357 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1359 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1361 std::cout << "failed FindIndex\n";
1364 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1366 std::cout << "failed FindKey\n";
1371 const Standard_Real aTime3 = aTimer.ElapsedTime();
1375 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1377 aIndxMap.RemoveLast();
1381 const Standard_Real aTime4 = aTimer.ElapsedTime();
1383 di << "Search time 1: " << aTime1 << "\n"
1384 << "Swapping time: " << aTime2 << "\n"
1385 << "Search time 2: " << aTime3 << "\n"
1386 << "Remove time: " << aTime4 << "\n";
1391 //=======================================================================
1392 //function : CommandsStl
1394 //=======================================================================
1395 void QANCollection::CommandsStl (Draw_Interpretor& theCommands)
1397 const char* aGroup = "QANCollection";
1399 theCommands.Add ("QANArray1StlIterator",
1400 "QANArray1StlIterator",
1402 QANArray1StlIterator,
1405 theCommands.Add ("QANListStlIterator",
1406 "QANListStlIterator",
1411 theCommands.Add ("QANSequenceStlIterator",
1412 "QANSequenceStlIterator",
1414 QANSequenceStlIterator,
1417 theCommands.Add ("QANVectorStlIterator",
1418 "QANVectorStlIterator",
1420 QANVectorStlIterator,
1423 theCommands.Add ("QANMapStlIterator",
1424 "QANMapStlIterator",
1429 theCommands.Add ("QANDataMapStlIterator",
1430 "QANDataMapStlIterator",
1432 QANDataMapStlIterator,
1435 theCommands.Add ("QANIndexedMapStlIterator",
1436 "QANIndexedMapStlIterator",
1438 QANIndexedMapStlIterator,
1441 theCommands.Add ("QANIndexedDataMapStlIterator",
1442 "QANIndexedDataMapStlIterator",
1444 QANIndexedDataMapStlIterator,
1447 theCommands.Add ("QANTestStlIterators",
1448 "QANTestStlIterators",
1450 QANTestStlIterators,
1453 theCommands.Add ("QANTestNCollectionPerformance",
1454 "QANTestNCollectionPerformance",
1456 QANTestNCollectionPerformance,
1459 theCommands.Add ("QANTestNCollectionIndexedMap",
1460 "QANTestNCollectionIndexedMap",
1462 QANTestNCollectionIndexedMap,
1465 theCommands.Add ("QANTestNCollectionIndexedDataMap",
1466 "QANTestNCollectionIndexedDataMap",
1468 QANTestNCollectionIndexedDataMap,