test_merge.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 TestMerge
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/types/pair.hpp>
  14. #include <boost/compute/algorithm/copy_n.hpp>
  15. #include <boost/compute/algorithm/merge.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/lambda.hpp>
  18. #include "check_macros.hpp"
  19. #include "context_setup.hpp"
  20. BOOST_AUTO_TEST_CASE(simple_merge_int)
  21. {
  22. int data1[] = { 1, 3, 5, 7 };
  23. int data2[] = { 2, 4, 6, 8 };
  24. boost::compute::vector<int> v1(4, context);
  25. boost::compute::vector<int> v2(4, context);
  26. boost::compute::vector<int> v3(8, context);
  27. boost::compute::copy_n(data1, 4, v1.begin(), queue);
  28. boost::compute::copy_n(data2, 4, v2.begin(), queue);
  29. boost::compute::fill(v3.begin(), v3.end(), 0, queue);
  30. // merge v1 with v2 into v3
  31. boost::compute::merge(
  32. v1.begin(), v1.end(),
  33. v2.begin(), v2.end(),
  34. v3.begin(),
  35. queue
  36. );
  37. CHECK_RANGE_EQUAL(int, 8, v3, (1, 2, 3, 4, 5, 6, 7, 8));
  38. // merge v2 with v1 into v3
  39. boost::compute::merge(
  40. v2.begin(), v2.end(),
  41. v1.begin(), v1.end(),
  42. v3.begin(),
  43. queue
  44. );
  45. CHECK_RANGE_EQUAL(int, 8, v3, (1, 2, 3, 4, 5, 6, 7, 8));
  46. // merge v1 with v1 into v3
  47. boost::compute::merge(
  48. v1.begin(), v1.end(),
  49. v1.begin(), v1.end(),
  50. v3.begin(),
  51. queue
  52. );
  53. CHECK_RANGE_EQUAL(int, 8, v3, (1, 1, 3, 3, 5, 5, 7, 7));
  54. // merge v2 with v2 into v3
  55. boost::compute::merge(
  56. v2.begin(), v2.end(),
  57. v2.begin(), v2.end(),
  58. v3.begin(),
  59. queue
  60. );
  61. CHECK_RANGE_EQUAL(int, 8, v3, (2, 2, 4, 4, 6, 6, 8, 8));
  62. // merge v1 with empty range into v3
  63. boost::compute::merge(
  64. v1.begin(), v1.end(),
  65. v1.begin(), v1.begin(),
  66. v3.begin(),
  67. queue
  68. );
  69. CHECK_RANGE_EQUAL(int, 4, v3, (1, 3, 5, 7));
  70. // merge v2 with empty range into v3
  71. boost::compute::merge(
  72. v1.begin(), v1.begin(),
  73. v2.begin(), v2.end(),
  74. v3.begin(),
  75. queue
  76. );
  77. CHECK_RANGE_EQUAL(int, 4, v3, (2, 4, 6, 8));
  78. }
  79. BOOST_AUTO_TEST_CASE(merge_pairs)
  80. {
  81. std::vector<std::pair<int, float> > data1;
  82. std::vector<std::pair<int, float> > data2;
  83. data1.push_back(std::make_pair(0, 0.1f));
  84. data1.push_back(std::make_pair(2, 2.1f));
  85. data1.push_back(std::make_pair(4, 4.1f));
  86. data1.push_back(std::make_pair(6, 6.1f));
  87. data2.push_back(std::make_pair(1, 1.1f));
  88. data2.push_back(std::make_pair(3, 3.1f));
  89. data2.push_back(std::make_pair(5, 5.1f));
  90. data2.push_back(std::make_pair(7, 7.1f));
  91. std::vector<std::pair<int, float> > data3(data1.size() + data2.size());
  92. std::fill(data3.begin(), data3.end(), std::make_pair(-1, -1.f));
  93. boost::compute::vector<std::pair<int, float> > v1(data1.size(), context);
  94. boost::compute::vector<std::pair<int, float> > v2(data2.size(), context);
  95. boost::compute::vector<std::pair<int, float> > v3(data3.size(), context);
  96. boost::compute::copy(data1.begin(), data1.end(), v1.begin(), queue);
  97. boost::compute::copy(data2.begin(), data2.end(), v2.begin(), queue);
  98. using ::boost::compute::lambda::_1;
  99. using ::boost::compute::lambda::_2;
  100. using ::boost::compute::lambda::get;
  101. boost::compute::merge(
  102. v1.begin(), v1.end(),
  103. v2.begin(), v2.end(),
  104. v3.begin(),
  105. get<0>(_1) < get<0>(_2),
  106. queue
  107. );
  108. boost::compute::copy(v3.begin(), v3.end(), data3.begin(), queue);
  109. BOOST_CHECK(v3[0] == std::make_pair(0, 0.1f));
  110. BOOST_CHECK(v3[1] == std::make_pair(1, 1.1f));
  111. BOOST_CHECK(v3[2] == std::make_pair(2, 2.1f));
  112. BOOST_CHECK(v3[3] == std::make_pair(3, 3.1f));
  113. BOOST_CHECK(v3[4] == std::make_pair(4, 4.1f));
  114. BOOST_CHECK(v3[5] == std::make_pair(5, 5.1f));
  115. BOOST_CHECK(v3[6] == std::make_pair(6, 6.1f));
  116. BOOST_CHECK(v3[7] == std::make_pair(7, 7.1f));
  117. }
  118. BOOST_AUTO_TEST_CASE(merge_floats)
  119. {
  120. float data1[] = { 1.1f, 2.2f, 3.3f, 4.4f,
  121. 5.5f, 6.6f, 7.7f, 8.8f };
  122. float data2[] = { 1.0f, 2.0f, 3.9f, 4.9f,
  123. 6.8f, 6.9f, 7.0f, 7.1f };
  124. boost::compute::vector<float> v1(8, context);
  125. boost::compute::vector<float> v2(8, context);
  126. boost::compute::vector<float> v3(v1.size() + v2.size(), context);
  127. boost::compute::copy_n(data1, 8, v1.begin(), queue);
  128. boost::compute::copy_n(data2, 8, v2.begin(), queue);
  129. boost::compute::fill(v3.begin(), v3.end(), 0.f, queue);
  130. boost::compute::merge(
  131. v1.begin(), v1.end(),
  132. v2.begin(), v2.end(),
  133. v3.begin(),
  134. queue
  135. );
  136. CHECK_RANGE_EQUAL(float, 16, v3,
  137. (1.0f, 1.1f, 2.0f, 2.2f, 3.3f, 3.9f, 4.4f, 4.9f,
  138. 5.5f, 6.6f, 6.8f, 6.9f, 7.0f, 7.1f, 7.7f, 8.8f)
  139. );
  140. }
  141. BOOST_AUTO_TEST_SUITE_END()