1 #include <vector>
2 #include <algorithm>
3 #include "fadapter.h"
4 
5 #include "cppunit/cppunit_proxy.h"
6 
7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
8 using namespace std;
9 #endif
10 
11 //
12 // TestCase class
13 //
14 class ForeachTest : public CPPUNIT_NS::TestCase
15 {
16   CPPUNIT_TEST_SUITE(ForeachTest);
17   CPPUNIT_TEST(foreach0);
18   CPPUNIT_TEST(foreach1);
19   CPPUNIT_TEST_SUITE_END();
20 
21 protected:
22   void foreach0();
23   void foreach1();
24 };
25 
26 CPPUNIT_TEST_SUITE_REGISTRATION(ForeachTest);
27 
28 //
29 // tests implementation
30 //
increase(int & a_)31 static void increase(int& a_)
32 {
33   a_ += 1;
34 }
foreach0()35 void ForeachTest::foreach0()
36 {
37   int numbers[10] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
38 
39   for_each(numbers, numbers + 10, ptr_fun(increase));
40 
41   CPPUNIT_ASSERT(numbers[0]==2);
42   CPPUNIT_ASSERT(numbers[1]==2);
43   CPPUNIT_ASSERT(numbers[2]==3);
44   CPPUNIT_ASSERT(numbers[3]==4);
45   CPPUNIT_ASSERT(numbers[4]==6);
46   CPPUNIT_ASSERT(numbers[5]==9);
47   CPPUNIT_ASSERT(numbers[6]==14);
48   CPPUNIT_ASSERT(numbers[7]==22);
49   CPPUNIT_ASSERT(numbers[8]==35);
50   CPPUNIT_ASSERT(numbers[9]==56);
51 }
sqr(int & a_)52 static void sqr(int& a_)
53 {
54   a_ = a_ * a_;
55 }
foreach1()56 void ForeachTest::foreach1()
57 {
58   vector<int> v1(10);
59   for (int i = 0; (size_t)i < v1.size(); ++i)
60     v1[i] = i;
61   for_each(v1.begin(), v1.end(), ptr_fun(sqr) );
62 
63   CPPUNIT_ASSERT(v1[0]==0);
64   CPPUNIT_ASSERT(v1[1]==1);
65   CPPUNIT_ASSERT(v1[2]==4);
66   CPPUNIT_ASSERT(v1[3]==9);
67   CPPUNIT_ASSERT(v1[4]==16);
68   CPPUNIT_ASSERT(v1[5]==25);
69   CPPUNIT_ASSERT(v1[6]==36);
70   CPPUNIT_ASSERT(v1[7]==49);
71   CPPUNIT_ASSERT(v1[8]==64);
72   CPPUNIT_ASSERT(v1[9]==81);
73 }
74