xref: /llvm-project/libc/test/src/stdio/snprintf_test.cpp (revision a21fc4c0a681000c23089301e8935f579517b96a)
19f1d905fSMichael Jones //===-- Unittests for snprintf --------------------------------------------===//
29f1d905fSMichael Jones //
39f1d905fSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49f1d905fSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
59f1d905fSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69f1d905fSMichael Jones //
79f1d905fSMichael Jones //===----------------------------------------------------------------------===//
89f1d905fSMichael Jones 
99f1d905fSMichael Jones #include "src/stdio/snprintf.h"
109f1d905fSMichael Jones 
11af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
129f1d905fSMichael Jones 
139f1d905fSMichael Jones // The sprintf test cases cover testing the shared printf functionality, so
149f1d905fSMichael Jones // these tests will focus on snprintf exclusive features.
159f1d905fSMichael Jones 
169f1d905fSMichael Jones TEST(LlvmLibcSNPrintfTest, CutOff) {
17b9f6c208SMichael Jones   char buff[100];
189f1d905fSMichael Jones   int written;
199f1d905fSMichael Jones 
20b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(buff, 16,
21b6bc9d72SGuillaume Chatelet                                      "A simple string with no conversions.");
229f1d905fSMichael Jones   EXPECT_EQ(written, 36);
239f1d905fSMichael Jones   ASSERT_STREQ(buff, "A simple string");
249f1d905fSMichael Jones 
25b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(buff, 5, "%s", "1234567890");
269f1d905fSMichael Jones   EXPECT_EQ(written, 10);
279f1d905fSMichael Jones   ASSERT_STREQ(buff, "1234");
289f1d905fSMichael Jones 
29b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(buff, 67, "%-101c", 'a');
30b9f6c208SMichael Jones   EXPECT_EQ(written, 101);
31b9f6c208SMichael Jones   ASSERT_STREQ(buff, "a "
32b9f6c208SMichael Jones                      "        " // Each of these is 8 spaces, and there are 8.
33b9f6c208SMichael Jones                      "        " // In total there are 65 spaces
34b9f6c208SMichael Jones                      "        " // 'a' + 65 spaces + '\0' = 67
35b9f6c208SMichael Jones                      "        "
36b9f6c208SMichael Jones                      "        "
37b9f6c208SMichael Jones                      "        "
38b9f6c208SMichael Jones                      "        "
39b9f6c208SMichael Jones                      "        ");
40b9f6c208SMichael Jones 
419f1d905fSMichael Jones   // passing null as the output pointer is allowed as long as buffsz is 0.
42b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(nullptr, 0, "%s and more", "1234567890");
439f1d905fSMichael Jones   EXPECT_EQ(written, 19);
44*a21fc4c0SMichael Jones 
45*a21fc4c0SMichael Jones   written = LIBC_NAMESPACE::snprintf(nullptr, 0, "%*s", INT_MIN, "nothing");
46*a21fc4c0SMichael Jones   EXPECT_EQ(written, INT_MAX);
479f1d905fSMichael Jones }
489f1d905fSMichael Jones 
499f1d905fSMichael Jones TEST(LlvmLibcSNPrintfTest, NoCutOff) {
509f1d905fSMichael Jones   char buff[64];
519f1d905fSMichael Jones   int written;
529f1d905fSMichael Jones 
53b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(buff, 37,
54b6bc9d72SGuillaume Chatelet                                      "A simple string with no conversions.");
559f1d905fSMichael Jones   EXPECT_EQ(written, 36);
569f1d905fSMichael Jones   ASSERT_STREQ(buff, "A simple string with no conversions.");
579f1d905fSMichael Jones 
58b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::snprintf(buff, 20, "%s", "1234567890");
599f1d905fSMichael Jones   EXPECT_EQ(written, 10);
609f1d905fSMichael Jones   ASSERT_STREQ(buff, "1234567890");
619f1d905fSMichael Jones }
62