| 1 | //===-- Unittests for vsprintf --------------------------------------------===// |
| 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 | // 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. |
| 12 | |
| 13 | #include "src/stdio/vsprintf.h" |
| 14 | |
| 15 | #include "test/UnitTest/Test.h" |
| 16 | |
| 17 | int call_vsprintf(char *__restrict buffer, const char *__restrict format, ...) { |
| 18 | va_list vlist; |
| 19 | va_start(vlist, format); |
| 20 | int ret = LIBC_NAMESPACE::vsprintf(buffer, format, vlist); |
| 21 | va_end(vlist); |
| 22 | return ret; |
| 23 | } |
| 24 | |
| 25 | TEST(LlvmLibcVSPrintfTest, SimpleNoConv) { |
| 26 | char buff[64]; |
| 27 | int written; |
| 28 | |
| 29 | written = call_vsprintf(buffer: buff, format: "A simple string with no conversions." ); |
| 30 | EXPECT_EQ(written, 36); |
| 31 | ASSERT_STREQ(buff, "A simple string with no conversions." ); |
| 32 | } |
| 33 | |
| 34 | TEST(LlvmLibcVSPrintfTest, PercentConv) { |
| 35 | char buff[64]; |
| 36 | int written; |
| 37 | |
| 38 | written = call_vsprintf(buffer: buff, format: "%%" ); |
| 39 | EXPECT_EQ(written, 1); |
| 40 | ASSERT_STREQ(buff, "%" ); |
| 41 | |
| 42 | written = call_vsprintf(buffer: buff, format: "abc %% def" ); |
| 43 | EXPECT_EQ(written, 9); |
| 44 | ASSERT_STREQ(buff, "abc % def" ); |
| 45 | |
| 46 | written = call_vsprintf(buffer: buff, format: "%%%%%%" ); |
| 47 | EXPECT_EQ(written, 3); |
| 48 | ASSERT_STREQ(buff, "%%%" ); |
| 49 | } |
| 50 | |
| 51 | TEST(LlvmLibcVSPrintfTest, CharConv) { |
| 52 | char buff[64]; |
| 53 | int written; |
| 54 | |
| 55 | written = call_vsprintf(buffer: buff, format: "%c" , 'a'); |
| 56 | EXPECT_EQ(written, 1); |
| 57 | ASSERT_STREQ(buff, "a" ); |
| 58 | |
| 59 | written = call_vsprintf(buffer: buff, format: "%3c %-3c" , '1', '2'); |
| 60 | EXPECT_EQ(written, 7); |
| 61 | ASSERT_STREQ(buff, " 1 2 " ); |
| 62 | |
| 63 | written = call_vsprintf(buffer: buff, format: "%*c" , 2, '3'); |
| 64 | EXPECT_EQ(written, 2); |
| 65 | ASSERT_STREQ(buff, " 3" ); |
| 66 | } |
| 67 | |