First public contribution.
4 #include "cppunit/cppunit_proxy.h"
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
13 class FindTest : public CPPUNIT_NS::TestCase
15 CPPUNIT_TEST_SUITE(FindTest);
18 CPPUNIT_TEST(findif0);
19 CPPUNIT_TEST(findif1);
20 CPPUNIT_TEST(find_char);
21 CPPUNIT_TEST_SUITE_END();
29 static bool odd(int a_);
30 static bool div_3(int a_);
33 CPPUNIT_TEST_SUITE_REGISTRATION(FindTest);
36 // tests implementation
38 void FindTest::find0()
40 int numbers[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64 };
42 int *location = find((int*)numbers, (int*)numbers + 10, 25);
44 CPPUNIT_ASSERT((location - numbers)==5);
46 int *out_range = find((int*)numbers, (int*)numbers + 10, 128);
48 CPPUNIT_ASSERT( out_range == (int *)(numbers + 10) );
55 /* This operator should rather be global and commutative
56 but implementing it this way show that STLport used to
57 ask too much from the user code. */
58 bool operator == (int d) const
64 void FindTest::find1()
66 int years[] = { 1942, 1952, 1962, 1972, 1982, 1992 };
68 const unsigned yearCount = sizeof(years) / sizeof(years[0]);
69 int* location = find((int*)years, (int*)years + yearCount, 1972);
71 CPPUNIT_ASSERT((location - years)==3);
74 void FindTest::findif0()
77 int numbers[6] = { 2, 4, 8, 15, 32, 64 };
78 int *location = find_if((int*)numbers, (int*)numbers + 6, odd);
80 CPPUNIT_ASSERT((location - numbers)==3);
82 int numbers_even[6] = { 2, 4, 8, 16, 32, 64 };
84 int *out_range = find_if((int*)numbers_even, (int*)numbers_even + 6, odd);
86 CPPUNIT_ASSERT( out_range == (int *)(numbers_even + 6) );
90 Key keys[10] = { {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
91 Key const* k = find(keys + 0, keys + 10, 5);
92 CPPUNIT_ASSERT( k == keys + 10 );
96 void FindTest::findif1()
98 typedef vector <int> IntVec;
100 for(int i = 0; (size_t)i < v.size(); ++i)
101 v[i] =(i + 1) *(i + 1);
102 IntVec::iterator iter;
103 iter = find_if(v.begin(), v.end(), div_3);
104 CPPUNIT_ASSERT((iter - v.begin())==2);
107 bool FindTest::odd(int a_)
109 return (a_ % 2) != 0;
112 bool FindTest::div_3(int a_)
114 return a_ % 3 ? 0 : 1;
117 void FindTest::find_char()
119 char str[] = "abcdefghij";
120 char *pstr = (char*)str;
121 const char* cpstr = (const char*)str;
122 size_t str_size = sizeof(str) / sizeof(char);
124 char *d = find(pstr, pstr + str_size, 'd');
125 CPPUNIT_ASSERT( *d == 'd' );
127 const char *e = find(cpstr, cpstr + str_size, 'e');
128 CPPUNIT_ASSERT( *e == 'e' );
130 char *last = find(pstr, pstr + str_size, 'x');
131 CPPUNIT_ASSERT( last == pstr + str_size );
133 const char *clast = find(cpstr, cpstr + str_size, 'x');
134 CPPUNIT_ASSERT( clast == cpstr + str_size );