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