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 #include <QANCollection.hxx>
17 #include <Draw_Interpretor.hxx>
19 #include <NCollection_Array1.hxx>
20 #include <NCollection_List.hxx>
21 #include <NCollection_Sequence.hxx>
22 #include <NCollection_Vector.hxx>
23 #include <NCollection_Map.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_IndexedMap.hxx>
26 #include <NCollection_IndexedDataMap.hxx>
27 #include <Standard_Assert.hxx>
28 #include <OSD_Timer.hxx>
29 #include <OSD_Parallel.hxx>
36 //! Size of test data sets.
37 const int THE_TEST_SIZE = 5000;
40 // Auxiliary class to use in std::random_shuffle()
41 struct RandomGenerator {
42 RandomGenerator () { srand(1); }
43 int operator () (int upper) const { return rand() % upper; }
47 template<class CollectionType, class StlType>
48 struct CollectionFiller
50 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
52 *theCollec = new CollectionType();
54 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
56 (*theCollec)->Append (rand());
60 static void Perform (StlType** theVector,
61 CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
63 CollectionFiller::Perform (theCollec, theSize);
65 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
69 template<class T, typename StlType>
70 struct CollectionFiller<NCollection_Array1<T>, StlType>
72 static void Perform (NCollection_Array1<T>** theCollec,
73 Standard_Integer theSize = THE_TEST_SIZE)
75 *theCollec = new NCollection_Array1<T> (0, theSize - 1);
77 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
79 (*theCollec)->ChangeValue (anIdx) = rand();
83 static void Perform (StlType** theVector,
84 NCollection_Array1<T>** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
86 CollectionFiller<NCollection_Array1<T>, StlType >::Perform (theCollec, theSize);
88 *theVector = new StlType ((*theCollec)->begin(), (*theCollec)->end());
92 template<class CollectionType, class T>
95 static void Perform (CollectionType** theCollec, Standard_Integer theSize = THE_TEST_SIZE)
97 *theCollec = new CollectionType();
99 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
101 (*theCollec)->Add (rand());
107 struct MapFiller<NCollection_DataMap<T, T>, T>
109 static void Perform (NCollection_DataMap<T, T>** theCollec1,
110 NCollection_DataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
112 *theCollec1 = new NCollection_DataMap<T, T>();
114 if (theCollec2 != NULL)
115 *theCollec2 = new NCollection_DataMap<T, T>();
117 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
119 const T aVal1 = rand();
120 const T aVal2 = rand();
122 (*theCollec1)->Bind (aVal1, aVal2);
124 if (theCollec2 != NULL)
125 (*theCollec2)->Bind (aVal1, aVal2);
131 struct MapFiller<NCollection_IndexedDataMap<T, T>, T>
133 static void Perform (NCollection_IndexedDataMap<T, T>** theCollec1,
134 NCollection_IndexedDataMap<T, T>** theCollec2 = NULL, Standard_Integer theSize = THE_TEST_SIZE)
136 *theCollec1 = new NCollection_IndexedDataMap<T, T>();
138 if (theCollec2 != NULL)
139 *theCollec2 = new NCollection_IndexedDataMap<T, T>();
141 for (Standard_Integer anIdx = 0; anIdx < theSize; ++anIdx)
143 const T aVal1 = rand();
144 const T aVal2 = rand();
146 (*theCollec1)->Add (aVal1, aVal2);
148 if (theCollec2 != NULL)
149 (*theCollec2)->Add (aVal1, aVal2);
154 //=======================================================================
155 //function : TestIteration
157 //=======================================================================
158 template<class CollectionType, class StlType>
159 Standard_Boolean TestIteration()
161 StlType* aVector (NULL);
162 CollectionType* aCollec (NULL);
164 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
166 typename StlType::iterator aVecIter = aVector->begin();
167 typename CollectionType::iterator aColIter = aCollec->begin();
169 Standard_Boolean aResult (Standard_True);
171 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
173 if (*aVecIter != *aColIter)
174 aResult = Standard_False;
177 if (aColIter != aCollec->end())
179 aResult = Standard_False;
188 //=======================================================================
189 //function : TestMinMax
191 //=======================================================================
192 template<class CollectionType, class StlType>
193 Standard_Boolean TestMinMax()
195 StlType* aVector (NULL);
196 CollectionType* aCollec (NULL);
198 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
200 typename StlType::value_type aValue1 = *std::min_element (aVector->begin(), aVector->end());
201 typename CollectionType::value_type aValue2 = *std::min_element (aCollec->begin(), aCollec->end());
203 Standard_Boolean aResult (Standard_True);
205 if (aValue1 != aValue2)
206 aResult = Standard_False;
208 aValue1 = *std::max_element (aVector->begin(), aVector->end());
209 aValue2 = *std::max_element (aCollec->begin(), aCollec->end());
211 if (aValue1 != aValue2)
212 aResult = Standard_False;
220 //=======================================================================
221 //function : TestReplace
223 //=======================================================================
224 template<class CollectionType, class StlType>
225 Standard_Boolean TestReplace()
227 StlType* aVector (NULL);
228 CollectionType* aCollec (NULL);
230 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
232 const typename StlType::value_type aValue = aVector->back();
234 std::replace (aVector->begin(), aVector->end(), aValue, static_cast<typename StlType::value_type> (-1));
235 std::replace (aCollec->begin(), aCollec->end(), aValue, static_cast<typename CollectionType::value_type> (-1));
237 typename StlType::iterator aVecIter = aVector->begin();
238 typename CollectionType::iterator aColIter = aCollec->begin();
240 Standard_Boolean aResult (Standard_True);
242 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
244 if (*aVecIter != *aColIter)
245 aResult = Standard_False;
248 if (aColIter != aCollec->end())
250 aResult = Standard_False;
259 //=======================================================================
260 //function : TestReverse
262 //=======================================================================
263 template<class CollectionType, class StlType>
264 Standard_Boolean TestReverse()
266 StlType* aVector (NULL);
267 CollectionType* aCollec (NULL);
269 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
271 std::reverse (aVector->begin(), aVector->end());
272 std::reverse (aCollec->begin(), aCollec->end());
274 typename StlType::iterator aVecIter = aVector->begin();
275 typename CollectionType::iterator aColIter = aCollec->begin();
277 Standard_Boolean aResult (Standard_True);
279 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
281 if (*aVecIter != *aColIter)
282 aResult = Standard_False;
285 if (aColIter != aCollec->end())
287 aResult = Standard_False;
296 //=======================================================================
297 //function : TestSort
299 //=======================================================================
300 template<class CollectionType, class StlType>
301 Standard_Boolean TestSort()
303 StlType* aVector (NULL);
304 CollectionType* aCollec (NULL);
306 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
308 std::sort (aVector->begin(), aVector->end());
309 std::sort (aCollec->begin(), aCollec->end());
311 typename StlType::iterator aVecIter = aVector->begin();
312 typename CollectionType::iterator aColIter = aCollec->begin();
314 Standard_Boolean aResult (Standard_True);
316 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
318 if (*aVecIter != *aColIter)
319 aResult = Standard_False;
322 if (aColIter != aCollec->end())
324 aResult = Standard_False;
333 template <typename T>
336 void operator()(T& theValue) const
342 //=======================================================================
343 //function : TestParallel
345 //=======================================================================
346 template<class CollectionType, class StlType>
347 Standard_Boolean TestParallel()
349 StlType* aVector (NULL);
350 CollectionType* aCollec (NULL);
352 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec);
354 OSD_Parallel::ForEach(aVector->begin(), aVector->end(), Invoker<typename StlType::value_type>());
355 OSD_Parallel::ForEach(aCollec->begin(), aCollec->end(), Invoker<typename CollectionType::value_type>());
357 typename StlType::iterator aVecIter = aVector->begin();
358 typename CollectionType::iterator aColIter = aCollec->begin();
360 Standard_Boolean aResult (Standard_True);
362 for (; aVecIter != aVector->end(); ++aVecIter, ++aColIter)
364 if (*aVecIter != *aColIter)
365 aResult = Standard_False;
368 if (aColIter != aCollec->end())
370 aResult = Standard_False;
379 //=======================================================================
380 //function : TestDataMapParallel
382 //=======================================================================
383 template<class CollectionType, class T>
384 Standard_Boolean TestDataMapParallel()
386 CollectionType* aCollec1 (NULL);
387 CollectionType* aCollec2 (NULL);
389 MapFiller<CollectionType, T>::Perform (&aCollec1, &aCollec2);
391 OSD_Parallel::ForEach(aCollec1->begin(), aCollec1->end(), Invoker<T>());
393 // create OCCT-style iterator
394 typename CollectionType::Iterator aOccIter (*aCollec2);
396 // create STL-compatible iterator
397 typename CollectionType::const_iterator aStlIter = aCollec1->cbegin();
399 Standard_Boolean aResult (Standard_True);
401 for (; aStlIter != aCollec1->cend(); ++aStlIter, aOccIter.Next())
403 if (static_cast<T> (2) * aOccIter.Value() != *aStlIter)
404 aResult = Standard_False;
409 aResult = Standard_False;
418 //=======================================================================
419 //function : TestMapIteration
421 //=======================================================================
422 template<class CollectionType, class T>
423 Standard_Boolean TestMapIteration()
425 CollectionType* aCollec (NULL);
427 MapFiller<CollectionType, T>::Perform (&aCollec);
429 // create OCCT-style iterator
430 typename CollectionType::Iterator aOccIter (*aCollec);
432 // create STL-compatible iterator
433 typename CollectionType::const_iterator aStlIter = aCollec->cbegin();
435 Standard_Boolean aResult (Standard_True);
437 for (; aStlIter != aCollec->cend(); ++aStlIter, aOccIter.Next())
439 if (aOccIter.Value() != *aStlIter)
440 aResult = Standard_False;
445 aResult = Standard_False;
453 //=======================================================================
454 //function : TestForwardIterator
455 //purpose : test basic features of iterator (forward)
456 //=======================================================================
457 template <class CollectionType>
458 void TestForwardIterator ()
460 CollectionType* aCollec (NULL);
462 CollectionFiller<CollectionType, void>::Perform (&aCollec);
464 // test non-const iteration
465 typename CollectionType::iterator it = aCollec->begin(); // copy construction
466 typename CollectionType::iterator it2; // default constructor
467 it2 = it; // assignment
468 it2 = it++; // postfix increment
469 if (it2 == it || ! (it2 != it))
470 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
471 it2 = ++it; // prefix increment
472 if (it2 != it || ! (it2 == it))
473 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
475 typename CollectionType::iterator::value_type t = *it;
477 *(it2.operator-> ()) = t;
479 // test const iteration
480 typename CollectionType::const_iterator cit = aCollec->cbegin(); // copy construction
481 typename CollectionType::const_iterator cit2; // default constructor
482 cit2 = cit; // assignment
483 cit2 = cit++; // postfix increment
484 if (cit2 == cit || ! (cit2 != cit))
485 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
486 cit2 = ++cit; // prefix increment
487 if (cit2 != it || ! (cit2 == cit))
488 std::cout << "Failed " << typeid(cit).name() << " equality check" << std::endl;
490 typename CollectionType::const_iterator::value_type ct = *cit;
494 // *(cit2.operator-> ()) = t;
499 //=======================================================================
500 //function : TestBidirIterator
501 //purpose : test features of bidirectional iterator
502 //=======================================================================
503 template <class CollectionType>
504 void TestBidirIterator ()
506 CollectionType* aCollec (NULL);
508 CollectionFiller<CollectionType, void>::Perform (&aCollec);
510 // test non-const iteration
511 typename CollectionType::iterator it = aCollec->end(); // copy construction
512 typename CollectionType::iterator it2 = it--; // postfix decrement
513 if (it2 == it || ! (it2 != it))
514 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
515 it2 = --it; // prefix decrement
516 if (it2 != it || ! (it2 == it))
517 std::cout << "Failed " << typeid(it).name() << " equality check" << std::endl;
522 //=======================================================================
523 //function : TestRandomIterator
524 //purpose : test features of random iterator
525 //=======================================================================
526 template <class CollectionType>
527 void TestRandomIterator ()
529 CollectionType* aCollec (NULL);
531 CollectionFiller<CollectionType, void>::Perform (&aCollec);
533 // test non-const iteration
534 typename CollectionType::iterator it = aCollec->begin(); // copy construction
535 typename CollectionType::iterator it2 = it + 5;
537 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
538 if (it2 < it || it2 <= it || ! (it2 > it) || ! (it2 >= it))
539 std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl;
542 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
544 if ((it2 - it) != -5)
545 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
546 if (it2 > it || it2 >= it || ! (it2 < it) || ! (it2 <= it))
547 std::cout << "Failed " << typeid(it).name() << " comparison" << std::endl;
550 std::cout << "Failed " << typeid(it).name() << " arithmetics" << std::endl;
552 typename CollectionType::value_type t = it[5]; // offset dereference
558 //=======================================================================
559 //function : QANListStlIterator
561 //=======================================================================
562 static Standard_Integer QANListStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
564 // compile-time tests
565 TestForwardIterator <NCollection_List<Standard_Integer> >();
568 Standard_Boolean aResult = TestIteration<NCollection_List<int>, std::list<int> >();
569 std::cout << "NCollection_List<int> Iteration: " <<
570 (aResult ? "SUCCESS" : "FAIL") << std::endl;
572 aResult = TestIteration<NCollection_List<double>, std::list<double> >();
573 std::cout << "NCollection_List<double> Iteration: " <<
574 (aResult ? "SUCCESS" : "FAIL") << std::endl;
576 aResult = TestMinMax<NCollection_List<int>, std::list<int> >();
577 std::cout << "NCollection_List<int> Min-Max: " <<
578 (aResult ? "SUCCESS" : "FAIL") << std::endl;
580 aResult = TestMinMax<NCollection_List<double>, std::list<double> >();
581 std::cout << "NCollection_List<double> Min-Max: " <<
582 (aResult ? "SUCCESS" : "FAIL") << std::endl;
584 aResult = TestReplace<NCollection_List<int>, std::list<int> >();
585 std::cout << "NCollection_List<int> Replace: " <<
586 (aResult ? "SUCCESS" : "FAIL") << std::endl;
588 aResult = TestReplace<NCollection_List<double>, std::list<double> >();
589 std::cout << "NCollection_List<double> Replace: " <<
590 (aResult ? "SUCCESS" : "FAIL") << std::endl;
592 aResult = TestParallel< NCollection_List<int>, std::list<int> >();
593 std::cout << "NCollection_List<int> Parallel: " <<
594 (aResult ? "SUCCESS" : "FAIL") << std::endl;
596 aResult = TestParallel<NCollection_List<double>, std::list<double> >();
597 std::cout << "NCollection_List<double> Parallel: " <<
598 (aResult ? "SUCCESS" : "FAIL") << std::endl;
603 //=======================================================================
604 //function : QANMapStlIterator
606 //=======================================================================
607 static Standard_Integer QANMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
609 // compile-time tests
610 // TestForwardIterator <NCollection_Map<Standard_Integer> >();
613 Standard_Boolean aResult = TestMapIteration<NCollection_Map<Standard_Integer>, Standard_Integer>();
614 std::cout << "NCollection_Map<int> Iteration: " <<
615 (aResult ? "SUCCESS" : "FAIL") << std::endl;
617 aResult = TestMapIteration<NCollection_Map<Standard_Real>, Standard_Real>();
618 std::cout << "NCollection_Map<double> Iteration: " <<
619 (aResult ? "SUCCESS" : "FAIL") << std::endl;
624 //=======================================================================
625 //function : QANIndexedMapStlIterator
627 //=======================================================================
628 static Standard_Integer QANIndexedMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
630 // compile-time tests
631 // TestForwardIterator <NCollection_IndexedMap<Standard_Integer> >();
632 // TestBidirIterator <NCollection_IndexedMap<Standard_Integer> >();
635 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedMap<Standard_Integer>, Standard_Integer>();
636 std::cout << "NCollection_IndexedMap<int> Iteration: " <<
637 (aResult ? "SUCCESS" : "FAIL") << std::endl;
639 aResult = TestMapIteration<NCollection_IndexedMap<Standard_Real>, Standard_Real>();
640 std::cout << "NCollection_IndexedMap<double> Iteration: " <<
641 (aResult ? "SUCCESS" : "FAIL") << std::endl;
646 //=======================================================================
647 //function : QANDataMapStlIterator
649 //=======================================================================
650 static Standard_Integer QANDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
652 // compile-time tests
653 // TestForwardIterator <NCollection_DataMap<int, int> >();
654 // TestBidirIterator <NCollection_DataMap<int, int> >();
657 Standard_Boolean aResult = TestMapIteration<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
658 std::cout << "NCollection_DataMap<int> Iteration: " <<
659 (aResult ? "SUCCESS" : "FAIL") << std::endl;
661 aResult = TestMapIteration<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
662 std::cout << "NCollection_DataMap<double> Iteration: " <<
663 (aResult ? "SUCCESS" : "FAIL") << std::endl;
665 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
666 std::cout << "NCollection_DataMap<int> Parallel: " <<
667 (aResult ? "SUCCESS" : "FAIL") << std::endl;
669 aResult = TestDataMapParallel<NCollection_DataMap<Standard_Real, Standard_Real>, Standard_Real>();
670 std::cout << "NCollection_DataMap<double> Parallel: " <<
671 (aResult ? "SUCCESS" : "FAIL") << std::endl;
676 //=======================================================================
677 //function : QANIndexedDataMapStlIterator
679 //=======================================================================
680 static Standard_Integer QANIndexedDataMapStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
682 // compile-time tests
683 // TestForwardIterator <NCollection_IndexedDataMap<int, int> >();
684 // TestBidirIterator <NCollection_IndexedDataMap<int, int> >();
687 Standard_Boolean aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
688 std::cout << "NCollection_IndexedDataMap<int> Iteration: " <<
689 (aResult ? "SUCCESS" : "FAIL") << std::endl;
691 aResult = TestMapIteration<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
692 std::cout << "NCollection_IndexedDataMap<double> Iteration: " <<
693 (aResult ? "SUCCESS" : "FAIL") << std::endl;
695 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Integer, Standard_Integer>, Standard_Integer>();
696 std::cout << "NCollection_IndexedDataMap<int> Parallel: " <<
697 (aResult ? "SUCCESS" : "FAIL") << std::endl;
699 aResult = TestDataMapParallel<NCollection_IndexedDataMap<Standard_Real, Standard_Real>, Standard_Real>();
700 std::cout << "NCollection_IndexedDataMap<double> Parallel: " <<
701 (aResult ? "SUCCESS" : "FAIL") << std::endl;
706 //=======================================================================
707 //function : QANSequenceStlIterator
709 //=======================================================================
710 static Standard_Integer QANSequenceStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
712 // compile-time tests
713 TestForwardIterator <NCollection_Sequence<int> >();
714 TestBidirIterator <NCollection_Sequence<int> >();
717 Standard_Boolean aResult = TestIteration<NCollection_Sequence<int>, std::list<int> >();
718 std::cout << "NCollection_Sequence<int> Iteration: " <<
719 (aResult ? "SUCCESS" : "FAIL") << std::endl;
721 aResult = TestIteration<NCollection_Sequence<double>, std::list<double> >();
722 std::cout << "NCollection_Sequence<double> Iteration: " <<
723 (aResult ? "SUCCESS" : "FAIL") << std::endl;
725 aResult = TestMinMax<NCollection_Sequence<int>, std::list<int> >();
726 std::cout << "NCollection_Sequence<int> Min-Max: " <<
727 (aResult ? "SUCCESS" : "FAIL") << std::endl;
729 aResult = TestMinMax<NCollection_Sequence<double>, std::list<double> >();
730 std::cout << "NCollection_Sequence<double> Min-Max: " <<
731 (aResult ? "SUCCESS" : "FAIL") << std::endl;
733 aResult = TestReplace<NCollection_Sequence<int>, std::list<int> >();
734 std::cout << "NCollection_Sequence<int> Replace: " <<
735 (aResult ? "SUCCESS" : "FAIL") << std::endl;
737 aResult = TestReplace<NCollection_Sequence<double>, std::list<double> >();
738 std::cout << "NCollection_Sequence<double> Replace: " <<
739 (aResult ? "SUCCESS" : "FAIL") << std::endl;
741 aResult = TestReverse<NCollection_Sequence<int>, std::list<int> >();
742 std::cout << "NCollection_Sequence<int> Reverse: " <<
743 (aResult ? "SUCCESS" : "FAIL") << std::endl;
745 aResult = TestReverse<NCollection_Sequence<double>, std::list<double> >();
746 std::cout << "NCollection_Sequence<double> Reverse: " <<
747 (aResult ? "SUCCESS" : "FAIL") << std::endl;
749 aResult = TestParallel<NCollection_Sequence<int>, std::list<int> >();
750 std::cout << "NCollection_Sequence<int> Parallel: " <<
751 (aResult ? "SUCCESS" : "FAIL") << std::endl;
753 aResult = TestParallel<NCollection_Sequence<double>, std::list<double> >();
754 std::cout << "NCollection_Sequence<double> Parallel: " <<
755 (aResult ? "SUCCESS" : "FAIL") << std::endl;
760 //=======================================================================
761 //function : QANVectorStlIterator
763 //=======================================================================
764 static Standard_Integer QANVectorStlIterator (Draw_Interpretor&, Standard_Integer, const char**)
766 // compile-time tests
767 TestForwardIterator <NCollection_Vector<int> >();
768 TestBidirIterator <NCollection_Vector<int> >();
769 TestRandomIterator <NCollection_Vector<int> >();
772 Standard_Boolean aResult = TestIteration<NCollection_Vector<int>, std::vector<int> >();
773 std::cout << "NCollection_Vector<int> Iteration: " <<
774 (aResult ? "SUCCESS" : "FAIL") << std::endl;
776 aResult = TestIteration<NCollection_Vector<double>, std::vector<double> >();
777 std::cout << "NCollection_Vector<double> Iteration: " <<
778 (aResult ? "SUCCESS" : "FAIL") << std::endl;
780 aResult = TestMinMax<NCollection_Vector<int>, std::vector<int> >();
781 std::cout << "NCollection_Vector<int> Min-Max: " <<
782 (aResult ? "SUCCESS" : "FAIL") << std::endl;
784 aResult = TestMinMax<NCollection_Vector<double>, std::vector<double> >();
785 std::cout << "NCollection_Vector<double> Min-Max: " <<
786 (aResult ? "SUCCESS" : "FAIL") << std::endl;
788 aResult = TestReplace<NCollection_Vector<int>, std::vector<int> >();
789 std::cout << "NCollection_Vector<int> Replace: " <<
790 (aResult ? "SUCCESS" : "FAIL") << std::endl;
792 aResult = TestReplace<NCollection_Vector<double>, std::vector<double> >();
793 std::cout << "NCollection_Vector<double> Replace: " <<
794 (aResult ? "SUCCESS" : "FAIL") << std::endl;
796 aResult = TestReverse<NCollection_Vector<int>, std::vector<int> >();
797 std::cout << "NCollection_Vector<int> Reverse: " <<
798 (aResult ? "SUCCESS" : "FAIL") << std::endl;
800 aResult = TestReverse<NCollection_Vector<double>, std::vector<double> >();
801 std::cout << "NCollection_Vector<double> Reverse: " <<
802 (aResult ? "SUCCESS" : "FAIL") << std::endl;
804 aResult = TestSort<NCollection_Vector<int>, std::vector<int> >();
805 std::cout << "NCollection_Vector<int> Sort: " <<
806 (aResult ? "SUCCESS" : "FAIL") << std::endl;
808 aResult = TestSort<NCollection_Vector<double>, std::vector<double> >();
809 std::cout << "NCollection_Vector<double> Sort: " <<
810 (aResult ? "SUCCESS" : "FAIL") << std::endl;
812 aResult = TestParallel<NCollection_Vector<int>, std::vector<int> >();
813 std::cout << "NCollection_Vector<int> Parallel: " <<
814 (aResult ? "SUCCESS" : "FAIL") << std::endl;
816 aResult = TestParallel<NCollection_Vector<double>, std::vector<double> >();
817 std::cout << "NCollection_Vector<double> Parallel: " <<
818 (aResult ? "SUCCESS" : "FAIL") << std::endl;
823 //=======================================================================
824 //function : QANArray1StlIterator
826 //=======================================================================
827 static Standard_Integer QANArray1StlIterator (Draw_Interpretor&, Standard_Integer, const char**)
829 // compile-time tests
830 TestForwardIterator <NCollection_Vector<int> >();
831 TestBidirIterator <NCollection_Vector<int> >();
832 TestRandomIterator <NCollection_Vector<int> >();
835 Standard_Boolean aResult = TestIteration<NCollection_Array1<int>, std::vector<int> >();
836 std::cout << "NCollection_Array1<int> Iteration: " <<
837 (aResult ? "SUCCESS" : "FAIL") << std::endl;
839 aResult = TestIteration<NCollection_Array1<double>, std::vector<double> >();
840 std::cout << "NCollection_Array1<double> Iteration: " <<
841 (aResult ? "SUCCESS" : "FAIL") << std::endl;
843 aResult = TestMinMax<NCollection_Array1<int>, std::vector<int> >();
844 std::cout << "NCollection_Array1<int> Min-Max: " <<
845 (aResult ? "SUCCESS" : "FAIL") << std::endl;
847 aResult = TestMinMax<NCollection_Array1<double>, std::vector<double> >();
848 std::cout << "NCollection_Array1<double> Min-Max: " <<
849 (aResult ? "SUCCESS" : "FAIL") << std::endl;
851 aResult = TestReplace<NCollection_Array1<int>, std::vector<int> >();
852 std::cout << "NCollection_Array1<int> Replace: " <<
853 (aResult ? "SUCCESS" : "FAIL") << std::endl;
855 aResult = TestReplace<NCollection_Array1<double>, std::vector<double> >();
856 std::cout << "NCollection_Array1<double> Replace: " <<
857 (aResult ? "SUCCESS" : "FAIL") << std::endl;
859 aResult = TestReverse<NCollection_Array1<int>, std::vector<int> >();
860 std::cout << "NCollection_Array1<int> Reverse: " <<
861 (aResult ? "SUCCESS" : "FAIL") << std::endl;
863 aResult = TestReverse<NCollection_Array1<double>, std::vector<double> >();
864 std::cout << "NCollection_Array1<double> Reverse: " <<
865 (aResult ? "SUCCESS" : "FAIL") << std::endl;
867 aResult = TestSort<NCollection_Array1<int>, std::vector<int> >();
868 std::cout << "NCollection_Array1<int> Sort: " <<
869 (aResult ? "SUCCESS" : "FAIL") << std::endl;
871 aResult = TestSort<NCollection_Array1<double>, std::vector<double> >();
872 std::cout << "NCollection_Array1<double> Sort: " <<
873 (aResult ? "SUCCESS" : "FAIL") << std::endl;
875 aResult = TestParallel<NCollection_Array1<int>, std::vector<int> >();
876 std::cout << "NCollection_Array1<int> Parallel: " <<
877 (aResult ? "SUCCESS" : "FAIL") << std::endl;
879 aResult = TestParallel<NCollection_Array1<double>, std::vector<double> >();
880 std::cout << "NCollection_Array1<double> Parallel: " <<
881 (aResult ? "SUCCESS" : "FAIL") << std::endl;
886 //=======================================================================
887 //function : QANTestStlIterators
889 //=======================================================================
890 static Standard_Integer QANTestStlIterators (
891 Draw_Interpretor& theInterpretor, Standard_Integer, const char**)
893 QANListStlIterator (theInterpretor, 0, NULL);
894 QANArray1StlIterator (theInterpretor, 0, NULL);
895 QANVectorStlIterator (theInterpretor, 0, NULL);
896 QANSequenceStlIterator (theInterpretor, 0, NULL);
897 QANMapStlIterator (theInterpretor, 0, NULL);
898 QANDataMapStlIterator (theInterpretor, 0, NULL);
899 QANIndexedMapStlIterator (theInterpretor, 0, NULL);
900 QANIndexedDataMapStlIterator (theInterpretor, 0, NULL);
905 //=======================================================================
906 //function : TestPerformanceRandomIterator
908 //=======================================================================
909 template<class CollectionType, class StlType>
910 void TestPerformanceRandomIterator(Draw_Interpretor& di)
914 StlType* aVector (NULL);
915 CollectionType* aCollec (NULL);
917 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
919 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
924 RandomGenerator aRandomGen;
925 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
927 std::sort (aVector->begin(), aVector->end());
928 std::random_shuffle (aVector->begin(), aVector->end(), aRandomGen);
933 Standard_Real aStlTime = aTimer.ElapsedTime();
938 RandomGenerator aRandomGen;
939 for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx)
941 std::sort (aCollec->begin(), aCollec->end());
942 std::random_shuffle (aCollec->begin(), aCollec->end(), aRandomGen);
947 Standard_Real aOccTime = aTimer.ElapsedTime();
949 di << aSize << "\t" << aStlTime << "\t" <<
950 aOccTime << "\t" << aOccTime / aStlTime << "\n";
952 // check that result is the same
953 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
954 di << "Error: sequences are not the same at the end!\n";
961 //=======================================================================
962 //function : TestPerformanceForwardIterator
964 //=======================================================================
965 template<class CollectionType, class StlType>
966 void TestPerformanceForwardIterator(Draw_Interpretor& di)
970 StlType* aVector = 0;
971 CollectionType* aCollec = 0;
973 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
975 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
980 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
982 std::replace (aVector->begin(), aVector->end(), *aVector->begin(), static_cast<typename StlType::value_type> (anIdx));
987 Standard_Real aStlTime = aTimer.ElapsedTime();
992 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
994 std::replace (aCollec->begin(), aCollec->end(), *aCollec->begin(), static_cast<typename CollectionType::value_type> (anIdx));
999 Standard_Real aOccTime = aTimer.ElapsedTime();
1001 di << aSize << "\t" << aStlTime << "\t" <<
1002 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1004 // check that result is the same
1005 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1006 di << "Error: sequences are not the same at the end!\n";
1013 //=======================================================================
1014 //function : TestPerformanceBidirIterator
1016 //=======================================================================
1017 template<class CollectionType, class StlType>
1018 void TestPerformanceBidirIterator(Draw_Interpretor& di)
1022 StlType* aVector = 0;
1023 CollectionType* aCollec = 0;
1025 for (Standard_Integer aSize = 10000; aSize <= 1280000; aSize *= 2)
1027 CollectionFiller<CollectionType, StlType>::Perform (&aVector, &aCollec, aSize);
1032 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1034 std::reverse (aVector->begin(), aVector->end());
1039 Standard_Real aStlTime = aTimer.ElapsedTime();
1044 for (Standard_Integer anIdx = 0; anIdx < 1000; ++anIdx)
1046 std::reverse (aCollec->begin(), aCollec->end());
1051 Standard_Real aOccTime = aTimer.ElapsedTime();
1053 di << aSize << "\t" << aStlTime << "\t" <<
1054 aOccTime << "\t" << aOccTime / aStlTime << "\n";
1056 // check that result is the same
1057 if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) )
1058 di << "Error: sequences are not the same at the end!\n";
1065 //=======================================================================
1066 //function : TestPerformanceMapAccess
1068 //=======================================================================
1069 template<class CollectionType, class T>
1070 void TestPerformanceMapAccess(Draw_Interpretor& di)
1074 CollectionType* aCollec (NULL);
1076 for (Standard_Integer aSize = 100000; aSize <= 3200000; aSize *= 2)
1078 MapFiller<CollectionType, T>::Perform (&aCollec, aSize);
1080 std::set<T> aSet (aCollec->cbegin(), aCollec->cend());
1081 std::vector<T> aVec (aCollec->cbegin(), aCollec->cend());
1083 Standard_Boolean aResult = Standard_True;
1088 for (Standard_Integer anIdx = 0; anIdx < 10000; ++anIdx)
1090 if (aSet.find (aVec[anIdx + 1000]) == aSet.end())
1091 aResult = Standard_False;
1092 if (aSet.find (aVec[anIdx + 2000]) == aSet.end())
1093 aResult = Standard_False;
1094 if (aSet.find (aVec[anIdx + 3000]) == aSet.end())
1095 aResult = Standard_False;
1096 if (aSet.find (aVec[anIdx + 4000]) == aSet.end())
1097 aResult = Standard_False;
1098 if (aSet.find (aVec[anIdx + 5000]) == aSet.end())
1099 aResult = Standard_False;
1104 Standard_Real aStlTime = aTimer.ElapsedTime();
1109 for (Standard_Integer anIdx = 0; anIdx < 10000; ++anIdx)
1111 if (!aCollec->Contains (aVec[anIdx + 1000]))
1112 aResult = Standard_False;
1113 if (!aCollec->Contains (aVec[anIdx + 2000]))
1114 aResult = Standard_False;
1115 if (!aCollec->Contains (aVec[anIdx + 3000]))
1116 aResult = Standard_False;
1117 if (!aCollec->Contains (aVec[anIdx + 4000]))
1118 aResult = Standard_False;
1119 if (!aCollec->Contains (aVec[anIdx + 5000]))
1120 aResult = Standard_False;
1125 Standard_Real aOccTime = aTimer.ElapsedTime();
1129 di << aSize << "\t" << aStlTime << "\t" <<
1130 aOccTime << "\t" << (aStlTime > 1e-16 ? aOccTime / aStlTime : -1) << "\n";
1137 //=======================================================================
1138 //function : QANTestNCollectionPerformance
1140 //=======================================================================
1141 static Standard_Integer QANTestNCollectionPerformance (Draw_Interpretor& di, Standard_Integer, const char**)
1143 di << "Testing performance (Size | STL time | OCCT time | STL/OCCT boost)\n";
1145 di << "\nstd::vector vs NCollection_Array1 (sort):\n\n";
1146 TestPerformanceRandomIterator<NCollection_Array1<double>, std::vector<double> >(di);
1148 di << "\nstd::vector vs NCollection_Vector (sort):\n\n";
1149 TestPerformanceRandomIterator<NCollection_Vector<double>, std::vector<double> >(di);
1151 di << "\nstd::vector vs NCollection_Array1 (replace):\n\n";
1152 TestPerformanceForwardIterator<NCollection_Array1<double>, std::vector<double> >(di);
1154 di << "\nstd::vector vs NCollection_Vector (replace):\n\n";
1155 TestPerformanceForwardIterator<NCollection_Vector<double>, std::vector<double> >(di);
1157 di << "\nstd::list vs NCollection_List (replace):\n\n";
1158 TestPerformanceForwardIterator<NCollection_List<double>, std::list<double> >(di);
1160 di << "\nstd::list vs NCollection_Sequence (replace):\n\n";
1161 TestPerformanceForwardIterator<NCollection_Sequence<double>, std::list<double> >(di);
1163 di << "\nstd::list vs NCollection_Sequence (reverse):\n\n";
1164 TestPerformanceBidirIterator<NCollection_Sequence<double>, std::list<double> >(di);
1166 di << "\nstd::set vs NCollection_Map (search):\n\n";
1167 TestPerformanceMapAccess<NCollection_Map<int>, int>(di);
1169 di << "\nstd::set vs NCollection_IndexedMap (search):\n\n";
1170 TestPerformanceMapAccess<NCollection_IndexedMap<int>, int>(di);
1175 //=======================================================================
1176 //function : QANTestNCollectionIndexedMap
1178 //=======================================================================
1179 static Standard_Integer QANTestNCollectionIndexedMap (Draw_Interpretor& di, Standard_Integer, const char**)
1183 std::vector<Standard_Integer> aIndxs;
1184 std::vector<Standard_Integer> aItems;
1185 NCollection_IndexedMap<Standard_Integer> aIndxMap;
1187 const Standard_Integer aNbItems = 1000000;
1190 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1192 const Standard_Integer aVal = anId * 2;
1194 aIndxs.push_back (anId);
1195 aItems.push_back (aVal);
1197 aIndxMap.Add (aVal);
1201 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1203 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1205 std::cout << "failed FindIndex\n";
1208 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1210 std::cout << "failed FindKey\n";
1215 const Standard_Real aTime1 = aTimer.ElapsedTime();
1219 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1221 const Standard_Integer anId2 = Min (aNbItems - 1,
1222 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1224 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1226 std::swap (aIndxs[anId], aIndxs[anId2]);
1230 const Standard_Real aTime2 = aTimer.ElapsedTime();
1234 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1236 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1238 std::cout << "failed FindIndex\n";
1241 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1243 std::cout << "failed FindKey\n";
1248 const Standard_Real aTime3 = aTimer.ElapsedTime();
1252 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1254 aIndxMap.RemoveLast();
1258 const Standard_Real aTime4 = aTimer.ElapsedTime();
1260 di << "Search time 1: " << aTime1 << "\n"
1261 << "Swapping time: " << aTime2 << "\n"
1262 << "Search time 2: " << aTime3 << "\n"
1263 << "Remove time: " << aTime4 << "\n";
1268 //=======================================================================
1269 //function : QANTestNCollectionIndexedDataMap
1271 //=======================================================================
1272 static Standard_Integer QANTestNCollectionIndexedDataMap (Draw_Interpretor& di, Standard_Integer, const char**)
1276 std::vector<Standard_Integer> aIndxs;
1277 std::vector<Standard_Integer> aItems;
1278 NCollection_IndexedDataMap<Standard_Integer, Standard_Integer> aIndxMap;
1280 const Standard_Integer aNbItems = 1000000;
1283 for (Standard_Integer anId = 1; anId <= aNbItems; ++anId)
1285 const Standard_Integer aVal = anId * 2;
1287 aIndxs.push_back (anId);
1288 aItems.push_back (aVal);
1290 aIndxMap.Add (aVal, aVal * 2);
1294 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1296 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1298 std::cout << "failed FindIndex\n";
1301 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1303 std::cout << "failed FindKey\n";
1308 const Standard_Real aTime1 = aTimer.ElapsedTime();
1312 for (Standard_Integer anId = 0; anId < aNbItems / 30; ++anId)
1314 const Standard_Integer anId2 = Min (aNbItems - 1,
1315 static_cast<Standard_Integer> (rand() / float (RAND_MAX) * aNbItems));
1317 aIndxMap.Swap (aIndxs[anId], aIndxs[anId2]);
1319 std::swap (aIndxs[anId], aIndxs[anId2]);
1323 const Standard_Real aTime2 = aTimer.ElapsedTime();
1327 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1329 if (aIndxMap.FindIndex (aItems[anId]) != aIndxs[anId])
1331 std::cout << "failed FindIndex\n";
1334 if (aIndxMap.FindKey (aIndxs[anId]) != aItems[anId])
1336 std::cout << "failed FindKey\n";
1341 const Standard_Real aTime3 = aTimer.ElapsedTime();
1345 for (Standard_Integer anId = 0; anId < aNbItems; ++anId)
1347 aIndxMap.RemoveLast();
1351 const Standard_Real aTime4 = aTimer.ElapsedTime();
1353 di << "Search time 1: " << aTime1 << "\n"
1354 << "Swapping time: " << aTime2 << "\n"
1355 << "Search time 2: " << aTime3 << "\n"
1356 << "Remove time: " << aTime4 << "\n";
1361 //=======================================================================
1362 //function : CommandsStl
1364 //=======================================================================
1365 void QANCollection::CommandsStl (Draw_Interpretor& theCommands)
1367 const char* aGroup = "QANCollection";
1369 theCommands.Add ("QANArray1StlIterator",
1370 "QANArray1StlIterator",
1372 QANArray1StlIterator,
1375 theCommands.Add ("QANListStlIterator",
1376 "QANListStlIterator",
1381 theCommands.Add ("QANSequenceStlIterator",
1382 "QANSequenceStlIterator",
1384 QANSequenceStlIterator,
1387 theCommands.Add ("QANVectorStlIterator",
1388 "QANVectorStlIterator",
1390 QANVectorStlIterator,
1393 theCommands.Add ("QANMapStlIterator",
1394 "QANMapStlIterator",
1399 theCommands.Add ("QANDataMapStlIterator",
1400 "QANDataMapStlIterator",
1402 QANDataMapStlIterator,
1405 theCommands.Add ("QANIndexedMapStlIterator",
1406 "QANIndexedMapStlIterator",
1408 QANIndexedMapStlIterator,
1411 theCommands.Add ("QANIndexedDataMapStlIterator",
1412 "QANIndexedDataMapStlIterator",
1414 QANIndexedDataMapStlIterator,
1417 theCommands.Add ("QANTestStlIterators",
1418 "QANTestStlIterators",
1420 QANTestStlIterators,
1423 theCommands.Add ("QANTestNCollectionPerformance",
1424 "QANTestNCollectionPerformance",
1426 QANTestNCollectionPerformance,
1429 theCommands.Add ("QANTestNCollectionIndexedMap",
1430 "QANTestNCollectionIndexedMap",
1432 QANTestNCollectionIndexedMap,
1435 theCommands.Add ("QANTestNCollectionIndexedDataMap",
1436 "QANTestNCollectionIndexedDataMap",
1438 QANTestNCollectionIndexedDataMap,