test_functional_as.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestFunctionalAs
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy_n.hpp>
  14. #include <boost/compute/algorithm/transform.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include <boost/compute/functional/as.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace compute = boost::compute;
  20. BOOST_AUTO_TEST_CASE(roundtrip_int_float)
  21. {
  22. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  23. compute::vector<int> input(8, context);
  24. compute::copy_n(data, 8, input.begin(), queue);
  25. // convert int -> float
  26. compute::vector<float> output(8, context);
  27. compute::transform(
  28. input.begin(),
  29. input.end(),
  30. output.begin(),
  31. compute::as<float>(),
  32. queue
  33. );
  34. // zero out input
  35. compute::fill(input.begin(), input.end(), 0, queue);
  36. // convert float -> int
  37. compute::transform(
  38. output.begin(),
  39. output.end(),
  40. input.begin(),
  41. compute::as<int>(),
  42. queue
  43. );
  44. // check values
  45. CHECK_RANGE_EQUAL(
  46. int, 8, input, (1, 2, 3, 4, 5, 6, 7, 8)
  47. );
  48. }
  49. BOOST_AUTO_TEST_SUITE_END()