genericopenlibs/cppstdlib/stl/test/unit/rotate_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 <numeric>
       
    18 #include <vector>
       
    19 #include <algorithm>
       
    20 
       
    21 #include "iota.h"
       
    22 #include "cppunit/cppunit_proxy.h"
       
    23 
       
    24 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    25 using namespace std;
       
    26 #endif
       
    27 
       
    28 //
       
    29 // TestCase class
       
    30 //
       
    31 class RotateTest : public CPPUNIT_NS::TestCase
       
    32 {
       
    33   CPPUNIT_TEST_SUITE(RotateTest);
       
    34   CPPUNIT_TEST(rotate0);
       
    35   CPPUNIT_TEST(rotate1);
       
    36   CPPUNIT_TEST(rotcopy0);
       
    37   CPPUNIT_TEST(rotcopy1);
       
    38   CPPUNIT_TEST_SUITE_END();
       
    39 
       
    40 protected:
       
    41   void rotate0();
       
    42   void rotate1();
       
    43   void rotcopy0();
       
    44   void rotcopy1();
       
    45 };
       
    46 
       
    47 CPPUNIT_TEST_SUITE_REGISTRATION(RotateTest);
       
    48 
       
    49 //
       
    50 // tests implementation
       
    51 //
       
    52 void RotateTest::rotate0()
       
    53 {
       
    54   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
       
    55   // 3 4 5 0 1 2
       
    56   rotate((int*)numbers, numbers + 3, numbers + 6);
       
    57   CPPUNIT_ASSERT(numbers[0]==3);
       
    58   CPPUNIT_ASSERT(numbers[1]==4);
       
    59   CPPUNIT_ASSERT(numbers[2]==5);
       
    60   CPPUNIT_ASSERT(numbers[3]==0);
       
    61   CPPUNIT_ASSERT(numbers[4]==1);
       
    62   CPPUNIT_ASSERT(numbers[5]==2);
       
    63 }
       
    64 void RotateTest::rotate1()
       
    65 {
       
    66   vector <int> v1(10);
       
    67   __iota(v1.begin(), v1.end(), 0);
       
    68 
       
    69   rotate(v1.begin(), v1.begin()+1, v1.end());
       
    70   CPPUNIT_ASSERT(v1[0]==1);
       
    71   CPPUNIT_ASSERT(v1[1]==2);
       
    72   CPPUNIT_ASSERT(v1[2]==3);
       
    73   CPPUNIT_ASSERT(v1[3]==4);
       
    74   CPPUNIT_ASSERT(v1[4]==5);
       
    75   CPPUNIT_ASSERT(v1[5]==6);
       
    76   CPPUNIT_ASSERT(v1[6]==7);
       
    77   CPPUNIT_ASSERT(v1[7]==8);
       
    78   CPPUNIT_ASSERT(v1[8]==9);
       
    79   CPPUNIT_ASSERT(v1[9]==0);
       
    80 
       
    81   rotate(v1.begin(), v1.begin()+2, v1.end());
       
    82   CPPUNIT_ASSERT(v1[0]==3);
       
    83   CPPUNIT_ASSERT(v1[1]==4);
       
    84   CPPUNIT_ASSERT(v1[2]==5);
       
    85   CPPUNIT_ASSERT(v1[3]==6);
       
    86   CPPUNIT_ASSERT(v1[4]==7);
       
    87   CPPUNIT_ASSERT(v1[5]==8);
       
    88   CPPUNIT_ASSERT(v1[6]==9);
       
    89   CPPUNIT_ASSERT(v1[7]==0);
       
    90   CPPUNIT_ASSERT(v1[8]==1);
       
    91   CPPUNIT_ASSERT(v1[9]==2);
       
    92 
       
    93   rotate(v1.begin(), v1.begin()+7, v1.end());
       
    94   CPPUNIT_ASSERT(v1[0]==0);
       
    95   CPPUNIT_ASSERT(v1[1]==1);
       
    96   CPPUNIT_ASSERT(v1[2]==2);
       
    97   CPPUNIT_ASSERT(v1[3]==3);
       
    98   CPPUNIT_ASSERT(v1[4]==4);
       
    99   CPPUNIT_ASSERT(v1[5]==5);
       
   100   CPPUNIT_ASSERT(v1[6]==6);
       
   101   CPPUNIT_ASSERT(v1[7]==7);
       
   102   CPPUNIT_ASSERT(v1[8]==8);
       
   103   CPPUNIT_ASSERT(v1[9]==9);
       
   104 
       
   105 }
       
   106 void RotateTest::rotcopy0()
       
   107 {
       
   108   int numbers[6] = { 0, 1, 2, 3, 4, 5 };
       
   109 
       
   110   int result[6];
       
   111   rotate_copy((int*)numbers, (int*)numbers + 3, (int*)numbers + 6, (int*)result);
       
   112   // 3 4 5 0 1 2
       
   113   CPPUNIT_ASSERT(result[0]==3);
       
   114   CPPUNIT_ASSERT(result[1]==4);
       
   115   CPPUNIT_ASSERT(result[2]==5);
       
   116   CPPUNIT_ASSERT(result[3]==0);
       
   117   CPPUNIT_ASSERT(result[4]==1);
       
   118   CPPUNIT_ASSERT(result[5]==2);
       
   119 }
       
   120 void RotateTest::rotcopy1()
       
   121 {
       
   122   vector <int> v1(10);
       
   123   __iota(v1.begin(), v1.end(), 0);
       
   124   vector <int> v2(v1.size());
       
   125 
       
   126   rotate_copy(v1.begin(), v1.begin()+1, v1.end(), v2.begin());
       
   127   CPPUNIT_ASSERT(v2[0]==1);
       
   128   CPPUNIT_ASSERT(v2[1]==2);
       
   129   CPPUNIT_ASSERT(v2[2]==3);
       
   130   CPPUNIT_ASSERT(v2[3]==4);
       
   131   CPPUNIT_ASSERT(v2[4]==5);
       
   132   CPPUNIT_ASSERT(v2[5]==6);
       
   133   CPPUNIT_ASSERT(v2[6]==7);
       
   134   CPPUNIT_ASSERT(v2[7]==8);
       
   135   CPPUNIT_ASSERT(v2[8]==9);
       
   136   CPPUNIT_ASSERT(v2[9]==0);
       
   137 
       
   138   rotate_copy(v1.begin(), v1.begin()+3, v1.end(), v2.begin());
       
   139   CPPUNIT_ASSERT(v2[0]==3);
       
   140   CPPUNIT_ASSERT(v2[1]==4);
       
   141   CPPUNIT_ASSERT(v2[2]==5);
       
   142   CPPUNIT_ASSERT(v2[3]==6);
       
   143   CPPUNIT_ASSERT(v2[4]==7);
       
   144   CPPUNIT_ASSERT(v2[5]==8);
       
   145   CPPUNIT_ASSERT(v2[6]==9);
       
   146   CPPUNIT_ASSERT(v2[7]==0);
       
   147   CPPUNIT_ASSERT(v2[8]==1);
       
   148   CPPUNIT_ASSERT(v2[9]==2);
       
   149 }