aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMarco Paland <marco@paland.com>2018-04-19 13:20:46 +0200
committerMarco Paland <marco@paland.com>2018-04-19 13:20:46 +0200
commitd74ad73009dd8ff4149a06904bb543f03a13f52e (patch)
tree8cf9a7323b6a82b2f404acedd959600d85e2734e /test
parentdd8432003c9b4f352c053ef137c012d8ed50faf5 (diff)
downloadprintf-d74ad73009dd8ff4149a06904bb543f03a13f52e.tar.gz
printf-d74ad73009dd8ff4149a06904bb543f03a13f52e.tar.bz2
printf-d74ad73009dd8ff4149a06904bb543f03a13f52e.zip
fix(printf): fix snprintf buffer termination
Fixes #7 (partly)
Diffstat (limited to 'test')
-rw-r--r--test/test_suite.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/test_suite.cpp b/test/test_suite.cpp
index a8de789..e152ac7 100644
--- a/test/test_suite.cpp
+++ b/test/test_suite.cpp
@@ -1017,6 +1017,59 @@ TEST_CASE("unknown flag", "[]" ) {
}
+TEST_CASE("buffer length", "[]" ) {
+ char buffer[100];
+ int ret;
+
+ // formatted length, this should return '4',
+ // but this feature is not implemented, returning 0
+ ret = test::snprintf(nullptr, 10, "%s", "Test");
+ REQUIRE(ret == 0);
+ ret = test::snprintf(nullptr, 0, "%s", "Test");
+ REQUIRE(ret == 0);
+
+ buffer[0] = (char)0xA5;
+ ret = test::snprintf(buffer, 0, "%s", "Test");
+ REQUIRE(buffer[0] == (char)0xA5);
+ REQUIRE(ret == 0);
+
+ buffer[0] = 0xCC;
+ test::snprintf(buffer, 1, "%s", "Test");
+ REQUIRE(buffer[0] == '\0');
+
+ test::snprintf(buffer, 2, "%s", "Hello");
+ REQUIRE(!strcmp(buffer, "H"));
+}
+
+
+TEST_CASE("ret value", "[]" ) {
+ char buffer[100] ;
+ int ret;
+
+ ret = test::snprintf(buffer, 6, "0%s", "1234");
+ REQUIRE(!strcmp(buffer, "01234"));
+ REQUIRE(ret == 5);
+
+ ret = test::snprintf(buffer, 6, "0%s", "12345");
+ REQUIRE(!strcmp(buffer, "01234"));
+ REQUIRE(ret == 6); // '5' is truncated
+
+ ret = test::snprintf(buffer, 6, "0%s", "1234567");
+ REQUIRE(!strcmp(buffer, "01234"));
+ REQUIRE(ret == 6); // '567' are truncated
+
+ ret = test::snprintf(buffer, 10, "hello, world");
+ REQUIRE(ret == 10);
+
+ ret = test::snprintf(buffer, 3, "%d", 10000);
+ REQUIRE(ret == 3); // '000' are truncated
+ REQUIRE(strlen(buffer) == 2U);
+ REQUIRE(buffer[0] == '1');
+ REQUIRE(buffer[1] == '0');
+ REQUIRE(buffer[2] == '\0');
+}
+
+
TEST_CASE("misc", "[]" ) {
char buffer[100];