-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegraltest.cpp
More file actions
52 lines (42 loc) · 1.23 KB
/
integraltest.cpp
File metadata and controls
52 lines (42 loc) · 1.23 KB
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
#include <QtTest/QTest>
#include "integraltest.h"
#include "functions.h"
#include <cmath>
// Константная функция
double constFunction(double x __attribute__((unused))) {
return 1;
}
double constFunction2(double x __attribute__((unused))) {
return 2;
}
double constFunction3(double x __attribute__((unused))) {
return 2.5;
}
#define DOUBLE_COMPARE(x,y) QVERIFY(fabs(x-y) < 0.01)
void IntegralTest::testConstant() {
DOUBLE_COMPARE(integral(constFunction, 0, 10),
10.0 ); // 1e-10
DOUBLE_COMPARE(integral(constFunction, 2, 10),
8.0 ); // 1e-10
DOUBLE_COMPARE( integral(constFunction2, 2.0, 10),
16.0 ); // 1e-10
DOUBLE_COMPARE( integral(constFunction3, 10, 20.0),
2.5 * 10 ); // 1e-10
}
// Константная функция
double xFunc(double x) {
return x;
}
void IntegralTest::testLinear() {
// y = x 0..1 -> 0.5
DOUBLE_COMPARE( integral(xFunc, 0, 1), 0.5 );
DOUBLE_COMPARE( integral(xFunc, -10, 10), 0.0 );
DOUBLE_COMPARE( integral(xFunc, 0, 4), 8.0 );
}
double x2Func(double x) {
return pow(x, 2);
}
void IntegralTest::testQuadratic() {
// y = x^2 x^3/3 0..1 -> 1/3
DOUBLE_COMPARE( integral(x2Func, 0, 1.0), 1.0 / 3.0 );
}