1e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <vector> 2e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <algorithm> 3e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include <numeric> 4e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 5e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#include "cppunit/cppunit_proxy.h" 6e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 7e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 8e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottusing namespace std; 9e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif 10e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 11e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 12e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// TestCase class 13e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 14e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottclass InnerprodTest : public CPPUNIT_NS::TestCase 15e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ 16e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_TEST_SUITE(InnerprodTest); 17e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_TEST(inprod0); 18e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_TEST(inprod1); 19e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_TEST(inprod2); 20e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_TEST_SUITE_END(); 21e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 22e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottprotected: 23e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott void inprod0(); 24e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott void inprod1(); 25e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott void inprod2(); 26e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 27e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott static size_t add(size_t a_, size_t b_) { 28e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott return a_ + b_; 29e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott } 30e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 31e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott static size_t mult(size_t a_, size_t b_) { 32e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott return a_ * b_; 33e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott } 34e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott}; 35e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 36e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick ScottCPPUNIT_TEST_SUITE_REGISTRATION(InnerprodTest); 37e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 38e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 39e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// tests implementation 40e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott// 41e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottvoid InnerprodTest::inprod0() 42e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ 43e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott int vector1[5] = { 1, 2, 3, 4, 5 }; 44e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott int vector2[5] = { 1, 2, 3, 4, 5 }; 45e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 46e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott int result; 47e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott result = inner_product(vector1, vector1 + 5, vector2, 0); 48e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_ASSERT(result==55); 49e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott} 50e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottvoid InnerprodTest::inprod1() 51e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ 52e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott vector<size_t> v1(3); 53e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott vector<size_t> v2(v1.size()); 54e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott for (size_t i = 0; i < v1.size(); ++i) { 55e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott v1[i] = i + 1; 56e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott v2[i] = v1.size() - i; 57e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott } 58e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott size_t result = inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)0); 59e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_ASSERT(result == 10); 60e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott} 61e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottvoid InnerprodTest::inprod2() 62e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ 63e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott vector<size_t> v1(3); 64e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott vector<size_t> v2(v1.size()); 65e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott for(size_t i = 0; i < v1.size(); ++i) { 66e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott v1[i] = i + 1; 67e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott v2[i] = v1.size() - i; 68e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott } 69e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott size_t result=inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)1, mult, add); 70e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott 71e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott CPPUNIT_ASSERT(result == 64); 72e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott} 73