File: CoinDenseVectorTest.cpp

package info (click to toggle)
coinutils 2.11.4%2Brepack1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,212 kB
  • sloc: cpp: 73,714; sh: 11,224; makefile: 276; ansic: 35
file content (86 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (14)
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);