os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/adj_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/adj_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,135 @@
     1.4 +#include <vector>
     1.5 +#include <numeric>
     1.6 +#include <algorithm>
     1.7 +
     1.8 +#include "cppunit/cppunit_proxy.h"
     1.9 +
    1.10 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.11 +using namespace std;
    1.12 +#endif
    1.13 +
    1.14 +//
    1.15 +// TestCase class
    1.16 +//
    1.17 +class AdjTest : public CPPUNIT_NS::TestCase
    1.18 +{
    1.19 +  CPPUNIT_TEST_SUITE(AdjTest);
    1.20 +  CPPUNIT_TEST(adjfind0);
    1.21 +  CPPUNIT_TEST(adjfind1);
    1.22 +  CPPUNIT_TEST(adjfind2);
    1.23 +  CPPUNIT_TEST(adjdiff0);
    1.24 +  CPPUNIT_TEST(adjdiff1);
    1.25 +  CPPUNIT_TEST(adjdiff2);
    1.26 +  CPPUNIT_TEST_SUITE_END();
    1.27 +
    1.28 +protected:
    1.29 +  void adjfind0();
    1.30 +  void adjfind1();
    1.31 +  void adjfind2();
    1.32 +  void adjdiff0();
    1.33 +  void adjdiff1();
    1.34 +  void adjdiff2();
    1.35 +  static int equal_length(const char* v1_, const char* v2_);
    1.36 +  static int mult(int a_, int b_);
    1.37 +};
    1.38 +
    1.39 +CPPUNIT_TEST_SUITE_REGISTRATION(AdjTest);
    1.40 +
    1.41 +//
    1.42 +// tests implementation
    1.43 +//
    1.44 +void AdjTest::adjfind0()
    1.45 +{
    1.46 +  int numbers1 [5] = { 1, 2, 4, 8, 16 };
    1.47 +  int numbers2 [5] = { 5, 3, 2, 1, 1 };
    1.48 +
    1.49 +  int* location = adjacent_find((int*)numbers1, (int*)numbers1 + 5);
    1.50 +  CPPUNIT_ASSERT(location == numbers1 + 5); // no adj so loc should be _last
    1.51 +
    1.52 +  location = adjacent_find((int*)numbers2, (int*)numbers2 + 5);
    1.53 +  CPPUNIT_ASSERT(location != numbers2 + 5); // adj location off should be 3 (first 1)
    1.54 +  CPPUNIT_ASSERT((location - numbers2)==3);
    1.55 +}
    1.56 +void AdjTest::adjfind1()
    1.57 +{
    1.58 +  typedef vector<int> IntVector;
    1.59 +  IntVector v(10);
    1.60 +  for (int i = 0; (size_t)i < v.size(); ++i)
    1.61 +    v[i] = i;
    1.62 +  IntVector::iterator location;
    1.63 +  location = adjacent_find(v.begin(), v.end());
    1.64 +  CPPUNIT_ASSERT(location == v.end());
    1.65 +  v[6] = 7;
    1.66 +  location = adjacent_find(v.begin(), v.end());
    1.67 +  CPPUNIT_ASSERT(location != v.end());
    1.68 +}
    1.69 +void AdjTest::adjfind2()
    1.70 +{
    1.71 +  typedef vector <char*> CStrVector;
    1.72 +
    1.73 +  char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
    1.74 +
    1.75 +  const int nameCount = sizeof(names)/sizeof(names[0]);
    1.76 +  CStrVector v(nameCount);
    1.77 +  for(int i = 0; i < nameCount; i++)
    1.78 +    v[i] = names[i];
    1.79 +  CStrVector::iterator location;
    1.80 +  location = adjacent_find(v.begin(), v.end(), equal_length);
    1.81 +
    1.82 +  CPPUNIT_ASSERT(location != v.end());
    1.83 +}
    1.84 +int AdjTest::equal_length(const char* v1_, const char* v2_)
    1.85 +{
    1.86 +  return ::strlen(v1_) == ::strlen(v2_);
    1.87 +}
    1.88 +void AdjTest::adjdiff0()
    1.89 +{
    1.90 +  int numbers[5] = { 1, 2, 4, 8, 16 };
    1.91 +  int difference[5];
    1.92 +  adjacent_difference(numbers, numbers + 5, (int*)difference);
    1.93 +  CPPUNIT_ASSERT(difference[0]==1);
    1.94 +  CPPUNIT_ASSERT(difference[1]==1);
    1.95 +  CPPUNIT_ASSERT(difference[2]==2);
    1.96 +  CPPUNIT_ASSERT(difference[3]==4);
    1.97 +  CPPUNIT_ASSERT(difference[4]==8);
    1.98 +}
    1.99 +void AdjTest::adjdiff1()
   1.100 +{
   1.101 +  vector <int> v(10);
   1.102 +  for(int i = 0; (size_t)i < v.size(); ++i)
   1.103 +    v[i] = i * i;
   1.104 +  vector<int> result(v.size());
   1.105 +  adjacent_difference(v.begin(), v.end(), result.begin());
   1.106 +  CPPUNIT_ASSERT(result[0]==0)
   1.107 +  CPPUNIT_ASSERT(result[1]==1)
   1.108 +  CPPUNIT_ASSERT(result[2]==3)
   1.109 +  CPPUNIT_ASSERT(result[3]==5)
   1.110 +  CPPUNIT_ASSERT(result[4]==7)
   1.111 +  CPPUNIT_ASSERT(result[5]==9)
   1.112 +  CPPUNIT_ASSERT(result[6]==11)
   1.113 +  CPPUNIT_ASSERT(result[7]==13)
   1.114 +  CPPUNIT_ASSERT(result[8]==15)
   1.115 +  CPPUNIT_ASSERT(result[9]==17)
   1.116 +}
   1.117 +void AdjTest::adjdiff2()
   1.118 +{
   1.119 +  vector <int> v(10);
   1.120 +  for (int i = 0; (size_t)i < v.size(); ++i)
   1.121 +    v[i] = i + 1;
   1.122 +  vector <int> result(v.size());
   1.123 +  adjacent_difference(v.begin(), v.end(), result.begin(), mult);
   1.124 +  CPPUNIT_ASSERT(result[0]==1)
   1.125 +  CPPUNIT_ASSERT(result[1]==2)
   1.126 +  CPPUNIT_ASSERT(result[2]==6)
   1.127 +  CPPUNIT_ASSERT(result[3]==12)
   1.128 +  CPPUNIT_ASSERT(result[4]==20)
   1.129 +  CPPUNIT_ASSERT(result[5]==30)
   1.130 +  CPPUNIT_ASSERT(result[6]==42)
   1.131 +  CPPUNIT_ASSERT(result[7]==56)
   1.132 +  CPPUNIT_ASSERT(result[8]==72)
   1.133 +  CPPUNIT_ASSERT(result[9]==90)
   1.134 +}
   1.135 +int AdjTest::mult(int a_, int b_)
   1.136 +{
   1.137 +  return a_ * b_;
   1.138 +}