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 RmCpTest : public CPPUNIT_NS::TestCase 14 { 15 CPPUNIT_TEST_SUITE(RmCpTest); 16 CPPUNIT_TEST(remcopy1); 17 CPPUNIT_TEST(remcpif1); 18 CPPUNIT_TEST(remif1); 19 CPPUNIT_TEST(remove1); 20 CPPUNIT_TEST(repcpif1); 21 CPPUNIT_TEST(replace0); 22 CPPUNIT_TEST(replace1); 23 CPPUNIT_TEST(replcpy1); 24 CPPUNIT_TEST(replif1); 25 CPPUNIT_TEST(revcopy1); 26 CPPUNIT_TEST(reverse1); 27 CPPUNIT_TEST_SUITE_END(); 28 29 protected: 30 void remcopy1(); 31 void remcpif1(); 32 void remif1(); 33 void remove1(); 34 void repcpif1(); 35 void replace0(); 36 void replace1(); 37 void replcpy1(); 38 void replif1(); 39 void revcopy1(); 40 void reverse1(); 41 42 }; 43 44 CPPUNIT_TEST_SUITE_REGISTRATION(RmCpTest); 45 46 static bool odd(int a_) 47 { 48 return (a_ % 2) != 0; 49 } 50 // 51 // tests implementation 52 // 53 54 void RmCpTest::reverse1() 55 { 56 int numbers[6] = { 0, 1, 2, 3, 4, 5 }; 57 58 reverse(numbers, numbers + 6); 59 CPPUNIT_ASSERT(numbers[0]==5); 60 CPPUNIT_ASSERT(numbers[1]==4); 61 CPPUNIT_ASSERT(numbers[2]==3); 62 CPPUNIT_ASSERT(numbers[3]==2); 63 CPPUNIT_ASSERT(numbers[4]==1); 64 CPPUNIT_ASSERT(numbers[5]==0); 65 } 66 67 void RmCpTest::revcopy1() 68 { 69 int numbers[6] = { 0, 1, 2, 3, 4, 5 }; 70 71 int result[6]; 72 reverse_copy((int*)numbers, (int*)numbers + 6, (int*)result); 73 // 5 4 3 2 1 0 74 CPPUNIT_ASSERT(result[0]==5); 75 CPPUNIT_ASSERT(result[1]==4); 76 CPPUNIT_ASSERT(result[2]==3); 77 CPPUNIT_ASSERT(result[3]==2); 78 CPPUNIT_ASSERT(result[4]==1); 79 CPPUNIT_ASSERT(result[5]==0); 80 } 81 82 void RmCpTest::replif1() 83 { 84 vector <int> v1(10); 85 for (int i = 0; (size_t)i < v1.size(); ++i) 86 v1[i] = i % 5; 87 88 replace_if(v1.begin(), v1.end(), odd, 42); 89 90 // 0 42 2 42 4 0 42 2 42 4 91 CPPUNIT_ASSERT(v1[0]==0); 92 CPPUNIT_ASSERT(v1[1]==42); 93 CPPUNIT_ASSERT(v1[2]==2); 94 CPPUNIT_ASSERT(v1[3]==42); 95 CPPUNIT_ASSERT(v1[4]==4); 96 CPPUNIT_ASSERT(v1[5]==0); 97 CPPUNIT_ASSERT(v1[6]==42); 98 CPPUNIT_ASSERT(v1[7]==2); 99 CPPUNIT_ASSERT(v1[8]==42); 100 CPPUNIT_ASSERT(v1[9]==4); 101 } 102 103 void RmCpTest::replcpy1() 104 { 105 int numbers[6] = { 0, 1, 2, 0, 1, 2 }; 106 int result[6] = { 0, 0, 0, 0, 0, 0 }; 107 108 replace_copy((int*)numbers, (int*)numbers + 6, (int*)result, 2, 42); 109 CPPUNIT_ASSERT(result[0]==0); 110 CPPUNIT_ASSERT(result[1]==1); 111 CPPUNIT_ASSERT(result[2]==42); 112 CPPUNIT_ASSERT(result[3]==0); 113 CPPUNIT_ASSERT(result[4]==1); 114 CPPUNIT_ASSERT(result[5]==42); 115 } 116 117 void RmCpTest::replace0() 118 { 119 int numbers[6] = { 0, 1, 2, 0, 1, 2 }; 120 121 replace(numbers, numbers + 6, 2, 42); 122 123 // 0 1 42 0 1 42 124 CPPUNIT_ASSERT(numbers[0]==0); 125 CPPUNIT_ASSERT(numbers[1]==1); 126 CPPUNIT_ASSERT(numbers[2]==42); 127 CPPUNIT_ASSERT(numbers[3]==0); 128 CPPUNIT_ASSERT(numbers[4]==1); 129 CPPUNIT_ASSERT(numbers[5]==42); 130 } 131 132 void RmCpTest::replace1() 133 { 134 vector <int> v1(10); 135 for (int i = 0; (size_t)i < v1.size(); ++i) 136 v1[i] = i % 5; 137 replace(v1.begin(), v1.end(), 2, 42); 138 139 // 0 1 2 3 4 0 1 2 3 4 140 // 0 1 42 3 4 0 1 42 3 4 141 CPPUNIT_ASSERT(v1[0]==0); 142 CPPUNIT_ASSERT(v1[1]==1); 143 CPPUNIT_ASSERT(v1[2]==42); 144 CPPUNIT_ASSERT(v1[3]==3); 145 CPPUNIT_ASSERT(v1[4]==4); 146 CPPUNIT_ASSERT(v1[5]==0); 147 CPPUNIT_ASSERT(v1[6]==1); 148 CPPUNIT_ASSERT(v1[7]==42); 149 CPPUNIT_ASSERT(v1[8]==3); 150 CPPUNIT_ASSERT(v1[9]==4); 151 } 152 153 void RmCpTest::repcpif1() 154 { 155 vector <int> v1(10); 156 for (int i = 0; (size_t)i < v1.size(); ++i) 157 v1[i] = i % 5; 158 vector <int> v2(v1.size()); 159 160 // 0 1 2 3 4 0 1 2 3 4 161 // 0 1 2 3 4 0 1 2 3 4 162 // 0 42 2 42 4 0 42 2 42 4 163 replace_copy_if(v1.begin(), v1.end(), v2.begin(), odd, 42); 164 CPPUNIT_ASSERT(v1[0]==0); 165 CPPUNIT_ASSERT(v1[1]==1); 166 CPPUNIT_ASSERT(v1[2]==2); 167 CPPUNIT_ASSERT(v1[3]==3); 168 CPPUNIT_ASSERT(v1[4]==4); 169 CPPUNIT_ASSERT(v1[5]==0); 170 CPPUNIT_ASSERT(v1[6]==1); 171 CPPUNIT_ASSERT(v1[7]==2); 172 CPPUNIT_ASSERT(v1[8]==3); 173 CPPUNIT_ASSERT(v1[9]==4); 174 175 CPPUNIT_ASSERT(v2[0]==0); 176 CPPUNIT_ASSERT(v2[1]==42); 177 CPPUNIT_ASSERT(v2[2]==2); 178 CPPUNIT_ASSERT(v2[3]==42); 179 CPPUNIT_ASSERT(v2[4]==4); 180 CPPUNIT_ASSERT(v2[5]==0); 181 CPPUNIT_ASSERT(v2[6]==42); 182 CPPUNIT_ASSERT(v2[7]==2); 183 CPPUNIT_ASSERT(v2[8]==42); 184 CPPUNIT_ASSERT(v2[9]==4); 185 } 186 187 void RmCpTest::remove1() 188 { 189 int numbers[6] = { 1, 2, 3, 1, 2, 3 }; 190 remove((int*)numbers, (int*)numbers + 6, 1); 191 192 // 2 3 2 3 2 3 193 CPPUNIT_ASSERT(numbers[0]==2); 194 CPPUNIT_ASSERT(numbers[1]==3); 195 CPPUNIT_ASSERT(numbers[2]==2); 196 CPPUNIT_ASSERT(numbers[3]==3); 197 CPPUNIT_ASSERT(numbers[4]==2); 198 CPPUNIT_ASSERT(numbers[5]==3); 199 } 200 201 void RmCpTest::remif1() 202 { 203 int numbers[6] = { 0, 0, 1, 1, 2, 2 }; 204 205 remove_if((int*)numbers, (int*)numbers + 6, odd); 206 207 // 0 0 2 2 2 2 208 CPPUNIT_ASSERT(numbers[0]==0); 209 CPPUNIT_ASSERT(numbers[1]==0); 210 CPPUNIT_ASSERT(numbers[2]==2); 211 CPPUNIT_ASSERT(numbers[3]==2); 212 CPPUNIT_ASSERT(numbers[4]==2); 213 CPPUNIT_ASSERT(numbers[5]==2); 214 } 215 216 void RmCpTest::remcopy1() 217 { 218 int numbers[6] = { 1, 2, 3, 1, 2, 3 }; 219 int result[6] = { 0, 0, 0, 0, 0, 0 }; 220 221 remove_copy((int*)numbers, (int*)numbers + 6, (int*)result, 2); 222 223 CPPUNIT_ASSERT(result[0]==1); 224 CPPUNIT_ASSERT(result[1]==3); 225 CPPUNIT_ASSERT(result[2]==1); 226 CPPUNIT_ASSERT(result[3]==3); 227 CPPUNIT_ASSERT(result[4]==0); 228 CPPUNIT_ASSERT(result[5]==0); 229 } 230 231 void RmCpTest::remcpif1() 232 { 233 int numbers[6] = { 1, 2, 3, 1, 2, 3 }; 234 int result[6] = { 0, 0, 0, 0, 0, 0 }; 235 236 remove_copy_if((int*)numbers, (int*)numbers + 6, (int*)result, odd); 237 238 // 2 2 0 0 0 0 239 CPPUNIT_ASSERT(result[0]==2); 240 CPPUNIT_ASSERT(result[1]==2); 241 CPPUNIT_ASSERT(result[2]==0); 242 CPPUNIT_ASSERT(result[3]==0); 243 CPPUNIT_ASSERT(result[4]==0); 244 CPPUNIT_ASSERT(result[5]==0); 245 } 246