This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 758
/
vector_manipulation.cu
96 lines (82 loc) · 2.55 KB
/
vector_manipulation.cu
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
#include <unittest/unittest.h>
#include <thrust/device_malloc_allocator.h>
#include <vector>
template <class Vector>
void TestVectorManipulation(size_t n)
{
typedef typename Vector::iterator Iterator;
typedef typename Vector::value_type T;
thrust::host_vector<T> src = unittest::random_samples<T>(n);
ASSERT_EQUAL(src.size(), n);
// basic initialization
Vector test0(n);
Vector test1(n, (T) 3);
ASSERT_EQUAL(test0.size(), n);
ASSERT_EQUAL(test1.size(), n);
ASSERT_EQUAL((test1 == std::vector<T>(n, (T) 3)), true);
// initializing from other vector
std::vector<T> stl_vector(src.begin(), src.end());
Vector cpy0 = src;
Vector cpy1(stl_vector);
Vector cpy2(stl_vector.begin(), stl_vector.end());
ASSERT_EQUAL(cpy0, src);
ASSERT_EQUAL(cpy1, src);
ASSERT_EQUAL(cpy2, src);
// resizing
Vector vec1(src);
vec1.resize(n + 3);
ASSERT_EQUAL(vec1.size(), n + 3);
vec1.resize(n);
ASSERT_EQUAL(vec1.size(), n);
ASSERT_EQUAL(vec1, src);
vec1.resize(n + 20, (T) 11);
Vector tail(vec1.begin() + n, vec1.end());
ASSERT_EQUAL( (tail == std::vector<T>(20, (T) 11)), true);
// shrinking a vector should not invalidate iterators
Iterator first = vec1.begin();
vec1.resize(10);
ASSERT_EQUAL_QUIET(first, vec1.begin());
vec1.resize(0);
ASSERT_EQUAL(vec1.size(), 0);
ASSERT_EQUAL(vec1.empty(), true);
vec1.resize(10);
ASSERT_EQUAL(vec1.size(), 10);
vec1.clear();
ASSERT_EQUAL(vec1.size(), 0);
vec1.resize(5);
ASSERT_EQUAL(vec1.size(), 5);
// push_back
Vector vec2;
for(size_t i = 0; i < 10; ++i)
{
ASSERT_EQUAL(vec2.size(), i);
vec2.push_back( (T) i );
ASSERT_EQUAL(vec2.size(), i + 1);
for(size_t j = 0; j <= i; j++)
ASSERT_EQUAL(vec2[j], j);
ASSERT_EQUAL(vec2.back(), i);
}
// pop_back
for(size_t i = 10; i > 0; --i)
{
ASSERT_EQUAL(vec2.size(), i);
ASSERT_EQUAL(vec2.back(), i-1);
vec2.pop_back();
ASSERT_EQUAL(vec2.size(), i-1);
for(size_t j = 0; j < i; j++)
ASSERT_EQUAL(vec2[j], j);
}
//TODO test swap, erase(pos), erase(begin, end)
}
template <typename T>
void TestVectorManipulationHost(size_t n)
{
TestVectorManipulation< thrust::host_vector<T> >(n);
}
DECLARE_VARIABLE_UNITTEST(TestVectorManipulationHost);
template <typename T>
void TestVectorManipulationDevice(size_t n)
{
TestVectorManipulation< thrust::device_vector<T> >(n);
}
DECLARE_VARIABLE_UNITTEST(TestVectorManipulationDevice);