1 #include <vector>
2 #include <algorithm>
3 #include <functional>
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 FuncTest : public CPPUNIT_NS::TestCase
15 {
16   CPPUNIT_TEST_SUITE(FuncTest);
17   CPPUNIT_TEST(func1);
18   CPPUNIT_TEST(func2);
19   CPPUNIT_TEST(func3);
20   CPPUNIT_TEST_SUITE_END();
21 
22 protected:
23   void func1();
24   void func2();
25   void func3();
26   static bool bigger(int i_);
27   static bool bigger_than(int x_, int y_);
28 };
29 
30 CPPUNIT_TEST_SUITE_REGISTRATION(FuncTest);
31 
32 //
33 // tests implementation
34 //
bigger(int i_)35 bool FuncTest::bigger(int i_)
36 {
37   return i_ > 3;
38 }
bigger_than(int x_,int y_)39 bool FuncTest::bigger_than(int x_, int y_)
40 {
41     return x_ > y_;
42 }
func1()43 void FuncTest::func1()
44 {
45   vector<int>v;
46   v.push_back(4);
47   v.push_back(1);
48   v.push_back(5);
49   int n = count_if(v.begin(), v.end(), bigger);
50   CPPUNIT_ASSERT( n == 2 )
51 }
52 
func2()53 void FuncTest::func2()
54 {
55   vector<int> v;
56   v.push_back(4);
57   v.push_back(1);
58   v.push_back(5);
59   sort(v.begin(), v.end(), bigger_than);
60 
61   CPPUNIT_ASSERT( v[0] == 5 );
62   CPPUNIT_ASSERT( v[1] == 4 );
63   CPPUNIT_ASSERT( v[2] == 1 );
64 }
func3()65 void FuncTest::func3()
66 {
67   vector<int> v;
68   v.push_back(4);
69   v.push_back(1);
70   v.push_back(5);
71   sort(v.begin(), v.end(), greater<int>());
72 
73   CPPUNIT_ASSERT( v[0] == 5 );
74   CPPUNIT_ASSERT( v[1] == 4 );
75   CPPUNIT_ASSERT( v[2] == 1 );
76 }
77