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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2005 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "array.hpp"
#include "utilities.hpp"
#include <ql/math/array.hpp>
#include <ql/utilities/dataformatters.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
class FSquared : std::unary_function<Real,Real> {
public:
Real operator()(Real x) const { return x*x; }
};
void ArrayTest::testConstruction() {
BOOST_MESSAGE("Testing array construction...");
// empty array
Array a1;
if (!a1.empty())
BOOST_ERROR("default-initialized array is not empty "
"(size = " << a1.size() << ")");
// sized array
Size size = 5;
Array a2(size);
if (a2.size() != size)
BOOST_ERROR("array not of the required size"
<< "\n required: " << size
<< "\n resulting: " << a2.size());
// sized array, constant values
Real value = 42.0;
Array a3(size, value);
if (a3.size() != size)
BOOST_ERROR("array not of the required size"
<< "\n required: " << size
<< "\n resulting: " << a3.size());
Size i;
for (i=0; i<size; i++) {
if (a3[i] != value)
BOOST_ERROR(io::ordinal(i) << " element not with required value"
<< "\n required: " << value
<< "\n resulting: " << a3[i]);
}
// sized array, incremental values
Real increment = 3.0;
Array a4(size, value, increment);
if (a4.size() != size)
BOOST_ERROR("array not of the required size"
<< "\n required: " << size
<< "\n resulting: " << a4.size());
for (i=0; i<size; i++) {
if (a4[i] != value + i*increment)
BOOST_ERROR(io::ordinal(i) << " element not with required value"
<< "\n required: " << value + i*increment
<< "\n resulting: " << a4[i]);
}
// copy constructor
Array a5(a1);
if (a5.size() != a1.size())
BOOST_ERROR("copy not of the same size as original"
<< "\n original: " << a1.size()
<< "\n copy: " << a5.size());
Array a6(a3);
if (a6.size() != a3.size())
BOOST_ERROR("copy not of the same size as original"
<< "\n original: " << a3.size()
<< "\n copy: " << a6.size());
for (i=0; i<a3.size(); i++) {
if (a6[i] != a3[i])
BOOST_ERROR(io::ordinal(i) << " element of copy "
"not with same value as original"
<< "\n original: " << a3[i]
<< "\n copy: " << a6[i]);
}
// creation of disposable array
Array temp1(size, value);
Disposable<Array> temp2(temp1);
if (temp2.size() != size || !temp1.empty())
BOOST_ERROR("array not correctly moved into disposable array"
<< "\n original size of source: " << size
<< "\n current size of source: " << temp1.size()
<< "\n current size of target: " << temp2.size());
for (i=0; i<size; i++) {
if (temp2[i] != value)
BOOST_ERROR(io::ordinal(i) << " element of disposable "
"not moved correctly"
<< "\n required: " << value
<< "\n resulting: " << temp2[i]);
}
// copy constructor from disposable
Array a7(temp2);
if (a7.size() != size || !temp2.empty())
BOOST_ERROR("disposable array not correctly moved into array"
<< "\n original size of source: " << size
<< "\n current size of source: " << temp2.size()
<< "\n current size of target: " << a7.size());
for (i=0; i<size; i++) {
if (a7[i] != value)
BOOST_ERROR(io::ordinal(i) << " element not moved correctly"
<< "\n required: " << value
<< "\n resulting: " << a7[i]);
}
// assignment
Array a8;
a8 = a7;
if (a8.size() != a7.size())
BOOST_ERROR("copy not of the same size as original"
<< "\n original: " << a7.size()
<< "\n copy: " << a8.size());
for (i=0; i<a7.size(); i++) {
if (a8[i] != a7[i])
BOOST_ERROR(io::ordinal(i) << " element of copy "
"not with same value as original"
<< "\n original: " << a7[i]
<< "\n copy: " << a8[i]);
}
// assignment from disposable
Array temp3(size, value);
Disposable<Array> temp4(temp3);
Array a9;
a9 = temp4;
if (a9.size() != size || !temp4.empty())
BOOST_ERROR("disposable array not correctly moved into array"
<< "\n original size of source: " << size
<< "\n current size of source: " << temp4.size()
<< "\n current size of target: " << a9.size());
for (i=0; i<size; i++) {
if (a9[i] != value)
BOOST_ERROR(io::ordinal(i) << " element not moved correctly"
<< "\n required: " << value
<< "\n resulting: " << a9[i]);
}
// transform
Array a10(5);
for (i=0; i < a10.size(); i++) {
a10[i] = i;
}
FSquared f2;
std::transform(a10.begin(), a10.end(), a10.begin(), FSquared());
for (i=0; i < a10.size(); i++) {
Real calculated = f2(static_cast<Real>(i));
if (std::fabs(a10[i] - calculated) >= 1e-5) {
BOOST_ERROR("Array transform test failed " << a10[i] << " "
<< calculated);
}
}
}
test_suite* ArrayTest::suite() {
test_suite* suite = BOOST_TEST_SUITE("array tests");
suite->add(BOOST_TEST_CASE(&ArrayTest::testConstruction));
return suite;
}
|