genericopenlibs/cppstdlib/stl/test/unit/iter_test.cpp
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <vector>
       
    18 #include <list>
       
    19 #include <algorithm>
       
    20 #include <numeric>
       
    21 
       
    22 #include "iota.h"
       
    23 #include "cppunit/cppunit_proxy.h"
       
    24 
       
    25 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    26 using namespace std;
       
    27 #endif
       
    28 
       
    29 //
       
    30 // TestCase class
       
    31 //
       
    32 class IterTest : public CPPUNIT_NS::TestCase
       
    33 {
       
    34   CPPUNIT_TEST_SUITE(IterTest);
       
    35   CPPUNIT_TEST(iter1);
       
    36   CPPUNIT_TEST(iter3);
       
    37   CPPUNIT_TEST(iter4);
       
    38   CPPUNIT_TEST(iterswp0);
       
    39   CPPUNIT_TEST(iterswp1);
       
    40   CPPUNIT_TEST(iterswp2);
       
    41   CPPUNIT_TEST(iterswp3);
       
    42   CPPUNIT_TEST_SUITE_END();
       
    43 
       
    44 protected:
       
    45   void iter1();
       
    46   void iter3();
       
    47   void iter4();
       
    48   void iterswp0();
       
    49   void iterswp1();
       
    50   void iterswp2();
       
    51   void iterswp3();
       
    52 };
       
    53 
       
    54 CPPUNIT_TEST_SUITE_REGISTRATION(IterTest);
       
    55 
       
    56 //
       
    57 // tests implementation
       
    58 //
       
    59 void IterTest::iter1()
       
    60 {
       
    61   vector<const char*> v; // Vector of character strings.
       
    62   v.push_back("zippy"); // First element.
       
    63   v.push_back("motorboy"); // Second element.
       
    64   typedef vector<const char*> vec;
       
    65   unsigned counter = 0;
       
    66   for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) {
       
    67     switch (counter) {
       
    68       case 0:
       
    69         CPPUNIT_ASSERT(!strcmp(*i, "zippy"));
       
    70         break;
       
    71       case 1:
       
    72         CPPUNIT_ASSERT(!strcmp(*i, "motorboy"));
       
    73         break;
       
    74       default:
       
    75         CPPUNIT_ASSERT(false);
       
    76     }
       
    77   }
       
    78 }
       
    79 void IterTest::iter3()
       
    80 {
       
    81   typedef vector<const char*> Vec;
       
    82   Vec v; // Vector of character strings.
       
    83   v.push_back("zippy"); // First element.
       
    84   v.push_back("motorboy"); // Second element.
       
    85   Vec::reverse_iterator it;
       
    86   unsigned counter = 0;
       
    87   for (it = v.rbegin(); it != v.rend(); ++it, ++counter) {
       
    88     switch (counter) {
       
    89       case 1:
       
    90         CPPUNIT_ASSERT(!strcmp(*it, "zippy"));
       
    91         break;
       
    92       case 0:
       
    93         CPPUNIT_ASSERT(!strcmp(*it, "motorboy"));
       
    94         break;
       
    95       default:
       
    96         CPPUNIT_ASSERT(false);
       
    97     }
       
    98   }
       
    99 
       
   100   CPPUNIT_ASSERT(true);
       
   101 }
       
   102 void IterTest::iter4()
       
   103 {
       
   104   vector<int> v; // Empty vector of integers.
       
   105   v.push_back(1);
       
   106   v.push_back(2);
       
   107   v.push_back(3);
       
   108   // Position immediately after last item.
       
   109   vector<int>::iterator i = v.end();
       
   110   // Move back one and then access.
       
   111   CPPUNIT_ASSERT((*--i)==3);
       
   112   i -= 2; // Jump back two items.
       
   113   CPPUNIT_ASSERT((*i)==1);
       
   114 }
       
   115 void IterTest::iterswp0()
       
   116 {
       
   117   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
       
   118 
       
   119   iter_swap(numbers, numbers + 3);
       
   120 
       
   121   CPPUNIT_ASSERT(numbers[0]==3);
       
   122   CPPUNIT_ASSERT(numbers[1]==1);
       
   123   CPPUNIT_ASSERT(numbers[2]==2);
       
   124   CPPUNIT_ASSERT(numbers[3]==0);
       
   125   CPPUNIT_ASSERT(numbers[4]==4);
       
   126   CPPUNIT_ASSERT(numbers[5]==5);
       
   127 
       
   128 }
       
   129 void IterTest::iterswp1()
       
   130 {
       
   131   vector<int> v1(6);
       
   132   __iota(v1.begin(), v1.end(), 0);
       
   133   iter_swap( v1.begin(), v1.begin() + 3 );
       
   134 
       
   135   CPPUNIT_ASSERT(v1[0]==3);
       
   136   CPPUNIT_ASSERT(v1[1]==1);
       
   137   CPPUNIT_ASSERT(v1[2]==2);
       
   138   CPPUNIT_ASSERT(v1[3]==0);
       
   139   CPPUNIT_ASSERT(v1[4]==4);
       
   140   CPPUNIT_ASSERT(v1[5]==5);
       
   141 }
       
   142 void IterTest::iterswp2()
       
   143 {
       
   144   vector<bool> boolVector;
       
   145 
       
   146   boolVector.push_back( true );
       
   147   boolVector.push_back( false );
       
   148 
       
   149   vector<bool>::iterator i1 = boolVector.begin();
       
   150   vector<bool>::iterator i2 = boolVector.begin();
       
   151   ++i2;
       
   152 
       
   153   bool v0 = *i1;
       
   154   bool v1 = *i2;
       
   155 
       
   156   iter_swap( i1, i2 );
       
   157 
       
   158   CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 ));
       
   159 }
       
   160 
       
   161 
       
   162 void IterTest::iterswp3()
       
   163 {
       
   164   vector<int> vvref(10, 10);
       
   165   vector<int> lvref(10, 20);
       
   166 
       
   167   vector<vector<int> > vvints(4, vvref);
       
   168   list<vector<int> > lvints(4, lvref);
       
   169 
       
   170   iter_swap(vvints.begin(), lvints.begin());
       
   171   CPPUNIT_CHECK( vvints.front() == lvref );
       
   172   CPPUNIT_CHECK( lvints.front() == vvref );
       
   173 
       
   174 #if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
       
   175   int *pvvint = &vvints.front().front();
       
   176   int *plvint = &lvints.front().front();
       
   177 
       
   178   iter_swap(vvints.begin(), lvints.begin());
       
   179   //Check that elements have been swaped:
       
   180   CPPUNIT_CHECK( pvvint == &lvints.front().front() );
       
   181   CPPUNIT_CHECK( plvint == &vvints.front().front() );
       
   182 #endif
       
   183 }