test_array.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 TestArray
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy.hpp>
  14. #include <boost/compute/container/array.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "check_macros.hpp"
  17. #include "context_setup.hpp"
  18. BOOST_AUTO_TEST_CASE(concept_check)
  19. {
  20. BOOST_CONCEPT_ASSERT((boost::Container<boost::compute::array<int, 3> >));
  21. // BOOST_CONCEPT_ASSERT((boost::SequenceConcept<boost::compute::array<int, 3> >));
  22. BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<boost::compute::array<int, 3>::iterator>));
  23. BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<boost::compute::array<int, 3>::const_iterator>));
  24. }
  25. BOOST_AUTO_TEST_CASE(size)
  26. {
  27. boost::compute::array<int, 0> empty_array(context);
  28. BOOST_CHECK_EQUAL(empty_array.size(), size_t(0));
  29. boost::compute::array<int, 10> array10(context);
  30. BOOST_CHECK_EQUAL(array10.size(), size_t(10));
  31. }
  32. BOOST_AUTO_TEST_CASE(at)
  33. {
  34. boost::compute::array<int, 3> array(context);
  35. array[0] = 3;
  36. array[1] = -2;
  37. array[2] = 5;
  38. BOOST_CHECK_EQUAL(array.at(0), 3);
  39. BOOST_CHECK_EQUAL(array.at(1), -2);
  40. BOOST_CHECK_EQUAL(array.at(2), 5);
  41. BOOST_CHECK_THROW(array.at(3), std::out_of_range);
  42. }
  43. BOOST_AUTO_TEST_CASE(copy_from_vector)
  44. {
  45. int data[] = { 3, 6, 9, 12 };
  46. boost::compute::vector<int> vector(data, data + 4, queue);
  47. boost::compute::array<int, 4> array(context);
  48. boost::compute::copy(vector.begin(), vector.end(), array.begin(), queue);
  49. CHECK_RANGE_EQUAL(int, 4, array, (3, 6, 9, 12));
  50. }
  51. BOOST_AUTO_TEST_CASE(fill)
  52. {
  53. boost::compute::array<int, 4> array(context);
  54. array.fill(0);
  55. CHECK_RANGE_EQUAL(int, 4, array, (0, 0, 0, 0));
  56. array.fill(17);
  57. CHECK_RANGE_EQUAL(int, 4, array, (17, 17, 17, 17));
  58. }
  59. BOOST_AUTO_TEST_CASE(swap)
  60. {
  61. int data[] = { 1, 2, 6, 9 };
  62. boost::compute::array<int, 4> a(context);
  63. boost::compute::copy(data, data + 4, a.begin(), queue);
  64. CHECK_RANGE_EQUAL(int, 4, a, (1, 2, 6, 9));
  65. boost::compute::array<int, 4> b(context);
  66. b.fill(3);
  67. CHECK_RANGE_EQUAL(int, 4, b, (3, 3, 3, 3));
  68. a.swap(b);
  69. CHECK_RANGE_EQUAL(int, 4, a, (3, 3, 3, 3));
  70. CHECK_RANGE_EQUAL(int, 4, b, (1, 2, 6, 9));
  71. }
  72. BOOST_AUTO_TEST_SUITE_END()