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