31
|
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 <numeric>
|
|
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 InnerprodTest : public CPPUNIT_NS::TestCase
|
|
31 |
{
|
|
32 |
CPPUNIT_TEST_SUITE(InnerprodTest);
|
|
33 |
CPPUNIT_TEST(inprod0);
|
|
34 |
CPPUNIT_TEST(inprod1);
|
|
35 |
CPPUNIT_TEST(inprod2);
|
|
36 |
CPPUNIT_TEST_SUITE_END();
|
|
37 |
|
|
38 |
protected:
|
|
39 |
void inprod0();
|
|
40 |
void inprod1();
|
|
41 |
void inprod2();
|
|
42 |
|
|
43 |
static size_t add(size_t a_, size_t b_) {
|
|
44 |
return a_ + b_;
|
|
45 |
}
|
|
46 |
|
|
47 |
static size_t mult(size_t a_, size_t b_) {
|
|
48 |
return a_ * b_;
|
|
49 |
}
|
|
50 |
};
|
|
51 |
|
|
52 |
CPPUNIT_TEST_SUITE_REGISTRATION(InnerprodTest);
|
|
53 |
|
|
54 |
//
|
|
55 |
// tests implementation
|
|
56 |
//
|
|
57 |
void InnerprodTest::inprod0()
|
|
58 |
{
|
|
59 |
int vector1[5] = { 1, 2, 3, 4, 5 };
|
|
60 |
int vector2[5] = { 1, 2, 3, 4, 5 };
|
|
61 |
|
|
62 |
int result;
|
|
63 |
result = inner_product(vector1, vector1 + 5, vector2, 0);
|
|
64 |
CPPUNIT_ASSERT(result==55);
|
|
65 |
}
|
|
66 |
void InnerprodTest::inprod1()
|
|
67 |
{
|
|
68 |
vector<size_t> v1(3);
|
|
69 |
vector<size_t> v2(v1.size());
|
|
70 |
for (size_t i = 0; i < v1.size(); ++i) {
|
|
71 |
v1[i] = i + 1;
|
|
72 |
v2[i] = v1.size() - i;
|
|
73 |
}
|
|
74 |
size_t result = inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)0);
|
|
75 |
CPPUNIT_ASSERT(result == 10);
|
|
76 |
}
|
|
77 |
void InnerprodTest::inprod2()
|
|
78 |
{
|
|
79 |
vector<size_t> v1(3);
|
|
80 |
vector<size_t> v2(v1.size());
|
|
81 |
for(size_t i = 0; i < v1.size(); ++i) {
|
|
82 |
v1[i] = i + 1;
|
|
83 |
v2[i] = v1.size() - i;
|
|
84 |
}
|
|
85 |
size_t result=inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)1, mult, add);
|
|
86 |
|
|
87 |
CPPUNIT_ASSERT(result == 64);
|
|
88 |
}
|