Revert "[lldb][test] Remove compiler version check and use regex" (#124101)
[llvm-project.git] / libc / test / src / stdio / vsprintf_test.cpp
blobddccdfa5cef7305cfdc07491e9c3442e6b2c0fdd
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 //===----------------------------------------------------------------------===//
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, ...) {
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;
25 TEST(LlvmLibcVSPrintfTest, SimpleNoConv) {
26 char buff[64];
27 int written;
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) {
35 char buff[64];
36 int written;
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) {
52 char buff[64];
53 int written;
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");