|
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 <numeric> |
|
19 #include <algorithm> |
|
20 |
|
21 #include "cppunit/cppunit_proxy.h" |
|
22 |
|
23 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
24 using namespace std; |
|
25 #endif |
|
26 |
|
27 // |
|
28 // TestCase class |
|
29 // |
|
30 class AdjTest : public CPPUNIT_NS::TestCase |
|
31 { |
|
32 CPPUNIT_TEST_SUITE(AdjTest); |
|
33 CPPUNIT_TEST(adjfind0); |
|
34 CPPUNIT_TEST(adjfind1); |
|
35 CPPUNIT_TEST(adjfind2); |
|
36 CPPUNIT_TEST(adjdiff0); |
|
37 CPPUNIT_TEST(adjdiff1); |
|
38 CPPUNIT_TEST(adjdiff2); |
|
39 CPPUNIT_TEST_SUITE_END(); |
|
40 |
|
41 protected: |
|
42 void adjfind0(); |
|
43 void adjfind1(); |
|
44 void adjfind2(); |
|
45 void adjdiff0(); |
|
46 void adjdiff1(); |
|
47 void adjdiff2(); |
|
48 static int equal_length(const char* v1_, const char* v2_); |
|
49 static int mult(int a_, int b_); |
|
50 }; |
|
51 |
|
52 CPPUNIT_TEST_SUITE_REGISTRATION(AdjTest); |
|
53 |
|
54 // |
|
55 // tests implementation |
|
56 // |
|
57 void AdjTest::adjfind0() |
|
58 { |
|
59 int numbers1 [5] = { 1, 2, 4, 8, 16 }; |
|
60 int numbers2 [5] = { 5, 3, 2, 1, 1 }; |
|
61 |
|
62 int* location = adjacent_find((int*)numbers1, (int*)numbers1 + 5); |
|
63 CPPUNIT_ASSERT(location == numbers1 + 5); // no adj so loc should be _last |
|
64 |
|
65 location = adjacent_find((int*)numbers2, (int*)numbers2 + 5); |
|
66 CPPUNIT_ASSERT(location != numbers2 + 5); // adj location off should be 3 (first 1) |
|
67 CPPUNIT_ASSERT((location - numbers2)==3); |
|
68 } |
|
69 void AdjTest::adjfind1() |
|
70 { |
|
71 typedef vector<int> IntVector; |
|
72 IntVector v(10); |
|
73 for (int i = 0; (size_t)i < v.size(); ++i) |
|
74 v[i] = i; |
|
75 IntVector::iterator location; |
|
76 location = adjacent_find(v.begin(), v.end()); |
|
77 CPPUNIT_ASSERT(location == v.end()); |
|
78 v[6] = 7; |
|
79 location = adjacent_find(v.begin(), v.end()); |
|
80 CPPUNIT_ASSERT(location != v.end()); |
|
81 } |
|
82 void AdjTest::adjfind2() |
|
83 { |
|
84 typedef vector <char*> CStrVector; |
|
85 |
|
86 char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" }; |
|
87 |
|
88 const int nameCount = sizeof(names)/sizeof(names[0]); |
|
89 CStrVector v(nameCount); |
|
90 for(int i = 0; i < nameCount; i++) |
|
91 v[i] = names[i]; |
|
92 CStrVector::iterator location; |
|
93 location = adjacent_find(v.begin(), v.end(), equal_length); |
|
94 |
|
95 CPPUNIT_ASSERT(location != v.end()); |
|
96 } |
|
97 int AdjTest::equal_length(const char* v1_, const char* v2_) |
|
98 { |
|
99 return ::strlen(v1_) == ::strlen(v2_); |
|
100 } |
|
101 void AdjTest::adjdiff0() |
|
102 { |
|
103 int numbers[5] = { 1, 2, 4, 8, 16 }; |
|
104 int difference[5]; |
|
105 adjacent_difference(numbers, numbers + 5, (int*)difference); |
|
106 CPPUNIT_ASSERT(difference[0]==1); |
|
107 CPPUNIT_ASSERT(difference[1]==1); |
|
108 CPPUNIT_ASSERT(difference[2]==2); |
|
109 CPPUNIT_ASSERT(difference[3]==4); |
|
110 CPPUNIT_ASSERT(difference[4]==8); |
|
111 } |
|
112 void AdjTest::adjdiff1() |
|
113 { |
|
114 vector <int> v(10); |
|
115 for(int i = 0; (size_t)i < v.size(); ++i) |
|
116 v[i] = i * i; |
|
117 vector<int> result(v.size()); |
|
118 adjacent_difference(v.begin(), v.end(), result.begin()); |
|
119 CPPUNIT_ASSERT(result[0]==0) |
|
120 CPPUNIT_ASSERT(result[1]==1) |
|
121 CPPUNIT_ASSERT(result[2]==3) |
|
122 CPPUNIT_ASSERT(result[3]==5) |
|
123 CPPUNIT_ASSERT(result[4]==7) |
|
124 CPPUNIT_ASSERT(result[5]==9) |
|
125 CPPUNIT_ASSERT(result[6]==11) |
|
126 CPPUNIT_ASSERT(result[7]==13) |
|
127 CPPUNIT_ASSERT(result[8]==15) |
|
128 CPPUNIT_ASSERT(result[9]==17) |
|
129 } |
|
130 void AdjTest::adjdiff2() |
|
131 { |
|
132 vector <int> v(10); |
|
133 for (int i = 0; (size_t)i < v.size(); ++i) |
|
134 v[i] = i + 1; |
|
135 vector <int> result(v.size()); |
|
136 adjacent_difference(v.begin(), v.end(), result.begin(), mult); |
|
137 CPPUNIT_ASSERT(result[0]==1) |
|
138 CPPUNIT_ASSERT(result[1]==2) |
|
139 CPPUNIT_ASSERT(result[2]==6) |
|
140 CPPUNIT_ASSERT(result[3]==12) |
|
141 CPPUNIT_ASSERT(result[4]==20) |
|
142 CPPUNIT_ASSERT(result[5]==30) |
|
143 CPPUNIT_ASSERT(result[6]==42) |
|
144 CPPUNIT_ASSERT(result[7]==56) |
|
145 CPPUNIT_ASSERT(result[8]==72) |
|
146 CPPUNIT_ASSERT(result[9]==90) |
|
147 } |
|
148 int AdjTest::mult(int a_, int b_) |
|
149 { |
|
150 return a_ * b_; |
|
151 } |