1//===-- RPC test to check args to printf ----------------------------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "test/IntegrationTest/test.h"
10
11#include "src/__support/GPU/utils.h"
12#include "src/stdio/fopen.h"
13#include "src/stdio/fprintf.h"
14
15using namespace LIBC_NAMESPACE;
16
17FILE *file = LIBC_NAMESPACE::fopen("testdata/test_data.txt", "w");
18
19TEST_MAIN(int argc, char **argv, char **envp) {
20 ASSERT_TRUE(file && "failed to open file");
21 // Check basic printing.
22 int written = 0;
23 written = LIBC_NAMESPACE::fprintf(file, "A simple string\n");
24 ASSERT_EQ(written, 16);
25
26 const char *str = "A simple string\n";
27 written = LIBC_NAMESPACE::fprintf(file, "%s", str);
28 ASSERT_EQ(written, 16);
29
30 // Check printing a different value with each thread.
31 uint64_t thread_id = gpu::get_thread_id();
32 written = LIBC_NAMESPACE::fprintf(file, "%8ld\n", thread_id);
33 ASSERT_EQ(written, 9);
34
35 written = LIBC_NAMESPACE::fprintf(file, "%d%c%.1f\n", 1, 'c', 1.0);
36 ASSERT_EQ(written, 6);
37
38 written = LIBC_NAMESPACE::fprintf(file, "%032b%s\n", 1, "A simple string\n");
39 ASSERT_EQ(written, 49);
40
41 // Check that the server correctly handles divergent numbers of arguments.
42 const char *format = gpu::get_thread_id() % 2 ? "%s" : "%20ld\n";
43 written = LIBC_NAMESPACE::fprintf(file, format, str);
44 ASSERT_EQ(written, gpu::get_thread_id() % 2 ? 16 : 21);
45
46 format = gpu::get_thread_id() % 2 ? "%s" : str;
47 written = LIBC_NAMESPACE::fprintf(file, format, str);
48 ASSERT_EQ(written, 16);
49
50 // Check that we handle null arguments correctly.
51 written = LIBC_NAMESPACE::fprintf(file, "%p", nullptr);
52 ASSERT_EQ(written, 9);
53
54#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
55 written = LIBC_NAMESPACE::fprintf(file, "%s", nullptr);
56 ASSERT_EQ(written, 6);
57#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
58
59 // Check for extremely abused variable width arguments
60 written = LIBC_NAMESPACE::fprintf(file, "%**d", 1, 2, 1.0);
61 ASSERT_EQ(written, 4);
62 written = LIBC_NAMESPACE::fprintf(file, "%**d%6d", 1, 2, 1.0);
63 ASSERT_EQ(written, 10);
64 written = LIBC_NAMESPACE::fprintf(file, "%**.**f", 1, 2, 1.0);
65 ASSERT_EQ(written, 7);
66
67 return 0;
68}
69

source code of libc/test/integration/src/stdio/gpu/printf_test.cpp