forked from gladk/trunk
/
NDimTableTest.cpp
277 lines (243 loc) · 8.4 KB
/
NDimTableTest.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
#define DEBUG_NDIMTABLE
#define YADE_FFTW3
#include <iostream>
#include "lib/base/NDimTable.hpp"
#include <complex>
typedef NDimTable<float> A;
A f(A a) {
return a;
}
struct B : A {
std::string s2;
int n;
B() =default ;
B(const B& other) = default ;
B(const A& other) : A(other) {std::cout <<"COPY B(const A& other)";};
B(B&& other): A(static_cast<A&&>(other)) { std::cout <<"MOVE B(B&& other): A(static_cast<A&&>(other))";};
B(A&& other): A(static_cast<A&&>(other)) { std::cout <<"MOVE B(A&& other): A(static_cast<A&&>(other))";};
B& operator=(const B&)=default;
B& operator=(B&&)=default;
// implicit move contructor B::(B&&) //
// calls A's move constructor //
// calls s2's move constructor //
// and makes a bitwise copy of n
};
struct C : B {
~C() {}; // destructor prevents implicit move ctor C::(C&&)
};
struct D : B {
D() {}
~D() {}; // destructor would prevent implicit move ctor D::(D&&)
D(D&&) = default; // force a move ctor anyway
};
int main(void){
std::cout << "./NDimTableTest > test.tmp 2>&1\ndiff -s test.tmp NDimTableTest_correct_output.txt\n";
std::vector<std::size_t> dim1;
dim1.push_back(3);
dim1.push_back(4);
//dim1.push_back(2);
NDimTable<float> T1(dim1);
for(int i=0 ; i<3; i++)
for(int j=0 ; j<4 ; j++)
//for(int k=0 ; k<4 ; k++)
//T1.at(i,j,k)=100*i+10*j+k;
T1.at(i,j )=100*i+10*j +1 ;
for(int i=0 ; i<3; i++) {
for(int j=0 ; j<4 ; j++) {
//for(int k=0 ; k<2 ; k++) {
//std::cout << T1.at(i,j,k) << " ";
std::cout << T1.at(i,j) << " ";
//};
//std::cout << "\n";
};
std::cout << "\n";
};
std::cout << "\n\nchecking print() call\n\n";
T1.print();
std::vector<std::size_t> dim2;
dim2.push_back(5);
//dim2.push_back(4);
//dim1.push_back(2);
std::cout << "\nCREATE T2\n";
NDimTable<float> T2(dim2);
for(int i=0 ; i<5; i++)
T2.at(i )=1000*i+1000 ;
for(int i=0 ; i<5; i++)
std::cout << T2.at(i ) << " ";
std::cout << "\n\nchecking print() call\n\n";
T2.print();
std::cout << "\n";
std::vector<const NDimTable<float>*> parts;
parts.push_back(&T1);
parts.push_back(&T2);
NDimTable<float> T3(parts);
std::cout << "constructed, print now ---- checking TENSOR PRODUCT\n";
double minRealT3(T3.at(0,0,0)),maxRealT3(T3.at(0,0,0)),sumAllT3(0);
for(int i=0 ; i<3; i++) {
for(int j=0 ; j<4 ; j++) {
for(int k=0 ; k<5 ; k++) {
std::cout << T3.at(i,j,k) << " (" << (100*i+10*j +1)*(1000*k+1000) << ") " ;
assert ( T3.at(i,j,k) == (100*i+10*j +1)*(1000*k+1000) );
minRealT3 = std::min(minRealT3, (double) (100*i+10*j +1)*(1000*k+1000) );
maxRealT3 = std::max(maxRealT3, (double) (100*i+10*j +1)*(1000*k+1000) );
sumAllT3 += (100*i+10*j +1)*(1000*k+1000) ;
//std::cout << T1.at(i,j) << " ";
};
std::cout << "\n";
};
std::cout << "\n";
};
std::cerr.precision(20);
std::cerr << "checking minReal.T3: " << minRealT3 << " " << T3.minReal() << "\n";
std::cerr << "checking maxReal.T3: " << maxRealT3 << " " << T3.maxReal() << "\n";
std::cerr << "checking sumAll.T3 : " << sumAllT3 << " " << T3.sumAll() << "\n";
assert(minRealT3 == T3.minReal());
assert(maxRealT3 == T3.maxReal());
assert(sumAllT3 == T3.sumAll());
std::cout << "checking print() call on T3\n\n";
T3.print();
std::cout << "T4\n";
NDimTable<float> T4;T4=T1;
T1.print();
T4.print();
std::cout << "T5\n";
// std::cout << "T1 address= "<< &T1 << "\n";
NDimTable<float> T5(std::move(T1));
T5.print();
T1.print(); //this must segfault, so don't do it.
// std::cout << "T1 address= "<< &T1 << "\n";
// std::cout << "T5 address= "<< &T5 << "\n";
std::cout << "restore content of T1=T5\n";
T1=T5;
// std::cout << "T1 address= "<< &T1 << "\n";
// std::cout << "T5 address= "<< &T5 << "\n";
T1.print(); //this must NOT segfault now.
std::cout << "\n\nTESTS from http://en.cppreference.com/w/cpp/language/move_constructor\n\n";
std::cout << " Trying to move A (a1) // move-construct from rvalue temporary ";
A a1 = f(A(dim1)); // move-construct from rvalue temporary
std::cout << " Trying to move A (a2) ";
A a2 = std::move(a1); // move-construct from xvalue
std::cout << " Trying to create A (first create) ";
A a4(dim1);
std::cout << " Trying to create B (first create) ";
B b1(dim1);
// std::cout << "!! FIXME - 'move failed! rank:2' above!\n\n"; // FIXED by adding B(A&& other): A(static_cast<A&&>(other))
std::cout << " Before move, b1 = \"" << b1 << "\"\n";
B b2 = std::move(b1); // calls implicit move ctor
std::cout << " After move, b2 = \"" << b2 << "\"\n";
std::cout << " After move, b1 = \"" << b1 << "\"\n";
std::cout << " Trying to move C ";
C c1;
C c2 = std::move(c1); // calls the copy constructor
std::cout << " Trying to move D ";
D d1;
D d2 = std::move(d1);
std::cout << "\n\n TESTING operators! \n\n";
T1.print();
std::cout << "T1-=4;\n";T1-=4; T1.print();
std::cout << "T1+=4;\n";T1+=4; T1.print();
std::cout << "T1*=4;\n";T1*=4; T1.print();
std::cout << "T1/=4;\n";T1/=4; T1.print();
std::cout << "T1*=T1;\n";T1*=T1; T1.print();
// std::cout << "T1.sqrt();\n";T1.sqrt(); T1.print();
std::cout << "T1/=T5;\n";T1/=T5; T1.print();
std::cout << "T1+=T1;\n";T1+=T1; T1.print();
std::cout << "T1-=T1;\n";T1-=T1; T1.print();
std::cout << "T6=T5-100; =========\n";
A T6=T5-100;
// A T6(T5);T6-=100;
T6.print();
T6.abs().print();
T6.print();
std::cout << "T6*=T6;\n";
T6*=T6;
T6.print();
std::cout << "T6.sqrt();\n";
T6.sqrt();
T6.print();
std::cout << "T6.pow(2.0001);\n";
T6.pow(2.0001);
T6.print();
std::cout << "T5="<<T5<<"\n";
std::cout << "(T5*2).print();\n";
(T5*2).print();
std::cout << "....above is FREE of the (T5*2).....\n\na4=T5*2;\n";
a4=T5*2;
a4.print();
std::cout << "a4=a4*2;\n";
a4=a4*2;
a4.print();
std::cout << "A a8=a4*2;\n";
A a8=a4*2;
a8.print();
std::cout << "A a9(a4*2);\n";
A a9(a4*2);
a9.print();
std::cout << "A a10=a9-1000; =========\n";
A a10=a9-1000;
a10.print();
std::cout << "A a11=a9/1000; =========\n";
A a11=a9/1000;
a11.print();
std::cout << "A a12=a9+1000; =========\n";
A a12=a9+1000;
a12.print();
std::cout << "A a13=a9*1000; =========\n";
A a13=a9*1000;
a13.print();
std::cout << "A a14=a10-a9 ; =========\n";
A a14=a10-a9 ;
a14.print();
std::cout << "A a15=a9 /a11; =========\n";
A a15=a9/a11 ;
a15.print();
std::cout << "A a16=a12-a9 ; =========\n";
A a16=a12-a9 ;
a16.print();
std::cout << "A a17=a13/a9 ; =========\n";
A a17=a13/a9 ;
a17.print();
std::cout << "-a11 ; =========\n";
(-a11).print();
// std::cout << "A a10=1000-a9; =========\n"; a10=1000-a9; a10.print();
// std::cout << "A a11=1000/a9; =========\n"; a11=1000/a9; a11.print();
// std::cout << "A a12=1000+a9; =========\n"; a12=1000+a9; a12.print();
// std::cout << "A a13=1000*a9; =========\n"; a13=1000*a9; a13.print();
NDimTable<float > Tf(T5);
Tf.print();
NDimTable<double> Td=Tf;
Td.print();
std::vector<std::size_t> dimF;
dimF.push_back(8);
NDimTable<std::complex<double> > in(dimF),out(dimF);
int i(0);
in.at(i++)=(10);
in.at(i++)=(20);
in.at(i++)=(1);
in.at(i++)=(-4);
in.at(i++)=(5);
in.at(i++)=(6);
in.at(i++)=(0);
in.at(i++)=(1);
in.print();
out.print();
out.becomesFFT(in);
in.print();
out.print();
in.becomesIFFT(out);
in.print();
out.print();
std::cout << "last printed tensor, out.maxReal() is " << out.maxReal() << "\n";
std::cout << "last printed tensor, out.minReal() is " << out.minReal() << "\n";
// debug output:
// FIXME, FIXME - add this to yade --check or test.
// FIXME, FIXME - this is important, because various FFT libraries divide by sqrt(N) or some other numbers.
//////////////////// that output was when I used rotateLeft(size/2-1)
// (10,-2.14306e-16), (20,4.71028e-16), (1,2.14306e-16), (-4,0), (5,-2.14306e-16), (6,-4.71028e-16), (1.57009e-16,2.14306e-16), (1,0),
// (-0.767767,3.44975), (-10.253,4.94975), (-2.76777,-6.44975), (13.7886,0), (-2.76777,6.44975), (-10.253,-4.94975), (-0.767767,-3.44975), (2.47487,0),
//(10,0), (20,0), (1,0), (-4,0), (5,0), (6,0), (0,0), (1,0),
//(39,0), (18.435,-7.36396), (14,-29), (-8.43503,-5.36396), (-7,0), (-8.43503,5.36396), (14,29), (18.435,7.36396),
//(10,0) (20,0) (1,0) (-4,0) (5,0) (6,0) (0,0) (1,0)
//(39,0) (18.435,-7.36396) (14,-29) (-8.43503,-5.36396) (-7,0) (-8.43503,5.36396) (14,29) (18.435,7.36396)
//
};