mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
55d0a4e598
* Bugfix/odr violations (#47) * Make common non-member functions inline, remove unreachable line from get_line_buffer * [skip ci] Fix namespace comments * Resolve clang-tidy warnings (#48) * Resolve clang-tidy warnings, update single_header_generator.py * Update single header test, resolve additional clang-tidy warnings * Add header and raw_header methods, update header usage methods error handling, write new and update existing unit tests * Update parser error messages, fix parser tests * Add [[nodiscard]] where fitting, update unit tests (#49) * Add const where fitting, make splitter class members private, add #pragma once to ssp.hpp * Modify header parsing for empty headers, update old and add new tests for header parsing * Enable the parser to accept a header with one empty field, update unit tests * Fix test CMakeLists.txt typo
149 lines
4.9 KiB
C++
149 lines
4.9 KiB
C++
#include "test_helpers.hpp"
|
|
|
|
#define SSP_DISABLE_FAST_FLOAT
|
|
#include <ss/extract.hpp>
|
|
|
|
TEST_CASE(
|
|
"testing extract functions for floating point values without fast float") {
|
|
CHECK_FLOATING_CONVERSION(123.456, float);
|
|
CHECK_FLOATING_CONVERSION(123.456, double);
|
|
|
|
CHECK_FLOATING_CONVERSION(59, float);
|
|
CHECK_FLOATING_CONVERSION(59, double);
|
|
|
|
CHECK_FLOATING_CONVERSION(4210., float);
|
|
CHECK_FLOATING_CONVERSION(4210., double);
|
|
|
|
CHECK_FLOATING_CONVERSION(0.123, float);
|
|
CHECK_FLOATING_CONVERSION(0.123, double);
|
|
|
|
CHECK_FLOATING_CONVERSION(123e4, float);
|
|
CHECK_FLOATING_CONVERSION(123e4, double);
|
|
}
|
|
|
|
TEST_CASE("extract test functions for numbers with invalid inputs without fast "
|
|
"float") {
|
|
// floating pint for int
|
|
CHECK_INVALID_CONVERSION("123.4", int);
|
|
|
|
// random input for float
|
|
CHECK_INVALID_CONVERSION("xxx1", float);
|
|
}
|
|
|
|
TEST_CASE("extract test functions for std::variant without fast float") {
|
|
{
|
|
std::string s = "22";
|
|
{
|
|
std::variant<int, double, std::string> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, double);
|
|
CHECK_NOT_VARIANT(var, std::string);
|
|
REQUIRE_VARIANT(var, 22, int);
|
|
}
|
|
{
|
|
std::variant<double, int, std::string> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, std::string);
|
|
REQUIRE_VARIANT(var, 22, double);
|
|
}
|
|
{
|
|
std::variant<std::string, double, int> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
REQUIRE_VARIANT(var, "22", std::string);
|
|
}
|
|
{
|
|
std::variant<int> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
REQUIRE_VARIANT(var, 22, int);
|
|
}
|
|
}
|
|
{
|
|
std::string s = "22.2";
|
|
{
|
|
std::variant<int, double, std::string> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, std::string);
|
|
REQUIRE_VARIANT(var, 22.2, double);
|
|
}
|
|
{
|
|
std::variant<double, int, std::string> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, std::string);
|
|
REQUIRE_VARIANT(var, 22.2, double);
|
|
}
|
|
{
|
|
std::variant<std::string, double, int> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
REQUIRE_VARIANT(var, "22.2", std::string);
|
|
}
|
|
}
|
|
{
|
|
std::string s = "2.2.2";
|
|
{
|
|
std::variant<int, double, std::string> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
REQUIRE_VARIANT(var, "2.2.2", std::string);
|
|
}
|
|
{
|
|
std::variant<double, std::string, int> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
REQUIRE_VARIANT(var, "2.2.2", std::string);
|
|
}
|
|
{
|
|
std::variant<std::string, double, int> var;
|
|
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
CHECK_NOT_VARIANT(var, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
REQUIRE_VARIANT(var, "2.2.2", std::string);
|
|
}
|
|
{
|
|
std::variant<int, double> var;
|
|
REQUIRE_FALSE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
|
|
REQUIRE_VARIANT(var, int{}, int);
|
|
CHECK_NOT_VARIANT(var, double);
|
|
}
|
|
{
|
|
std::variant<double, int> var;
|
|
REQUIRE_FALSE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
|
|
REQUIRE_VARIANT(var, double{}, double);
|
|
CHECK_NOT_VARIANT(var, int);
|
|
}
|
|
{
|
|
std::variant<int> var;
|
|
REQUIRE_FALSE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
|
|
|
|
REQUIRE_VARIANT(var, int{}, int);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|