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