genericopenlibs/cppstdlib/stl/test/unit/search_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 <vector>
       
    18 #include <numeric>
       
    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 SearchTest : public CPPUNIT_NS::TestCase
       
    32 {
       
    33   CPPUNIT_TEST_SUITE(SearchTest);
       
    34   CPPUNIT_TEST(search0);
       
    35   CPPUNIT_TEST(search1);
       
    36   CPPUNIT_TEST(search2);
       
    37   CPPUNIT_TEST_SUITE_END();
       
    38 
       
    39 protected:
       
    40   void search0();
       
    41   void search1();
       
    42   void search2();
       
    43 
       
    44   static bool str_equal(const char* a_, const char* b_)
       
    45   {
       
    46     return strcmp(a_, b_) == 0 ? 1 : 0;
       
    47   }
       
    48 };
       
    49 
       
    50 CPPUNIT_TEST_SUITE_REGISTRATION(SearchTest);
       
    51 
       
    52 //
       
    53 // tests implementation
       
    54 //
       
    55 void SearchTest::search0()
       
    56 {
       
    57   int v1[6] = { 1, 1, 2, 3, 5, 8 };
       
    58   int v2[6] = { 0, 1, 2, 3, 4, 5 };
       
    59   int v3[2] = { 3, 4 };
       
    60 
       
    61   int* location;
       
    62   location = search((int*)v1, (int*)v1 + 6, (int*)v3, (int*)v3 + 2);
       
    63   CPPUNIT_ASSERT(location == v1 + 6);
       
    64 
       
    65   location = search((int*)v2, (int*)v2 + 6, (int*)v3, (int*)v3 + 2);
       
    66   CPPUNIT_ASSERT(location != v2 + 6);
       
    67   CPPUNIT_ASSERT(location - v2 == 3);
       
    68 }
       
    69 void SearchTest::search1()
       
    70 {
       
    71   typedef vector <int> IntVec;
       
    72   IntVec v1(10);
       
    73   __iota(v1.begin(), v1.end(), 0);
       
    74   IntVec v2(3);
       
    75   __iota(v2.begin(), v2.end(), 50);
       
    76 
       
    77   IntVec::iterator location;
       
    78   location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
       
    79 
       
    80   CPPUNIT_ASSERT(location == v1.end());
       
    81 
       
    82   __iota(v2.begin(), v2.end(), 4);
       
    83 
       
    84   location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
       
    85 
       
    86   CPPUNIT_ASSERT(location != v1.end());
       
    87   CPPUNIT_ASSERT(location - v1.begin() == 4);
       
    88 }
       
    89 void SearchTest::search2()
       
    90 {
       
    91   char const* grades[] = { "A", "B", "C", "D", "F" };
       
    92   char const* letters[] = { "Q", "E", "D" };
       
    93   const unsigned gradeCount = sizeof(grades) / sizeof(grades[0]);
       
    94   const unsigned letterCount = sizeof(letters) / sizeof(letters[0]);
       
    95   char const** location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
       
    96 
       
    97   CPPUNIT_ASSERT(location == grades + gradeCount);
       
    98 
       
    99   copy((char const**)grades + 1, (char const**)grades + 1 + letterCount, (char const**)letters);
       
   100   location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
       
   101 
       
   102   CPPUNIT_ASSERT(location != grades + gradeCount);
       
   103   CPPUNIT_ASSERT(location - grades == 1);
       
   104 
       
   105 }