genericopenlibs/cppstdlib/stl/test/unit/innerprod_test.cpp
author Pat Downey <patd@symbian.org>
Fri, 04 Jun 2010 16:20:51 +0100
changeset 31 ce057bb09d0b
child 34 5fae379060a7
permissions -rw-r--r--
Revert last code drop.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     1
/*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     3
* All rights reserved.
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     4
* This component and the accompanying materials are made available
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     6
* which accompanies this distribution, and is available
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     8
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     9
* Initial Contributors:
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    11
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    12
* Contributors:
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    13
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    14
* Description: 
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    15
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    16
*/
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    17
#include <vector>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    18
#include <algorithm>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    19
#include <numeric>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    20
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    21
#include "cppunit/cppunit_proxy.h"
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    22
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    23
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    24
using namespace std;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    25
#endif
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    26
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    27
//
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    28
// TestCase class
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    29
//
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    30
class InnerprodTest : public CPPUNIT_NS::TestCase
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    31
{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    32
  CPPUNIT_TEST_SUITE(InnerprodTest);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    33
  CPPUNIT_TEST(inprod0);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    34
  CPPUNIT_TEST(inprod1);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    35
  CPPUNIT_TEST(inprod2);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    36
  CPPUNIT_TEST_SUITE_END();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    37
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    38
protected:
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    39
  void inprod0();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    40
  void inprod1();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    41
  void inprod2();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    42
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    43
  static size_t add(size_t a_, size_t b_) {
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    44
    return a_ + b_;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    45
  }
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    46
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    47
  static size_t mult(size_t a_, size_t b_) {
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    48
    return a_ * b_;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    49
  }
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    50
};
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    51
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    52
CPPUNIT_TEST_SUITE_REGISTRATION(InnerprodTest);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    53
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    54
//
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    55
// tests implementation
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    56
//
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    57
void InnerprodTest::inprod0()
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    58
{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    59
  int vector1[5] = { 1, 2, 3, 4, 5 };
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    60
  int vector2[5] = { 1, 2, 3, 4, 5 };
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    61
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    62
  int result;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    63
  result = inner_product(vector1, vector1 + 5, vector2, 0);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    64
  CPPUNIT_ASSERT(result==55);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    65
}
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    66
void InnerprodTest::inprod1()
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    67
{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    68
  vector<size_t> v1(3);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    69
  vector<size_t> v2(v1.size());
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    70
  for (size_t i = 0; i < v1.size(); ++i) {
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    71
    v1[i] = i + 1;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    72
    v2[i] = v1.size() - i;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    73
  }
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    74
  size_t result = inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)0);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    75
  CPPUNIT_ASSERT(result == 10);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    76
}
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    77
void InnerprodTest::inprod2()
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    78
{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    79
  vector<size_t> v1(3);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    80
  vector<size_t> v2(v1.size());
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    81
  for(size_t i = 0; i < v1.size(); ++i) {
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    82
    v1[i] = i + 1;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    83
    v2[i] = v1.size() - i;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    84
  }
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    85
  size_t result=inner_product(v1.begin(), v1.end(), v2.begin(), (size_t)1, mult, add);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    86
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    87
  CPPUNIT_ASSERT(result == 64);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    88
}