1 #include <vector> 2 #include <algorithm> 3 #include <numeric> 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 InnerprodTest : public CPPUNIT_NS::TestCase 15 { 16 CPPUNIT_TEST_SUITE(InnerprodTest); 17 CPPUNIT_TEST(inprod0); 18 CPPUNIT_TEST(inprod1); 19 CPPUNIT_TEST(inprod2); 20 CPPUNIT_TEST_SUITE_END(); 21 22 protected: 23 void inprod0(); 24 void inprod1(); 25 void inprod2(); 26 27 static size_t add(size_t a_, size_t b_) { 28 return a_ + b_; 29 } 30 31 static size_t mult(size_t a_, size_t b_) { 32 return a_ * b_; 33 } 34 }; 35 36 CPPUNIT_TEST_SUITE_REGISTRATION(InnerprodTest); 37 38 // 39 // tests implementation 40 // 41 void InnerprodTest::inprod0() 42 { 43 int vector1[5] = { 1, 2, 3, 4, 5 }; 44 int vector2[5] = { 1, 2, 3, 4, 5 }; 45 46 int result; 47 result = inner_product(vector1, vector1 + 5, vector2, 0); 48 CPPUNIT_ASSERT(result==55); 49 } 50 void InnerprodTest::inprod1() 51 { 52 vector<size_t> v1(3); 53 vector<size_t> v2(v1.size()); 54 for (size_t i = 0; i < v1.size(); ++i) { 55 v1[i] = i + 1; 56 v2[i] = v1.size() - i; 57 } 58 size_t result = inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)0); 59 CPPUNIT_ASSERT(result == 10); 60 } 61 void InnerprodTest::inprod2() 62 { 63 vector<size_t> v1(3); 64 vector<size_t> v2(v1.size()); 65 for(size_t i = 0; i < v1.size(); ++i) { 66 v1[i] = i + 1; 67 v2[i] = v1.size() - i; 68 } 69 size_t result=inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)1, mult, add); 70 71 CPPUNIT_ASSERT(result == 64); 72 } 73