1 //===-- Unittests for vsprintf --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // These tests are shortened copies of the non-v variants of the printf
10 // functions. This is because these functions are identical in every way except
11 // for how the varargs are passed.
13 #include "src/stdio/vsprintf.h"
15 #include "test/UnitTest/Test.h"
17 int call_vsprintf(char *__restrict buffer
, const char *__restrict format
, ...) {
19 va_start(vlist
, format
);
20 int ret
= LIBC_NAMESPACE::vsprintf(buffer
, format
, vlist
);
25 TEST(LlvmLibcVSPrintfTest
, SimpleNoConv
) {
29 written
= call_vsprintf(buff
, "A simple string with no conversions.");
30 EXPECT_EQ(written
, 36);
31 ASSERT_STREQ(buff
, "A simple string with no conversions.");
34 TEST(LlvmLibcVSPrintfTest
, PercentConv
) {
38 written
= call_vsprintf(buff
, "%%");
39 EXPECT_EQ(written
, 1);
40 ASSERT_STREQ(buff
, "%");
42 written
= call_vsprintf(buff
, "abc %% def");
43 EXPECT_EQ(written
, 9);
44 ASSERT_STREQ(buff
, "abc % def");
46 written
= call_vsprintf(buff
, "%%%%%%");
47 EXPECT_EQ(written
, 3);
48 ASSERT_STREQ(buff
, "%%%");
51 TEST(LlvmLibcVSPrintfTest
, CharConv
) {
55 written
= call_vsprintf(buff
, "%c", 'a');
56 EXPECT_EQ(written
, 1);
57 ASSERT_STREQ(buff
, "a");
59 written
= call_vsprintf(buff
, "%3c %-3c", '1', '2');
60 EXPECT_EQ(written
, 7);
61 ASSERT_STREQ(buff
, " 1 2 ");
63 written
= call_vsprintf(buff
, "%*c", 2, '3');
64 EXPECT_EQ(written
, 2);
65 ASSERT_STREQ(buff
, " 3");