| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| //===-- Unittests for vfprintf --------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // These tests are copies of the non-v variants of the printf functions. This is | ||
| // because these functions are identical in every way except for how the varargs | ||
| // are passed. | ||
|
|
||
| #ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE | ||
| #include "src/stdio/fclose.h" | ||
| #include "src/stdio/ferror.h" | ||
| #include "src/stdio/fopen.h" | ||
| #include "src/stdio/fread.h" | ||
| #endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE | ||
|
|
||
| #include "src/stdio/vfprintf.h" | ||
|
|
||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| namespace printf_test { | ||
| #ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE | ||
| using __llvm_libc::fclose; | ||
| using __llvm_libc::ferror; | ||
| using __llvm_libc::fopen; | ||
| using __llvm_libc::fread; | ||
| #else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE) | ||
| using ::fclose; | ||
| using ::ferror; | ||
| using ::fopen; | ||
| using ::fread; | ||
| #endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE | ||
| } // namespace printf_test | ||
|
|
||
| int call_vfprintf(::FILE *__restrict stream, const char *__restrict format, | ||
| ...) { | ||
| va_list vlist; | ||
| va_start(vlist, format); | ||
| int ret = __llvm_libc::vfprintf(stream, format, vlist); | ||
| va_end(vlist); | ||
| return ret; | ||
| } | ||
|
|
||
| TEST(LlvmLibcVFPrintfTest, WriteToFile) { | ||
| const char *FILENAME = "fprintf_output.test"; | ||
| auto FILE_PATH = libc_make_test_file_path(FILENAME); | ||
|
|
||
| ::FILE *file = printf_test::fopen(FILE_PATH, "w"); | ||
| ASSERT_FALSE(file == nullptr); | ||
|
|
||
| int written; | ||
|
|
||
| constexpr char simple[] = "A simple string with no conversions.\n"; | ||
| written = call_vfprintf(file, simple); | ||
| EXPECT_EQ(written, 37); | ||
|
|
||
| constexpr char numbers[] = "1234567890\n"; | ||
| written = call_vfprintf(file, "%s", numbers); | ||
| EXPECT_EQ(written, 11); | ||
|
|
||
| constexpr char format_more[] = "%s and more\n"; | ||
| constexpr char short_numbers[] = "1234"; | ||
| written = call_vfprintf(file, format_more, short_numbers); | ||
| EXPECT_EQ(written, 14); | ||
|
|
||
| ASSERT_EQ(0, printf_test::fclose(file)); | ||
|
|
||
| file = printf_test::fopen(FILE_PATH, "r"); | ||
| ASSERT_FALSE(file == nullptr); | ||
|
|
||
| char data[50]; | ||
| ASSERT_EQ(printf_test::fread(data, 1, sizeof(simple) - 1, file), | ||
| sizeof(simple) - 1); | ||
| data[sizeof(simple) - 1] = '\0'; | ||
| ASSERT_STREQ(data, simple); | ||
| ASSERT_EQ(printf_test::fread(data, 1, sizeof(numbers) - 1, file), | ||
| sizeof(numbers) - 1); | ||
| data[sizeof(numbers) - 1] = '\0'; | ||
| ASSERT_STREQ(data, numbers); | ||
| ASSERT_EQ(printf_test::fread( | ||
| data, 1, sizeof(format_more) + sizeof(short_numbers) - 4, file), | ||
| sizeof(format_more) + sizeof(short_numbers) - 4); | ||
| data[sizeof(format_more) + sizeof(short_numbers) - 4] = '\0'; | ||
| ASSERT_STREQ(data, "1234 and more\n"); | ||
|
|
||
| ASSERT_EQ(printf_test::ferror(file), 0); | ||
|
|
||
| written = call_vfprintf(file, "Writing to a read only file should fail."); | ||
| EXPECT_LT(written, 0); | ||
|
|
||
| ASSERT_EQ(printf_test::fclose(file), 0); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //===-- Unittests for vprintf --------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // These tests are copies of the non-v variants of the printf functions. This is | ||
| // because these functions are identical in every way except for how the varargs | ||
| // are passed. | ||
|
|
||
| #include "src/stdio/vprintf.h" | ||
|
|
||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| int call_vprintf(const char *__restrict format, ...) { | ||
| va_list vlist; | ||
| va_start(vlist, format); | ||
| int ret = __llvm_libc::vprintf(format, vlist); | ||
| va_end(vlist); | ||
| return ret; | ||
| } | ||
|
|
||
| TEST(LlvmLibcVPrintfTest, PrintOut) { | ||
| int written; | ||
|
|
||
| constexpr char simple[] = "A simple string with no conversions.\n"; | ||
| written = call_vprintf(simple); | ||
| EXPECT_EQ(written, static_cast<int>(sizeof(simple) - 1)); | ||
|
|
||
| constexpr char numbers[] = "1234567890\n"; | ||
| written = call_vprintf("%s", numbers); | ||
| EXPECT_EQ(written, static_cast<int>(sizeof(numbers) - 1)); | ||
|
|
||
| constexpr char format_more[] = "%s and more\n"; | ||
| constexpr char short_numbers[] = "1234"; | ||
| written = call_vprintf(format_more, short_numbers); | ||
| EXPECT_EQ(written, | ||
| static_cast<int>(sizeof(format_more) + sizeof(short_numbers) - 4)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===-- Unittests for snprintf --------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // These tests are copies of the non-v variants of the printf functions. This is | ||
| // because these functions are identical in every way except for how the varargs | ||
| // are passed. | ||
|
|
||
| #include "src/stdio/vsnprintf.h" | ||
|
|
||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| int call_vsnprintf(char *__restrict buffer, size_t buffsz, | ||
| const char *__restrict format, ...) { | ||
| va_list vlist; | ||
| va_start(vlist, format); | ||
| int ret = __llvm_libc::vsnprintf(buffer, buffsz, format, vlist); | ||
| va_end(vlist); | ||
| return ret; | ||
| } | ||
|
|
||
| // The sprintf test cases cover testing the shared printf functionality, so | ||
| // these tests will focus on snprintf exclusive features. | ||
|
|
||
| TEST(LlvmLibcVSNPrintfTest, CutOff) { | ||
| char buff[100]; | ||
| int written; | ||
|
|
||
| written = call_vsnprintf(buff, 16, "A simple string with no conversions."); | ||
| EXPECT_EQ(written, 36); | ||
| ASSERT_STREQ(buff, "A simple string"); | ||
|
|
||
| written = call_vsnprintf(buff, 5, "%s", "1234567890"); | ||
| EXPECT_EQ(written, 10); | ||
| ASSERT_STREQ(buff, "1234"); | ||
|
|
||
| written = call_vsnprintf(buff, 67, "%-101c", 'a'); | ||
| EXPECT_EQ(written, 101); | ||
| ASSERT_STREQ(buff, "a " | ||
| " " // Each of these is 8 spaces, and there are 8. | ||
| " " // In total there are 65 spaces | ||
| " " // 'a' + 65 spaces + '\0' = 67 | ||
| " " | ||
| " " | ||
| " " | ||
| " " | ||
| " "); | ||
|
|
||
| // passing null as the output pointer is allowed as long as buffsz is 0. | ||
| written = call_vsnprintf(nullptr, 0, "%s and more", "1234567890"); | ||
| EXPECT_EQ(written, 19); | ||
| } | ||
|
|
||
| TEST(LlvmLibcVSNPrintfTest, NoCutOff) { | ||
| char buff[64]; | ||
| int written; | ||
|
|
||
| written = call_vsnprintf(buff, 37, "A simple string with no conversions."); | ||
| EXPECT_EQ(written, 36); | ||
| ASSERT_STREQ(buff, "A simple string with no conversions."); | ||
|
|
||
| written = call_vsnprintf(buff, 20, "%s", "1234567890"); | ||
| EXPECT_EQ(written, 10); | ||
| ASSERT_STREQ(buff, "1234567890"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //===-- Unittests for vsprintf --------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // These tests are shortened copies of the non-v variants of the printf | ||
| // functions. This is because these functions are identical in every way except | ||
| // for how the varargs are passed. | ||
|
|
||
| #include "src/stdio/vsprintf.h" | ||
|
|
||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| int call_vsprintf(char *__restrict buffer, const char *__restrict format, ...) { | ||
| va_list vlist; | ||
| va_start(vlist, format); | ||
| int ret = __llvm_libc::vsprintf(buffer, format, vlist); | ||
| va_end(vlist); | ||
| return ret; | ||
| } | ||
|
|
||
| TEST(LlvmLibcVSPrintfTest, SimpleNoConv) { | ||
| char buff[64]; | ||
| int written; | ||
|
|
||
| written = call_vsprintf(buff, "A simple string with no conversions."); | ||
| EXPECT_EQ(written, 36); | ||
| ASSERT_STREQ(buff, "A simple string with no conversions."); | ||
| } | ||
|
|
||
| TEST(LlvmLibcVSPrintfTest, PercentConv) { | ||
| char buff[64]; | ||
| int written; | ||
|
|
||
| written = call_vsprintf(buff, "%%"); | ||
| EXPECT_EQ(written, 1); | ||
| ASSERT_STREQ(buff, "%"); | ||
|
|
||
| written = call_vsprintf(buff, "abc %% def"); | ||
| EXPECT_EQ(written, 9); | ||
| ASSERT_STREQ(buff, "abc % def"); | ||
|
|
||
| written = call_vsprintf(buff, "%%%%%%"); | ||
| EXPECT_EQ(written, 3); | ||
| ASSERT_STREQ(buff, "%%%"); | ||
| } | ||
|
|
||
| TEST(LlvmLibcVSPrintfTest, CharConv) { | ||
| char buff[64]; | ||
| int written; | ||
|
|
||
| written = call_vsprintf(buff, "%c", 'a'); | ||
| EXPECT_EQ(written, 1); | ||
| ASSERT_STREQ(buff, "a"); | ||
|
|
||
| written = call_vsprintf(buff, "%3c %-3c", '1', '2'); | ||
| EXPECT_EQ(written, 7); | ||
| ASSERT_STREQ(buff, " 1 2 "); | ||
|
|
||
| written = call_vsprintf(buff, "%*c", 2, '3'); | ||
| EXPECT_EQ(written, 2); | ||
| ASSERT_STREQ(buff, " 3"); | ||
| } |