test_literal_conversion.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca>
  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 TestLiteralConversion
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <sstream>
  14. #include <vector>
  15. #include <boost/compute/detail/literal.hpp>
  16. BOOST_AUTO_TEST_CASE(literal_conversion_float)
  17. {
  18. std::vector<float> values, roundtrip;
  19. values.push_back(1.2345679f);
  20. values.push_back(1.2345680f);
  21. values.push_back(1.2345681f);
  22. for (size_t i = 0; i < values.size(); i++) {
  23. std::string literal = boost::compute::detail::make_literal(values[i]);
  24. // Check that we got something in the literal, and that at least the first
  25. // 6 characters (5 digits) look good
  26. BOOST_CHECK_EQUAL(literal.substr(0, 6), "1.2345");
  27. // libc++ stream extraction fails on a value such as "1.2f" rather than extracting 1.2
  28. // and leaving the f; so check the "f", then slice it off for the extraction below:
  29. BOOST_CHECK_EQUAL(literal.substr(literal.length()-1), "f");
  30. std::istringstream iss(literal.substr(0, literal.length()-1));
  31. float x;
  32. iss >> x;
  33. roundtrip.push_back(x);
  34. }
  35. BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
  36. BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
  37. BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
  38. }
  39. BOOST_AUTO_TEST_CASE(literal_conversion_double)
  40. {
  41. std::vector<double> values, roundtrip;
  42. values.push_back(1.2345678901234567);
  43. values.push_back(1.2345678901234569);
  44. values.push_back(1.2345678901234571);
  45. for (size_t i = 0; i < values.size(); i++) {
  46. std::string literal = boost::compute::detail::make_literal(values[i]);
  47. // Check that we got something in the literal, and that at least the first
  48. // 11 characters (10 digits) look good
  49. BOOST_CHECK_EQUAL(literal.substr(0, 11), "1.234567890");
  50. // Stuff the literal into a stream then extract it, and make sure we get a numerically
  51. // identical result. (Since there is no suffix, we don't have to worry about removing the
  52. // suffix like we have to above, for float--but we also check to make sure there is no
  53. // (unextracted) suffix).
  54. std::istringstream iss(literal);
  55. double x;
  56. std::string suffix;
  57. iss >> x >> suffix;
  58. BOOST_CHECK_EQUAL(suffix, "");
  59. roundtrip.push_back(x);
  60. }
  61. BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
  62. BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
  63. BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
  64. }