sl@0
|
1 |
#include <vector>
|
sl@0
|
2 |
#include <numeric>
|
sl@0
|
3 |
#include <algorithm>
|
sl@0
|
4 |
|
sl@0
|
5 |
#include "iota.h"
|
sl@0
|
6 |
#include "cppunit/cppunit_proxy.h"
|
sl@0
|
7 |
|
sl@0
|
8 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
sl@0
|
9 |
using namespace std;
|
sl@0
|
10 |
#endif
|
sl@0
|
11 |
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// TestCase class
|
sl@0
|
14 |
//
|
sl@0
|
15 |
class SearchTest : public CPPUNIT_NS::TestCase
|
sl@0
|
16 |
{
|
sl@0
|
17 |
CPPUNIT_TEST_SUITE(SearchTest);
|
sl@0
|
18 |
CPPUNIT_TEST(search0);
|
sl@0
|
19 |
CPPUNIT_TEST(search1);
|
sl@0
|
20 |
CPPUNIT_TEST(search2);
|
sl@0
|
21 |
CPPUNIT_TEST_SUITE_END();
|
sl@0
|
22 |
|
sl@0
|
23 |
protected:
|
sl@0
|
24 |
void search0();
|
sl@0
|
25 |
void search1();
|
sl@0
|
26 |
void search2();
|
sl@0
|
27 |
|
sl@0
|
28 |
static bool str_equal(const char* a_, const char* b_)
|
sl@0
|
29 |
{
|
sl@0
|
30 |
return strcmp(a_, b_) == 0 ? 1 : 0;
|
sl@0
|
31 |
}
|
sl@0
|
32 |
};
|
sl@0
|
33 |
|
sl@0
|
34 |
CPPUNIT_TEST_SUITE_REGISTRATION(SearchTest);
|
sl@0
|
35 |
|
sl@0
|
36 |
//
|
sl@0
|
37 |
// tests implementation
|
sl@0
|
38 |
//
|
sl@0
|
39 |
void SearchTest::search0()
|
sl@0
|
40 |
{
|
sl@0
|
41 |
int v1[6] = { 1, 1, 2, 3, 5, 8 };
|
sl@0
|
42 |
int v2[6] = { 0, 1, 2, 3, 4, 5 };
|
sl@0
|
43 |
int v3[2] = { 3, 4 };
|
sl@0
|
44 |
|
sl@0
|
45 |
int* location;
|
sl@0
|
46 |
location = search((int*)v1, (int*)v1 + 6, (int*)v3, (int*)v3 + 2);
|
sl@0
|
47 |
CPPUNIT_ASSERT(location == v1 + 6);
|
sl@0
|
48 |
|
sl@0
|
49 |
location = search((int*)v2, (int*)v2 + 6, (int*)v3, (int*)v3 + 2);
|
sl@0
|
50 |
CPPUNIT_ASSERT(location != v2 + 6);
|
sl@0
|
51 |
CPPUNIT_ASSERT(location - v2 == 3);
|
sl@0
|
52 |
}
|
sl@0
|
53 |
void SearchTest::search1()
|
sl@0
|
54 |
{
|
sl@0
|
55 |
typedef vector <int> IntVec;
|
sl@0
|
56 |
IntVec v1(10);
|
sl@0
|
57 |
__iota(v1.begin(), v1.end(), 0);
|
sl@0
|
58 |
IntVec v2(3);
|
sl@0
|
59 |
__iota(v2.begin(), v2.end(), 50);
|
sl@0
|
60 |
|
sl@0
|
61 |
IntVec::iterator location;
|
sl@0
|
62 |
location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
|
sl@0
|
63 |
|
sl@0
|
64 |
CPPUNIT_ASSERT(location == v1.end());
|
sl@0
|
65 |
|
sl@0
|
66 |
__iota(v2.begin(), v2.end(), 4);
|
sl@0
|
67 |
|
sl@0
|
68 |
location = search(v1.begin(), v1.end(), v2.begin(), v2.end());
|
sl@0
|
69 |
|
sl@0
|
70 |
CPPUNIT_ASSERT(location != v1.end());
|
sl@0
|
71 |
CPPUNIT_ASSERT(location - v1.begin() == 4);
|
sl@0
|
72 |
}
|
sl@0
|
73 |
void SearchTest::search2()
|
sl@0
|
74 |
{
|
sl@0
|
75 |
char const* grades[] = { "A", "B", "C", "D", "F" };
|
sl@0
|
76 |
char const* letters[] = { "Q", "E", "D" };
|
sl@0
|
77 |
const unsigned gradeCount = sizeof(grades) / sizeof(grades[0]);
|
sl@0
|
78 |
const unsigned letterCount = sizeof(letters) / sizeof(letters[0]);
|
sl@0
|
79 |
char const** location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
|
sl@0
|
80 |
|
sl@0
|
81 |
CPPUNIT_ASSERT(location == grades + gradeCount);
|
sl@0
|
82 |
|
sl@0
|
83 |
copy((char const**)grades + 1, (char const**)grades + 1 + letterCount, (char const**)letters);
|
sl@0
|
84 |
location = search((char const**)grades, (char const**)grades + gradeCount, (char const**)letters, (char const**)letters + letterCount, str_equal);
|
sl@0
|
85 |
|
sl@0
|
86 |
CPPUNIT_ASSERT(location != grades + gradeCount);
|
sl@0
|
87 |
CPPUNIT_ASSERT(location - grades == 1);
|
sl@0
|
88 |
|
sl@0
|
89 |
}
|