File: integration_test.cpp

package info (click to toggle)
libitpp 4.3.1-14
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (100 lines) | stat: -rw-r--r-- 2,977 bytes parent folder | download | duplicates (6)
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
/*!
 * \file
 * \brief Transforms test program
 * \author Tony Ottosson, Thomas Eriksson, Simon Wood, Adam Piatyszek, Andy Panov and Bogdan Cristea
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2013  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ 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 GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/itbase.h>
#include "gtest/gtest.h"

using namespace std;
using namespace itpp;

//Integration is tested with x*ln(x) function
double test_integrand(const double x)
{
  return x * log(x);
}

struct Test_Integrand {
  double operator()(double x) {
    return x * log(x);
  }
};

//set test tolerance (measure of relative and absolute error)
const double max_rel_error = 1e-6;
const double max_abs_error = 1e-6;

//results tester
inline void test_result(double q, double ref)
{
  if(abs(q - ref) < max_abs_error) return; //handle numbers with absolute value close to zero (relative error can be huge for them)
  double rel_error = abs(q - ref) / abs(q);
  ASSERT_LE(rel_error, max_rel_error);
}

//analytically computed integral
double integral_value(double a, double b)
{
  return 0.25 * b * b * (2 * log(b) - 1.0) - 0.25 * a * a * (2 * log(a) - 1.0);
}
//----------------------------------------------
//Gtest test cases
//----------------------------------------------
static const double low_lim = 1.5;
static const double hi_lim = 3.5;
static const double ref_integral_value = integral_value(low_lim, hi_lim);

TEST(Integration, Simpson)
{
  {
    SCOPED_TRACE("Testing function object");
    double q = itpp::quad(Test_Integrand(), low_lim, hi_lim);
    test_result(q, ref_integral_value);
  }
  {
    SCOPED_TRACE("Testing double f(double)");
    double q = itpp::quad(test_integrand, low_lim, hi_lim);
    test_result(q, ref_integral_value);
  }
}

TEST(Integration, Lobatto)
{
  {
    SCOPED_TRACE("Testing function object");
    double q = itpp::quadl(Test_Integrand(), low_lim, hi_lim);
    test_result(q, ref_integral_value);
  }
  {
    SCOPED_TRACE("Testing double f(double)");
    double q = itpp::quadl(test_integrand, low_lim, hi_lim);
    test_result(q, ref_integral_value);
  }
}