genericopenlibs/cppstdlib/stl/test/unit/mismatch_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 MismatchTest : public CPPUNIT_NS::TestCase
       
    32 {
       
    33   CPPUNIT_TEST_SUITE(MismatchTest);
       
    34   CPPUNIT_TEST(mismatch0);
       
    35   CPPUNIT_TEST(mismatch1);
       
    36   CPPUNIT_TEST(mismatch2);
       
    37   CPPUNIT_TEST_SUITE_END();
       
    38 
       
    39 protected:
       
    40   void mismatch0();
       
    41   void mismatch1();
       
    42   void mismatch2();
       
    43 };
       
    44 
       
    45 CPPUNIT_TEST_SUITE_REGISTRATION(MismatchTest);
       
    46 
       
    47 //
       
    48 // tests implementation
       
    49 //
       
    50 bool str_equal(const char* a_, const char* b_)
       
    51 {
       
    52   return strcmp(a_, b_) == 0 ? 1 : 0;
       
    53 }
       
    54 void MismatchTest::mismatch0()
       
    55 {
       
    56   int n1[5] = { 1, 2, 3, 4, 5 };
       
    57   int n2[5] = { 1, 2, 3, 4, 5 };
       
    58   int n3[5] = { 1, 2, 3, 2, 1 };
       
    59 
       
    60   pair <int*, int*> result = mismatch((int*)n1, (int*)n1 + 5, (int*)n2);
       
    61   CPPUNIT_ASSERT(result.first ==(n1 + 5) && result.second ==(n2 + 5));
       
    62 
       
    63   result = mismatch((int*)n1, (int*)n1 + 5, (int*)n3);
       
    64   CPPUNIT_ASSERT(!(result.first ==(n1 + 5) && result.second ==(n3 + 5)));
       
    65   CPPUNIT_ASSERT((result.first - n1)==3);
       
    66 }
       
    67 void MismatchTest::mismatch1()
       
    68 {
       
    69   typedef vector<int> IntVec;
       
    70   IntVec v1(10);
       
    71   __iota(v1.begin(), v1.end(), 0);
       
    72   IntVec v2(v1);
       
    73 
       
    74   pair <IntVec::iterator, IntVec::iterator> result = mismatch(v1.begin(), v1.end(), v2.begin());
       
    75 
       
    76   CPPUNIT_ASSERT(result.first == v1.end() && result.second == v2.end());
       
    77 
       
    78   v2[v2.size()/2] = 42;
       
    79   result = mismatch(v1.begin(), v1.end(), v2.begin());
       
    80   CPPUNIT_ASSERT(!(result.first == v1.end() && result.second == v2.end()));
       
    81   CPPUNIT_ASSERT((result.first - v1.begin())==5);
       
    82 }
       
    83 void MismatchTest::mismatch2()
       
    84 {
       
    85   const unsigned size = 5;
       
    86   char const* n1[size] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
       
    87 
       
    88   char const* n2[size];
       
    89   copy(n1, n1 + 5, (char const**)n2);
       
    90   pair <char const**, char const**> result = mismatch((char const**)n1, (char const**)n1 + size, (char const**)n2, str_equal);
       
    91 
       
    92   CPPUNIT_ASSERT(result.first == n1 + size && result.second == n2 + size);
       
    93 
       
    94   n2[2] = "QED";
       
    95   result = mismatch((char const**)n1, (char const**)n1 + size, (char const**)n2, str_equal);
       
    96   CPPUNIT_ASSERT(!(result.first == n2 + size && result.second == n2 + size));
       
    97   CPPUNIT_ASSERT((result.first - n1)==2);
       
    98 }