1 #include <vector>
2 #include <algorithm>
3 
4 #include "cppunit/cppunit_proxy.h"
5 
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
7 using namespace std;
8 #endif
9 
10 //
11 // TestCase class
12 //
13 class BoundTest : public CPPUNIT_NS::TestCase
14 {
15   CPPUNIT_TEST_SUITE(BoundTest);
16   CPPUNIT_TEST(lwrbnd1);
17   CPPUNIT_TEST(lwrbnd2);
18   CPPUNIT_TEST(uprbnd1);
19   CPPUNIT_TEST(uprbnd2);
20   CPPUNIT_TEST_SUITE_END();
21 
22 protected:
23   void lwrbnd1();
24   void lwrbnd2();
25   void uprbnd1();
26   void uprbnd2();
27 
char_str_less(const char * a_,const char * b_)28   static bool char_str_less(const char* a_, const char* b_)
29   {
30     return strcmp(a_, b_) < 0 ? 1 : 0;
31   }
32 };
33 
34 CPPUNIT_TEST_SUITE_REGISTRATION(BoundTest);
35 
36 //
37 // tests implementation
38 //
uprbnd1()39 void BoundTest::uprbnd1()
40 {
41   int arr[20];
42   for(int i = 0; i < 20; i++)
43   {
44     arr[i] = i/4;
45   }
46   int location = upper_bound((int*)arr, (int*)arr + 20, 3) - arr;
47   CPPUNIT_ASSERT(location==16);
48 }
49 
uprbnd2()50 void BoundTest::uprbnd2()
51 {
52   char const* str [] = { "a", "a", "b", "b", "q", "w", "z" };
53 
54   const unsigned strCt = sizeof(str)/sizeof(str[0]);
55 
56   int location = (upper_bound((char const**)str,  (char const**)str + strCt, (const char *)"d", char_str_less) - str);
57   CPPUNIT_ASSERT(location==4);
58 }
lwrbnd1()59 void BoundTest::lwrbnd1()
60 {
61   vector <int> v1(20);
62   for (int i = 0; (size_t)i < v1.size(); ++i)
63   {
64     v1[i] = i/4;
65   }
66   // 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
67   vector<int>::iterator location = lower_bound(v1.begin(), v1.end(), 3);
68 
69   CPPUNIT_ASSERT((location - v1.begin())==12);
70 }
71 
lwrbnd2()72 void BoundTest::lwrbnd2()
73 {
74   char const* str [] = { "a", "a", "b", "b", "q", "w", "z" };
75 
76   const unsigned strCt = sizeof(str)/sizeof(str[0]);
77   char const** location = lower_bound((char const**)str,  (char const**)str + strCt, (const char *)"d", char_str_less);
78 
79   CPPUNIT_ASSERT((location - str) == 4);
80 }
81