2
0
mirror of https://github.com/boostorg/compute.git synced 2026-01-30 07:42:41 +00:00
Files
compute/test/test_function.cpp
Kyle Lutz 4b67907023 Change BOOST_COMPUTE_FUNCTION() to use custom argument names
This changes the BOOST_COMPUTE_FUNCTION() macro (and the related
BOOST_COMPUTE_CLOSURE() macro) to use custom, user-provided argument
names instead of auto-generating them based on their index.

This is an API-breaking change. Users should now provide argument
names when using the BOOST_COMPUTE_FUNCTION() macro. The examples
and documentation have been updated to reflect the new API.
2014-04-20 19:13:48 -07:00

137 lines
4.2 KiB
C++

//---------------------------------------------------------------------------//
// 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://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#define BOOST_TEST_MODULE TestFunction
#include <boost/test/unit_test.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/accumulate.hpp>
#include <boost/compute/algorithm/transform.hpp>
#include <boost/compute/algorithm/sort.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/iterator/zip_iterator.hpp>
#include <boost/compute/types/pair.hpp>
#include "check_macros.hpp"
#include "context_setup.hpp"
namespace compute = boost::compute;
BOOST_AUTO_TEST_CASE(add_three)
{
BOOST_COMPUTE_FUNCTION(int, add_three, (int x),
{
return x + 3;
});
int data[] = { 1, 2, 3, 4 };
compute::vector<int> vector(data, data + 4, queue);
compute::transform(
vector.begin(), vector.end(), vector.begin(), add_three, queue
);
CHECK_RANGE_EQUAL(int, 4, vector, (4, 5, 6, 7));
}
BOOST_AUTO_TEST_CASE(sum_odd_values)
{
BOOST_COMPUTE_FUNCTION(int, add_odd_value, (int sum, int value),
{
if(value & 1){
return sum + value;
}
else {
return sum + 0;
}
});
int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
compute::vector<int> vector(data, data + 8, queue);
int result = compute::accumulate(
vector.begin(), vector.end(), 0, add_odd_value, queue
);
BOOST_CHECK_EQUAL(result, 16);
}
BOOST_AUTO_TEST_CASE(sort_pairs)
{
std::vector<std::pair<int, float> > data;
data.push_back(std::make_pair(1, 2.3f));
data.push_back(std::make_pair(0, 4.2f));
data.push_back(std::make_pair(2, 1.0f));
compute::vector<std::pair<int, float> > vector(data.size());
compute::copy(data.begin(), data.end(), vector.begin(), queue);
// sort by first component
BOOST_COMPUTE_FUNCTION(bool, compare_first, (std::pair<int, float> a, std::pair<int, float> b),
{
return a.first < b.first;
});
compute::sort(vector.begin(), vector.end(), compare_first, queue);
compute::copy(vector.begin(), vector.end(), data.begin(), queue);
BOOST_CHECK(data[0] == std::make_pair(0, 4.2f));
BOOST_CHECK(data[1] == std::make_pair(1, 2.3f));
BOOST_CHECK(data[2] == std::make_pair(2, 1.0f));
// sort by second component
BOOST_COMPUTE_FUNCTION(bool, compare_second, (std::pair<int, float> a, std::pair<int, float> b),
{
return a.second < b.second;
});
compute::sort(vector.begin(), vector.end(), compare_second, queue);
compute::copy(vector.begin(), vector.end(), data.begin(), queue);
BOOST_CHECK(data[0] == std::make_pair(2, 1.0f));
BOOST_CHECK(data[1] == std::make_pair(1, 2.3f));
BOOST_CHECK(data[2] == std::make_pair(0, 4.2f));
}
BOOST_AUTO_TEST_CASE(transform_zip_iterator)
{
float float_data[] = { 1.f, 2.f, 3.f, 4.f };
compute::vector<float> input_floats(float_data, float_data + 4, queue);
int int_data[] = { 2, 4, 6, 8 };
compute::vector<int> input_ints(int_data, int_data + 4, queue);
compute::vector<float> results(4, context);
BOOST_COMPUTE_FUNCTION(float, tuple_pown, (boost::tuple<float, int> x),
{
return pown(boost_tuple_get(x, 0), boost_tuple_get(x, 1));
});
compute::transform(
compute::make_zip_iterator(
boost::make_tuple(input_floats.begin(), input_ints.begin())
),
compute::make_zip_iterator(
boost::make_tuple(input_floats.end(), input_ints.end())
),
results.begin(),
tuple_pown,
queue
);
float results_data[4];
compute::copy(results.begin(), results.end(), results_data, queue);
BOOST_CHECK_CLOSE(results_data[0], 1.f, 1e-4);
BOOST_CHECK_CLOSE(results_data[1], 16.f, 1e-4);
BOOST_CHECK_CLOSE(results_data[2], 729.f, 1e-4);
BOOST_CHECK_CLOSE(results_data[3], 65536.f, 1e-4);
}
BOOST_AUTO_TEST_SUITE_END()