-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_complex.t
128 lines (113 loc) · 2.31 KB
/
test_complex.t
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terratest/terratest"
local complex = require("complex")
testenv "Complex numbers" do
for _, T in pairs({float, double, int8, int16, int32, int64}) do
local complex = complex.complex(T)
testset(T) "Initialization" do
terracode
var x = complex.from(1, 2)
var y = 1 + 2 * complex.I()
end
test x == y
end
testset(T) "Copy" do
terracode
var x = 2 + 3 * complex.I()
var y = x
end
test x == y
end
testset(T) "Cast" do
terracode
var x: T = 2
var y: complex = x
var xc = complex.from(x, 0)
end
test y == xc
end
testset(T) "Add" do
terracode
var x = 1 + 1 * complex.I()
var y = 2 + 3 * complex.I()
var z = 3 + 4 * complex.I()
end
test x + y == z
end
testset(T) "Mul" do
terracode
var x = -1 + complex.I()
var y = 2 - 3 * complex.I()
var z = 1 + 5 * complex.I()
end
test x * y == z
end
testset(T) "Neg" do
terracode
var x = -1 + 2 * complex.I()
var y = 1 - 2 * complex.I()
end
test x == -y
end
testset(T) "Normsq" do
terracode
var x = 3 + 4 * complex.I()
var y = 25
end
test x:normsq() == y
end
testset(T) "Real and imaginary parts" do
terracode
var x = -3 + 5 * complex.I()
var xre = -3
var xim = 5
end
test x:real() == xre
test x:imag() == xim
end
testset(T) "Conj" do
terracode
var x = 5 - 3 * complex.I()
var xc = 5 + 3 * complex.I()
end
test x:conj() == xc
end
if T:isfloat() then
testset(T) "Inverse" do
terracode
var x = -3 + 5 * complex.I()
var y = -[T](3) / 34 - [T](5) / 34 * complex.I()
end
test x:inverse() == y
end
end
testset(T) "Sub" do
terracode
var x = 2 - 3 * complex.I()
var y = 5 + 4 * complex.I()
var z = - 3 - 7 * complex.I()
end
test x - y == z
end
if T:isfloat() then
testset(T) "Div" do
terracode
var x = -5 + complex.I()
var y = 1 + complex.I()
var z = -2 + 3 * complex.I()
end
test x / y == z
end
end
testset(T) "Unit" do
terracode
var u = complex.from(0, 1)
end
test u == complex.I()
test u == complex.unit()
end
end
end