Skip to content

Commit

Permalink
Fixes mpaland#110 + other test suite changes:
Browse files Browse the repository at this point in the history
* Split the "float" testcase into several cases regarding float-point specifiers.
* Renamed "types" -> "integer types
* Added a few testcases with non-numeric floating-point values and the `E` specifier.
  • Loading branch information
eyalroz committed Feb 21, 2022
1 parent a5e157a commit 4b0a54c
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ TEST_CASE("length - non-standard format", "[]" ) {


#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
TEST_CASE("float", "[]" ) {
TEST_CASE("infinity and not-a-number values", "[]" ) {
char buffer[100];

// test special-case floats using math.h macros
Expand All @@ -844,12 +844,16 @@ TEST_CASE("float", "[]" ) {
PRINTING_CHECK("-inf ", ==, sprintf_, buffer, "%-8f", (double) -INFINITY);
#endif
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
PRINTING_CHECK(" +inf", ==, sprintf_, buffer, "%+8e", (double) INFINITY);
PRINTING_CHECK(" nan", ==, sprintf_, buffer, "%8e", (double) NAN);
PRINTING_CHECK(" inf", ==, sprintf_, buffer, "%8e", (double) INFINITY);
PRINTING_CHECK("-inf ", ==, sprintf_, buffer, "%-8e", (double) -INFINITY);
#endif
#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
PRINTING_CHECK("3.1415", ==, sprintf_, buffer, "%.4f", 3.1415354);
PRINTING_CHECK("30343.142", ==, sprintf_, buffer, "%.3f", 30343.1415354);
}
#endif // PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS

#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
TEST_CASE("floating-point specifiers with 31-32 bit integer values", "[]" ) {
char buffer[100];
PRINTING_CHECK("2.1474836470e+09", ==, sprintf_, buffer, "%.10f", 2147483647.0); // 2^31 - 1
PRINTING_CHECK("2.1474836480e+09", ==, sprintf_, buffer, "%.10f", 2147483648.0); // 2^31
PRINTING_CHECK("4.2949672950e+09", ==, sprintf_, buffer, "%.10f", 4294967295.0); // 2^32 - 1
Expand All @@ -858,15 +862,17 @@ TEST_CASE("float", "[]" ) {
PRINTING_CHECK("2147483648", ==, sprintf_, buffer, "%.10g", 2147483648.0); // 2^31
PRINTING_CHECK("4294967295", ==, sprintf_, buffer, "%.10g", 4294967295.0); // 2^32 - 1
PRINTING_CHECK("4294967296", ==, sprintf_, buffer, "%.10g", 4294967296.0); // 2^32
}
#endif

// switch from decimal to exponential representation
//
#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
TEST_CASE("fallback from decimal to exponential", "[]" ) {
char buffer[100];
CAPTURE_AND_PRINT(sprintf_, buffer, "%.0f", (double) ((int64_t)1 * 1000 ) );
if (PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL < 3) {
CHECK(!strcmp(buffer, "1e+3"));
}
else {

CHECK(!strcmp(buffer, "1000"));
}

Expand Down Expand Up @@ -909,6 +915,24 @@ TEST_CASE("float", "[]" ) {
else {
CHECK(!strcmp(buffer, "1000000000000000"));
}

// A value which should Should definitely be out of range for float
CAPTURE_AND_PRINT(sprintf_, buffer, "%.1f", 1E20);
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
CHECK(!strcmp(buffer, "1.0e+20"));
#else
CHECK(!strcmp(buffer, ""));
#endif

}
#endif // PRINTF_SUPPORT_DECIMAL_SPECIFIERS

#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
TEST_CASE("floating-point specifiers, precision and flags", "[]" ) {
char buffer[100];
#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
PRINTING_CHECK("3.1415", ==, sprintf_, buffer, "%.4f", 3.1415354);
PRINTING_CHECK("30343.142", ==, sprintf_, buffer, "%.3f", 30343.1415354);
PRINTING_CHECK("34", ==, sprintf_, buffer, "%.0f", 34.1415354);
PRINTING_CHECK("1", ==, sprintf_, buffer, "%.0f", 1.3);
PRINTING_CHECK("2", ==, sprintf_, buffer, "%.0f", 1.55);
Expand Down Expand Up @@ -961,15 +985,6 @@ TEST_CASE("float", "[]" ) {
PRINTING_CHECK("-8.380923438e+04", ==, sprintf_, buffer, "%.9e", -83809.234375);
#endif

// out of range for float: should switch to exp notation if supported, else empty
#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
CAPTURE_AND_PRINT(sprintf_, buffer, "%.1f", 1E20);
#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
CHECK(!strcmp(buffer, "1.0e+20"));
#else
CHECK(!strcmp(buffer, ""));
#endif
#endif

#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS
// brute force float
Expand Down Expand Up @@ -1001,7 +1016,7 @@ TEST_CASE("float", "[]" ) {
#endif // PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS


TEST_CASE("types", "[]" ) {
TEST_CASE("integer types", "[]" ) {
char buffer[100];
PRINTING_CHECK("0", ==, sprintf_, buffer, "%i", 0);
PRINTING_CHECK("1234", ==, sprintf_, buffer, "%i", 1234);
Expand Down

0 comments on commit 4b0a54c

Please sign in to comment.