//===-- Unittests for sprintf ---------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/stdio/sprintf.h" #include "utils/UnitTest/Test.h" TEST(LlvmLibcSPrintfTest, SimpleNoConv) { char buff[64]; int written; written = __llvm_libc::sprintf(buff, "A simple string with no conversions."); EXPECT_EQ(written, 36); ASSERT_STREQ(buff, "A simple string with no conversions."); } TEST(LlvmLibcSPrintfTest, PercentConv) { char buff[64]; int written; written = __llvm_libc::sprintf(buff, "%%"); EXPECT_EQ(written, 1); ASSERT_STREQ(buff, "%"); written = __llvm_libc::sprintf(buff, "abc %% def"); EXPECT_EQ(written, 9); ASSERT_STREQ(buff, "abc % def"); written = __llvm_libc::sprintf(buff, "%%%%%%"); EXPECT_EQ(written, 3); ASSERT_STREQ(buff, "%%%"); } TEST(LlvmLibcSPrintfTest, CharConv) { char buff[64]; int written; written = __llvm_libc::sprintf(buff, "%c", 'a'); EXPECT_EQ(written, 1); ASSERT_STREQ(buff, "a"); written = __llvm_libc::sprintf(buff, "%3c %-3c", '1', '2'); EXPECT_EQ(written, 7); ASSERT_STREQ(buff, " 1 2 "); written = __llvm_libc::sprintf(buff, "%*c", 2, '3'); EXPECT_EQ(written, 2); ASSERT_STREQ(buff, " 3"); } TEST(LlvmLibcSPrintfTest, StringConv) { char buff[64]; int written; written = __llvm_libc::sprintf(buff, "%s", "abcDEF123"); EXPECT_EQ(written, 9); ASSERT_STREQ(buff, "abcDEF123"); written = __llvm_libc::sprintf(buff, "%10s %-10s", "centered", "title"); EXPECT_EQ(written, 21); ASSERT_STREQ(buff, " centered title "); written = __llvm_libc::sprintf(buff, "%-5.4s%-4.4s", "words can describe", "soups most delicious"); EXPECT_EQ(written, 9); ASSERT_STREQ(buff, "word soup"); written = __llvm_libc::sprintf(buff, "%*s %.*s %*.*s", 10, "beginning", 2, "isn't", 12, 10, "important. Ever."); EXPECT_EQ(written, 26); ASSERT_STREQ(buff, " beginning is important."); } #ifndef LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE TEST(LlvmLibcSPrintfTest, IndexModeParsing) { char buff[64]; int written; written = __llvm_libc::sprintf(buff, "%1$s", "abcDEF123"); EXPECT_EQ(written, 9); ASSERT_STREQ(buff, "abcDEF123"); written = __llvm_libc::sprintf(buff, "%1$s %%", "abcDEF123"); EXPECT_EQ(written, 11); ASSERT_STREQ(buff, "abcDEF123 %"); written = __llvm_libc::sprintf(buff, "%3$s %1$s %2$s", "is", "hard", "ordering"); EXPECT_EQ(written, 16); ASSERT_STREQ(buff, "ordering is hard"); written = __llvm_libc::sprintf( buff, "%10$s %9$s %8$c %7$s %6$s, %6$s %5$s %4$-*1$s %3$.*11$s %2$s. %%", 6, "pain", "alphabetical", "such", "is", "this", "do", 'u', "would", "why", 1); EXPECT_EQ(written, 45); ASSERT_STREQ(buff, "why would u do this, this is such a pain. %"); } #endif // LLVM_LIBC_PRINTF_DISABLE_INDEX_MODE