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