Home | History | Annotate | Download | only in unit
      1 #include <vector>
      2 #include <list>
      3 #include <algorithm>
      4 #include <numeric>
      5 
      6 #include "iota.h"
      7 #include "cppunit/cppunit_proxy.h"
      8 
      9 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
     10 using namespace std;
     11 #endif
     12 
     13 //
     14 // TestCase class
     15 //
     16 class IterTest : public CPPUNIT_NS::TestCase
     17 {
     18   CPPUNIT_TEST_SUITE(IterTest);
     19   CPPUNIT_TEST(iter1);
     20   CPPUNIT_TEST(iter3);
     21   CPPUNIT_TEST(iter4);
     22   CPPUNIT_TEST(iterswp0);
     23   CPPUNIT_TEST(iterswp1);
     24   CPPUNIT_TEST(iterswp2);
     25   CPPUNIT_TEST(iterswp3);
     26   CPPUNIT_TEST_SUITE_END();
     27 
     28 protected:
     29   void iter1();
     30   void iter3();
     31   void iter4();
     32   void iterswp0();
     33   void iterswp1();
     34   void iterswp2();
     35   void iterswp3();
     36 };
     37 
     38 CPPUNIT_TEST_SUITE_REGISTRATION(IterTest);
     39 
     40 //
     41 // tests implementation
     42 //
     43 void IterTest::iter1()
     44 {
     45   vector<const char*> v; // Vector of character strings.
     46   v.push_back("zippy"); // First element.
     47   v.push_back("motorboy"); // Second element.
     48   typedef vector<const char*> vec;
     49   unsigned counter = 0;
     50   for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) {
     51     switch (counter) {
     52       case 0:
     53         CPPUNIT_ASSERT(!strcmp(*i, "zippy"));
     54         break;
     55       case 1:
     56         CPPUNIT_ASSERT(!strcmp(*i, "motorboy"));
     57         break;
     58       default:
     59         CPPUNIT_FAIL;
     60     }
     61   }
     62 }
     63 void IterTest::iter3()
     64 {
     65   typedef vector<const char*> Vec;
     66   Vec v; // Vector of character strings.
     67   v.push_back("zippy"); // First element.
     68   v.push_back("motorboy"); // Second element.
     69   Vec::reverse_iterator it;
     70   unsigned counter = 0;
     71   for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
     72     switch (counter) {
     73       case 1:
     74         CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
     75         break;
     76       case 0:
     77         CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
     78         break;
     79       default:
     80         CPPUNIT_FAIL;
     81     }
     82   }
     83 }
     84 void IterTest::iter4()
     85 {
     86   vector<int> v; // Empty vector of integers.
     87   v.push_back(1);
     88   v.push_back(2);
     89   v.push_back(3);
     90   // Position immediately after last item.
     91   vector<int>::iterator i = v.end();
     92   // Move back one and then access.
     93   CPPUNIT_ASSERT((*--i)==3);
     94   i -= 2; // Jump back two items.
     95   CPPUNIT_ASSERT((*i)==1);
     96 }
     97 void IterTest::iterswp0()
     98 {
     99   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
    100 
    101   iter_swap(numbers, numbers + 3);
    102 
    103   CPPUNIT_ASSERT(numbers[0]==3);
    104   CPPUNIT_ASSERT(numbers[1]==1);
    105   CPPUNIT_ASSERT(numbers[2]==2);
    106   CPPUNIT_ASSERT(numbers[3]==0);
    107   CPPUNIT_ASSERT(numbers[4]==4);
    108   CPPUNIT_ASSERT(numbers[5]==5);
    109 
    110 }
    111 void IterTest::iterswp1()
    112 {
    113   vector<int> v1(6);
    114   __iota(v1.begin(), v1.end(), 0);
    115   iter_swap( v1.begin(), v1.begin() + 3 );
    116 
    117   CPPUNIT_ASSERT(v1[0]==3);
    118   CPPUNIT_ASSERT(v1[1]==1);
    119   CPPUNIT_ASSERT(v1[2]==2);
    120   CPPUNIT_ASSERT(v1[3]==0);
    121   CPPUNIT_ASSERT(v1[4]==4);
    122   CPPUNIT_ASSERT(v1[5]==5);
    123 }
    124 void IterTest::iterswp2()
    125 {
    126   vector<bool> boolVector;
    127 
    128   boolVector.push_back( true );
    129   boolVector.push_back( false );
    130 
    131   vector<bool>::iterator i1 = boolVector.begin();
    132   vector<bool>::iterator i2 = boolVector.begin();
    133   ++i2;
    134 
    135   bool v0 = *i1;
    136   bool v1 = *i2;
    137 
    138   iter_swap( i1, i2 );
    139 
    140   CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 ));
    141 }
    142 
    143 
    144 void IterTest::iterswp3()
    145 {
    146   vector<int> vvref(10, 10);
    147   vector<int> lvref(10, 20);
    148 
    149   vector<vector<int> > vvints(4, vvref);
    150   list<vector<int> > lvints(4, lvref);
    151 
    152   iter_swap(vvints.begin(), lvints.begin());
    153   CPPUNIT_CHECK( vvints.front() == lvref );
    154   CPPUNIT_CHECK( lvints.front() == vvref );
    155 
    156   //const vector<vector<int> > &cvvints = vvints;
    157   //iter_swap(cvvints.begin(), lvints.begin());
    158   //iter_swap(lvints.begin(), cvvints.begin());
    159 
    160 #if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
    161   int *pvvint = &vvints.front().front();
    162   int *plvint = &lvints.front().front();
    163 
    164   iter_swap(vvints.begin(), lvints.begin());
    165   //Check that elements have been swaped:
    166   CPPUNIT_CHECK( pvvint == &lvints.front().front() );
    167   CPPUNIT_CHECK( plvint == &vvints.front().front() );
    168 #endif
    169 }
    170