5 #include "cppunit/cppunit_proxy.h"
7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
14 class AdjTest
: public CPPUNIT_NS::TestCase
16 CPPUNIT_TEST_SUITE(AdjTest
);
17 CPPUNIT_TEST(adjfind0
);
18 CPPUNIT_TEST(adjfind1
);
19 CPPUNIT_TEST(adjfind2
);
20 CPPUNIT_TEST(adjdiff0
);
21 CPPUNIT_TEST(adjdiff1
);
22 CPPUNIT_TEST(adjdiff2
);
23 CPPUNIT_TEST_SUITE_END();
32 static int equal_length(const char* v1_
, const char* v2_
);
33 static int mult(int a_
, int b_
);
36 CPPUNIT_TEST_SUITE_REGISTRATION(AdjTest
);
39 // tests implementation
41 void AdjTest::adjfind0()
43 int numbers1
[5] = { 1, 2, 4, 8, 16 };
44 int numbers2
[5] = { 5, 3, 2, 1, 1 };
46 int* location
= adjacent_find((int*)numbers1
, (int*)numbers1
+ 5);
47 CPPUNIT_ASSERT(location
== numbers1
+ 5); // no adj so loc should be _last
49 location
= adjacent_find((int*)numbers2
, (int*)numbers2
+ 5);
50 CPPUNIT_ASSERT(location
!= numbers2
+ 5); // adj location off should be 3 (first 1)
51 CPPUNIT_ASSERT((location
- numbers2
)==3);
53 void AdjTest::adjfind1()
55 typedef vector
<int> IntVector
;
57 for (int i
= 0; (size_t)i
< v
.size(); ++i
)
59 IntVector::iterator location
;
60 location
= adjacent_find(v
.begin(), v
.end());
61 CPPUNIT_ASSERT(location
== v
.end());
63 location
= adjacent_find(v
.begin(), v
.end());
64 CPPUNIT_ASSERT(location
!= v
.end());
66 void AdjTest::adjfind2()
68 typedef vector
<const char*> CStrVector
;
70 const char* names
[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
72 const int nameCount
= sizeof(names
)/sizeof(names
[0]);
73 CStrVector
v(nameCount
);
74 for(int i
= 0; i
< nameCount
; i
++)
76 CStrVector::iterator location
;
77 location
= adjacent_find(v
.begin(), v
.end(), equal_length
);
79 CPPUNIT_ASSERT(location
!= v
.end());
81 int AdjTest::equal_length(const char* v1_
, const char* v2_
)
83 return ::strlen(v1_
) == ::strlen(v2_
);
85 void AdjTest::adjdiff0()
87 int numbers
[5] = { 1, 2, 4, 8, 16 };
89 adjacent_difference(numbers
, numbers
+ 5, (int*)difference
);
90 CPPUNIT_ASSERT(difference
[0]==1);
91 CPPUNIT_ASSERT(difference
[1]==1);
92 CPPUNIT_ASSERT(difference
[2]==2);
93 CPPUNIT_ASSERT(difference
[3]==4);
94 CPPUNIT_ASSERT(difference
[4]==8);
96 void AdjTest::adjdiff1()
99 for(int i
= 0; (size_t)i
< v
.size(); ++i
)
101 vector
<int> result(v
.size());
102 adjacent_difference(v
.begin(), v
.end(), result
.begin());
103 CPPUNIT_ASSERT(result
[0]==0)
104 CPPUNIT_ASSERT(result
[1]==1)
105 CPPUNIT_ASSERT(result
[2]==3)
106 CPPUNIT_ASSERT(result
[3]==5)
107 CPPUNIT_ASSERT(result
[4]==7)
108 CPPUNIT_ASSERT(result
[5]==9)
109 CPPUNIT_ASSERT(result
[6]==11)
110 CPPUNIT_ASSERT(result
[7]==13)
111 CPPUNIT_ASSERT(result
[8]==15)
112 CPPUNIT_ASSERT(result
[9]==17)
114 void AdjTest::adjdiff2()
117 for (int i
= 0; (size_t)i
< v
.size(); ++i
)
119 vector
<int> result(v
.size());
120 adjacent_difference(v
.begin(), v
.end(), result
.begin(), mult
);
121 CPPUNIT_ASSERT(result
[0]==1)
122 CPPUNIT_ASSERT(result
[1]==2)
123 CPPUNIT_ASSERT(result
[2]==6)
124 CPPUNIT_ASSERT(result
[3]==12)
125 CPPUNIT_ASSERT(result
[4]==20)
126 CPPUNIT_ASSERT(result
[5]==30)
127 CPPUNIT_ASSERT(result
[6]==42)
128 CPPUNIT_ASSERT(result
[7]==56)
129 CPPUNIT_ASSERT(result
[8]==72)
130 CPPUNIT_ASSERT(result
[9]==90)
132 int AdjTest::mult(int a_
, int b_
)