xref: /aosp_15_r20/external/llvm-libc/test/src/stdio/vprintf_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Unittests for vprintf --------------------------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker // These tests are copies of the non-v variants of the printf functions. This is
10*71db0c75SAndroid Build Coastguard Worker // because these functions are identical in every way except for how the varargs
11*71db0c75SAndroid Build Coastguard Worker // are passed.
12*71db0c75SAndroid Build Coastguard Worker 
13*71db0c75SAndroid Build Coastguard Worker #include "src/stdio/vprintf.h"
14*71db0c75SAndroid Build Coastguard Worker 
15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
16*71db0c75SAndroid Build Coastguard Worker 
call_vprintf(const char * __restrict format,...)17*71db0c75SAndroid Build Coastguard Worker int call_vprintf(const char *__restrict format, ...) {
18*71db0c75SAndroid Build Coastguard Worker   va_list vlist;
19*71db0c75SAndroid Build Coastguard Worker   va_start(vlist, format);
20*71db0c75SAndroid Build Coastguard Worker   int ret = LIBC_NAMESPACE::vprintf(format, vlist);
21*71db0c75SAndroid Build Coastguard Worker   va_end(vlist);
22*71db0c75SAndroid Build Coastguard Worker   return ret;
23*71db0c75SAndroid Build Coastguard Worker }
24*71db0c75SAndroid Build Coastguard Worker 
TEST(LlvmLibcVPrintfTest,PrintOut)25*71db0c75SAndroid Build Coastguard Worker TEST(LlvmLibcVPrintfTest, PrintOut) {
26*71db0c75SAndroid Build Coastguard Worker   int written;
27*71db0c75SAndroid Build Coastguard Worker 
28*71db0c75SAndroid Build Coastguard Worker   constexpr char simple[] = "A simple string with no conversions.\n";
29*71db0c75SAndroid Build Coastguard Worker   written = call_vprintf(simple);
30*71db0c75SAndroid Build Coastguard Worker   EXPECT_EQ(written, static_cast<int>(sizeof(simple) - 1));
31*71db0c75SAndroid Build Coastguard Worker 
32*71db0c75SAndroid Build Coastguard Worker   constexpr char numbers[] = "1234567890\n";
33*71db0c75SAndroid Build Coastguard Worker   written = call_vprintf("%s", numbers);
34*71db0c75SAndroid Build Coastguard Worker   EXPECT_EQ(written, static_cast<int>(sizeof(numbers) - 1));
35*71db0c75SAndroid Build Coastguard Worker 
36*71db0c75SAndroid Build Coastguard Worker   constexpr char format_more[] = "%s and more\n";
37*71db0c75SAndroid Build Coastguard Worker   constexpr char short_numbers[] = "1234";
38*71db0c75SAndroid Build Coastguard Worker   written = call_vprintf(format_more, short_numbers);
39*71db0c75SAndroid Build Coastguard Worker   EXPECT_EQ(written,
40*71db0c75SAndroid Build Coastguard Worker             static_cast<int>(sizeof(format_more) + sizeof(short_numbers) - 4));
41*71db0c75SAndroid Build Coastguard Worker }
42