1 //===-- Unittests for sprintf ---------------------------------------------===//
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 #include "src/stdio/sprintf.h"
10 
11 #include "utils/UnitTest/Test.h"
12 
13 TEST(LlvmLibcSPrintfTest, SimpleNoConv) {
14   char buff[64];
15   int written;
16 
17   written = __llvm_libc::sprintf(buff, "A simple string with no conversions.");
18   EXPECT_EQ(written, 36);
19   ASSERT_STREQ(buff, "A simple string with no conversions.");
20 }
21 
22 TEST(LlvmLibcSPrintfTest, PercentConv) {
23   char buff[64];
24   int written;
25 
26   written = __llvm_libc::sprintf(buff, "%%");
27   EXPECT_EQ(written, 1);
28   ASSERT_STREQ(buff, "%");
29 
30   written = __llvm_libc::sprintf(buff, "abc %% def");
31   EXPECT_EQ(written, 9);
32   ASSERT_STREQ(buff, "abc % def");
33 
34   written = __llvm_libc::sprintf(buff, "%%%%%%");
35   EXPECT_EQ(written, 3);
36   ASSERT_STREQ(buff, "%%%");
37 }
38 
39 TEST(LlvmLibcSPrintfTest, CharConv) {
40   char buff[64];
41   int written;
42 
43   written = __llvm_libc::sprintf(buff, "%c", 'a');
44   EXPECT_EQ(written, 1);
45   ASSERT_STREQ(buff, "a");
46 
47   written = __llvm_libc::sprintf(buff, "%3c %-3c", '1', '2');
48   EXPECT_EQ(written, 7);
49   ASSERT_STREQ(buff, "  1 2  ");
50 
51   written = __llvm_libc::sprintf(buff, "%*c", 2, '3');
52   EXPECT_EQ(written, 2);
53   ASSERT_STREQ(buff, " 3");
54 }
55 
56 TEST(LlvmLibcSPrintfTest, StringConv) {
57   char buff[64];
58   int written;
59 
60   written = __llvm_libc::sprintf(buff, "%s", "abcDEF123");
61   EXPECT_EQ(written, 9);
62   ASSERT_STREQ(buff, "abcDEF123");
63 
64   written = __llvm_libc::sprintf(buff, "%10s %-10s", "centered", "title");
65   EXPECT_EQ(written, 21);
66   ASSERT_STREQ(buff, "  centered title     ");
67 
68   written = __llvm_libc::sprintf(buff, "%-5.4s%-4.4s", "words can describe",
69                                  "soups most delicious");
70   EXPECT_EQ(written, 9);
71   ASSERT_STREQ(buff, "word soup");
72 
73   written = __llvm_libc::sprintf(buff, "%*s %.*s %*.*s", 10, "beginning", 2,
74                                  "isn't", 12, 10, "important. Ever.");
75   EXPECT_EQ(written, 26);
76   ASSERT_STREQ(buff, " beginning is   important.");
77 }
78 
79 #ifndef LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE
80 TEST(LlvmLibcSPrintfTest, IndexModeParsing) {
81   char buff[64];
82   int written;
83 
84   written = __llvm_libc::sprintf(buff, "%1$s", "abcDEF123");
85   EXPECT_EQ(written, 9);
86   ASSERT_STREQ(buff, "abcDEF123");
87 
88   written = __llvm_libc::sprintf(buff, "%1$s %%", "abcDEF123");
89   EXPECT_EQ(written, 11);
90   ASSERT_STREQ(buff, "abcDEF123 %");
91 
92   written =
93       __llvm_libc::sprintf(buff, "%3$s %1$s %2$s", "is", "hard", "ordering");
94   EXPECT_EQ(written, 16);
95   ASSERT_STREQ(buff, "ordering is hard");
96 
97   written = __llvm_libc::sprintf(
98       buff, "%10$s %9$s %8$c %7$s %6$s, %6$s %5$s %4$-*1$s %3$.*11$s %2$s. %%",
99       6, "pain", "alphabetical", "such", "is", "this", "do", 'u', "would",
100       "why", 1);
101   EXPECT_EQ(written, 45);
102   ASSERT_STREQ(buff, "why would u do this, this is such   a pain. %");
103 }
104 #endif // LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE
105