mirror of
https://github.com/red0124/ssp.git
synced 2025-12-14 21:59:55 +01:00
Add ability to convert larger numbers without fast_float, write unit tests
This commit is contained in:
@@ -63,11 +63,6 @@ TEST_CASE("extract test functions for numbers with invalid inputs") {
|
||||
// random input for float
|
||||
CHECK_INVALID_CONVERSION("xxx1", float);
|
||||
|
||||
// number too big
|
||||
CHECK_INVALID_CONVERSION((std::string(40, '1') + "." +
|
||||
std::string(40, '2')),
|
||||
double);
|
||||
|
||||
// random input for int
|
||||
CHECK_INVALID_CONVERSION("xxx1", int);
|
||||
|
||||
@@ -280,3 +275,20 @@ TEST_CASE("extract test functions for std::variant") {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("extract test with long number string") {
|
||||
{
|
||||
std::string string_num =
|
||||
std::string(20, '1') + "." + std::string(20, '2');
|
||||
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, float, stof);
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||
}
|
||||
|
||||
{
|
||||
std::string string_num =
|
||||
std::string(50, '1') + "." + std::string(50, '2');
|
||||
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,6 @@ TEST_CASE("extract test functions for numbers with invalid inputs without fast "
|
||||
|
||||
// random input for float
|
||||
CHECK_INVALID_CONVERSION("xxx1", float);
|
||||
|
||||
// number too big
|
||||
CHECK_INVALID_CONVERSION((std::string(40, '1') + "." +
|
||||
std::string(40, '2')),
|
||||
double);
|
||||
}
|
||||
|
||||
TEST_CASE("extract test functions for std::variant without fast float") {
|
||||
@@ -135,3 +130,20 @@ TEST_CASE("extract test functions for std::variant without fast float") {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("extract test with long number string without fast float") {
|
||||
{
|
||||
std::string string_num =
|
||||
std::string(20, '1') + "." + std::string(20, '2');
|
||||
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, float, stof);
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||
}
|
||||
|
||||
{
|
||||
std::string string_num =
|
||||
std::string(50, '1') + "." + std::string(50, '2');
|
||||
|
||||
CHECK_FLOATING_CONVERSION_LONG_NUMBER(string_num, double, stod);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
#include <doctest/doctest.h>
|
||||
@@ -75,6 +75,18 @@ struct unique_file_name {
|
||||
CHECK_LT(std::abs(t.value() - type(-input)), eps); \
|
||||
}
|
||||
|
||||
#define CHECK_FLOATING_CONVERSION_LONG_NUMBER(STRING_NUMBER, TYPE, CONVERTER) \
|
||||
{ \
|
||||
auto begin = STRING_NUMBER.c_str(); \
|
||||
auto end = begin + STRING_NUMBER.size(); \
|
||||
\
|
||||
auto number = ss::to_num<TYPE>(begin, end); \
|
||||
REQUIRE(number.has_value()); \
|
||||
\
|
||||
auto expected_number = CONVERTER(STRING_NUMBER); \
|
||||
CHECK_EQ(number.value(), expected_number); \
|
||||
}
|
||||
|
||||
#define CHECK_INVALID_CONVERSION(input, type) \
|
||||
{ \
|
||||
std::string s = input; \
|
||||
|
||||
Reference in New Issue
Block a user