Skip to content

Commit

Permalink
Add more tests for floating number parsing
Browse files Browse the repository at this point in the history
Attempt to replicate issue reported in #230
  • Loading branch information
vincentlaucsb committed May 26, 2024
1 parent 7396a54 commit 3a88c95
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
38 changes: 38 additions & 0 deletions tests/shared/float_test_cases.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <string>
#include <tuple>

using std::make_tuple;

namespace csv_test {
static const std::initializer_list<std::tuple<std::string, long double>> FLOAT_TEST_CASES = {
make_tuple("3.14", 3.14L),
make_tuple("+3.14", 3.14L),
make_tuple(" -3.14 ", -3.14L),
make_tuple("2.71828", 2.71828L),

// Test uniform distribution values
make_tuple("0.12", 0.12L),
make_tuple("0.334", 0.334L),
make_tuple("0.625", 0.625L),
make_tuple("0.666666", 0.666666L),
make_tuple("0.69", 0.69L),

// Test negative values between 0 and 1
make_tuple("-0.12", -0.12L),
make_tuple("-0.334", -0.334L),
make_tuple("-0.625", -0.625L),
make_tuple("-0.666666", -0.666666L),
make_tuple("-0.69", -0.69L),

// Larger numbers
make_tuple("1000.00", 1000L),
make_tuple("1000000.00", 1000000L),
make_tuple("9999999.99", 9999999.99L),
make_tuple("99999999.999", 99999999.999L),

make_tuple("-1000.00", -1000L),
make_tuple("-1000000.00", -1000000L),
make_tuple("-9999999.99", -9999999.99L),
make_tuple("-99999999.999", -99999999.999L),
};
}
30 changes: 24 additions & 6 deletions tests/test_csv_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include <catch2/catch_all.hpp>
#include <cmath>
#include <iostream>

using namespace csv;

#include "./shared/float_test_cases.hpp"

TEMPLATE_TEST_CASE("CSVField get<> - String Value", "[test_csv_field_get_string]",
signed char, short int, int, long long int, double, long double) {
CSVField field("applesauce");
Expand Down Expand Up @@ -80,12 +83,27 @@ TEMPLATE_TEST_CASE("CSVField get<>() - Integral Value to Int", "[test_csv_field_
}

TEST_CASE("CSVField get<>() - Floating Point Value", "[test_csv_field_get_float]") {
CSVField euler("2.718");
REQUIRE(euler.get<>() == "2.718");
REQUIRE(euler.get<csv::string_view>() == "2.718");
REQUIRE(euler.get<float>() == 2.718f);
REQUIRE(euler.get<double>() == 2.718);
REQUIRE(euler.get<long double>() == 2.718l);
SECTION("Test get() with various float types") {
CSVField euler("2.718");
REQUIRE(euler.get<>() == "2.718");
REQUIRE(euler.get<csv::string_view>() == "2.718");
REQUIRE(euler.get<float>() == 2.718f);
REQUIRE(euler.get<double>() == 2.718);
REQUIRE(euler.get<long double>() == 2.718l);
}

SECTION("Test get() with various values") {
std::string input;
long double expected = 0;

std::tie(input, expected) =
GENERATE(table<std::string, long double>(
csv_test::FLOAT_TEST_CASES));

CSVField testField(input);

REQUIRE(internals::is_equal(testField.get<long double>(), expected));
}
}

TEST_CASE("CSVField try_parse_hex()", "[test_csv_field_parse_hex]") {
Expand Down
28 changes: 13 additions & 15 deletions tests/test_data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "csv.hpp"
#include <string>

#include "./shared/float_test_cases.hpp"

using namespace csv;
using namespace csv::internals;

Expand Down Expand Up @@ -33,24 +35,20 @@ TEST_CASE( "Recognize Null Properly", "[dtype_null]" ) {
}

TEST_CASE( "Recognize Floats Properly", "[dtype_float]" ) {
std::string float_a("3.14"),
float_a1("+3.14"),
float_b(" -3.14 "),
e("2.71828");

long double out = 0;

REQUIRE(data_type(float_a, &out) == DataType::CSV_DOUBLE);
REQUIRE(is_equal(out, 3.14L));
using std::make_tuple;

REQUIRE(data_type(float_a1, &out) == DataType::CSV_DOUBLE);
REQUIRE(is_equal(out, 3.14L));
SECTION("Parse One Float") {
std::string input;
long double out = 0;
long double expected = 0;

REQUIRE(data_type(float_b, &out) == DataType::CSV_DOUBLE);
REQUIRE(is_equal(out, -3.14L));
std::tie(input, expected) =
GENERATE(table<std::string, long double>(
csv_test::FLOAT_TEST_CASES));

REQUIRE(data_type(e, &out) == DataType::CSV_DOUBLE);
REQUIRE(is_equal(out, 2.71828L));
REQUIRE(data_type(input, &out) == DataType::CSV_DOUBLE);
REQUIRE(is_equal(out, expected));
}
}

TEST_CASE("Integer Size Recognition", "[int_sizes]") {
Expand Down

0 comments on commit 3a88c95

Please sign in to comment.