test_any_all_none_of.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 TestAnyAllNoneOf
  11. #include <boost/test/unit_test.hpp>
  12. #include <limits>
  13. #include <cmath>
  14. #include <boost/compute/lambda.hpp>
  15. #include <boost/compute/algorithm/all_of.hpp>
  16. #include <boost/compute/algorithm/any_of.hpp>
  17. #include <boost/compute/algorithm/none_of.hpp>
  18. #include <boost/compute/container/vector.hpp>
  19. #include "context_setup.hpp"
  20. namespace bc = boost::compute;
  21. namespace compute = boost::compute;
  22. BOOST_AUTO_TEST_CASE(any_all_none_of)
  23. {
  24. int data[] = { 1, 2, 3, 4, 5, 6 };
  25. bc::vector<int> v(data, data + 6, queue);
  26. using ::boost::compute::_1;
  27. BOOST_CHECK(bc::any_of(v.begin(), v.end(), _1 == 6) == true);
  28. BOOST_CHECK(bc::any_of(v.begin(), v.end(), _1 == 9) == false);
  29. BOOST_CHECK(bc::none_of(v.begin(), v.end(), _1 == 6) == false);
  30. BOOST_CHECK(bc::none_of(v.begin(), v.end(), _1 == 9) == true);
  31. BOOST_CHECK(bc::all_of(v.begin(), v.end(), _1 == 6) == false);
  32. BOOST_CHECK(bc::all_of(v.begin(), v.end(), _1 < 9) == true);
  33. BOOST_CHECK(bc::all_of(v.begin(), v.end(), _1 < 6) == false);
  34. BOOST_CHECK(bc::all_of(v.begin(), v.end(), _1 >= 1) == true);
  35. }
  36. BOOST_AUTO_TEST_CASE(any_nan_inf)
  37. {
  38. using ::boost::compute::_1;
  39. using ::boost::compute::lambda::isinf;
  40. using ::boost::compute::lambda::isnan;
  41. using ::boost::compute::lambda::isfinite;
  42. float nan = std::sqrt(-1.f);
  43. float inf = std::numeric_limits<float>::infinity();
  44. float data[] = { 1.2f, 2.3f, nan, nan, 3.4f, inf, 4.5f, inf };
  45. compute::vector<float> vector(data, data + 8, queue);
  46. BOOST_CHECK(compute::any_of(vector.begin(), vector.end(),
  47. isinf(_1) || isnan(_1), queue) == true);
  48. BOOST_CHECK(compute::any_of(vector.begin(), vector.end(),
  49. isfinite(_1), queue) == true);
  50. BOOST_CHECK(compute::all_of(vector.begin(), vector.end(),
  51. isfinite(_1), queue) == false);
  52. BOOST_CHECK(compute::all_of(vector.begin(), vector.begin() + 2,
  53. isfinite(_1), queue) == true);
  54. BOOST_CHECK(compute::all_of(vector.begin() + 2, vector.begin() + 4,
  55. isnan(_1), queue) == true);
  56. BOOST_CHECK(compute::none_of(vector.begin(), vector.end(),
  57. isinf(_1), queue) == false);
  58. BOOST_CHECK(compute::none_of(vector.begin(), vector.begin() + 4,
  59. isinf(_1), queue) == true);
  60. }
  61. BOOST_AUTO_TEST_CASE(any_of_doctest)
  62. {
  63. using boost::compute::lambda::_1;
  64. int data[] = { 1, 2, 3, 4 };
  65. boost::compute::vector<int> v(data, data + 4, queue);
  66. bool result =
  67. //! [any_of]
  68. boost::compute::any_of(v.begin(), v.end(), _1 < 0, queue);
  69. //! [any_of]
  70. BOOST_CHECK(result == false);
  71. }
  72. BOOST_AUTO_TEST_SUITE_END()