[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / stdio / vsnprintf_test.cpp
blob2aef1d71917eeeb9cd3b421409576050b47ecd63
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 //===----------------------------------------------------------------------===//
9 // These tests are copies of the non-v variants of the printf functions. This is
10 // because these functions are identical in every way except for how the varargs
11 // are passed.
13 #include "src/stdio/vsnprintf.h"
15 #include "test/UnitTest/Test.h"
17 int call_vsnprintf(char *__restrict buffer, size_t buffsz,
18 const char *__restrict format, ...) {
19 va_list vlist;
20 va_start(vlist, format);
21 int ret = LIBC_NAMESPACE::vsnprintf(buffer, buffsz, format, vlist);
22 va_end(vlist);
23 return ret;
26 // The sprintf test cases cover testing the shared printf functionality, so
27 // these tests will focus on snprintf exclusive features.
29 TEST(LlvmLibcVSNPrintfTest, CutOff) {
30 char buff[100];
31 int written;
33 written = call_vsnprintf(buff, 16, "A simple string with no conversions.");
34 EXPECT_EQ(written, 36);
35 ASSERT_STREQ(buff, "A simple string");
37 written = call_vsnprintf(buff, 5, "%s", "1234567890");
38 EXPECT_EQ(written, 10);
39 ASSERT_STREQ(buff, "1234");
41 written = call_vsnprintf(buff, 67, "%-101c", 'a');
42 EXPECT_EQ(written, 101);
43 ASSERT_STREQ(buff, "a "
44 " " // Each of these is 8 spaces, and there are 8.
45 " " // In total there are 65 spaces
46 " " // 'a' + 65 spaces + '\0' = 67
47 " "
48 " "
49 " "
50 " "
51 " ");
53 // passing null as the output pointer is allowed as long as buffsz is 0.
54 written = call_vsnprintf(nullptr, 0, "%s and more", "1234567890");
55 EXPECT_EQ(written, 19);
58 TEST(LlvmLibcVSNPrintfTest, NoCutOff) {
59 char buff[64];
60 int written;
62 written = call_vsnprintf(buff, 37, "A simple string with no conversions.");
63 EXPECT_EQ(written, 36);
64 ASSERT_STREQ(buff, "A simple string with no conversions.");
66 written = call_vsnprintf(buff, 20, "%s", "1234567890");
67 EXPECT_EQ(written, 10);
68 ASSERT_STREQ(buff, "1234567890");