From d75320daf6941ffcb09a0aff494dd38576f487d7 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Mon, 2 Aug 2021 17:49:17 +0300 Subject: [PATCH] Added checks for printing extremal values of the various signed and unsigned integer types; regards issue mpaland/printf#118. --- test/test_suite.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 85848775..2bd089f0 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -33,7 +33,7 @@ #include #include #include - +#include namespace test { // use functions in own test namespace to avoid stdio conflicts @@ -1055,6 +1055,53 @@ TEST_CASE("misc", "[]" ) { #endif } +TEST_CASE("extremal signed integer values", "[]" ) { + char buffer[100]; + char expected[100]; + + std::sprintf(expected, "%hd", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%hd", std::numeric_limits::max()); + + std::sprintf(expected, "%hd", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%hd", std::numeric_limits::max()); + + std::sprintf(expected, "%d", std::numeric_limits::min()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%d", std::numeric_limits::min()); + + std::sprintf(expected, "%d", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%d", std::numeric_limits::max()); + + std::sprintf(expected, "%ld", std::numeric_limits::min()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%ld", std::numeric_limits::min()); + + std::sprintf(expected, "%ld", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%ld", std::numeric_limits::max()); + + std::sprintf(expected, "%lld", std::numeric_limits::min()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%lld", std::numeric_limits::min()); + + std::sprintf(expected, "%lld", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%lld", std::numeric_limits::max()); +} + +TEST_CASE("extremal unsigned integer values", "[]" ) { + char buffer[100]; + char expected[100]; + + std::sprintf(expected, "%hu", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%hu", std::numeric_limits::max()); + + std::sprintf(expected, "%u", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%u", std::numeric_limits::max()); + + std::sprintf(expected, "%lu", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%lu", std::numeric_limits::max()); + + std::sprintf(expected, "%llu", std::numeric_limits::max()); + PRINTING_CHECK(expected, ==, test::sprintf_, buffer, "%llu", std::numeric_limits::max()); +} + + #ifdef TEST_WITH_NON_STANDARD_FORMAT_STRINGS DISABLE_WARNING_POP #endif