From ebde3063371a22a85959f38013293da89419198b Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Mon, 10 Oct 2005 15:02:57 +0000 Subject: [PATCH] Bug [ 1314453 ] AssocVector<>: hinted insert does not preservedering. Thanks to Christopher Twigg git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@294 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/AssocVector.h | 9 ++-- test/RegressionTest/AssocVectorTest.h | 73 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/include/loki/AssocVector.h b/include/loki/AssocVector.h index 142ba77..47f694e 100644 --- a/include/loki/AssocVector.h +++ b/include/loki/AssocVector.h @@ -175,13 +175,12 @@ namespace Loki } return std::make_pair(i, !found); } - + //Section [23.1.2], Table 69 + //http://developer.apple.com/documentation/DeveloperTools/gcc-3.3/libstdc++/23_containers/howto.html#4 iterator insert(iterator pos, const value_type& val) { - if (pos != end() && this->operator()(*pos, val) && - (pos == end() - 1 || - !this->operator()(val, pos[1]) && - this->operator()(pos[1], val))) + if( (pos == begin() || this->operator()(*(pos-1),val)) && + (pos == end() || this->operator()(val, *pos)) ) { return Base::insert(pos, val); } diff --git a/test/RegressionTest/AssocVectorTest.h b/test/RegressionTest/AssocVectorTest.h index bb87488..044d995 100755 --- a/test/RegressionTest/AssocVectorTest.h +++ b/test/RegressionTest/AssocVectorTest.h @@ -17,6 +17,7 @@ #include #include +#include #include #include "UnitTest.h" @@ -350,6 +351,77 @@ void test_vect5() } } +/////////////////////////////////////////////////////////////////////////////// +// test_vect6 +/////////////////////////////////////////////////////////////////////////////// + +void test_vect6() +{ + srand( (unsigned int) time(NULL) ); + typedef Loki::AssocVector IntMap; + + const unsigned int numTests = 20; + const unsigned int numSamples = 50; + for( unsigned int i = 0; i < numTests; ++i ) + { + IntMap testVec; + for( unsigned int i = 0; i < numSamples; ++i ) + { + std::pair newValue( + rand() % numSamples, + rand() % numSamples ); + + IntMap backupVec( testVec ); + + // assuming the basic insert operator is correct: + testVec.insert( newValue ); + + // test the boundary cases: + { + IntMap newVec( backupVec ); + newVec.insert( newVec.begin(), newValue ); + assert( newVec == testVec ); + + // check to make sure repeated inserts do not + // break anything + newVec.insert( newVec.begin(), newValue ); + newVec.insert( newVec.end(), newValue ); + newVec.insert( newVec.begin() + newVec.size()/2, newValue ); + newVec.insert( newVec.begin() + rand() % newVec.size(), newValue ); + assert( newVec == testVec ); + } + + { + IntMap newVec( backupVec ); + newVec.insert( newVec.end(), newValue ); + assert( newVec == testVec ); + } + + // try the middle: + { + IntMap newVec( backupVec ); + IntMap::iterator itr = newVec.begin(); + std::advance( itr, newVec.size()/2 ); + newVec.insert( itr, newValue ); + assert( newVec == testVec ); + } + + // try a random spot: + if( testVec.size() >= 2 ) + { + IntMap newVec( backupVec ); + IntMap::iterator itr = newVec.begin(); + std::advance( itr, rand() % (testVec.size()-1) + 1 ); + newVec.insert( itr, newValue ); + assert( newVec == testVec ); + } + } + } +} + + + + class AssocVectorTest : public Test { public: @@ -364,6 +436,7 @@ public: test_vect3(); test_vect4(); test_vect5(); + test_vect6(); testAssert("AssocVector",true,result),