Skip to content

Commit 0dd4b64

Browse files
committed
test(test_suite): added support for PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1 parent b65be83 commit 0dd4b64

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

test/test_suite.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <sstream>
3535
#include <math.h>
3636

37+
3738
namespace test {
3839
// use functions in own test namespace to avoid stdio conflicts
3940
#include "../printf.h"
@@ -368,10 +369,18 @@ TEST_CASE("- flag", "[]" ) {
368369
REQUIRE(!strcmp(buffer, "-42 "));
369370

370371
test::sprintf(buffer, "%0-15.3e", -42.);
372+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
371373
REQUIRE(!strcmp(buffer, "-4.200e+01 "));
374+
#else
375+
REQUIRE(!strcmp(buffer, "e"));
376+
#endif
372377

373378
test::sprintf(buffer, "%0-15.3g", -42.);
379+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
374380
REQUIRE(!strcmp(buffer, "-42.0 "));
381+
#else
382+
REQUIRE(!strcmp(buffer, "g"));
383+
#endif
375384
}
376385

377386

@@ -940,6 +949,7 @@ TEST_CASE("float padding neg numbers", "[]" ) {
940949
test::sprintf(buffer, "% 5.1f", -5.);
941950
REQUIRE(!strcmp(buffer, " -5.0"));
942951

952+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
943953
test::sprintf(buffer, "% 6.1g", -5.);
944954
REQUIRE(!strcmp(buffer, " -5"));
945955

@@ -948,6 +958,7 @@ TEST_CASE("float padding neg numbers", "[]" ) {
948958

949959
test::sprintf(buffer, "% 10.1e", -5.);
950960
REQUIRE(!strcmp(buffer, " -5.0e+00"));
961+
#endif
951962

952963
// zero padding
953964
test::sprintf(buffer, "%03.1f", -5.);
@@ -959,9 +970,6 @@ TEST_CASE("float padding neg numbers", "[]" ) {
959970
test::sprintf(buffer, "%05.1f", -5.);
960971
REQUIRE(!strcmp(buffer, "-05.0"));
961972

962-
test::sprintf(buffer, "%010.1e", -5.);
963-
REQUIRE(!strcmp(buffer, "-005.0e+00"));
964-
965973
// zero padding no decimal point
966974
test::sprintf(buffer, "%01.0f", -5.);
967975
REQUIRE(!strcmp(buffer, "-5"));
@@ -972,11 +980,16 @@ TEST_CASE("float padding neg numbers", "[]" ) {
972980
test::sprintf(buffer, "%03.0f", -5.);
973981
REQUIRE(!strcmp(buffer, "-05"));
974982

983+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
984+
test::sprintf(buffer, "%010.1e", -5.);
985+
REQUIRE(!strcmp(buffer, "-005.0e+00"));
986+
975987
test::sprintf(buffer, "%07.0E", -5.);
976988
REQUIRE(!strcmp(buffer, "-05E+00"));
977989

978990
test::sprintf(buffer, "%03.0g", -5.);
979991
REQUIRE(!strcmp(buffer, "-05"));
992+
#endif
980993
}
981994

982995
TEST_CASE("length", "[]" ) {
@@ -1075,8 +1088,10 @@ TEST_CASE("float", "[]" ) {
10751088
test::sprintf(buffer, "%-8f", -INFINITY);
10761089
REQUIRE(!strcmp(buffer, "-inf "));
10771090

1091+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
10781092
test::sprintf(buffer, "%+8e", INFINITY);
10791093
REQUIRE(!strcmp(buffer, " +inf"));
1094+
#endif
10801095

10811096
test::sprintf(buffer, "%.4f", 3.1415354);
10821097
REQUIRE(!strcmp(buffer, "3.1415"));
@@ -1157,6 +1172,7 @@ TEST_CASE("float", "[]" ) {
11571172
test::sprintf(buffer, "a%-5.1fend", 0.5);
11581173
REQUIRE(!strcmp(buffer, "a0.5 end"));
11591174

1175+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
11601176
test::sprintf(buffer, "%G", 12345.678);
11611177
REQUIRE(!strcmp(buffer, "12345.7"));
11621178

@@ -1186,10 +1202,15 @@ TEST_CASE("float", "[]" ) {
11861202

11871203
test::sprintf(buffer, "%+.3E", 1.23e+308);
11881204
REQUIRE(!strcmp(buffer, "+1.230E+308"));
1205+
#endif
11891206

1190-
// out of range for float: should switch to exp notation
1207+
// out of range for float: should switch to exp notation if supported, else empty
11911208
test::sprintf(buffer, "%.1f", 1E20);
1209+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
11921210
REQUIRE(!strcmp(buffer, "1.0e+20"));
1211+
#else
1212+
REQUIRE(!strcmp(buffer, ""));
1213+
#endif
11931214

11941215
// brute force float
11951216
bool fail = false;
@@ -1203,6 +1224,8 @@ TEST_CASE("float", "[]" ) {
12031224
}
12041225
REQUIRE(!fail);
12051226

1227+
1228+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
12061229
// brute force exp
12071230
str.setf(std::ios::scientific, std::ios::floatfield);
12081231
for (float i = -1e20; i < 1e20; i += 1e15) {
@@ -1212,6 +1235,7 @@ TEST_CASE("float", "[]" ) {
12121235
fail = fail || !!strcmp(buffer, str.str().c_str());
12131236
}
12141237
REQUIRE(!fail);
1238+
#endif
12151239
}
12161240

12171241

@@ -1464,12 +1488,6 @@ TEST_CASE("misc", "[]" ) {
14641488
test::sprintf(buffer, "%.*f", 2, 0.33333333);
14651489
REQUIRE(!strcmp(buffer, "0.33"));
14661490

1467-
test::sprintf(buffer, "%.*g", 2, 0.33333333);
1468-
REQUIRE(!strcmp(buffer, "0.33"));
1469-
1470-
test::sprintf(buffer, "%.*e", 2, 0.33333333);
1471-
REQUIRE(!strcmp(buffer, "3.33e-01"));
1472-
14731491
test::sprintf(buffer, "%.*d", -1, 1);
14741492
REQUIRE(!strcmp(buffer, "1"));
14751493

@@ -1484,4 +1502,12 @@ TEST_CASE("misc", "[]" ) {
14841502

14851503
test::sprintf(buffer, "%*sx", -3, "hi");
14861504
REQUIRE(!strcmp(buffer, "hi x"));
1505+
1506+
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1507+
test::sprintf(buffer, "%.*g", 2, 0.33333333);
1508+
REQUIRE(!strcmp(buffer, "0.33"));
1509+
1510+
test::sprintf(buffer, "%.*e", 2, 0.33333333);
1511+
REQUIRE(!strcmp(buffer, "3.33e-01"));
1512+
#endif
14871513
}

0 commit comments

Comments
 (0)