1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#include "PTensor.h" |
8 |
|
|
|
9 |
|
|
using namespace std; |
10 |
|
|
|
11 |
|
|
///Test the Tensor with one dimension |
12 |
|
|
/** @param mode : allocation mode |
13 |
|
|
* @param nbElement : number of element of the tensor |
14 |
|
|
* @return true on success, false otherwise |
15 |
|
|
*/ |
16 |
|
|
template<typename T> |
17 |
|
132 |
bool testSizeTensor1d(AllocMode::AllocMode mode, size_t nbElement){ |
18 |
✓ |
132 |
PTensor<T> tensor(mode, nbElement); |
19 |
✓✓ |
12276 |
for(size_t i(0lu); i < nbElement; ++i){ |
20 |
|
12144 |
tensor.setValue(i, (T)(2lu*i + 1lu)); |
21 |
|
|
} |
22 |
✓ |
132 |
size_t sizeTensor = data_size(tensor); |
23 |
|
132 |
size_t expectedSize(8lu + 8lu + 4lu + nbElement*sizeof(T) + 8lu + 8lu); |
24 |
|
132 |
bool b(sizeTensor == expectedSize); |
25 |
|
|
|
26 |
✓✓✓✓ ✓ |
132 |
std::cout << "testSizeTensor1d : sizeTensor = " << sizeTensor << ", expectedSize = " << expectedSize << std::endl; |
27 |
✓✗ |
132 |
if(b){ |
28 |
✓✓ |
132 |
std::cout << "testSizeTensor1d : OK" << std::endl; |
29 |
|
|
}else{ |
30 |
|
|
std::cout << "testSizeTensor1d : WRONG" << std::endl; |
31 |
|
|
} |
32 |
|
264 |
return b; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
///Test the Tensor with one dimension |
36 |
|
|
/** @param nbElement : number of element of the tensor |
37 |
|
|
* @return true on success, false otherwise |
38 |
|
|
*/ |
39 |
|
|
template<typename T> |
40 |
|
44 |
bool testSizeModeTensor1d(size_t nbElement){ |
41 |
|
44 |
bool b(true); |
42 |
|
44 |
b &= testSizeTensor1d<T>(AllocMode::NONE, nbElement); |
43 |
|
44 |
b &= testSizeTensor1d<T>(AllocMode::ALIGNED, nbElement); |
44 |
|
44 |
b &= testSizeTensor1d<T>(AllocMode::PADDING, nbElement); |
45 |
|
44 |
return b; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
///Test the Tensor with one dimension |
49 |
|
|
/** @param nbElement : number of element of the tensor |
50 |
|
|
* @return true on success, false otherwise |
51 |
|
|
*/ |
52 |
|
2 |
bool testSizeTensor1(size_t nbElement){ |
53 |
|
2 |
bool b(true); |
54 |
|
2 |
b &= testSizeModeTensor1d<bool>(nbElement); |
55 |
|
2 |
b &= testSizeModeTensor1d<char>(nbElement); |
56 |
|
2 |
b &= testSizeModeTensor1d<short>(nbElement); |
57 |
|
2 |
b &= testSizeModeTensor1d<int>(nbElement); |
58 |
|
2 |
b &= testSizeModeTensor1d<long int>(nbElement); |
59 |
|
|
|
60 |
|
2 |
b &= testSizeModeTensor1d<unsigned char>(nbElement); |
61 |
|
2 |
b &= testSizeModeTensor1d<unsigned short>(nbElement); |
62 |
|
2 |
b &= testSizeModeTensor1d<unsigned int>(nbElement); |
63 |
|
2 |
b &= testSizeModeTensor1d<long unsigned int>(nbElement); |
64 |
|
|
|
65 |
|
2 |
b &= testSizeModeTensor1d<float>(nbElement); |
66 |
|
2 |
b &= testSizeModeTensor1d<double>(nbElement); |
67 |
|
|
|
68 |
|
2 |
return b; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
///Test the Tensor with one dimension |
73 |
|
|
/** @param mode : allocation mode |
74 |
|
|
* @param nbRow : number of rows of the tensor |
75 |
|
|
* @param nbCol : number of columns of the tensor |
76 |
|
|
* @return true on success, false otherwise |
77 |
|
|
*/ |
78 |
|
|
template<typename T> |
79 |
|
264 |
bool testSizeTensor2d(AllocMode::AllocMode mode, size_t nbRow, size_t nbCol){ |
80 |
✓ |
264 |
PTensor<T> tensor(mode, nbRow, nbCol); |
81 |
✓✓ |
23298 |
for(size_t i(0lu); i < nbRow; ++i){ |
82 |
✓✓ |
975018 |
for(size_t j(0lu); j < nbCol; ++j){ |
83 |
|
951984 |
tensor.setValue(i, j, (T)(2lu*(i*nbCol + j) + 1lu)); |
84 |
|
|
} |
85 |
|
|
} |
86 |
✓ |
264 |
size_t sizeTensor = data_size(tensor); |
87 |
|
264 |
size_t expectedSize(8lu + 2lu*8lu + 4lu + nbRow*nbCol*sizeof(T) + 8lu + 8lu); |
88 |
|
264 |
bool b(sizeTensor == expectedSize); |
89 |
|
|
|
90 |
✓✓✓✓ ✓ |
264 |
std::cout << "testSizeTensor2d : sizeTensor = " << sizeTensor << ", expectedSize = " << expectedSize << std::endl; |
91 |
✓✗ |
264 |
if(b){ |
92 |
✓✓ |
264 |
std::cout << "testSizeTensor2d : OK" << std::endl; |
93 |
|
|
}else{ |
94 |
|
|
std::cout << "testSizeTensor2d : WRONG" << std::endl; |
95 |
|
|
} |
96 |
|
528 |
return b; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
///Test the Tensor with one dimension |
100 |
|
|
/** @param nbRow : number of rows of the tensor |
101 |
|
|
* @param nbCol : number of columns of the tensor |
102 |
|
|
* @return true on success, false otherwise |
103 |
|
|
*/ |
104 |
|
|
template<typename T> |
105 |
|
88 |
bool testSizeModeTensor2d(size_t nbRow, size_t nbCol){ |
106 |
|
88 |
bool b(true); |
107 |
|
88 |
b &= testSizeTensor2d<T>(AllocMode::NONE, nbRow, nbCol); |
108 |
|
88 |
b &= testSizeTensor2d<T>(AllocMode::ALIGNED, nbRow, nbCol); |
109 |
|
88 |
b &= testSizeTensor2d<T>(AllocMode::PADDING, nbRow, nbCol); |
110 |
|
88 |
return b; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
///Test the Tensor with one dimension |
114 |
|
|
/** @param nbRow : number of rows of the tensor |
115 |
|
|
* @param nbCol : number of columns of the tensor |
116 |
|
|
* @return true on success, false otherwise |
117 |
|
|
*/ |
118 |
|
4 |
bool testSizeTensor2(size_t nbRow, size_t nbCol){ |
119 |
|
4 |
bool b(true); |
120 |
|
4 |
b &= testSizeModeTensor2d<bool>(nbRow, nbCol); |
121 |
|
4 |
b &= testSizeModeTensor2d<char>(nbRow, nbCol); |
122 |
|
4 |
b &= testSizeModeTensor2d<short>(nbRow, nbCol); |
123 |
|
4 |
b &= testSizeModeTensor2d<int>(nbRow, nbCol); |
124 |
|
4 |
b &= testSizeModeTensor2d<long int>(nbRow, nbCol); |
125 |
|
|
|
126 |
|
4 |
b &= testSizeModeTensor2d<unsigned char>(nbRow, nbCol); |
127 |
|
4 |
b &= testSizeModeTensor2d<unsigned short>(nbRow, nbCol); |
128 |
|
4 |
b &= testSizeModeTensor2d<unsigned int>(nbRow, nbCol); |
129 |
|
4 |
b &= testSizeModeTensor2d<long unsigned int>(nbRow, nbCol); |
130 |
|
|
|
131 |
|
4 |
b &= testSizeModeTensor2d<float>(nbRow, nbCol); |
132 |
|
4 |
b &= testSizeModeTensor2d<double>(nbRow, nbCol); |
133 |
|
|
|
134 |
|
4 |
return b; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
///Test the Tensor with one dimension |
138 |
|
|
/** @param mode : allocation mode |
139 |
|
|
* @param nbSlice : number of slices of the tensor |
140 |
|
|
* @param nbRow : number of rows of the tensor |
141 |
|
|
* @param nbCol : number of columns of the tensor |
142 |
|
|
* @return true on success, false otherwise |
143 |
|
|
*/ |
144 |
|
|
template<typename T> |
145 |
|
264 |
bool testSizeTensor3d(AllocMode::AllocMode mode, size_t nbSlice, size_t nbRow, size_t nbCol){ |
146 |
✓ |
264 |
PTensor<T> tensor(mode, nbSlice, nbRow, nbCol); |
147 |
✓✓ |
100254 |
for(size_t i(0lu); i < nbSlice*nbRow; ++i){ |
148 |
✓✓ |
4126782 |
for(size_t j(0lu); j < nbCol; ++j){ |
149 |
|
4026792 |
tensor.setValue(i, j, (T)(2lu*(i*nbCol + j) + 1lu)); |
150 |
|
|
} |
151 |
|
|
} |
152 |
✓ |
264 |
size_t sizeTensor = data_size(tensor); |
153 |
|
264 |
size_t expectedSize(8lu + 3lu*8lu + 4lu + nbSlice*nbRow*nbCol*sizeof(T) + 8lu + 8lu); |
154 |
|
264 |
bool b(sizeTensor == expectedSize); |
155 |
|
|
|
156 |
✓✓✓✓ ✓ |
264 |
std::cout << "testSizeTensor3d : sizeTensor = " << sizeTensor << ", expectedSize = " << expectedSize << std::endl; |
157 |
✓✗ |
264 |
if(b){ |
158 |
✓✓ |
264 |
std::cout << "testSizeTensor3d : OK" << std::endl; |
159 |
|
|
}else{ |
160 |
|
|
std::cout << "testSizeTensor3d : WRONG" << std::endl; |
161 |
|
|
} |
162 |
|
528 |
return b; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
///Test the Tensor with one dimension |
166 |
|
|
/** @param nbSlice : number of slices of the tensor |
167 |
|
|
* @param nbRow : number of rows of the tensor |
168 |
|
|
* @param nbCol : number of columns of the tensor |
169 |
|
|
* @return true on success, false otherwise |
170 |
|
|
*/ |
171 |
|
|
template<typename T> |
172 |
|
88 |
bool testSizeModeTensor3d(size_t nbSlice, size_t nbRow, size_t nbCol){ |
173 |
|
88 |
bool b(true); |
174 |
|
88 |
b &= testSizeTensor3d<T>(AllocMode::NONE, nbSlice, nbRow, nbCol); |
175 |
|
88 |
b &= testSizeTensor3d<T>(AllocMode::ALIGNED, nbSlice, nbRow, nbCol); |
176 |
|
88 |
b &= testSizeTensor3d<T>(AllocMode::PADDING, nbSlice, nbRow, nbCol); |
177 |
|
88 |
return b; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
///Test the Tensor with one dimension |
181 |
|
|
/** @param nbSlice : number of slices of the tensor |
182 |
|
|
* @param nbRow : number of rows of the tensor |
183 |
|
|
* @param nbCol : number of columns of the tensor |
184 |
|
|
* @return true on success, false otherwise |
185 |
|
|
*/ |
186 |
|
4 |
bool testSizeTensor3(size_t nbSlice, size_t nbRow, size_t nbCol){ |
187 |
|
4 |
bool b(true); |
188 |
|
4 |
b &= testSizeModeTensor3d<bool>(nbSlice, nbRow, nbCol); |
189 |
|
4 |
b &= testSizeModeTensor3d<char>(nbSlice, nbRow, nbCol); |
190 |
|
4 |
b &= testSizeModeTensor3d<short>(nbSlice, nbRow, nbCol); |
191 |
|
4 |
b &= testSizeModeTensor3d<int>(nbSlice, nbRow, nbCol); |
192 |
|
4 |
b &= testSizeModeTensor3d<long int>(nbSlice, nbRow, nbCol); |
193 |
|
|
|
194 |
|
4 |
b &= testSizeModeTensor3d<unsigned char>(nbSlice, nbRow, nbCol); |
195 |
|
4 |
b &= testSizeModeTensor3d<unsigned short>(nbSlice, nbRow, nbCol); |
196 |
|
4 |
b &= testSizeModeTensor3d<unsigned int>(nbSlice, nbRow, nbCol); |
197 |
|
4 |
b &= testSizeModeTensor3d<long unsigned int>(nbSlice, nbRow, nbCol); |
198 |
|
|
|
199 |
|
4 |
b &= testSizeModeTensor3d<float>(nbSlice, nbRow, nbCol); |
200 |
|
4 |
b &= testSizeModeTensor3d<double>(nbSlice, nbRow, nbCol); |
201 |
|
|
|
202 |
|
4 |
return b; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
///Test the PTensor |
206 |
|
|
void testPTensor(){ |
207 |
|
|
PTensor<float> tensor; |
208 |
|
|
tensor.resize(AllocMode::NONE, 4lu, 5lu); |
209 |
|
|
tensor.fill(1.0f); |
210 |
|
|
cout << tensor << endl; |
211 |
|
|
|
212 |
|
|
PTensor<float> alignedTensor; |
213 |
|
|
alignedTensor.resize(AllocMode::ALIGNED, 4lu, 5lu); |
214 |
|
|
alignedTensor.fill(1.0f); |
215 |
|
|
cout << alignedTensor << endl; |
216 |
|
|
|
217 |
|
|
PTensor<float> paddedTensor; |
218 |
|
|
paddedTensor.resize(AllocMode::PADDING, 4lu, 5lu); |
219 |
|
|
paddedTensor.fill(1.0f); |
220 |
|
|
paddedTensor.setPaddingValue(42.f); |
221 |
|
|
cout << paddedTensor << endl; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
1 |
int main(int argc, char** argv){ |
225 |
|
1 |
bool b(testSizeTensor1(42lu)); |
226 |
|
1 |
b &= testSizeTensor1(142lu); |
227 |
|
1 |
b &= testSizeTensor2(42lu, 21lu); |
228 |
|
1 |
b &= testSizeTensor2(142lu, 21lu); |
229 |
|
1 |
b &= testSizeTensor2(23lu, 64lu); |
230 |
|
1 |
b &= testSizeTensor2(142lu, 64lu); |
231 |
|
|
|
232 |
|
1 |
b &= testSizeTensor3(3lu, 42lu, 21lu); |
233 |
|
1 |
b &= testSizeTensor3(5lu, 142lu, 21lu); |
234 |
|
1 |
b &= testSizeTensor3(11lu, 23lu, 64lu); |
235 |
|
1 |
b &= testSizeTensor3(3lu, 142lu, 64lu); |
236 |
|
|
|
237 |
✓✗ |
1 |
if(b){return 0;} |
238 |
|
|
else{return -1;} |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
|