mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-07 01:06:48 +08:00
More testing.
This commit is contained in:
parent
c975e468a1
commit
693fa66fa4
@ -116,6 +116,7 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
// Otherwise, we will be ignoring the 'e'.
|
// Otherwise, we will be ignoring the 'e'.
|
||||||
|
p = location_of_e;
|
||||||
} else {
|
} else {
|
||||||
while ((p != pend) && is_integer(*p)) {
|
while ((p != pend) && is_integer(*p)) {
|
||||||
uint8_t digit = uint8_t(*p - '0');
|
uint8_t digit = uint8_t(*p - '0');
|
||||||
|
|||||||
@ -111,13 +111,10 @@ bool basic_test_64bit(std::string vals, double val) {
|
|||||||
std::cout << std::hexfloat << result_value << " == " << val << std::endl;
|
std::cout << std::hexfloat << result_value << " == " << val << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool basiciss_test_64bit(double val) {
|
|
||||||
std::string long_vals = to_long_string(val);
|
|
||||||
std::string vals = to_string(val);
|
|
||||||
return basic_test_64bit(long_vals, val) && basic_test_64bit(vals, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool issue8() {
|
bool issue8() {
|
||||||
|
std::cout << __func__ << std::endl;
|
||||||
const char* s =
|
const char* s =
|
||||||
"3."
|
"3."
|
||||||
"141592653589793238462643383279502884197169399375105820974944592307816406"
|
"141592653589793238462643383279502884197169399375105820974944592307816406"
|
||||||
@ -148,6 +145,7 @@ bool issue8() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool check_behavior() {
|
bool check_behavior() {
|
||||||
|
std::cout << __func__ << std::endl;
|
||||||
const std::string input = "abc";
|
const std::string input = "abc";
|
||||||
double result;
|
double result;
|
||||||
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
|
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
|
||||||
@ -165,6 +163,7 @@ bool check_behavior() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool issue19() {
|
bool issue19() {
|
||||||
|
std::cout << __func__ << std::endl;
|
||||||
const std::string input = "3.14e";
|
const std::string input = "3.14e";
|
||||||
double result;
|
double result;
|
||||||
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
|
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
|
||||||
@ -173,14 +172,61 @@ bool issue19() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::cout << "parsed the number " << result << std::endl;
|
std::cout << "parsed the number " << result << std::endl;
|
||||||
if(answer.ptr != input.data() + 4) {
|
if(answer.ptr == input.data() + 4) {
|
||||||
|
std::cout << "Parsed the number and stopped at the right character." << result << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::cout << "stopped after " << (answer.ptr - input.data()) << " characters" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool test_scientific_only() {
|
||||||
|
std::cout << __func__ << std::endl;
|
||||||
|
// first, we try with something that should fail...
|
||||||
|
std::string input = "3.14";
|
||||||
|
double result;
|
||||||
|
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::scientific);
|
||||||
|
if(answer.ec == std::errc()) {
|
||||||
|
std::cerr << "It is not scientific!\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
input = "3.14e10";
|
||||||
|
result;
|
||||||
|
answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::scientific);
|
||||||
|
if(answer.ec != std::errc()) {
|
||||||
|
std::cerr << "It is scientific!\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::cout << "parsed the number " << result << std::endl;
|
||||||
|
if(answer.ptr == input.data() + input.size()) {
|
||||||
std::cout << "Parsed the number and stopped at the right character." << result << std::endl;
|
std::cout << "Parsed the number and stopped at the right character." << result << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool test_fixed_only() {
|
||||||
|
std::cout << __func__ << std::endl;
|
||||||
|
const std::string input = "3.14e10";
|
||||||
|
double result;
|
||||||
|
auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::fixed);
|
||||||
|
if(answer.ec != std::errc()) {
|
||||||
|
std::cerr << "We want to parse up to 3.14\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::cout << "parsed the number " << result << std::endl;
|
||||||
|
if(answer.ptr == input.data() + 4) {
|
||||||
|
std::cout << "Parsed the number and stopped at the right character." << result << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
Assert(test_fixed_only());
|
||||||
|
Assert(test_scientific_only());
|
||||||
Assert(issue19());
|
Assert(issue19());
|
||||||
Assert(check_behavior());
|
Assert(check_behavior());
|
||||||
std::cout << "======= 64 bits " << std::endl;
|
std::cout << "======= 64 bits " << std::endl;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user