ddemidov / vexcl
1
#define BOOST_TEST_MODULE VectorCreate
2
#include <boost/test/unit_test.hpp>
3
#include <vexcl/vector.hpp>
4
#include <vexcl/function.hpp>
5
#include "context_setup.hpp"
6

7 6
BOOST_AUTO_TEST_CASE(empty)
8
{
9 6
    vex::vector<double> x;
10

11 6
    BOOST_CHECK(0U == x.size());
12 6
    BOOST_CHECK(x.end() - x.begin() == 0);
13
}
14

15 6
BOOST_AUTO_TEST_CASE(size)
16
{
17
    const size_t N = 1024;
18 6
    vex::vector<double> x(ctx, N);
19

20 6
    BOOST_CHECK(x.size() == N);
21 6
    BOOST_CHECK(x.end() == x.begin() + N);
22
}
23

24 6
BOOST_AUTO_TEST_CASE(std_vector)
25
{
26
    const size_t N = 1024;
27

28 6
    std::vector<double> x = random_vector<double>(N);
29 6
    vex::vector<double> X(ctx, x);
30

31 6
    BOOST_CHECK(X.size() == x.size());
32

33 6
    std::vector<double> y(N);
34 6
    copy(X, y);
35

36 6
    check_sample(x, y, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
37
}
38

39 6
BOOST_AUTO_TEST_CASE(host_pointer)
40
{
41
    const size_t N = 1024;
42

43 6
    std::vector<double> x = random_vector<double>(N);
44 6
    vex::vector<double> X(ctx, N, x.data());
45

46 6
    BOOST_CHECK(X.size() == x.size());
47

48 6
    std::vector<double> y(N);
49 6
    copy(X, y);
50

51 6
    check_sample(x, y, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
52
}
53

54
#ifndef VEXCL_NO_COPY_CONSTRUCTORS
55 6
BOOST_AUTO_TEST_CASE(copy_constructor)
56
{
57
    const size_t N = 1024;
58

59 6
    vex::vector<double> x1;
60 6
    vex::vector<double> x2(x1);
61

62 6
    BOOST_CHECK(x1.size() == 0U);
63 6
    BOOST_CHECK(x1.size() == x2.size());
64

65 6
    vex::vector<double> y1(ctx, random_vector<double>(N));
66 6
    vex::vector<double> y2(y1);
67

68 6
    BOOST_CHECK(y1.size() == N);
69 6
    BOOST_CHECK(y1.size() == y2.size());
70

71 6
    check_sample(y1, y2, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
72
}
73
#endif
74

75 6
BOOST_AUTO_TEST_CASE(move_constructor)
76
{
77
    const size_t N = 1024;
78

79 6
    vex::vector<double> x(ctx, N);
80 6
    x = 42;
81

82 6
    vex::vector<double> y = std::move(x);
83

84 6
    BOOST_CHECK(x.size() == 0U);
85 6
    BOOST_CHECK(y.size() == N);
86

87 6
    check_sample(y, [](size_t, double a) { BOOST_CHECK(a == 42); });
88
}
89

90 6
BOOST_AUTO_TEST_CASE(move_assign)
91
{
92
    const size_t N = 1024;
93 6
    std::vector<double> x = random_vector<double>(N);
94 6
    vex::vector<double> X(ctx, x);
95

96 6
    vex::vector<double> Y;
97 6
    Y = std::move(X);
98

99 6
    BOOST_CHECK(Y.size() == x.size());
100

101 6
    check_sample(Y, x, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
102
}
103

104 6
BOOST_AUTO_TEST_CASE(vector_swap)
105
{
106
    const size_t N = 1024;
107
    const size_t M = 512;
108

109 6
    vex::vector<double> x(ctx, N);
110 6
    vex::vector<double> y(ctx, M);
111

112 6
    x = 42;
113 6
    y = 67;
114

115 6
    swap(x, y);
116

117 6
    BOOST_CHECK(y.size() == N);
118 6
    BOOST_CHECK(x.size() == M);
119

120 6
    check_sample(x, [](size_t, double a) { BOOST_CHECK(a == 67); });
121 6
    check_sample(y, [](size_t, double a) { BOOST_CHECK(a == 42); });
122
}
123

124 6
BOOST_AUTO_TEST_CASE(vector_resize_to_std_vector)
125
{
126
    const size_t N = 1024;
127

128 6
    std::vector<double> x = random_vector<double>(N);
129 6
    vex::vector<double> X;
130

131 6
    X.resize(ctx, x);
132 6
    BOOST_CHECK(X.size() == x.size());
133

134 6
    check_sample(X, x, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
135
}
136

137 6
BOOST_AUTO_TEST_CASE(vector_resize_to_vex_vector)
138
{
139
    const size_t N = 1024;
140

141 6
    vex::vector<double> x(ctx, N);
142 6
    x = 42;
143

144 6
    vex::vector<double> y;
145 6
    y.resize(x);
146

147 6
    BOOST_CHECK(y.size() == x.size());
148

149 6
    check_sample(x, y, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
150
}
151

152 6
BOOST_AUTO_TEST_CASE(stl_container_of_vex_vector)
153
{
154
    typedef vex::device_vector<unsigned>::raw_type raw_mem;
155 6
    const size_t N = 1024;
156 6
    const size_t M = 16 + generator<size_t>::get() ;
157

158 6
    std::vector< vex::vector<unsigned> > x;
159

160 6
    std::vector< raw_mem > bufs;
161

162 6
    for(size_t i = 0; i < M; ++i) {
163 6
        x.push_back( vex::vector<unsigned>(ctx, random_vector<unsigned>(N)) );
164 6
        x.back() = i;
165 6
        bufs.push_back( x.back()(0).raw() );
166
    }
167

168 6
    for(size_t i = 0; i < M; ++i)
169 6
        x[i] -= i;
170

171 6
    for(size_t i = 0; i < M; ++i) {
172 6
        BOOST_CHECK_EQUAL(N, x[i].size());
173 6
        BOOST_CHECK_EQUAL(bufs[i], x[i](0).raw());
174
    }
175
}
176

177 6
BOOST_AUTO_TEST_CASE(initialize_with_expression)
178
{
179
    const size_t n = 1024;
180

181 6
    std::vector<double> x = random_vector<double>(n);
182

183 6
    vex::vector<double> X(ctx, x);
184 6
    vex::vector<double> Y = sin(X);
185

186 6
    check_sample(Y, [&](size_t idx, double v) { BOOST_CHECK_CLOSE(v, sin(x[idx]), 1e-8); });
187
}
188

189 6
BOOST_AUTO_TEST_CASE(some_devices_are_empty)
190
{
191 6
    vex::vector<double> x(ctx, 1);
192 6
    x = 0;
193 6
    BOOST_CHECK(x[0] == 0);
194
}
195

196 6
BOOST_AUTO_TEST_CASE(expression_properties)
197
{
198 6
    const size_t n = 16;
199 6
    vex::vector<int> x(ctx, n);
200

201 6
    std::vector<vex::backend::command_queue> q;
202 6
    size_t s;
203

204 6
    std::tie(q, s) = vex::expression_properties(2 * x);
205 6
    BOOST_CHECK_EQUAL(q.size(), ctx.size());
206 6
    BOOST_CHECK_EQUAL(s, n);
207

208 6
    std::tie(q, s) = vex::expression_properties(std::make_tuple(2 * x, x - 1));
209 6
    BOOST_CHECK_EQUAL(q.size(), ctx.size());
210 6
    BOOST_CHECK_EQUAL(s, n);
211
}
212

213 6
BOOST_AUTO_TEST_SUITE_END()

Read our documentation on viewing source code .

Loading