os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/search_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/search_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,89 @@
     1.4 +#include <vector>
     1.5 +#include <numeric>
     1.6 +#include <algorithm>
     1.7 +
     1.8 +#include "iota.h"
     1.9 +#include "cppunit/cppunit_proxy.h"
    1.10 +
    1.11 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.12 +using namespace std;
    1.13 +#endif
    1.14 +
    1.15 +//
    1.16 +// TestCase class
    1.17 +//
    1.18 +class SearchTest : public CPPUNIT_NS::TestCase
    1.19 +{
    1.20 +  CPPUNIT_TEST_SUITE(SearchTest);
    1.21 +  CPPUNIT_TEST(search0);
    1.22 +  CPPUNIT_TEST(search1);
    1.23 +  CPPUNIT_TEST(search2);
    1.24 +  CPPUNIT_TEST_SUITE_END();
    1.25 +
    1.26 +protected:
    1.27 +  void search0();
    1.28 +  void search1();
    1.29 +  void search2();
    1.30 +
    1.31 +  static bool str_equal(const char* a_, const char* b_)
    1.32 +  {
    1.33 +    return strcmp(a_, b_) == 0 ? 1 : 0;
    1.34 +  }
    1.35 +};
    1.36 +
    1.37 +CPPUNIT_TEST_SUITE_REGISTRATION(SearchTest);
    1.38 +
    1.39 +//
    1.40 +// tests implementation
    1.41 +//
    1.42 +void SearchTest::search0()
    1.43 +{
    1.44 +  int v1[6] = { 1, 1, 2, 3, 5, 8 };
    1.45 +  int v2[6] = { 0, 1, 2, 3, 4, 5 };
    1.46 +  int v3[2] = { 3, 4 };
    1.47 +
    1.48 +  int* location;
    1.49 +  location = search((int*)v1, (int*)v1 + 6, (int*)v3, (int*)v3 + 2);
    1.50 +  CPPUNIT_ASSERT(location == v1 + 6);
    1.51 +
    1.52 +  location = search((int*)v2, (int*)v2 + 6, (int*)v3, (int*)v3 + 2);
    1.53 +  CPPUNIT_ASSERT(location != v2 + 6);
    1.54 +  CPPUNIT_ASSERT(location - v2 == 3);
    1.55 +}
    1.56 +void SearchTest::search1()
    1.57 +{
    1.58 +  typedef vector <int> IntVec;
    1.59 +  IntVec v1(10);
    1.60 +  __iota(v1.begin(), v1.end(), 0);
    1.61 +  IntVec v2(3);
    1.62 +  __iota(v2.begin(), v2.end(), 50);
    1.63 +
    1.64 +  IntVec::iterator location;
    1.65 +  location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
    1.66 +
    1.67 +  CPPUNIT_ASSERT(location == v1.end());
    1.68 +
    1.69 +  __iota(v2.begin(), v2.end(), 4);
    1.70 +
    1.71 +  location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
    1.72 +
    1.73 +  CPPUNIT_ASSERT(location != v1.end());
    1.74 +  CPPUNIT_ASSERT(location - v1.begin() == 4);
    1.75 +}
    1.76 +void SearchTest::search2()
    1.77 +{
    1.78 +  char const* grades[] = { "A", "B", "C", "D", "F" };
    1.79 +  char const* letters[] = { "Q", "E", "D" };
    1.80 +  const unsigned gradeCount = sizeof(grades) / sizeof(grades[0]);
    1.81 +  const unsigned letterCount = sizeof(letters) / sizeof(letters[0]);
    1.82 +  char const** location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
    1.83 +
    1.84 +  CPPUNIT_ASSERT(location == grades + gradeCount);
    1.85 +
    1.86 +  copy((char const**)grades + 1, (char const**)grades + 1 + letterCount, (char const**)letters);
    1.87 +  location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
    1.88 +
    1.89 +  CPPUNIT_ASSERT(location != grades + gradeCount);
    1.90 +  CPPUNIT_ASSERT(location - grades == 1);
    1.91 +
    1.92 +}