1 #include <algorithm>
2 
3 #include "cppunit/cppunit_proxy.h"
4 
5 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
6 using namespace std;
7 #endif
8 
9 //
10 // TestCase class
11 //
12 class BsearchTest : public CPPUNIT_NS::TestCase
13 {
14   CPPUNIT_TEST_SUITE(BsearchTest);
15   CPPUNIT_TEST(bsearch1);
16   CPPUNIT_TEST(bsearch2);
17   CPPUNIT_TEST_SUITE_END();
18 
19 protected:
20   void bsearch1();
21   void bsearch2();
22   static bool str_compare(const char* a_, const char* b_);
23 };
24 
25 CPPUNIT_TEST_SUITE_REGISTRATION(BsearchTest);
26 
27 //
28 // tests implementation
29 //
bsearch1()30 void BsearchTest::bsearch1()
31 {
32   int vector[100];
33   for(int i = 0; i < 100; i++)
34     vector[i] = i;
35   CPPUNIT_ASSERT(binary_search(vector, vector + 100, 42));
36 }
37 
bsearch2()38 void BsearchTest::bsearch2()
39 {
40   char const* labels[] = { "aa", "dd", "ff", "jj", "ss", "zz" };
41   const unsigned count = sizeof(labels) / sizeof(labels[0]);
42   // DEC C++ generates incorrect template instatiation code
43   // for "ff" so must cast
44   CPPUNIT_ASSERT(binary_search(labels, labels + count, (const char *)"ff", str_compare));
45 }
str_compare(const char * a_,const char * b_)46 bool BsearchTest::str_compare(const char* a_, const char* b_)
47 {
48   return strcmp(a_, b_) < 0 ? 1 : 0;
49 }
50