Skip to content

Commit 3a88c95

Browse files
committed
Add more tests for floating number parsing
Attempt to replicate issue reported in #230
1 parent 7396a54 commit 3a88c95

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

tests/shared/float_test_cases.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <string>
2+
#include <tuple>
3+
4+
using std::make_tuple;
5+
6+
namespace csv_test {
7+
static const std::initializer_list<std::tuple<std::string, long double>> FLOAT_TEST_CASES = {
8+
make_tuple("3.14", 3.14L),
9+
make_tuple("+3.14", 3.14L),
10+
make_tuple(" -3.14 ", -3.14L),
11+
make_tuple("2.71828", 2.71828L),
12+
13+
// Test uniform distribution values
14+
make_tuple("0.12", 0.12L),
15+
make_tuple("0.334", 0.334L),
16+
make_tuple("0.625", 0.625L),
17+
make_tuple("0.666666", 0.666666L),
18+
make_tuple("0.69", 0.69L),
19+
20+
// Test negative values between 0 and 1
21+
make_tuple("-0.12", -0.12L),
22+
make_tuple("-0.334", -0.334L),
23+
make_tuple("-0.625", -0.625L),
24+
make_tuple("-0.666666", -0.666666L),
25+
make_tuple("-0.69", -0.69L),
26+
27+
// Larger numbers
28+
make_tuple("1000.00", 1000L),
29+
make_tuple("1000000.00", 1000000L),
30+
make_tuple("9999999.99", 9999999.99L),
31+
make_tuple("99999999.999", 99999999.999L),
32+
33+
make_tuple("-1000.00", -1000L),
34+
make_tuple("-1000000.00", -1000000L),
35+
make_tuple("-9999999.99", -9999999.99L),
36+
make_tuple("-99999999.999", -99999999.999L),
37+
};
38+
}

tests/test_csv_field.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
#include <catch2/catch_all.hpp>
33
#include <cmath>
44
#include <iostream>
5+
56
using namespace csv;
67

8+
#include "./shared/float_test_cases.hpp"
9+
710
TEMPLATE_TEST_CASE("CSVField get<> - String Value", "[test_csv_field_get_string]",
811
signed char, short int, int, long long int, double, long double) {
912
CSVField field("applesauce");
@@ -80,12 +83,27 @@ TEMPLATE_TEST_CASE("CSVField get<>() - Integral Value to Int", "[test_csv_field_
8083
}
8184

8285
TEST_CASE("CSVField get<>() - Floating Point Value", "[test_csv_field_get_float]") {
83-
CSVField euler("2.718");
84-
REQUIRE(euler.get<>() == "2.718");
85-
REQUIRE(euler.get<csv::string_view>() == "2.718");
86-
REQUIRE(euler.get<float>() == 2.718f);
87-
REQUIRE(euler.get<double>() == 2.718);
88-
REQUIRE(euler.get<long double>() == 2.718l);
86+
SECTION("Test get() with various float types") {
87+
CSVField euler("2.718");
88+
REQUIRE(euler.get<>() == "2.718");
89+
REQUIRE(euler.get<csv::string_view>() == "2.718");
90+
REQUIRE(euler.get<float>() == 2.718f);
91+
REQUIRE(euler.get<double>() == 2.718);
92+
REQUIRE(euler.get<long double>() == 2.718l);
93+
}
94+
95+
SECTION("Test get() with various values") {
96+
std::string input;
97+
long double expected = 0;
98+
99+
std::tie(input, expected) =
100+
GENERATE(table<std::string, long double>(
101+
csv_test::FLOAT_TEST_CASES));
102+
103+
CSVField testField(input);
104+
105+
REQUIRE(internals::is_equal(testField.get<long double>(), expected));
106+
}
89107
}
90108

91109
TEST_CASE("CSVField try_parse_hex()", "[test_csv_field_parse_hex]") {

tests/test_data_type.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "csv.hpp"
33
#include <string>
44

5+
#include "./shared/float_test_cases.hpp"
6+
57
using namespace csv;
68
using namespace csv::internals;
79

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

3537
TEST_CASE( "Recognize Floats Properly", "[dtype_float]" ) {
36-
std::string float_a("3.14"),
37-
float_a1("+3.14"),
38-
float_b(" -3.14 "),
39-
e("2.71828");
40-
41-
long double out = 0;
42-
43-
REQUIRE(data_type(float_a, &out) == DataType::CSV_DOUBLE);
44-
REQUIRE(is_equal(out, 3.14L));
38+
using std::make_tuple;
4539

46-
REQUIRE(data_type(float_a1, &out) == DataType::CSV_DOUBLE);
47-
REQUIRE(is_equal(out, 3.14L));
40+
SECTION("Parse One Float") {
41+
std::string input;
42+
long double out = 0;
43+
long double expected = 0;
4844

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

52-
REQUIRE(data_type(e, &out) == DataType::CSV_DOUBLE);
53-
REQUIRE(is_equal(out, 2.71828L));
49+
REQUIRE(data_type(input, &out) == DataType::CSV_DOUBLE);
50+
REQUIRE(is_equal(out, expected));
51+
}
5452
}
5553

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

0 commit comments

Comments
 (0)