mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 05:55:46 +00:00
Fix CI: useless casts in the float token test of json_view
GCC -Werror=useless-cast on Linux x86-64 rejected static_cast<std::size_t>(tokens() % n): std::mt19937_64 yields std::uint_fast64_t, which is std::size_t there. Draw the numbers through a lambda that casts a named std::uint64_t, which also makes the conversions for std::string's count explicit where std::size_t is 32 bits wide. The sequence of draws is unchanged. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -1123,31 +1123,38 @@ TEST_CASE("json_view dump")
|
||||
// through the conversion; both as dump() writes them
|
||||
{
|
||||
std::mt19937_64 tokens(1170); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed)
|
||||
// a number below n; the remainder is a std::uint64_t, which is
|
||||
// std::size_t on some platforms and wider on others
|
||||
const auto draw = [&tokens](std::size_t n)
|
||||
{
|
||||
const std::uint64_t r = tokens() % n;
|
||||
return static_cast<std::size_t>(r);
|
||||
};
|
||||
std::string many_tokens = "[";
|
||||
for (int i = 0; i < 20000; ++i)
|
||||
{
|
||||
const auto length = static_cast<std::size_t>(1 + (tokens() % 17));
|
||||
std::string digits(1, static_cast<char>('1' + (tokens() % 9)));
|
||||
const std::size_t length = 1 + draw(17);
|
||||
std::string digits(1, static_cast<char>('1' + draw(9)));
|
||||
for (std::size_t k = 1; k < length; ++k)
|
||||
{
|
||||
digits += static_cast<char>('0' + (tokens() % 10));
|
||||
digits += static_cast<char>('0' + draw(10));
|
||||
}
|
||||
digits += std::string(tokens() % 4, '0'); // trailing zeros
|
||||
std::string token = tokens() % 3 == 0 ? "-" : "";
|
||||
const auto point = static_cast<std::size_t>(tokens() % (digits.size() + 1));
|
||||
digits += std::string(draw(4), '0'); // trailing zeros
|
||||
std::string token = draw(3) == 0 ? "-" : "";
|
||||
const std::size_t point = draw(digits.size() + 1);
|
||||
if (point == 0)
|
||||
{
|
||||
token += "0." + std::string(tokens() % 5, '0') + digits;
|
||||
token += "0." + std::string(draw(5), '0') + digits;
|
||||
}
|
||||
else
|
||||
{
|
||||
token += digits.substr(0, point) + (point < digits.size() ? "." + digits.substr(point) : "");
|
||||
}
|
||||
// an exponent that keeps the value between about 1e-320 and 1e300
|
||||
const int exponent = static_cast<int>(tokens() % 600) - 300 - static_cast<int>(point);
|
||||
if (tokens() % 4 != 0)
|
||||
const int exponent = static_cast<int>(draw(600)) - 300 - static_cast<int>(point);
|
||||
if (draw(4) != 0)
|
||||
{
|
||||
token += (tokens() % 2 == 0 ? "e" : "E") + std::string(exponent >= 0 && tokens() % 2 == 0 ? "+" : "") + std::to_string(exponent);
|
||||
token += (draw(2) == 0 ? "e" : "E") + std::string(exponent >= 0 && draw(2) == 0 ? "+" : "") + std::to_string(exponent);
|
||||
}
|
||||
else if (point == digits.size())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user