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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* -*- 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 "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/math/array.hpp>
#include <ql/utilities/dataformatters.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(ArrayTests)
class FSquared {
public:
Real operator()(Real x) const { return x*x; }
};
BOOST_AUTO_TEST_CASE(testConstruction) {
BOOST_TEST_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+1) << " 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+1) << " element not with required value"
<< "\n required: " << value + i*increment
<< "\n resulting: " << a4[i]);
}
// copy constructor
Array a5(a1); // NOLINT(performance-unnecessary-copy-initialization)
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+1) << " element of copy "
"not with same value as original"
<< "\n original: " << a3[i]
<< "\n copy: " << a6[i]);
}
// transform
Array a10(5);
for (i=0; i < a10.size(); i++) {
a10[i] = static_cast<Real>(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);
}
}
// recast initializer list to Real
Array a11{1, 2, 3, 4, 5};
if (a2.size() != size)
BOOST_ERROR("Array not of the required size"
<< "\n required: " << size
<< "\n resulting: " << a2.size());
for (i=0; i<size; ++i) {
if (a11[i] != Real(i+1))
BOOST_ERROR(io::ordinal(i+1) << " element not with required value"
<< "\n required: " << Real(i+1)
<< "\n resulting: " << a11[i]);
}
}
BOOST_AUTO_TEST_CASE(testArrayFunctions) {
BOOST_TEST_MESSAGE("Testing array functions...");
auto get_array = []() {
Array a(5);
for (Size i=0; i < a.size(); ++i) {
a[i] = std::sin(Real(i))+1.1;
}
return a;
};
const Array a = get_array();
constexpr double exponential = -2.3;
const Array p_lvalue = Pow(a, exponential);
const Array e_lvalue = Exp(a);
const Array l_lvalue = Log(a);
const Array s_lvalue = Sqrt(a);
const Array a_lvalue = Abs(a);
const Array p_rvalue = Pow(get_array(), exponential);
const Array e_rvalue = Exp(get_array());
const Array l_rvalue = Log(get_array());
const Array s_rvalue = Sqrt(get_array());
const Array a_rvalue = Abs(get_array());
constexpr double tol = 10*QL_EPSILON;
for (Size i=0; i < a.size(); ++i) {
if (std::fabs(p_lvalue[i]-std::pow(a[i], exponential)) > tol) {
BOOST_FAIL("Array function test Pow failed (lvalue)");
}
if (std::fabs(p_rvalue[i]-std::pow(a[i], exponential)) > tol) {
BOOST_FAIL("Array function test Pow failed (lvalue)");
}
if (std::fabs(e_lvalue[i]-std::exp(a[i])) > tol) {
BOOST_FAIL("Array function test Exp failed (lvalue)");
}
if (std::fabs(e_rvalue[i]-std::exp(a[i])) > tol) {
BOOST_FAIL("Array function test Exp failed (rvalue)");
}
if (std::fabs(l_lvalue[i]-std::log(a[i])) > tol) {
BOOST_FAIL("Array function test Log failed (lvalue)");
}
if (std::fabs(l_rvalue[i]-std::log(a[i])) > tol) {
BOOST_FAIL("Array function test Log failed (rvalue)");
}
if (std::fabs(s_lvalue[i]-std::sqrt(a[i])) > tol) {
BOOST_FAIL("Array function test Sqrt failed (lvalue)");
}
if (std::fabs(s_rvalue[i]-std::sqrt(a[i])) > tol) {
BOOST_FAIL("Array function test Sqrt failed (rvalue)");
}
if (std::fabs(a_lvalue[i]-std::abs(a[i])) > tol) {
BOOST_FAIL("Array function test Abs failed (lvalue)");
}
if (std::fabs(a_rvalue[i]-std::abs(a[i])) > tol) {
BOOST_FAIL("Array function test Abs failed (rvalue)");
}
}
}
BOOST_AUTO_TEST_CASE(testArrayResize) {
BOOST_TEST_MESSAGE("Testing array resize...");
Array a(10,1.0,1.0);
for (Size i=0; i < 10; ++i)
QL_CHECK_CLOSE(a[i], Real(1+i), 10*QL_EPSILON);
a.resize(5);
BOOST_CHECK(a.size() == 5);
for (Size i=0; i < 5; ++i)
QL_CHECK_CLOSE(a[i], Real(1+i), 10*QL_EPSILON);
a.resize(15);
BOOST_CHECK(a.size() == 15);
for (Size i=0; i < 5; ++i)
QL_CHECK_CLOSE(a[i], Real(1+i), 10*QL_EPSILON);
const Array::const_iterator iter = a.begin();
a.resize(a.size());
BOOST_CHECK(iter == a.begin());
a.resize(10);
BOOST_CHECK(a.size() == 10);
BOOST_CHECK(iter == a.begin());
}
#define QL_CHECK_CLOSE_ARRAY(actual, expected) \
BOOST_REQUIRE(actual.size() == expected.size()); \
for (auto i = 0u; i < actual.size(); i++) { \
QL_CHECK_CLOSE(actual[i], expected[i], 100 * QL_EPSILON); \
} \
BOOST_AUTO_TEST_CASE(testArrayOperators) {
BOOST_TEST_MESSAGE("Testing array operators...");
auto get_array = []() {
return Array{1.1, 2.2, 3.3};
};
const auto a = get_array();
const auto positive = Array{1.1, 2.2, 3.3};
const auto lvalue_positive = +a;
const auto rvalue_positive = +get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_positive, positive);
QL_CHECK_CLOSE_ARRAY(rvalue_positive, positive);
const auto negative = Array{-1.1, -2.2, -3.3};
const auto lvalue_negative = -a;
const auto rvalue_negative = -get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_negative, negative);
QL_CHECK_CLOSE_ARRAY(rvalue_negative, negative);
const auto array_sum = Array{2.2, 4.4, 6.6};
const auto lvalue_lvalue_sum = a + a;
const auto lvalue_rvalue_sum = a + get_array();
const auto rvalue_lvalue_sum = get_array() + a;
const auto rvalue_rvalue_sum = get_array() + get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_lvalue_sum, array_sum);
QL_CHECK_CLOSE_ARRAY(lvalue_rvalue_sum, array_sum);
QL_CHECK_CLOSE_ARRAY(rvalue_lvalue_sum, array_sum);
QL_CHECK_CLOSE_ARRAY(rvalue_rvalue_sum, array_sum);
const auto scalar_sum = Array{2.2, 3.3, 4.4};
const auto lvalue_real_sum = a + 1.1;
const auto rvalue_real_sum = get_array() + 1.1;
const auto real_lvalue_sum = 1.1 + a;
const auto real_rvalue_sum = 1.1 + get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_real_sum, scalar_sum);
QL_CHECK_CLOSE_ARRAY(rvalue_real_sum, scalar_sum);
QL_CHECK_CLOSE_ARRAY(real_lvalue_sum, scalar_sum);
QL_CHECK_CLOSE_ARRAY(real_rvalue_sum, scalar_sum);
const auto array_difference = Array{0.0, 0.0, 0.0};
const auto lvalue_lvalue_difference = a - a; // NOLINT(misc-redundant-expression)
const auto lvalue_rvalue_difference = a - get_array();
const auto rvalue_lvalue_difference = get_array() - a;
const auto rvalue_rvalue_difference = get_array() - get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_lvalue_difference, array_difference);
QL_CHECK_CLOSE_ARRAY(lvalue_rvalue_difference, array_difference);
QL_CHECK_CLOSE_ARRAY(rvalue_lvalue_difference, array_difference);
QL_CHECK_CLOSE_ARRAY(rvalue_rvalue_difference, array_difference);
const auto scalar_difference_1 = Array{0.0, +1.1, +2.2};
const auto scalar_difference_2 = Array{0.0, -1.1, -2.2};
const auto lvalue_real_difference = a - 1.1;
const auto rvalue_real_difference = get_array() - 1.1;
const auto real_lvalue_difference = 1.1 - a;
const auto real_rvalue_difference = 1.1 - get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_real_difference, scalar_difference_1);
QL_CHECK_CLOSE_ARRAY(rvalue_real_difference, scalar_difference_1);
QL_CHECK_CLOSE_ARRAY(real_lvalue_difference, scalar_difference_2);
QL_CHECK_CLOSE_ARRAY(real_rvalue_difference, scalar_difference_2);
const auto array_product = Array{1.1 * 1.1, 2.2 * 2.2, 3.3 * 3.3};
const auto lvalue_lvalue_product = a * a;
const auto lvalue_rvalue_product = a * get_array();
const auto rvalue_lvalue_product = get_array() * a;
const auto rvalue_rvalue_product = get_array() * get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_lvalue_product, array_product);
QL_CHECK_CLOSE_ARRAY(lvalue_rvalue_product, array_product);
QL_CHECK_CLOSE_ARRAY(rvalue_lvalue_product, array_product);
QL_CHECK_CLOSE_ARRAY(rvalue_rvalue_product, array_product);
const auto scalar_product = Array{1.1 * 1.1, 2.2 * 1.1, 3.3 * 1.1};
const auto lvalue_real_product = a * 1.1;
const auto rvalue_real_product = get_array() * 1.1;
const auto real_lvalue_product = 1.1 * a;
const auto real_rvalue_product = 1.1 * get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_real_product, scalar_product);
QL_CHECK_CLOSE_ARRAY(rvalue_real_product, scalar_product);
QL_CHECK_CLOSE_ARRAY(real_lvalue_product, scalar_product);
QL_CHECK_CLOSE_ARRAY(real_rvalue_product, scalar_product);
const auto array_quotient = Array{1.0, 1.0, 1.0};
const auto lvalue_lvalue_quotient = a / a; // NOLINT(misc-redundant-expression)
const auto lvalue_rvalue_quotient = a / get_array();
const auto rvalue_lvalue_quotient = get_array() / a;
const auto rvalue_rvalue_quotient = get_array() / get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_lvalue_quotient, array_quotient);
QL_CHECK_CLOSE_ARRAY(lvalue_rvalue_quotient, array_quotient);
QL_CHECK_CLOSE_ARRAY(rvalue_lvalue_quotient, array_quotient);
QL_CHECK_CLOSE_ARRAY(rvalue_rvalue_quotient, array_quotient);
const auto scalar_quotient_1 = Array{1.1 / 1.1, 2.2 / 1.1, 3.3 / 1.1};
const auto scalar_quotient_2 = Array{1.1 / 1.1, 1.1 / 2.2, 1.1 / 3.3};
const auto lvalue_real_quotient = a / 1.1;
const auto rvalue_real_quotient = get_array() / 1.1;
const auto real_lvalue_quotient = 1.1 / a;
const auto real_rvalue_quotient = 1.1 / get_array();
QL_CHECK_CLOSE_ARRAY(lvalue_real_quotient, scalar_quotient_1);
QL_CHECK_CLOSE_ARRAY(rvalue_real_quotient, scalar_quotient_1);
QL_CHECK_CLOSE_ARRAY(real_lvalue_quotient, scalar_quotient_2);
QL_CHECK_CLOSE_ARRAY(real_rvalue_quotient, scalar_quotient_2);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|