-
Notifications
You must be signed in to change notification settings - Fork 0
/
testComplex.cpp
319 lines (240 loc) · 7.52 KB
/
testComplex.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// Created by af on 31.01.21.
//
#define AML_USE_STD_COMPLEX
#include "amathlib.h"
#include <iostream>
#include <iomanip>
#define ASSERT_AML(A, B) if(!checkEquality(A,B)){std::cerr<<"Test " << testCaseId << " failed in line " << __LINE__ << std::endl;return -1;}
#define ASSERT_AML_BOOL(A) if(!A){std::cerr<<"Test "<< testCaseId <<" failed in line" << __LINE__ << std::endl;return -1;}
using namespace std::complex_literals;
struct ieedouble {
unsigned long frac: 52;
unsigned int exp: 11;
unsigned int sign: 1;
};
union double_seg {
double value;
ieedouble seg;
};
bool checkEquality(Complex64 a, Complex64 b) {
Complex64 diff = (a - b);
if (a.c.c[0] != b.c.c[0]) {
return false;
}
if (a.c.c[1] != b.c.c[1]) {
return false;
}
return true;
}
bool checkEquality(const Complex64 a, const std::complex<double> b) {
Complex64 diff = (a - b);
if (a.c.c[0] != b.real()) {
std::cout << diff.abs() << std::endl;
double_seg real1;
real1.value = a.c.c[0];
double_seg real2;
real2.value = b.real();
std::cout << real1.seg.frac << "\n" << real2.seg.frac << std::endl;
std::cout << real1.seg.exp << "\n" << real2.seg.exp << std::endl;
return false;
}
if (a.c.c[1] != b.imag()) {
std::cout << diff.abs() << std::endl;
double_seg img1;
img1.value = a.c.c[0];
double_seg img2;
img2.value = b.imag();
std::cout << img1.seg.frac << "\n" << img2.seg.frac << std::endl;
std::cout << img1.seg.exp << "\n" << img2.seg.exp << std::endl;
return false;
}
return true;
}
bool checkEquality(Complex32 a, Complex32 b) {
if (a.c.c[0] != b.c.c[0]) {
return false;
}
if (a.c.c[1] != b.c.c[1]) {
return false;
}
return true;
}
bool checkEquality(Complex32 a, std::complex<float> b) {
if (a.c.c[0] != b.real()) {
return false;
}
if (a.c.c[1] != b.imag()) {
return false;
}
return true;
}
const char *getTruthValue(bool value) {
if (value) {
return "true";
}
return "false";
}
int main() {
uint32_t testCaseId = 1;
std::complex<double> sc1(6.2, 3.0);
auto c1 = (Complex64) sc1;
std::cout << testCaseId << "\t:\t" << c1 << "\t\t\t\t\t:\t" << sc1 << std::endl;//1
ASSERT_AML(c1, sc1);
testCaseId++;
std::complex<double> sc2(4.6, 2.9);
auto c2 = (Complex64) sc2;
c2 += c1;
sc2 += sc1;
std::cout << testCaseId << "\t:\t" << c2 << "\t\t\t\t\t:\t" << sc2 << std::endl;//2
ASSERT_AML(c2, sc2);
testCaseId++;
std::complex<double> sc3 = 2.0 + 4.7i;
Complex64 c3 = 2.0 + 4.7_i;
std::cout << testCaseId << "\t:\t" << c3 << "\t\t\t\t\t:\t" << sc3 << std::endl;//3
ASSERT_AML(c3, sc3);
testCaseId++;
std::complex<double> sc4 = 2.0 + 4i;
Complex64 c4 = 2 + 4_i;
std::cout << testCaseId << "\t:\t" << c4 << "\t\t\t\t\t\t:\t" << sc4 << std::endl;//4
ASSERT_AML(c4, sc4);
testCaseId++;
Complex64 c5 = 3.4 - 0.9_i;
std::complex<double> sc5 = toStdComplex(c5);
std::cout << testCaseId << "\t:\t" << c5 << "\t\t\t\t\t:\t" << sc5 << std::endl;//5
ASSERT_AML(c5, sc5);
testCaseId++;
std::complex<double> sc6 = 0.2;
Complex64 c6 = 0.2;
std::cout << testCaseId << "\t:\t" << c6 << "\t\t\t\t\t:\t" << sc6 << std::endl;//6
ASSERT_AML(c6, sc6);
testCaseId++;
std::complex<double> sc7 = 0.2 + 0.1i;
Complex64 c7 = 0.2 + 0.1_i;
std::complex<double> sc8 = -0.7 + 9.3i;
Complex64 c8 = -0.7 + 9.3_i;
c7.add(c8);
sc7 = sc7 + sc8;
std::cout << testCaseId << "\t:\t" << c7 << "\t\t\t\t\t:\t" << sc7 << std::endl;//7
ASSERT_AML(c7, sc7);
testCaseId++;
std::complex<double> sc9 = std::polar<double>(20, 1);
Complex64 c9;
c9.polar(20, 1);
std::cout << testCaseId << "\t:\t" << c9 << "\t\t\t:\t" << sc9 << std::endl;//8
ASSERT_AML(c9, sc9);
testCaseId++;
Complex64 c10;
c10.polar(c9.length(), c9.angle());
std::cout << testCaseId << "\t:\t" << c9 << "\t\t\t:\t" << c10 << std::endl;//9
ASSERT_AML(c10, c9);
testCaseId++;
c10 = Complex64(c9.real(), c9.imaginary());
std::cout << testCaseId << "\t:\t" << c9 << "\t\t\t:\t" << c10 << std::endl;//10
ASSERT_AML(c10, c9);
testCaseId++;
std::cout << testCaseId << "\t:\t" << c10.abs() << " == |" << c10 << "| \t:\t"
<< getTruthValue(c10.abs_eq(c10.abs()))
<< std::endl;//11
ASSERT_AML_BOOL(c10.abs_eq(c10.abs()));
testCaseId++;
std::cout << testCaseId << "\t:\t" << c10.abs() << " == |" << c10 << "| \t:\t" << getTruthValue(c10.abs_eq(c10))
<< std::endl;//12
ASSERT_AML_BOOL(c10.abs_eq(c10));
testCaseId++;
std::cout << testCaseId << "\t:\t" << c10.abs() << " == |" << (c10 + 1) << "| \t:\t"
<< getTruthValue(c10.abs_eq(c10 + 1)) << std::endl;//13
ASSERT_AML_BOOL((!c10.abs_eq(c10 + 1)));
testCaseId++;
Complex64 c11 = c10;
c10 += 1_i;
std::cout << testCaseId << "\t:\t" << c11.abs() << " == |" << c10 << "| \t:\t"
<< getTruthValue(c11.abs_eq(c10.abs()))
<< std::endl;//14
ASSERT_AML_BOOL((!c11.abs_eq(c10.abs())));
testCaseId++;
std::complex<double> sc12 = 150.2 + 213.23i;
Complex64 c12 = 150.2 + 213.23_i;
std::complex<double> sc13 = 432.3 + 2.3i;
Complex64 c13 = 432.3 + 2.3_i;
c12.subtract(c13);
sc12 = sc12 - sc13;
std::cout << testCaseId << "\t:\t" << c12 << "\t\t\t:\t" << sc12 << std::endl;//15
ASSERT_AML(c12, sc12);
testCaseId++;
c12.subtract(c13, false);
std::cout << testCaseId << "\t:\t" << c12 << "\t\t\t:\t" << sc12 << std::endl;//16
ASSERT_AML(c12, sc12);
testCaseId++;
c12.subtract(c13, true);
sc12 = sc12 - sc13;
std::cout << testCaseId << "\t:\t" << c12 << "\t\t\t:\t" << sc12 << std::endl;//17
ASSERT_AML(c12, sc12);
testCaseId++;
std::complex<double> sc14 = 12.4 + 42.4i;
Complex64 c14 = 12.4 + 42.4_i;
c14 = 1.0 / c14;
sc14 = 1.0 / sc14;
std::cout << testCaseId << "\t:\t" << c14 << "\t:\t" << sc14 << std::endl;//18
ASSERT_AML(c14, sc14);
testCaseId++;
std::complex<double> sc15 = 12.4 + 42.4i;
std::complex<double> sc16 = 19.6 + 11.3i;
Complex64 c15 = 12.4 + 42.4_i;
c15 = sc16 + c15;
sc15 = sc16 + sc15;
std::cout << testCaseId << "\t:\t" << c15 << "\t\t\t\t\t:\t" << sc15 << std::endl;//19
ASSERT_AML(c15, sc15);
testCaseId++;
c15 = sc16 - c15;
sc15 = sc16 - sc15;
std::cout << testCaseId << "\t:\t" << c15 << "\t\t\t\t:\t" << sc15 << std::endl;//20
ASSERT_AML(c15, sc15);
testCaseId++;
c15 = sc16 * c15;
sc15 = sc16 * sc15;
std::cout << testCaseId << "\t:\t" << c15 << "\t\t\t:\t" << sc15 << std::endl;//21
ASSERT_AML(c15, sc15);
testCaseId++;
c15 = sc16 / c15;
sc15 = sc16 / sc15;
std::cout << std::setprecision(100) << testCaseId << "\t:\t" << c15 << "\t:\t" << sc15 << std::endl;//22
ASSERT_AML(c15, sc15);
testCaseId++;
Complex64 c17 = 1.0 + 5.0_i;
std::complex<double> sc17 = (STD_COMPLEX64_CAST) c17;
std::cout << testCaseId << "\t:\t" << c17 << "\t\t\t\t\t\t:\t" << sc17 << std::endl;//23
ASSERT_AML(c17, sc17);
testCaseId++;
Complex64 c18 = 1.7 + 4.1_i;
std::complex<double> sc18 = (STD_COMPLEX64_CAST) c18;
c18 = log(c18);
sc18 = log(sc18);
std::cout << testCaseId << "\t:\t" << c18 << "\t\t\t:\t" << sc18 << std::endl;//24
ASSERT_AML(c18, sc18);
testCaseId++;
// vector test
Complex64 c19 = 8.7 - 3.2_i;
Array2Complex64 a2c19(c19);
Array4Complex64 a4c19(c19);
Array8Complex64 a8c19(c19);
std::cout << testCaseId << "\t:\t" << c19 << "\t\t\t\t\t:\t" << a2c19[0] << std::endl;//25
ASSERT_AML(c19, a2c19[0]);
ASSERT_AML(c19, a2c19[1]);
ASSERT_AML(c19, a4c19[0]);
ASSERT_AML(c19, a4c19[0]);
ASSERT_AML(c19, a4c19[1]);
ASSERT_AML(c19, a4c19[2]);
ASSERT_AML(c19, a4c19[3]);
ASSERT_AML(c19, a8c19[0]);
ASSERT_AML(c19, a8c19[1]);
ASSERT_AML(c19, a8c19[2]);
ASSERT_AML(c19, a8c19[3]);
ASSERT_AML(c19, a8c19[4]);
ASSERT_AML(c19, a8c19[5]);
ASSERT_AML(c19, a8c19[6]);
ASSERT_AML(c19, a8c19[7]);
testCaseId++;
std::cout << "test complete" << std::endl;
return 0;
}