1 #include <vector> 2 #include <numeric> 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 AccumTest : public CPPUNIT_NS::TestCase 14 { 15 CPPUNIT_TEST_SUITE(AccumTest); 16 CPPUNIT_TEST(accum1); 17 CPPUNIT_TEST(accum2); 18 CPPUNIT_TEST_SUITE_END(); 19 20 protected: 21 void accum1(); 22 void accum2(); 23 static int mult(int initial_, int element_); 24 }; 25 26 CPPUNIT_TEST_SUITE_REGISTRATION(AccumTest); 27 28 // 29 // tests implementation 30 // accum1()31void AccumTest::accum1() 32 { 33 vector<int> v(5); 34 for(int i = 0; (size_t)i < v.size(); ++i) 35 v[i] = i + 1; 36 int sum = accumulate(v.begin(), v.end(), 0); 37 CPPUNIT_ASSERT(sum==15); 38 } accum2()39void AccumTest::accum2() 40 { 41 vector<int> v(5); 42 for(int i = 0; (size_t)i < v.size(); ++i) 43 v[i] = i + 1; 44 int prod = accumulate(v.begin(), v.end(), 1, mult); 45 CPPUNIT_ASSERT(prod==120); 46 } mult(int initial_,int element_)47int AccumTest::mult(int initial_, int element_) 48 { 49 return initial_ * element_; 50 } 51