test_invoke.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://kylelutz.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestInvoke
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/function.hpp>
  13. #include <boost/compute/lambda.hpp>
  14. #include <boost/compute/functional/integer.hpp>
  15. #include <boost/compute/functional/math.hpp>
  16. #include <boost/compute/utility/invoke.hpp>
  17. #include "context_setup.hpp"
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(invoke_builtin)
  20. {
  21. BOOST_CHECK_EQUAL(compute::invoke(compute::abs<int>(), queue, -3), 3);
  22. BOOST_CHECK_CLOSE(compute::invoke(compute::pow<float>(), queue, 2.f, 8.f), 256.f, 1e-4);
  23. }
  24. BOOST_AUTO_TEST_CASE(invoke_function)
  25. {
  26. BOOST_COMPUTE_FUNCTION(int, plus_two, (int x),
  27. {
  28. return x + 2;
  29. });
  30. BOOST_CHECK_EQUAL(compute::invoke(plus_two, queue, 2), 4);
  31. BOOST_COMPUTE_FUNCTION(float, get_max, (float x, float y),
  32. {
  33. if(x > y)
  34. return x;
  35. else
  36. return y;
  37. });
  38. BOOST_CHECK_EQUAL(compute::invoke(get_max, queue, 10.f, 20.f), 20.f);
  39. }
  40. BOOST_AUTO_TEST_CASE(invoke_lambda)
  41. {
  42. using boost::compute::lambda::_1;
  43. using boost::compute::lambda::_2;
  44. BOOST_CHECK_EQUAL(compute::invoke(_1 / 2, queue, 4), 2);
  45. BOOST_CHECK_EQUAL(compute::invoke(_1 * _2 + 1, queue, 3, 3), 10);
  46. }
  47. BOOST_AUTO_TEST_SUITE_END()