os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/mismatch_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
#include <numeric>
sl@0
     2
#include <vector>
sl@0
     3
#include <algorithm>
sl@0
     4
sl@0
     5
#include "iota.h"
sl@0
     6
#include "cppunit/cppunit_proxy.h"
sl@0
     7
sl@0
     8
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
     9
using namespace std;
sl@0
    10
#endif
sl@0
    11
sl@0
    12
//
sl@0
    13
// TestCase class
sl@0
    14
//
sl@0
    15
class MismatchTest : public CPPUNIT_NS::TestCase
sl@0
    16
{
sl@0
    17
  CPPUNIT_TEST_SUITE(MismatchTest);
sl@0
    18
  CPPUNIT_TEST(mismatch0);
sl@0
    19
  CPPUNIT_TEST(mismatch1);
sl@0
    20
  CPPUNIT_TEST(mismatch2);
sl@0
    21
  CPPUNIT_TEST_SUITE_END();
sl@0
    22
sl@0
    23
protected:
sl@0
    24
  void mismatch0();
sl@0
    25
  void mismatch1();
sl@0
    26
  void mismatch2();
sl@0
    27
};
sl@0
    28
sl@0
    29
CPPUNIT_TEST_SUITE_REGISTRATION(MismatchTest);
sl@0
    30
sl@0
    31
//
sl@0
    32
// tests implementation
sl@0
    33
//
sl@0
    34
bool str_equal(const char* a_, const char* b_)
sl@0
    35
{
sl@0
    36
  return strcmp(a_, b_) == 0 ? 1 : 0;
sl@0
    37
}
sl@0
    38
void MismatchTest::mismatch0()
sl@0
    39
{
sl@0
    40
  int n1[5] = { 1, 2, 3, 4, 5 };
sl@0
    41
  int n2[5] = { 1, 2, 3, 4, 5 };
sl@0
    42
  int n3[5] = { 1, 2, 3, 2, 1 };
sl@0
    43
sl@0
    44
  pair <int*, int*> result = mismatch((int*)n1, (int*)n1 + 5, (int*)n2);
sl@0
    45
  CPPUNIT_ASSERT(result.first ==(n1 + 5) && result.second ==(n2 + 5));
sl@0
    46
sl@0
    47
  result = mismatch((int*)n1, (int*)n1 + 5, (int*)n3);
sl@0
    48
  CPPUNIT_ASSERT(!(result.first ==(n1 + 5) && result.second ==(n3 + 5)));
sl@0
    49
  CPPUNIT_ASSERT((result.first - n1)==3);
sl@0
    50
}
sl@0
    51
void MismatchTest::mismatch1()
sl@0
    52
{
sl@0
    53
  typedef vector<int> IntVec;
sl@0
    54
  IntVec v1(10);
sl@0
    55
  __iota(v1.begin(), v1.end(), 0);
sl@0
    56
  IntVec v2(v1);
sl@0
    57
sl@0
    58
  pair <IntVec::iterator, IntVec::iterator> result = mismatch(v1.begin(), v1.end(), v2.begin());
sl@0
    59
sl@0
    60
  CPPUNIT_ASSERT(result.first == v1.end() && result.second == v2.end());
sl@0
    61
sl@0
    62
  v2[v2.size()/2] = 42;
sl@0
    63
  result = mismatch(v1.begin(), v1.end(), v2.begin());
sl@0
    64
  CPPUNIT_ASSERT(!(result.first == v1.end() && result.second == v2.end()));
sl@0
    65
  CPPUNIT_ASSERT((result.first - v1.begin())==5);
sl@0
    66
}
sl@0
    67
void MismatchTest::mismatch2()
sl@0
    68
{
sl@0
    69
  const unsigned size = 5;
sl@0
    70
  char const* n1[size] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
sl@0
    71
sl@0
    72
  char const* n2[size];
sl@0
    73
  copy(n1, n1 + 5, (char const**)n2);
sl@0
    74
  pair <char const**, char const**> result = mismatch((char const**)n1, (char const**)n1 + size, (char const**)n2, str_equal);
sl@0
    75
sl@0
    76
  CPPUNIT_ASSERT(result.first == n1 + size && result.second == n2 + size);
sl@0
    77
sl@0
    78
  n2[2] = "QED";
sl@0
    79
  result = mismatch((char const**)n1, (char const**)n1 + size, (char const**)n2, str_equal);
sl@0
    80
  CPPUNIT_ASSERT(!(result.first == n2 + size && result.second == n2 + size));
sl@0
    81
  CPPUNIT_ASSERT((result.first - n1)==2);
sl@0
    82
}