|
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 <algorithm> |
|
19 #include <functional> |
|
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 FuncTest : public CPPUNIT_NS::TestCase |
|
31 { |
|
32 CPPUNIT_TEST_SUITE(FuncTest); |
|
33 CPPUNIT_TEST(func1); |
|
34 CPPUNIT_TEST(func2); |
|
35 CPPUNIT_TEST(func3); |
|
36 CPPUNIT_TEST_SUITE_END(); |
|
37 |
|
38 protected: |
|
39 void func1(); |
|
40 void func2(); |
|
41 void func3(); |
|
42 static bool bigger(int i_); |
|
43 static bool bigger_than(int x_, int y_); |
|
44 }; |
|
45 |
|
46 CPPUNIT_TEST_SUITE_REGISTRATION(FuncTest); |
|
47 |
|
48 // |
|
49 // tests implementation |
|
50 // |
|
51 bool FuncTest::bigger(int i_) |
|
52 { |
|
53 return i_ > 3; |
|
54 } |
|
55 bool FuncTest::bigger_than(int x_, int y_) |
|
56 { |
|
57 return x_ > y_; |
|
58 } |
|
59 void FuncTest::func1() |
|
60 { |
|
61 vector<int>v; |
|
62 v.push_back(4); |
|
63 v.push_back(1); |
|
64 v.push_back(5); |
|
65 int n = count_if(v.begin(), v.end(), bigger); |
|
66 CPPUNIT_ASSERT( n == 2 ) |
|
67 } |
|
68 |
|
69 void FuncTest::func2() |
|
70 { |
|
71 vector<int> v; |
|
72 v.push_back(4); |
|
73 v.push_back(1); |
|
74 v.push_back(5); |
|
75 sort(v.begin(), v.end(), bigger_than); |
|
76 |
|
77 CPPUNIT_ASSERT( v[0] == 5 ); |
|
78 CPPUNIT_ASSERT( v[1] == 4 ); |
|
79 CPPUNIT_ASSERT( v[2] == 1 ); |
|
80 } |
|
81 void FuncTest::func3() |
|
82 { |
|
83 vector<int> v; |
|
84 v.push_back(4); |
|
85 v.push_back(1); |
|
86 v.push_back(5); |
|
87 sort(v.begin(), v.end(), greater<int>()); |
|
88 |
|
89 CPPUNIT_ASSERT( v[0] == 5 ); |
|
90 CPPUNIT_ASSERT( v[1] == 4 ); |
|
91 CPPUNIT_ASSERT( v[2] == 1 ); |
|
92 } |