1 #include <numeric> 2 #include <vector> 3 #include <algorithm> 4 5 #include "iota.h" 6 #include "cppunit/cppunit_proxy.h" 7 8 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 9 using namespace std; 10 #endif 11 12 // 13 // TestCase class 14 // 15 class RndShuffleTest : public CPPUNIT_NS::TestCase 16 { 17 class MyRandomGenerator 18 { 19 public: 20 unsigned long operator()(unsigned long n_) 21 { 22 return rand() % n_; 23 } 24 }; 25 CPPUNIT_TEST_SUITE(RndShuffleTest); 26 CPPUNIT_TEST(rndshuf0); 27 CPPUNIT_TEST(rndshuf2); 28 CPPUNIT_TEST_SUITE_END(); 29 30 protected: 31 void rndshuf0(); 32 void rndshuf2(); 33 }; 34 35 CPPUNIT_TEST_SUITE_REGISTRATION(RndShuffleTest); 36 37 // 38 // tests implementation 39 // 40 void RndShuffleTest::rndshuf0() 41 { 42 int numbers[6] = { 1, 2, 3, 4, 5, 6 }; 43 44 random_shuffle(numbers, numbers + 6); 45 46 CPPUNIT_ASSERT(count(numbers, numbers+6, 1)==1); 47 CPPUNIT_ASSERT(count(numbers, numbers+6, 2)==1); 48 CPPUNIT_ASSERT(count(numbers, numbers+6, 3)==1); 49 CPPUNIT_ASSERT(count(numbers, numbers+6, 4)==1); 50 CPPUNIT_ASSERT(count(numbers, numbers+6, 5)==1); 51 CPPUNIT_ASSERT(count(numbers, numbers+6, 6)==1); 52 } 53 void RndShuffleTest::rndshuf2() 54 { 55 vector <int> v1(10); 56 __iota(v1.begin(), v1.end(), 0); 57 58 MyRandomGenerator r; 59 for(int i = 0; i < 3; i++) 60 { 61 random_shuffle(v1.begin(), v1.end(), r); 62 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 0)==1); 63 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 1)==1); 64 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 2)==1); 65 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 3)==1); 66 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 4)==1); 67 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 5)==1); 68 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 6)==1); 69 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 7)==1); 70 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 8)==1); 71 CPPUNIT_ASSERT(count(v1.begin(), v1.end(), 9)==1); 72 } 73 } 74