test_is_sorted.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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 TestIsSorted
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/iota.hpp>
  14. #include <boost/compute/algorithm/is_sorted.hpp>
  15. #include <boost/compute/algorithm/sort.hpp>
  16. #include <boost/compute/algorithm/reverse.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "context_setup.hpp"
  19. namespace compute = boost::compute;
  20. BOOST_AUTO_TEST_CASE(is_sorted_int)
  21. {
  22. compute::vector<int> vec(context);
  23. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  24. vec.push_back(1, queue);
  25. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  26. vec.push_back(2, queue);
  27. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  28. vec.push_back(0, queue);
  29. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue) == false);
  30. vec.push_back(-2, queue);
  31. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue) == false);
  32. compute::sort(vec.begin(), vec.end(), queue);
  33. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  34. }
  35. BOOST_AUTO_TEST_CASE(is_sorted_ones)
  36. {
  37. compute::vector<int> vec(2048, context);
  38. compute::fill(vec.begin(), vec.end(), 1, queue);
  39. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  40. }
  41. BOOST_AUTO_TEST_CASE(is_sorted_iota)
  42. {
  43. // create vector with values from 1..1000
  44. compute::vector<int> vec(1000, context);
  45. compute::iota(vec.begin(), vec.end(), 1, queue);
  46. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  47. // reverse the range
  48. compute::reverse(vec.begin(), vec.end(), queue);
  49. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue) == false);
  50. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compute::greater<int>(), queue));
  51. // reverse it back
  52. compute::reverse(vec.begin(), vec.end(), queue);
  53. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), queue));
  54. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compute::greater<int>(), queue) == false);
  55. }
  56. BOOST_AUTO_TEST_SUITE_END()