xref: /llvm-project/libc/test/integration/src/stdio/sprintf_size_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
1 //===-- Unittests for getenv ----------------------------------------------===//
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 <stddef.h>
10 
11 #ifndef INTEGRATION_DISABLE_PRINTF
12 #include "src/stdio/sprintf.h"
13 #endif
14 
15 #include "test/IntegrationTest/test.h"
16 
my_streq(const char * lhs,const char * rhs)17 static bool my_streq(const char *lhs, const char *rhs) {
18   if (lhs == rhs)
19     return true;
20   if (((lhs == static_cast<char *>(nullptr)) &&
21        (rhs != static_cast<char *>(nullptr))) ||
22       ((lhs != static_cast<char *>(nullptr)) &&
23        (rhs == static_cast<char *>(nullptr)))) {
24     return false;
25   }
26   const char *l, *r;
27   for (l = lhs, r = rhs; *l != '\0' && *r != '\0'; ++l, ++r)
28     if (*l != *r)
29       return false;
30 
31   return *l == '\0' && *r == '\0';
32 }
33 
my_strlen(const char * str)34 static int my_strlen(const char *str) {
35   const char *other = str;
36   while (*other)
37     ++other;
38   return static_cast<int>(other - str);
39 }
40 
TEST_MAIN(int argc,char ** argv,char ** envp)41 TEST_MAIN(int argc, char **argv, char **envp) {
42   ASSERT_EQ(argc, 5);
43   ASSERT_TRUE(my_streq(argv[1], "%s %c %d"));
44   ASSERT_EQ(my_strlen(argv[1]), 8);
45   ASSERT_TRUE(my_streq(argv[2], "First arg"));
46   ASSERT_EQ(my_strlen(argv[2]), 9);
47   ASSERT_TRUE(my_streq(argv[3], "a"));
48   ASSERT_EQ(my_strlen(argv[3]), 1);
49   ASSERT_TRUE(my_streq(argv[4], "0"));
50   ASSERT_EQ(my_strlen(argv[4]), 1);
51 
52 #ifndef INTEGRATION_DISABLE_PRINTF
53   char buf[100];
54   ASSERT_EQ(
55       LIBC_NAMESPACE::sprintf(buf, argv[1], argv[2], argv[3][0], argv[4][0]),
56       14);
57   ASSERT_TRUE(my_streq(buf, "First arg a 48"));
58 #endif
59 
60   return 0;
61 }
62