aboutsummaryrefslogtreecommitdiff
path: root/test/unit/bsearch_test.cpp
blob: 27dcc0264d74751d9d4d70ca57c92710ed6452e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <algorithm>

#include "cppunit/cppunit_proxy.h"

#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
#endif

//
// TestCase class
//
class BsearchTest : public CPPUNIT_NS::TestCase
{
  CPPUNIT_TEST_SUITE(BsearchTest);
  CPPUNIT_TEST(bsearch1);
  CPPUNIT_TEST(bsearch2);
  CPPUNIT_TEST_SUITE_END();

protected:
  void bsearch1();
  void bsearch2();
  static bool str_compare(const char* a_, const char* b_);
};

CPPUNIT_TEST_SUITE_REGISTRATION(BsearchTest);

//
// tests implementation
//
void BsearchTest::bsearch1()
{
  int vector[100];
  for(int i = 0; i < 100; i++)
    vector[i] = i;
  CPPUNIT_ASSERT(binary_search(vector, vector + 100, 42));
}

void BsearchTest::bsearch2()
{
  char const* labels[] = { "aa", "dd", "ff", "jj", "ss", "zz" };
  const unsigned count = sizeof(labels) / sizeof(labels[0]);
  // DEC C++ generates incorrect template instatiation code
  // for "ff" so must cast
  CPPUNIT_ASSERT(binary_search(labels, labels + count, (const char *)"ff", str_compare));
}
bool BsearchTest::str_compare(const char* a_, const char* b_)
{
  return strcmp(a_, b_) < 0 ? 1 : 0;
}