File: test_tuple.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (141 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download | duplicates (18)
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
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestTuple
#include <boost/test/unit_test.hpp>

#include <iostream>

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>

#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/fill.hpp>
#include <boost/compute/algorithm/find.hpp>
#include <boost/compute/algorithm/transform.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/types/tuple.hpp>

#include "quirks.hpp"
#include "check_macros.hpp"
#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(vector_tuple_int_float)
{
    boost::compute::vector<boost::tuple<int, float> > vector(context);

    vector.push_back(boost::make_tuple(1, 2.1f), queue);
    vector.push_back(boost::make_tuple(2, 3.2f), queue);
    vector.push_back(boost::make_tuple(3, 4.3f), queue);
}

BOOST_AUTO_TEST_CASE(copy_vector_tuple)
{
    // create vector of tuples on device
    boost::compute::vector<boost::tuple<char, int, float> > input(context);
    input.push_back(boost::make_tuple('a', 1, 2.3f), queue);
    input.push_back(boost::make_tuple('c', 3, 4.5f), queue);
    input.push_back(boost::make_tuple('f', 6, 7.8f), queue);

    // copy on device
    boost::compute::vector<boost::tuple<char, int, float> > output(context);

    boost::compute::copy(
        input.begin(),
        input.end(),
        output.begin(),
        queue
    );

    // copy to host
    std::vector<boost::tuple<char, int, float> > host_output(3);

    boost::compute::copy(
        input.begin(),
        input.end(),
        host_output.begin(),
        queue
    );

    // check tuple data
    BOOST_CHECK_EQUAL(host_output[0], boost::make_tuple('a', 1, 2.3f));
    BOOST_CHECK_EQUAL(host_output[1], boost::make_tuple('c', 3, 4.5f));
    BOOST_CHECK_EQUAL(host_output[2], boost::make_tuple('f', 6, 7.8f));
}

BOOST_AUTO_TEST_CASE(extract_tuple_elements)
{
    compute::vector<boost::tuple<char, int, float> > vector(context);
    vector.push_back(boost::make_tuple('a', 1, 2.3f), queue);
    vector.push_back(boost::make_tuple('c', 3, 4.5f), queue);
    vector.push_back(boost::make_tuple('f', 6, 7.8f), queue);

    compute::vector<char> chars(3, context);
    compute::transform(
        vector.begin(), vector.end(), chars.begin(), compute::get<0>(), queue
    );
    CHECK_RANGE_EQUAL(char, 3, chars, ('a', 'c', 'f'));

    compute::vector<int> ints(3, context);
    compute::transform(
        vector.begin(), vector.end(), ints.begin(), compute::get<1>(), queue
    );
    CHECK_RANGE_EQUAL(int, 3, ints, (1, 3, 6));

    compute::vector<float> floats(3, context);
    compute::transform(
        vector.begin(), vector.end(), floats.begin(), compute::get<2>(), queue
    );
    CHECK_RANGE_EQUAL(float, 3, floats, (2.3f, 4.5f, 7.8f));
}

BOOST_AUTO_TEST_CASE(fill_tuple_vector)
{
    if(bug_in_struct_assignment(device)){
        std::cerr << "skipping fill_tuple_vector test" << std::endl;
        return;
    }

    compute::vector<boost::tuple<char, int, float> > vector(5, context);
    compute::fill(vector.begin(), vector.end(), boost::make_tuple('z', 4, 3.14f), queue);

    std::vector<boost::tuple<char, int, float> > host_output(5);
    compute::copy(vector.begin(), vector.end(), host_output.begin(), queue);
    BOOST_CHECK_EQUAL(host_output[0], boost::make_tuple('z', 4, 3.14f));
    BOOST_CHECK_EQUAL(host_output[1], boost::make_tuple('z', 4, 3.14f));
    BOOST_CHECK_EQUAL(host_output[2], boost::make_tuple('z', 4, 3.14f));
    BOOST_CHECK_EQUAL(host_output[3], boost::make_tuple('z', 4, 3.14f));
    BOOST_CHECK_EQUAL(host_output[4], boost::make_tuple('z', 4, 3.14f));
}

#ifndef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
BOOST_AUTO_TEST_CASE(variadic_tuple)
{
    BOOST_CHECK_EQUAL(
        (compute::type_name<boost::tuple<char, short, int, float> >()),
        "boost_tuple_char_short_int_float_t"
    );
}
#endif // BOOST_COMPUTE_NO_VARIADIC_TEMPLATES

#ifndef BOOST_COMPUTE_NO_STD_TUPLE
BOOST_AUTO_TEST_CASE(std_tuple)
{
    BOOST_CHECK_EQUAL(
        (compute::type_name<std::tuple<char, short, int, float>>()),
        "std_tuple_char_short_int_float_t"
    );
}
#endif // BOOST_COMPUTE_NO_STD_TUPLE

BOOST_AUTO_TEST_SUITE_END()