genericopenlibs/cppstdlib/stl/test/unit/unique_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 <algorithm>
       
    18 
       
    19 #include "cppunit/cppunit_proxy.h"
       
    20 
       
    21 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    22 using namespace std;
       
    23 #endif
       
    24 
       
    25 //
       
    26 // TestCase class
       
    27 //
       
    28 class UniqueTest : public CPPUNIT_NS::TestCase
       
    29 {
       
    30   CPPUNIT_TEST_SUITE(UniqueTest);
       
    31   CPPUNIT_TEST(uniqcpy1);
       
    32   CPPUNIT_TEST(uniqcpy2);
       
    33   CPPUNIT_TEST(unique1);
       
    34   CPPUNIT_TEST(unique2);
       
    35   CPPUNIT_TEST_SUITE_END();
       
    36 
       
    37 protected:
       
    38   void uniqcpy1();
       
    39   void uniqcpy2();
       
    40   void unique1();
       
    41   void unique2();
       
    42 };
       
    43 
       
    44 CPPUNIT_TEST_SUITE_REGISTRATION(UniqueTest);
       
    45 
       
    46 static bool str_equal(const char* a_, const char* b_)
       
    47 { return *a_ == *b_; }
       
    48 //
       
    49 // tests implementation
       
    50 //
       
    51 void UniqueTest::unique1()
       
    52 {
       
    53   int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
       
    54   unique((int*)numbers, (int*)numbers + 8);
       
    55   // 0 1 2 3 4 2 3 4
       
    56   CPPUNIT_ASSERT(numbers[0]==0);
       
    57   CPPUNIT_ASSERT(numbers[1]==1);
       
    58   CPPUNIT_ASSERT(numbers[2]==2);
       
    59   CPPUNIT_ASSERT(numbers[3]==3);
       
    60   CPPUNIT_ASSERT(numbers[4]==4);
       
    61   CPPUNIT_ASSERT(numbers[5]==2);
       
    62   CPPUNIT_ASSERT(numbers[6]==3);
       
    63   CPPUNIT_ASSERT(numbers[7]==4);
       
    64 }
       
    65 
       
    66 void UniqueTest::unique2()
       
    67 {
       
    68   const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
       
    69 
       
    70   const unsigned count = sizeof(labels) / sizeof(labels[0]);
       
    71 
       
    72   unique((const char**)labels, (const char**)labels + count, str_equal);
       
    73 
       
    74   // QWERTY
       
    75   CPPUNIT_ASSERT(*labels[0] == 'Q');
       
    76   CPPUNIT_ASSERT(*labels[1] == 'W');
       
    77   CPPUNIT_ASSERT(*labels[2] == 'E');
       
    78   CPPUNIT_ASSERT(*labels[3] == 'R');
       
    79   CPPUNIT_ASSERT(*labels[4] == 'T');
       
    80   CPPUNIT_ASSERT(*labels[5] == 'Y');
       
    81 
       
    82 }
       
    83 
       
    84 void UniqueTest::uniqcpy1()
       
    85 {
       
    86   int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
       
    87   int result[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
       
    88 
       
    89   unique_copy((int*)numbers, (int*)numbers + 8, (int*)result);
       
    90 
       
    91   // 0 1 2 3 4 0 0 0
       
    92   CPPUNIT_ASSERT(result[0]==0);
       
    93   CPPUNIT_ASSERT(result[1]==1);
       
    94   CPPUNIT_ASSERT(result[2]==2);
       
    95   CPPUNIT_ASSERT(result[3]==3);
       
    96   CPPUNIT_ASSERT(result[4]==4);
       
    97   CPPUNIT_ASSERT(result[5]==0);
       
    98   CPPUNIT_ASSERT(result[6]==0);
       
    99   CPPUNIT_ASSERT(result[7]==0);
       
   100 }
       
   101 
       
   102 void UniqueTest::uniqcpy2()
       
   103 {
       
   104   const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
       
   105   const char **plabels = (const char**)labels;
       
   106 
       
   107   const size_t count = sizeof(labels) / sizeof(labels[0]);
       
   108   char* uCopy[count];
       
   109   const char **puCopy = (const char**)uCopy;
       
   110   fill(puCopy, puCopy + count, "");
       
   111 
       
   112   unique_copy(plabels, plabels + count, puCopy, str_equal);
       
   113 
       
   114   //QWERTY
       
   115   CPPUNIT_ASSERT(*uCopy[0] == 'Q');
       
   116   CPPUNIT_ASSERT(*uCopy[1] == 'W');
       
   117   CPPUNIT_ASSERT(*uCopy[2] == 'E');
       
   118   CPPUNIT_ASSERT(*uCopy[3] == 'R');
       
   119   CPPUNIT_ASSERT(*uCopy[4] == 'T');
       
   120   CPPUNIT_ASSERT(*uCopy[5] == 'Y');
       
   121 }