fast_float/tests/json_fmt.cpp
Maya Warrier 7b1fc2f95d Add an option to allow inf/nan even in json mode
- Most JSON parsers offer this option too
2023-09-14 21:07:22 -04:00

40 lines
1.3 KiB
C++

#include <cstdlib>
#include <iostream>
#include <vector>
// test that this option is ignored
#define FASTFLOAT_ALLOWS_LEADING_PLUS
#include "fast_float/fast_float.h"
int main()
{
const std::vector<double> expected{ -0.2, 0.02, 0.002, 1., 0., std::numeric_limits<double>::infinity() };
const std::vector<std::string> accept{ "-0.2", "0.02", "0.002", "1e+0000", "0e-2", "inf" };
const std::vector<std::string> reject{ "-.2", "00.02", "0.e+1", "00.e+1", ".25", "+0.25", "inf", "nan(snan)" };
for (std::size_t i = 0; i < accept.size(); ++i)
{
const auto& f = accept[i];
double result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fast_float::chars_format::json_or_infnan);
if (answer.ec != std::errc() || result != expected[i]) {
std::cerr << "json fmt rejected valid json " << f << std::endl;
return EXIT_FAILURE;
}
}
for (std::size_t i = 0; i < reject.size(); ++i)
{
const auto& f = reject[i];
double result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, fast_float::chars_format::json);
if (answer.ec == std::errc()) {
std::cerr << "json fmt accepted invalid json " << f << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}