aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/stdio/vprintf_test.cpp
blob: 40eba723a0070714d13f65a6d0d69831d2ca94c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 = LIBC_NAMESPACE::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));
}