1 //===-- Unittests for snprintf --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/stdio/snprintf.h" 10 11 #include "utils/UnitTest/Test.h" 12 13 // The sprintf test cases cover testing the shared printf functionality, so 14 // these tests will focus on snprintf exclusive features. 15 16 TEST(LlvmLibcSNPrintfTest, CutOff) { 17 char buff[64]; 18 int written; 19 20 written = 21 __llvm_libc::snprintf(buff, 16, "A simple string with no conversions."); 22 EXPECT_EQ(written, 36); 23 ASSERT_STREQ(buff, "A simple string"); 24 25 written = __llvm_libc::snprintf(buff, 5, "%s", "1234567890"); 26 EXPECT_EQ(written, 10); 27 ASSERT_STREQ(buff, "1234"); 28 29 // passing null as the output pointer is allowed as long as buffsz is 0. 30 written = __llvm_libc::snprintf(nullptr, 0, "%s and more", "1234567890"); 31 EXPECT_EQ(written, 19); 32 } 33 34 TEST(LlvmLibcSNPrintfTest, NoCutOff) { 35 char buff[64]; 36 int written; 37 38 written = 39 __llvm_libc::snprintf(buff, 37, "A simple string with no conversions."); 40 EXPECT_EQ(written, 36); 41 ASSERT_STREQ(buff, "A simple string with no conversions."); 42 43 written = __llvm_libc::snprintf(buff, 20, "%s", "1234567890"); 44 EXPECT_EQ(written, 10); 45 ASSERT_STREQ(buff, "1234567890"); 46 } 47