/[escript]/branches/diaplayground/cusplibrary/testing/array1d_view.cu
ViewVC logotype

Contents of /branches/diaplayground/cusplibrary/testing/array1d_view.cu

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (show annotations)
Tue May 20 04:33:15 2014 UTC (5 years, 4 months ago) by caltinay
File size: 12663 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

1 #include <unittest/unittest.h>
2
3 #include <cusp/array1d.h>
4
5 #include <vector>
6
7 #include <thrust/host_vector.h>
8 #include <thrust/device_vector.h>
9 #include <thrust/iterator/counting_iterator.h>
10 #include <thrust/iterator/zip_iterator.h>
11
12 template <typename MemorySpace>
13 void TestArray1dView(void)
14 {
15 typedef typename cusp::array1d<int, MemorySpace> Array;
16 typedef typename Array::iterator Iterator;
17 typedef typename Array::const_iterator ConstIterator;
18 typedef typename std::vector<int> Vector;
19 typedef typename thrust::host_vector<int> HostVector;
20 typedef typename thrust::device_vector<int> DeviceVector;
21
22 // view to container
23 {
24 typedef cusp::array1d_view<Iterator> View;
25
26 Array A(4);
27 A[0] = 10; A[1] = 20; A[2] = 30; A[3] = 40;
28
29 View V(A.begin(), A.end());
30
31 ASSERT_EQUAL(V.size(), 4);
32 ASSERT_EQUAL(V.capacity(), 4);
33 ASSERT_EQUAL(V[0], 10);
34 ASSERT_EQUAL(V[1], 20);
35 ASSERT_EQUAL(V[2], 30);
36 ASSERT_EQUAL(V[3], 40);
37 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
38 ASSERT_EQUAL_QUIET(V.end(), A.end());
39
40 V[1] = 17;
41
42 ASSERT_EQUAL(V[1], 17);
43 ASSERT_EQUAL(A[1], 17);
44
45 View W(A.begin(), A.end());
46 ASSERT_EQUAL(W.size(), 4);
47 ASSERT_EQUAL(W.capacity(), 4);
48 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
49 ASSERT_EQUAL_QUIET(W.end(), A.end());
50 }
51
52 // view to const container
53 {
54 typedef cusp::array1d_view<ConstIterator> View;
55
56 const Array A(4, 10);
57
58 View V(A.begin(), A.end());
59
60 ASSERT_EQUAL(V.size(), 4);
61 ASSERT_EQUAL(V.capacity(), 4);
62 ASSERT_EQUAL(V[0], 10);
63 ASSERT_EQUAL(V[1], 10);
64 ASSERT_EQUAL(V[2], 10);
65 ASSERT_EQUAL(V[3], 10);
66 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
67 ASSERT_EQUAL_QUIET(V.end(), A.end());
68
69 View W(A.begin(), A.end());
70 ASSERT_EQUAL(W.size(), 4);
71 ASSERT_EQUAL(W.capacity(), 4);
72 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
73 ASSERT_EQUAL_QUIET(W.end(), A.end());
74 }
75
76 // const view to container
77 {
78 typedef const cusp::array1d_view<Iterator> View;
79
80 Array A(4);
81 A[0] = 10; A[1] = 20; A[2] = 30; A[3] = 40;
82
83 View V(A);
84
85 ASSERT_EQUAL(V.size(), 4);
86 ASSERT_EQUAL(V.capacity(), 4);
87 ASSERT_EQUAL(V[0], 10);
88 ASSERT_EQUAL(V[1], 20);
89 ASSERT_EQUAL(V[2], 30);
90 ASSERT_EQUAL(V[3], 40);
91 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
92 ASSERT_EQUAL_QUIET(V.end(), A.end());
93
94 V[1] = 17;
95
96 ASSERT_EQUAL(V[1], 17);
97 ASSERT_EQUAL(A[1], 17);
98
99 View W(A.begin(), A.end());
100 ASSERT_EQUAL(W.size(), 4);
101 ASSERT_EQUAL(W.capacity(), 4);
102 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
103 ASSERT_EQUAL_QUIET(W.end(), A.end());
104 }
105
106 // const view to const container
107 {
108 typedef const cusp::array1d_view<ConstIterator> View;
109
110 const Array A(4, 10);
111
112 View V(A);
113
114 ASSERT_EQUAL(V.size(), 4);
115 ASSERT_EQUAL(V.capacity(), 4);
116 ASSERT_EQUAL(V[0], 10);
117 ASSERT_EQUAL(V[1], 10);
118 ASSERT_EQUAL(V[2], 10);
119 ASSERT_EQUAL(V[3], 10);
120 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
121 ASSERT_EQUAL_QUIET(V.end(), A.end());
122
123 View W(A.begin(), A.end());
124 ASSERT_EQUAL(W.size(), 4);
125 ASSERT_EQUAL(W.capacity(), 4);
126 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
127 ASSERT_EQUAL_QUIET(W.end(), A.end());
128 }
129
130
131 // view to std::vector
132 {
133 typedef cusp::array1d_view<Vector::iterator> View;
134
135 Vector A(4);
136
137 View V(A);
138
139 ASSERT_EQUAL(V.size(), 4);
140 ASSERT_EQUAL(V.capacity(), 4);
141 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
142 ASSERT_EQUAL_QUIET(V.end(), A.end());
143
144 View W(A.begin(), A.end());
145
146 ASSERT_EQUAL(W.size(), 4);
147 ASSERT_EQUAL(W.capacity(), 4);
148 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
149 ASSERT_EQUAL_QUIET(W.end(), A.end());
150
151 View U = View(A);
152
153 ASSERT_EQUAL(U.size(), 4);
154 ASSERT_EQUAL(U.capacity(), 4);
155 ASSERT_EQUAL_QUIET(U.begin(), A.begin());
156 ASSERT_EQUAL_QUIET(U.end(), A.end());
157 }
158
159 // view to thrust::host_vector
160 {
161 typedef cusp::array1d_view<HostVector::iterator> View;
162
163 HostVector A(4);
164
165 View V(A);
166
167 ASSERT_EQUAL(V.size(), 4);
168 ASSERT_EQUAL(V.capacity(), 4);
169 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
170 ASSERT_EQUAL_QUIET(V.end(), A.end());
171
172 View W(A.begin(), A.end());
173
174 ASSERT_EQUAL(W.size(), 4);
175 ASSERT_EQUAL(W.capacity(), 4);
176 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
177 ASSERT_EQUAL_QUIET(W.end(), A.end());
178
179 View U = View(A);
180
181 ASSERT_EQUAL(U.size(), 4);
182 ASSERT_EQUAL(U.capacity(), 4);
183 ASSERT_EQUAL_QUIET(U.begin(), A.begin());
184 ASSERT_EQUAL_QUIET(U.end(), A.end());
185 }
186
187 // view to thrust::device_vector
188 {
189 typedef cusp::array1d_view<DeviceVector::iterator> View;
190
191 DeviceVector A(4);
192
193 View V(A);
194
195 ASSERT_EQUAL(V.size(), 4);
196 ASSERT_EQUAL(V.capacity(), 4);
197 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
198 ASSERT_EQUAL_QUIET(V.end(), A.end());
199
200 View W(A.begin(), A.end());
201
202 ASSERT_EQUAL(W.size(), 4);
203 ASSERT_EQUAL(W.capacity(), 4);
204 ASSERT_EQUAL_QUIET(W.begin(), A.begin());
205 ASSERT_EQUAL_QUIET(W.end(), A.end());
206
207 View U = View(A);
208
209 ASSERT_EQUAL(U.size(), 4);
210 ASSERT_EQUAL(U.capacity(), 4);
211 ASSERT_EQUAL_QUIET(U.begin(), A.begin());
212 ASSERT_EQUAL_QUIET(U.end(), A.end());
213 }
214
215 }
216 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dView);
217
218
219 template <typename MemorySpace>
220 void TestMakeArray1dView(void)
221 {
222 typedef typename cusp::array1d<int, MemorySpace> Array;
223 typedef typename Array::iterator Iterator;
224 typedef cusp::array1d_view<Iterator> View;
225 typedef const Array ConstArray;
226 typedef typename Array::const_iterator ConstIterator;
227 typedef cusp::array1d_view<ConstIterator> ConstView;
228
229 // construct from iterators
230 {
231 Array A(4);
232
233 View V = cusp::make_array1d_view(A.begin(), A.end());
234
235 ASSERT_EQUAL(V.size(), 4);
236 ASSERT_EQUAL(V.capacity(), 4);
237 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
238 ASSERT_EQUAL_QUIET(V.end(), A.end());
239
240 // check that view::iterator is mutable
241 V[1] = 17;
242
243 ASSERT_EQUAL(V[1], 17);
244 ASSERT_EQUAL(A[1], 17);
245 }
246
247 // construct from container
248 {
249 Array A(4);
250
251 View V = cusp::make_array1d_view(A);
252
253 ASSERT_EQUAL(V.size(), 4);
254 ASSERT_EQUAL(V.capacity(), 4);
255 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
256 ASSERT_EQUAL_QUIET(V.end(), A.end());
257
258 // check that view::iterator is mutable
259 V[1] = 17;
260
261 ASSERT_EQUAL(V[1], 17);
262 ASSERT_EQUAL(A[1], 17);
263 }
264
265 // construct from const container
266 {
267 ConstArray A(4);
268
269 ConstView V = cusp::make_array1d_view(A);
270
271 ASSERT_EQUAL(V.size(), 4);
272 ASSERT_EQUAL(V.capacity(), 4);
273 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
274 ASSERT_EQUAL_QUIET(V.end(), A.end());
275 }
276
277 // construct from view
278 {
279 Array A(4);
280
281 View X = cusp::make_array1d_view(A);
282 View V = cusp::make_array1d_view(X);
283
284 ASSERT_EQUAL(V.size(), 4);
285 ASSERT_EQUAL(V.capacity(), 4);
286 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
287 ASSERT_EQUAL_QUIET(V.end(), A.end());
288
289 // check that view::iterator is mutable
290 V[1] = 17;
291
292 ASSERT_EQUAL(V[1], 17);
293 ASSERT_EQUAL(A[1], 17);
294 }
295 }
296 DECLARE_HOST_DEVICE_UNITTEST(TestMakeArray1dView);
297
298
299 template <typename MemorySpace>
300 void TestArray1dViewAssignment(void)
301 {
302 typedef typename cusp::array1d<int, MemorySpace> Array;
303 typedef typename Array::iterator Iterator;
304 typedef cusp::array1d_view<Iterator> View;
305
306 Array A(4);
307 Array B(8);
308
309 View V(A.begin(), A.end());
310
311 ASSERT_EQUAL(V.size(), 4);
312 ASSERT_EQUAL(V.capacity(), 4);
313 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
314 ASSERT_EQUAL_QUIET(V.end(), A.end());
315
316 V = View(A);
317
318 ASSERT_EQUAL(V.size(), 4);
319 ASSERT_EQUAL(V.capacity(), 4);
320 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
321 ASSERT_EQUAL_QUIET(V.end(), A.end());
322
323 V = View(B);
324
325 ASSERT_EQUAL(V.size(), 8);
326 ASSERT_EQUAL(V.capacity(), 8);
327 ASSERT_EQUAL_QUIET(V.begin(), B.begin());
328 ASSERT_EQUAL_QUIET(V.end(), B.end());
329
330 const View W = View(V);
331
332 ASSERT_EQUAL(W.size(), 8);
333 ASSERT_EQUAL(W.capacity(), 8);
334 ASSERT_EQUAL_QUIET(W.begin(), B.begin());
335 ASSERT_EQUAL_QUIET(W.end(), B.end());
336 }
337 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewAssignment);
338
339
340 template <typename MemorySpace>
341 void TestArray1dViewResize(void)
342 {
343 typedef typename cusp::array1d<int, MemorySpace> Array;
344 typedef typename Array::iterator Iterator;
345 typedef cusp::array1d_view<Iterator> View;
346
347 Array A(4);
348 A[0] = 10; A[1] = 20; A[2] = 30; A[3] = 40;
349
350 View V(A.begin(), A.end());
351
352 V.resize(3);
353
354 ASSERT_EQUAL(V.size(), 3);
355 ASSERT_EQUAL(V.capacity(), 4);
356 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
357 ASSERT_EQUAL_QUIET(V.end(), A.begin() + 3);
358
359 V.resize(2);
360
361 ASSERT_EQUAL(V.size(), 2);
362 ASSERT_EQUAL(V.capacity(), 4);
363 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
364 ASSERT_EQUAL_QUIET(V.end(), A.begin() + 2);
365
366 V.resize(4);
367
368 ASSERT_EQUAL(V.size(), 4);
369 ASSERT_EQUAL(V.capacity(), 4);
370 ASSERT_EQUAL_QUIET(V.begin(), A.begin());
371 ASSERT_EQUAL_QUIET(V.end(), A.begin() + 4);
372
373 ASSERT_THROWS(V.resize(5), cusp::not_implemented_exception);
374 }
375 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewResize);
376
377
378 template <typename MemorySpace>
379 void TestArray1dViewSize(void)
380 {
381 typedef typename cusp::array1d<int, MemorySpace> Array;
382 typedef typename Array::iterator Iterator;
383 typedef cusp::array1d_view<Iterator> View;
384
385 Array A(4);
386
387 View V(A.begin(), A.end());
388
389 ASSERT_EQUAL(V.size(), 4);
390
391 V.resize(2);
392
393 ASSERT_EQUAL(V.size(), 2);
394
395 View W = V;
396
397 ASSERT_EQUAL(W.size(), 2);
398
399 V = View(A);
400
401 ASSERT_EQUAL(V.size(), 4);
402 }
403 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewSize);
404
405
406 template <typename MemorySpace>
407 void TestArray1dViewCapacity(void)
408 {
409 typedef typename cusp::array1d<int, MemorySpace> Array;
410 typedef typename Array::iterator Iterator;
411 typedef cusp::array1d_view<Iterator> View;
412
413 Array A(4);
414
415 View V(A.begin(), A.end());
416
417 ASSERT_EQUAL(V.size(), 4);
418 ASSERT_EQUAL(V.capacity(), 4);
419
420 A.resize(2);
421
422 V = View(A);
423
424 ASSERT_EQUAL(V.size(), 2);
425 ASSERT_EQUAL(V.capacity(), 4);
426 }
427 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewCapacity);
428
429
430 template <typename MemorySpace>
431 void TestArray1dViewCountingIterator(void)
432 {
433 typedef thrust::counting_iterator<int> Iterator;
434
435 cusp::array1d_view<Iterator> V(Iterator(5), Iterator(9));
436
437 ASSERT_EQUAL(V.size(), 4);
438 ASSERT_EQUAL(V[0], 5);
439 ASSERT_EQUAL(V[3], 8);
440 }
441 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewCountingIterator);
442
443
444 template <typename MemorySpace>
445 void TestArray1dViewZipIterator(void)
446 {
447 cusp::array1d<int, MemorySpace> A(4);
448 cusp::array1d<int, MemorySpace> B(4);
449 A[0] = 10; A[1] = 20; A[2] = 30; A[3] = 40;
450 B[0] = 50; B[1] = 60; B[2] = 70; B[3] = 80;
451
452 typedef typename cusp::array1d<int, MemorySpace>::iterator Iterator;
453 typedef typename thrust::tuple<Iterator,Iterator> IteratorTuple;
454 typedef typename thrust::zip_iterator<IteratorTuple> ZipIterator;
455
456 ZipIterator begin = thrust::make_zip_iterator(thrust::make_tuple(A.begin(), B.begin()));
457
458 cusp::array1d_view<ZipIterator> V(begin, begin + 4);
459
460 ASSERT_EQUAL(V.size(), 4);
461 ASSERT_EQUAL_QUIET(V[0], thrust::make_tuple(10,50));
462 ASSERT_EQUAL_QUIET(V[3], thrust::make_tuple(40,80));
463 }
464 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewZipIterator);
465
466
467 template <typename MemorySpace>
468 void TestArray1dViewEquality(void)
469 {
470 typedef typename cusp::array1d<int, MemorySpace> Array;
471 typedef typename Array::iterator Iterator;
472 typedef cusp::array1d_view<Iterator> View;
473
474 Array A(2);
475 A[0] = 10;
476 A[1] = 20;
477
478 Array B(3);
479 B[0] = 10;
480 B[1] = 20;
481 B[2] = 30;
482
483 View V(A);
484 View W(B);
485
486 ASSERT_EQUAL_QUIET(A == V, true);
487 ASSERT_EQUAL_QUIET(V == A, true);
488 ASSERT_EQUAL_QUIET(V == V, true);
489 ASSERT_EQUAL_QUIET(A != V, false);
490 ASSERT_EQUAL_QUIET(V != A, false);
491 ASSERT_EQUAL_QUIET(V != V, false);
492
493 ASSERT_EQUAL_QUIET(V == B, false);
494 ASSERT_EQUAL_QUIET(B == V, false);
495 ASSERT_EQUAL_QUIET(V == W, false);
496 ASSERT_EQUAL_QUIET(W == V, false);
497 ASSERT_EQUAL_QUIET(V != B, true);
498 ASSERT_EQUAL_QUIET(B != V, true);
499 ASSERT_EQUAL_QUIET(V != W, true);
500 ASSERT_EQUAL_QUIET(W != V, true);
501
502 W.resize(2);
503
504 ASSERT_EQUAL_QUIET(V == W, true);
505 ASSERT_EQUAL_QUIET(V != W, false);
506 }
507 DECLARE_HOST_DEVICE_UNITTEST(TestArray1dViewEquality);
508

  ViewVC Help
Powered by ViewVC 1.1.26