test_generate.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 TestGenerate
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/function.hpp>
  14. #include <boost/compute/algorithm/generate.hpp>
  15. #include <boost/compute/algorithm/generate_n.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/types/pair.hpp>
  18. #include "quirks.hpp"
  19. #include "check_macros.hpp"
  20. #include "context_setup.hpp"
  21. namespace bc = boost::compute;
  22. BOOST_AUTO_TEST_CASE(generate4)
  23. {
  24. bc::vector<int> vector(4, context);
  25. bc::fill(vector.begin(), vector.end(), 2, queue);
  26. CHECK_RANGE_EQUAL(int, 4, vector, (2, 2, 2, 2));
  27. BOOST_COMPUTE_FUNCTION(int, ret4, (void),
  28. {
  29. return 4;
  30. });
  31. bc::generate(vector.begin(), vector.end(), ret4, queue);
  32. CHECK_RANGE_EQUAL(int, 4, vector, (4, 4, 4, 4));
  33. }
  34. BOOST_AUTO_TEST_CASE(generate_pair)
  35. {
  36. if(bug_in_struct_assignment(device)){
  37. std::cerr << "skipping generate_pair test" << std::endl;
  38. return;
  39. }
  40. // in order to use std::pair<T1, T2> with BOOST_COMPUTE_FUNCTION() macro we
  41. // need a typedef. otherwise the commas in the type declaration screw it up.
  42. typedef std::pair<int, float> pair_type;
  43. bc::vector<pair_type> vector(3, context);
  44. BOOST_COMPUTE_FUNCTION(pair_type, generate_pair, (void),
  45. {
  46. return boost_make_pair(int, 2, float, 3.14f);
  47. });
  48. bc::generate(vector.begin(), vector.end(), generate_pair, queue);
  49. // check results
  50. std::vector<pair_type> host_vector(3);
  51. bc::copy(vector.begin(), vector.end(), host_vector.begin(), queue);
  52. BOOST_CHECK(host_vector[0] == std::make_pair(2, 3.14f));
  53. BOOST_CHECK(host_vector[1] == std::make_pair(2, 3.14f));
  54. BOOST_CHECK(host_vector[2] == std::make_pair(2, 3.14f));
  55. }
  56. BOOST_AUTO_TEST_SUITE_END()