Repository navigation
Expand file tree
/
Copy pathtest_regression_bugs.cpp
More file actions
120 lines (97 loc) · 3.85 KB
/
Copy pathtest_regression_bugs.cpp
File metadata and controls
120 lines (97 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <gtest/gtest.h>
#include <clocale>
#include <cstdint>
#include <string>
#include <rfl.hpp>
#include <rfl/cli.hpp>
// cli::Reader stoull accepts negative numbers for unsigned types
// File: include/rfl/cli/Reader.hpp:77
// std::stoull("-1") returns ULLONG_MAX (wraps around), then static_cast<T>
// wraps again. No check for negative input.
namespace test_cli_stoull_negative {
struct Config {
uint16_t port;
};
TEST(cli, cli_rejects_negative_for_unsigned) {
const char* args[] = {"program", "--port=-1"};
const auto result = rfl::cli::read<Config>(2, const_cast<char**>(args));
EXPECT_FALSE(result)
<< "cli::read should reject negative value for uint16_t, "
"but parsed port=" << result.value().port;
}
} // namespace test_cli_stoull_negative
// cli::Reader no range check on narrowing cast
// File: include/rfl/cli/Reader.hpp:77
// static_cast<T>(stoull(str)) silently truncates. E.g. stoull("99999") = 99999,
// then static_cast<uint16_t>(99999) = 34463.
namespace test_cli_narrowing_cast {
struct Config {
uint16_t port;
};
TEST(cli, cli_rejects_out_of_range_for_narrow_type) {
const char* args[] = {"program", "--port=99999"};
const auto result = rfl::cli::read<Config>(2, const_cast<char**>(args));
EXPECT_FALSE(result)
<< "cli::read should reject 99999 for uint16_t (max 65535), "
"but parsed port=" << result.value().port;
}
} // namespace test_cli_narrowing_cast
// cli::Reader signed overload — no range check on narrowing cast
// File: include/rfl/cli/Reader.hpp:101-102
// static_cast<T>(stoll(str)) silently truncates for int8_t/int16_t/int32_t.
// E.g. stoll("99999") cast to int16_t wraps silently.
namespace test_cli_signed_narrowing_cast {
struct SignedConfig {
int8_t level;
};
TEST(cli, cli_rejects_out_of_range_for_signed_narrow_type) {
const char* args[] = {"program", "--level=200"};
const auto result =
rfl::cli::read<SignedConfig>(2, const_cast<char**>(args));
EXPECT_FALSE(result)
<< "cli::read should reject 200 for int8_t (max 127), "
"but parsed level="
<< static_cast<int>(result.value().level);
}
TEST(cli, cli_rejects_large_negative_for_signed_narrow_type) {
const char* args[] = {"program", "--level=-200"};
const auto result =
rfl::cli::read<SignedConfig>(2, const_cast<char**>(args));
EXPECT_FALSE(result)
<< "cli::read should reject -200 for int8_t (min -128), "
"but parsed level="
<< static_cast<int>(result.value().level);
}
} // namespace test_cli_signed_narrowing_cast
// cli::Reader uses std::stod which is locale-dependent
// File: include/rfl/cli/Reader.hpp:64
// std::stod uses the current C locale. In locales where the decimal
// separator is a comma (e.g. de_DE), "3.14" parses incorrectly.
namespace test_cli_stod_locale {
struct FloatConfig {
double rate;
};
TEST(cli, cli_float_parsing_ignores_locale) {
// Save current locale
const char* old_locale = std::setlocale(LC_NUMERIC, nullptr);
std::string saved_locale = old_locale ? old_locale : "C";
// Try to set a locale that uses comma as decimal separator
const char* result = std::setlocale(LC_NUMERIC, "de_DE.UTF-8");
if (!result) {
result = std::setlocale(LC_NUMERIC, "de_DE");
}
if (!result) {
// Locale not available on this system, skip test
GTEST_SKIP() << "de_DE locale not available, skipping locale test";
}
const char* args[] = {"program", "--rate=3.14"};
const auto res = rfl::cli::read<FloatConfig>(2, const_cast<char**>(args));
// Restore locale before assertions
std::setlocale(LC_NUMERIC, saved_locale.c_str());
ASSERT_TRUE(res) << "cli::read should parse '3.14' even in de_DE locale. "
"Error: " << res.error().what();
EXPECT_DOUBLE_EQ(res.value().rate, 3.14)
<< "cli::read parsed '3.14' as " << res.value().rate
<< " due to locale-dependent std::stod";
}
} // namespace test_cli_stod_locale