test_threefry_engine.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Muhammad Junaid Muzammil <mjunaidmuzammil@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://kylelutz.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestThreefry
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/random/threefry_engine.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/random/uniform_real_distribution.hpp>
  15. #include "check_macros.hpp"
  16. #include "context_setup.hpp"
  17. BOOST_AUTO_TEST_CASE(generate_uint)
  18. {
  19. using boost::compute::uint_;
  20. boost::compute::threefry_engine<> random_engine(queue);
  21. boost::compute::vector<uint_> random_values(19, context);
  22. random_engine.generate(random_values.begin(), random_values.end(), queue);
  23. queue.finish();
  24. CHECK_RANGE_EQUAL(
  25. uint_, 19, random_values,
  26. (uint_(0x6b200159),
  27. uint_(0x99ba4efe),
  28. uint_(0x508efb2c),
  29. uint_(0xc0de3f32),
  30. uint_(0x64a626ec),
  31. uint_(0xfc15e573),
  32. uint_(0xb8abc4d1),
  33. uint_(0x537eb86),
  34. uint_(0xac6dc2bb),
  35. uint_(0xa7adb3c3),
  36. uint_(0x5641e094),
  37. uint_(0xe4ab4fd),
  38. uint_(0xa53c1ce9),
  39. uint_(0xabcf1dba),
  40. uint_(0x2677a25a),
  41. uint_(0x76cf5efc),
  42. uint_(0x2d08247f),
  43. uint_(0x815480f1),
  44. uint_(0x2d1fa53a))
  45. );
  46. }
  47. BOOST_AUTO_TEST_CASE(generate_float)
  48. {
  49. using boost::compute::float_;
  50. boost::compute::threefry_engine<> random_engine(queue);
  51. boost::compute::uniform_real_distribution<float_> random_distribution(0.f, 4.f);
  52. boost::compute::vector<float_> random_values(1024, context);
  53. random_distribution.generate(
  54. random_values.begin(), random_values.end(), random_engine, queue
  55. );
  56. std::vector<float_> random_values_host(1024);
  57. boost::compute::copy(
  58. random_values.begin(), random_values.end(),
  59. random_values_host.begin(),
  60. queue
  61. );
  62. queue.finish();
  63. double sum = 0.0;
  64. for(size_t i = 0; i < random_values_host.size(); i++)
  65. {
  66. BOOST_CHECK_LT(random_values_host[i], 4.0f);
  67. BOOST_CHECK_GE(random_values_host[i], 0.0f);
  68. sum += random_values_host[i];
  69. }
  70. double mean = sum / random_values_host.size();
  71. // For 1024 it can be 10% off
  72. BOOST_CHECK_CLOSE(mean, 2.0f, 10.0);
  73. }
  74. BOOST_AUTO_TEST_SUITE_END()