brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 0e003f5 Raw
101 lines · cpp
1//===-- Unittests for vfprintf --------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9// These tests are copies of the non-v variants of the printf functions. This is10// because these functions are identical in every way except for how the varargs11// are passed.12 13#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE14#include "src/stdio/fclose.h"15#include "src/stdio/ferror.h"16#include "src/stdio/fopen.h"17#include "src/stdio/fread.h"18#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE19 20#include "src/stdio/vfprintf.h"21 22#include "test/UnitTest/ErrnoCheckingTest.h"23#include "test/UnitTest/ErrnoSetterMatcher.h"24#include "test/UnitTest/Test.h"25 26namespace printf_test {27#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE28using LIBC_NAMESPACE::fclose;29using LIBC_NAMESPACE::ferror;30using LIBC_NAMESPACE::fopen;31using LIBC_NAMESPACE::fread;32#else  // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)33using ::fclose;34using ::ferror;35using ::fopen;36using ::fread;37#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE38} // namespace printf_test39 40int call_vfprintf(::FILE *__restrict stream, const char *__restrict format,41                  ...) {42  va_list vlist;43  va_start(vlist, format);44  int ret = LIBC_NAMESPACE::vfprintf(stream, format, vlist);45  va_end(vlist);46  return ret;47}48 49using LlvmLibcVFPrintfTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;50 51TEST(LlvmLibcVFPrintfTest, WriteToFile) {52  const char *FILENAME = APPEND_LIBC_TEST("vfprintf_output.test");53  auto FILE_PATH = libc_make_test_file_path(FILENAME);54 55  ::FILE *file = printf_test::fopen(FILE_PATH, "w");56  ASSERT_FALSE(file == nullptr);57 58  int written;59 60  constexpr char simple[] = "A simple string with no conversions.\n";61  written = call_vfprintf(file, simple);62  EXPECT_EQ(written, 37);63 64  constexpr char numbers[] = "1234567890\n";65  written = call_vfprintf(file, "%s", numbers);66  EXPECT_EQ(written, 11);67 68  constexpr char format_more[] = "%s and more\n";69  constexpr char short_numbers[] = "1234";70  written = call_vfprintf(file, format_more, short_numbers);71  EXPECT_EQ(written, 14);72 73  ASSERT_EQ(0, printf_test::fclose(file));74 75  file = printf_test::fopen(FILE_PATH, "r");76  ASSERT_FALSE(file == nullptr);77 78  char data[50];79  ASSERT_EQ(printf_test::fread(data, 1, sizeof(simple) - 1, file),80            sizeof(simple) - 1);81  data[sizeof(simple) - 1] = '\0';82  ASSERT_STREQ(data, simple);83  ASSERT_EQ(printf_test::fread(data, 1, sizeof(numbers) - 1, file),84            sizeof(numbers) - 1);85  data[sizeof(numbers) - 1] = '\0';86  ASSERT_STREQ(data, numbers);87  ASSERT_EQ(printf_test::fread(88                data, 1, sizeof(format_more) + sizeof(short_numbers) - 4, file),89            sizeof(format_more) + sizeof(short_numbers) - 4);90  data[sizeof(format_more) + sizeof(short_numbers) - 4] = '\0';91  ASSERT_STREQ(data, "1234 and more\n");92 93  ASSERT_EQ(printf_test::ferror(file), 0);94 95  written = call_vfprintf(file, "Writing to a read only file should fail.");96  EXPECT_LT(written, 0);97  ASSERT_ERRNO_FAILURE();98 99  ASSERT_EQ(printf_test::fclose(file), 0);100}101