genericopenlibs/cppstdlib/stl/test/unit/rotate_test.cpp
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 RotateTest : public CPPUNIT_NS::TestCase
       
    16 {
       
    17   CPPUNIT_TEST_SUITE(RotateTest);
       
    18   CPPUNIT_TEST(rotate0);
       
    19   CPPUNIT_TEST(rotate1);
       
    20   CPPUNIT_TEST(rotcopy0);
       
    21   CPPUNIT_TEST(rotcopy1);
       
    22   CPPUNIT_TEST_SUITE_END();
       
    23 
       
    24 protected:
       
    25   void rotate0();
       
    26   void rotate1();
       
    27   void rotcopy0();
       
    28   void rotcopy1();
       
    29 };
       
    30 
       
    31 CPPUNIT_TEST_SUITE_REGISTRATION(RotateTest);
       
    32 
       
    33 //
       
    34 // tests implementation
       
    35 //
       
    36 void RotateTest::rotate0()
       
    37 {
       
    38   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
       
    39   // 3 4 5 0 1 2
       
    40   rotate((int*)numbers, numbers + 3, numbers + 6);
       
    41   CPPUNIT_ASSERT(numbers[0]==3);
       
    42   CPPUNIT_ASSERT(numbers[1]==4);
       
    43   CPPUNIT_ASSERT(numbers[2]==5);
       
    44   CPPUNIT_ASSERT(numbers[3]==0);
       
    45   CPPUNIT_ASSERT(numbers[4]==1);
       
    46   CPPUNIT_ASSERT(numbers[5]==2);
       
    47 }
       
    48 void RotateTest::rotate1()
       
    49 {
       
    50   vector <int> v1(10);
       
    51   __iota(v1.begin(), v1.end(), 0);
       
    52 
       
    53   rotate(v1.begin(), v1.begin()+1, v1.end());
       
    54   CPPUNIT_ASSERT(v1[0]==1);
       
    55   CPPUNIT_ASSERT(v1[1]==2);
       
    56   CPPUNIT_ASSERT(v1[2]==3);
       
    57   CPPUNIT_ASSERT(v1[3]==4);
       
    58   CPPUNIT_ASSERT(v1[4]==5);
       
    59   CPPUNIT_ASSERT(v1[5]==6);
       
    60   CPPUNIT_ASSERT(v1[6]==7);
       
    61   CPPUNIT_ASSERT(v1[7]==8);
       
    62   CPPUNIT_ASSERT(v1[8]==9);
       
    63   CPPUNIT_ASSERT(v1[9]==0);
       
    64 
       
    65   rotate(v1.begin(), v1.begin()+2, v1.end());
       
    66   CPPUNIT_ASSERT(v1[0]==3);
       
    67   CPPUNIT_ASSERT(v1[1]==4);
       
    68   CPPUNIT_ASSERT(v1[2]==5);
       
    69   CPPUNIT_ASSERT(v1[3]==6);
       
    70   CPPUNIT_ASSERT(v1[4]==7);
       
    71   CPPUNIT_ASSERT(v1[5]==8);
       
    72   CPPUNIT_ASSERT(v1[6]==9);
       
    73   CPPUNIT_ASSERT(v1[7]==0);
       
    74   CPPUNIT_ASSERT(v1[8]==1);
       
    75   CPPUNIT_ASSERT(v1[9]==2);
       
    76 
       
    77   rotate(v1.begin(), v1.begin()+7, v1.end());
       
    78   CPPUNIT_ASSERT(v1[0]==0);
       
    79   CPPUNIT_ASSERT(v1[1]==1);
       
    80   CPPUNIT_ASSERT(v1[2]==2);
       
    81   CPPUNIT_ASSERT(v1[3]==3);
       
    82   CPPUNIT_ASSERT(v1[4]==4);
       
    83   CPPUNIT_ASSERT(v1[5]==5);
       
    84   CPPUNIT_ASSERT(v1[6]==6);
       
    85   CPPUNIT_ASSERT(v1[7]==7);
       
    86   CPPUNIT_ASSERT(v1[8]==8);
       
    87   CPPUNIT_ASSERT(v1[9]==9);
       
    88 
       
    89 }
       
    90 void RotateTest::rotcopy0()
       
    91 {
       
    92   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
       
    93 
       
    94   int result[6];
       
    95   rotate_copy((int*)numbers, (int*)numbers + 3, (int*)numbers + 6, (int*)result);
       
    96   // 3 4 5 0 1 2
       
    97   CPPUNIT_ASSERT(result[0]==3);
       
    98   CPPUNIT_ASSERT(result[1]==4);
       
    99   CPPUNIT_ASSERT(result[2]==5);
       
   100   CPPUNIT_ASSERT(result[3]==0);
       
   101   CPPUNIT_ASSERT(result[4]==1);
       
   102   CPPUNIT_ASSERT(result[5]==2);
       
   103 }
       
   104 void RotateTest::rotcopy1()
       
   105 {
       
   106   vector <int> v1(10);
       
   107   __iota(v1.begin(), v1.end(), 0);
       
   108   vector <int> v2(v1.size());
       
   109 
       
   110   rotate_copy(v1.begin(), v1.begin()+1, v1.end(), v2.begin());
       
   111   CPPUNIT_ASSERT(v2[0]==1);
       
   112   CPPUNIT_ASSERT(v2[1]==2);
       
   113   CPPUNIT_ASSERT(v2[2]==3);
       
   114   CPPUNIT_ASSERT(v2[3]==4);
       
   115   CPPUNIT_ASSERT(v2[4]==5);
       
   116   CPPUNIT_ASSERT(v2[5]==6);
       
   117   CPPUNIT_ASSERT(v2[6]==7);
       
   118   CPPUNIT_ASSERT(v2[7]==8);
       
   119   CPPUNIT_ASSERT(v2[8]==9);
       
   120   CPPUNIT_ASSERT(v2[9]==0);
       
   121 
       
   122   rotate_copy(v1.begin(), v1.begin()+3, v1.end(), v2.begin());
       
   123   CPPUNIT_ASSERT(v2[0]==3);
       
   124   CPPUNIT_ASSERT(v2[1]==4);
       
   125   CPPUNIT_ASSERT(v2[2]==5);
       
   126   CPPUNIT_ASSERT(v2[3]==6);
       
   127   CPPUNIT_ASSERT(v2[4]==7);
       
   128   CPPUNIT_ASSERT(v2[5]==8);
       
   129   CPPUNIT_ASSERT(v2[6]==9);
       
   130   CPPUNIT_ASSERT(v2[7]==0);
       
   131   CPPUNIT_ASSERT(v2[8]==1);
       
   132   CPPUNIT_ASSERT(v2[9]==2);
       
   133 }