|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include <vector> |
|
18 #include <algorithm> |
|
19 |
|
20 #include "cppunit/cppunit_proxy.h" |
|
21 |
|
22 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
23 using namespace std; |
|
24 #endif |
|
25 |
|
26 // |
|
27 // TestCase class |
|
28 // |
|
29 class BoundTest : public CPPUNIT_NS::TestCase |
|
30 { |
|
31 CPPUNIT_TEST_SUITE(BoundTest); |
|
32 CPPUNIT_TEST(lwrbnd1); |
|
33 CPPUNIT_TEST(lwrbnd2); |
|
34 CPPUNIT_TEST(uprbnd1); |
|
35 CPPUNIT_TEST(uprbnd2); |
|
36 CPPUNIT_TEST_SUITE_END(); |
|
37 |
|
38 protected: |
|
39 void lwrbnd1(); |
|
40 void lwrbnd2(); |
|
41 void uprbnd1(); |
|
42 void uprbnd2(); |
|
43 |
|
44 static bool char_str_less(const char* a_, const char* b_) |
|
45 { |
|
46 return strcmp(a_, b_) < 0 ? 1 : 0; |
|
47 } |
|
48 }; |
|
49 |
|
50 CPPUNIT_TEST_SUITE_REGISTRATION(BoundTest); |
|
51 |
|
52 // |
|
53 // tests implementation |
|
54 // |
|
55 void BoundTest::uprbnd1() |
|
56 { |
|
57 int arr[20]; |
|
58 for(int i = 0; i < 20; i++) |
|
59 { |
|
60 arr[i] = i/4; |
|
61 } |
|
62 int location = upper_bound((int*)arr, (int*)arr + 20, 3) - arr; |
|
63 CPPUNIT_ASSERT(location==16); |
|
64 } |
|
65 |
|
66 void BoundTest::uprbnd2() |
|
67 { |
|
68 char const* str [] = { "a", "a", "b", "b", "q", "w", "z" }; |
|
69 |
|
70 const unsigned strCt = sizeof(str)/sizeof(str[0]); |
|
71 |
|
72 int location = (upper_bound((char const**)str, (char const**)str + strCt, (const char *)"d", char_str_less) - str); |
|
73 CPPUNIT_ASSERT(location==4); |
|
74 } |
|
75 void BoundTest::lwrbnd1() |
|
76 { |
|
77 vector <int> v1(20); |
|
78 for (int i = 0; (size_t)i < v1.size(); ++i) |
|
79 { |
|
80 v1[i] = i/4; |
|
81 } |
|
82 // 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 |
|
83 vector<int>::iterator location = lower_bound(v1.begin(), v1.end(), 3); |
|
84 |
|
85 CPPUNIT_ASSERT((location - v1.begin())==12); |
|
86 } |
|
87 |
|
88 void BoundTest::lwrbnd2() |
|
89 { |
|
90 char const* str [] = { "a", "a", "b", "b", "q", "w", "z" }; |
|
91 |
|
92 const unsigned strCt = sizeof(str)/sizeof(str[0]); |
|
93 char const** location = lower_bound((char const**)str, (char const**)str + strCt, (const char *)"d", char_str_less); |
|
94 |
|
95 CPPUNIT_ASSERT((location - str) == 4); |
|
96 } |