Skip to content

Commit 3878cd8

Browse files
committed
Add functionality testing
1 parent 4672ae4 commit 3878cd8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ run-fail test_fprintf.cpp ;
149149
run test_frexp_ldexp.cpp ;
150150
run test_from_chars.cpp /boost/charconv//boost_charconv ;
151151
run test_from_chars_nan_payloads.cpp ;
152+
run test_from_string.cpp ;
152153
run test_git_issue_266.cpp ;
153154
run test_git_issue_271.cpp ;
154155
run test_hash.cpp ;

test/test_from_string.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#include <boost/decimal.hpp>
6+
#include <boost/core/lightweight_test.hpp>
7+
#include <random>
8+
#include <string>
9+
10+
using namespace boost::decimal;
11+
12+
static std::mt19937_64 rng {42};
13+
static constexpr std::size_t N {1024};
14+
15+
template <typename T>
16+
T recover_value(const std::string& str, std::size_t* ptr);
17+
18+
template <>
19+
decimal32_t recover_value<decimal32_t>(const std::string& str, std::size_t* ptr)
20+
{
21+
return stod32(str, ptr);
22+
}
23+
24+
template <>
25+
decimal_fast32_t recover_value<decimal_fast32_t>(const std::string& str, std::size_t* ptr)
26+
{
27+
return stod32f(str, ptr);
28+
}
29+
30+
template <>
31+
decimal64_t recover_value<decimal64_t>(const std::string& str, std::size_t* ptr)
32+
{
33+
return stod64(str, ptr);
34+
}
35+
36+
template <>
37+
decimal_fast64_t recover_value<decimal_fast64_t>(const std::string& str, std::size_t* ptr)
38+
{
39+
return stod64f(str, ptr);
40+
}
41+
42+
template <>
43+
decimal128_t recover_value<decimal128_t>(const std::string& str, std::size_t* ptr)
44+
{
45+
return stod128(str, ptr);
46+
}
47+
48+
template <>
49+
decimal_fast128_t recover_value<decimal_fast128_t>(const std::string& str, std::size_t* ptr)
50+
{
51+
return stod128f(str, ptr);
52+
}
53+
54+
template <typename T>
55+
void test()
56+
{
57+
std::uniform_int_distribution<std::int32_t> sig_dist {-9'999'999, 9'999'999};
58+
std::uniform_int_distribution<std::int32_t> exp_dist {-50, 50};
59+
60+
for (std::size_t i {}; i < N; ++i)
61+
{
62+
const T val {sig_dist(rng), exp_dist(rng)};
63+
64+
char buffer[64];
65+
const auto r {to_chars(buffer, buffer + sizeof(buffer), val)};
66+
BOOST_TEST(r);
67+
const auto dist {static_cast<std::size_t>(r.ptr - buffer)};
68+
69+
*r.ptr = '\0';
70+
const std::string str {buffer};
71+
std::size_t idx {};
72+
const T return_value {recover_value<T>(str, &idx)};
73+
74+
BOOST_TEST_EQ(return_value, val);
75+
BOOST_TEST_EQ(idx, dist);
76+
}
77+
}
78+
79+
int main()
80+
{
81+
test<decimal32_t>();
82+
test<decimal_fast32_t>();
83+
test<decimal64_t>();
84+
test<decimal_fast64_t>();
85+
test<decimal128_t>();
86+
test<decimal_fast128_t>();
87+
88+
return boost::report_errors();
89+
}
90+

0 commit comments

Comments
 (0)