-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
95 lines (80 loc) · 2 KB
/
test.cpp
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
// Catch2 is a unit testing library
// Here we let it create a main() function for us
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "complex.hpp"
TEST_CASE("Complex numbers are constructed real/imag parts readable") {
const Complex zero;
REQUIRE(zero.real() == 0.0);
REQUIRE(zero.imag() == 0.0);
const Complex one{1.0};
REQUIRE(one.real() == 1.0);
REQUIRE(one.imag() == 0.0);
const Complex i{0, 1};
REQUIRE(i.real() == 0.0);
REQUIRE(i.imag() == 1.0);
const Complex z1{1, -83};
const Complex z2 = z1;
REQUIRE(i.real() == 0.0);
REQUIRE(i.imag() == 1.0);
}
TEST_CASE("Complex numbers can be compared") {
const Complex zero;
const Complex one{1.0};
const Complex i{0, 1};
REQUIRE(zero == zero);
REQUIRE(one == one);
REQUIRE(i == i);
REQUIRE(zero != one);
REQUIRE(one != i);
}
TEST_CASE("Complex numbers can have magnitude computed") {
REQUIRE(Complex{}.norm2() == 0.0);
REQUIRE(Complex{3,4}.norm2() == 25.0);
}
// Pure real => z == z*
void CheckConjReal(double x) {
Complex z{x};
REQUIRE(z == z.conj());
}
// Pure imaginary => z* == -z
void CheckConjImag(double y) {
Complex z{0.0, y};
REQUIRE(z == -z.conj());
}
TEST_CASE("Complex numbers be conjugated") {
CheckConjReal(0);
CheckConjReal(1);
CheckConjReal(-3.14);
CheckConjReal(1.876e6);
CheckConjImag(0);
CheckConjImag(1);
CheckConjImag(-3.14);
CheckConjImag(1.876e6);
}
void CheckZplusZeq2Z(const Complex& z) {
REQUIRE(z + z == Complex{2*z.real(), 2*z.imag()});
}
void CheckZminusZeq0(const Complex& z) {
REQUIRE(z - z == Complex{});
}
TEST_CASE("Complex number can be added and subtracted") {
CheckZplusZeq2Z(1);
CheckZplusZeq2Z(0);
CheckZplusZeq2Z(-1);
CheckZminusZeq0(1);
CheckZminusZeq0(0);
CheckZminusZeq0(-1);
CheckZminusZeq0(Complex{1,2});
CheckZminusZeq0(Complex{-42, 1e-3});
}
TEST_CASE("Complex numbers can be multiplied") {
const Complex i{0, 1};
Complex z{1};
z = z*i;
REQUIRE(z == i);
z = z*i;
REQUIRE(z == Complex{-1});
z = z*i;
REQUIRE(z == -i);
}