Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion include/boost/compute/detail/literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ template<class T>
std::string make_literal(T x)
{
std::stringstream s;
s << std::setprecision(std::numeric_limits<T>::digits10)
s << std::setprecision(
#ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
std::numeric_limits<T>::max_digits10
#else
// We don't have max_digits10, so add 3 other digits (this is what is required for
// float, and is one more than required for double).
3 + std::numeric_limits<T>::digits10
#endif
)
<< std::scientific
<< x;

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ add_compute_test("experimental.tabulate" test_tabulate.cpp)
add_compute_test("misc.amd_cpp_kernel_language" test_amd_cpp_kernel_language.cpp)
add_compute_test("misc.lambda" test_lambda.cpp)
add_compute_test("misc.user_defined_types" test_user_defined_types.cpp)
add_compute_test("misc.literal_conversion" test_literal_conversion.cpp)

# extra tests (interop tests, linkage tests, etc.)
add_subdirectory(extra)
60 changes: 60 additions & 0 deletions test/test_literal_conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestLiteralConversion
#include <boost/test/unit_test.hpp>

#include <string>
#include <sstream>
#include <vector>

#include <boost/compute/detail/literal.hpp>

BOOST_AUTO_TEST_CASE(literal_conversion_float)
{
std::vector<float> values, roundtrip;
values.push_back(1.2345679f);
values.push_back(1.2345680f);
values.push_back(1.2345681f);
for (int i = 0; i < values.size(); i++) {
std::istringstream iss(boost::compute::detail::make_literal(values[i]));
float x;
BOOST_CHECK(iss >> x);
BOOST_CHECK_EQUAL(char(iss.get()), 'f');
// Make sure we're at the end:
iss.peek();
BOOST_CHECK(iss.eof());

roundtrip.push_back(x);
}
BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
}

BOOST_AUTO_TEST_CASE(literal_conversion_double)
{
std::vector<double> values, roundtrip;
values.push_back(1.2345678901234567);
values.push_back(1.2345678901234569);
values.push_back(1.2345678901234571);
for (int i = 0; i < values.size(); i++) {
std::istringstream iss(boost::compute::detail::make_literal(values[i]));
double x;
BOOST_CHECK(iss >> x);
// Make sure we're at the end:
iss.peek();
BOOST_CHECK(iss.eof());
roundtrip.push_back(x);
}
BOOST_CHECK_EQUAL(values[0], roundtrip[0]);
BOOST_CHECK_EQUAL(values[1], roundtrip[1]);
BOOST_CHECK_EQUAL(values[2], roundtrip[2]);
}