1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
// Copyright (C) 2000, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#if defined(_MSC_VER)
// Turn off compiler warning about long names
# pragma warning(disable:4786)
#endif
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#include "CoinDenseVector.hpp"
#include "CoinFloatEqual.hpp"
//--------------------------------------------------------------------------
template <typename T> void
CoinDenseVectorUnitTest(T dummy)
{
// Test default constructor
{
CoinDenseVector<T> r;
assert( r.getElements() == 0 );
assert( r.getNumElements() == 0 );
}
const int ne = 4;
T el[ne] = { 10, 40, 1, 50 };
// Create vector and set its value
CoinDenseVector<T> r(ne,el);
// access each element
assert( r.getElements()[0]==10. );
assert( r.getElements()[1]==40. );
assert( r.getElements()[2]== 1. );
assert( r.getElements()[3]==50. );
// Test norms etc
assert( r.sum() == 10.+40.+1.+50. );
assert( r.oneNorm() == 101.0);
// std namespace removed to compile with Microsoft Visual C++ V6
//assert( r.twoNorm() == /*std::*/sqrt(100.0 + 1600. + 1. + 2500.));
CoinRelFltEq eq;
assert( eq(r.twoNorm() , /*std::*/sqrt(100.0 + 1600. + 1. + 2500.)));
assert( r.infNorm() == 50.);
assert(r[0]+r[1]+r[2]+r[3]==101.);
// Test for equality
CoinDenseVector<T> r1;
r1=r;
assert( r1.getElements()[0]==10. );
assert( r1.getElements()[1]==40. );
assert( r1.getElements()[2]== 1. );
assert( r1.getElements()[3]==50. );
// Add dense vectors.
CoinDenseVector<T> add = r + r1;
assert( add[0] == 10.+10. );
assert( add[1] == 40.+40. );
assert( add[2] == 1.+ 1. );
assert( add[3] == 50.+50. );
// Similarly for copy constructor and subtraction and multiplication
CoinDenseVector<T> r2(r);
CoinDenseVector<T> diff = r - r2;
assert(diff.sum() == 0.0);
CoinDenseVector<T> mult = r * r2;
assert( mult[0] == 10.*10. );
assert( mult[1] == 40.*40. );
assert( mult[2] == 1.* 1. );
assert( mult[3] == 50.*50. );
// and division.
CoinDenseVector<T> div = r / r1;
assert(div.sum() == 4.0);
}
template void CoinDenseVectorUnitTest<float>(float);
template void CoinDenseVectorUnitTest<double>(double);
//template void CoinDenseVectorUnitTest<int>(int);
|