-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_complex.t
159 lines (139 loc) · 2.94 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
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terratest/terratest"
local C = terralib.includec("string.h")
local tmath = require("tmath")
local concepts = require("concepts")
local complex = require("complex")
local nfloat = require("nfloat")
local float256 = nfloat.FixedFloat(256)
testenv "Complex numbers" do
for _, T in pairs({float, double, float256, int8, int16, int32, int64}) do
local complex_t = complex.complex(T)
local im = complex_t:unit()
testset(T) "Initialization" do
terracode
var x = complex_t.from(1, 2)
var y = 1 + 2 * im
end
test x == y
end
testset(T) "printing" do
local format = tmath.numtostr.format[T]
if concepts.Float(T) then
terracode
format = "%0.1f"
var s1 = tmath.numtostr(complex_t.from(1, 2))
var s2 = tmath.numtostr(complex_t.from(1, -2))
end
test C.strcmp(&s1[0], "1.0+2.0im") == 0
test C.strcmp(&s2[0], "1.0-2.0im") == 0
elseif concepts.Integer(T) then
terracode
format = "%d"
var s1 = tmath.numtostr(complex_t.from(1, 2))
var s2 = tmath.numtostr(complex_t.from(1, -2))
end
test C.strcmp(&s1[0], "1+2im") == 0
test C.strcmp(&s2[0], "1-2im") == 0
end
end
testset(T) "Copy" do
terracode
var x = 2 + 3 * im
var y = x
end
test x == y
end
testset(T) "Cast" do
terracode
var x: T = 2
var y: complex_t = x
var xc = complex_t.from(x, 0)
end
test y == xc
end
testset(T) "Add" do
terracode
var x = 1 + 1 * im
var y = 2 + 3 * im
var z = 3 + 4 * im
end
test x + y == z
end
testset(T) "Mul" do
terracode
var x = -1 + im
var y = 2 - 3 * im
var z = 1 + 5 * im
end
test x * y == z
end
testset(T) "Neg" do
terracode
var x = -1 + 2 * im
var y = 1 - 2 * im
end
test x == -y
end
testset(T) "Normsq" do
terracode
var x = 3 + 4 * im
var y = 25
end
test x:normsq() == y
end
testset(T) "Real and imaginary parts" do
terracode
var x = -3 + 5 * im
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 * im
var xc = 5 + 3 * im
end
test x:conj() == xc
end
if T:isfloat() then
testset(T) "Inverse" do
terracode
var x = -3 + 5 * im
var y = -[T](3) / 34 - [T](5) / 34 * im
end
test x:inverse() == y
end
end
testset(T) "Sub" do
terracode
var x = 2 - 3 * im
var y = 5 + 4 * im
var z = - 3 - 7 * im
end
test x - y == z
end
if T:isfloat() then
testset(T) "Div" do
terracode
var x = -5 + im
var y = 1 + im
var z = -2 + 3 * im
end
test x / y == z
end
end
testset(T) "Unit" do
terracode
var u = complex_t.from(0, 1)
end
test u == im
test u == [complex_t:unit()]
end
end
end