xref: /llvm-project/libc/test/integration/src/stdio/sprintf_size_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
10e27dfd5SMichael Jones //===-- Unittests for getenv ----------------------------------------------===//
20e27dfd5SMichael Jones //
30e27dfd5SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40e27dfd5SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
50e27dfd5SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60e27dfd5SMichael Jones //
70e27dfd5SMichael Jones //===----------------------------------------------------------------------===//
80e27dfd5SMichael Jones 
90e27dfd5SMichael Jones #include <stddef.h>
100e27dfd5SMichael Jones 
110e27dfd5SMichael Jones #ifndef INTEGRATION_DISABLE_PRINTF
120e27dfd5SMichael Jones #include "src/stdio/sprintf.h"
130e27dfd5SMichael Jones #endif
140e27dfd5SMichael Jones 
15af1315c2SSiva Chandra Reddy #include "test/IntegrationTest/test.h"
160e27dfd5SMichael Jones 
my_streq(const char * lhs,const char * rhs)170e27dfd5SMichael Jones static bool my_streq(const char *lhs, const char *rhs) {
180e27dfd5SMichael Jones   if (lhs == rhs)
190e27dfd5SMichael Jones     return true;
200e27dfd5SMichael Jones   if (((lhs == static_cast<char *>(nullptr)) &&
210e27dfd5SMichael Jones        (rhs != static_cast<char *>(nullptr))) ||
220e27dfd5SMichael Jones       ((lhs != static_cast<char *>(nullptr)) &&
230e27dfd5SMichael Jones        (rhs == static_cast<char *>(nullptr)))) {
240e27dfd5SMichael Jones     return false;
250e27dfd5SMichael Jones   }
260e27dfd5SMichael Jones   const char *l, *r;
270e27dfd5SMichael Jones   for (l = lhs, r = rhs; *l != '\0' && *r != '\0'; ++l, ++r)
280e27dfd5SMichael Jones     if (*l != *r)
290e27dfd5SMichael Jones       return false;
300e27dfd5SMichael Jones 
310e27dfd5SMichael Jones   return *l == '\0' && *r == '\0';
320e27dfd5SMichael Jones }
330e27dfd5SMichael Jones 
my_strlen(const char * str)340e27dfd5SMichael Jones static int my_strlen(const char *str) {
350e27dfd5SMichael Jones   const char *other = str;
360e27dfd5SMichael Jones   while (*other)
370e27dfd5SMichael Jones     ++other;
380e27dfd5SMichael Jones   return static_cast<int>(other - str);
390e27dfd5SMichael Jones }
400e27dfd5SMichael Jones 
TEST_MAIN(int argc,char ** argv,char ** envp)410e27dfd5SMichael Jones TEST_MAIN(int argc, char **argv, char **envp) {
420e27dfd5SMichael Jones   ASSERT_EQ(argc, 5);
430e27dfd5SMichael Jones   ASSERT_TRUE(my_streq(argv[1], "%s %c %d"));
440e27dfd5SMichael Jones   ASSERT_EQ(my_strlen(argv[1]), 8);
450e27dfd5SMichael Jones   ASSERT_TRUE(my_streq(argv[2], "First arg"));
460e27dfd5SMichael Jones   ASSERT_EQ(my_strlen(argv[2]), 9);
470e27dfd5SMichael Jones   ASSERT_TRUE(my_streq(argv[3], "a"));
480e27dfd5SMichael Jones   ASSERT_EQ(my_strlen(argv[3]), 1);
490e27dfd5SMichael Jones   ASSERT_TRUE(my_streq(argv[4], "0"));
500e27dfd5SMichael Jones   ASSERT_EQ(my_strlen(argv[4]), 1);
510e27dfd5SMichael Jones 
520e27dfd5SMichael Jones #ifndef INTEGRATION_DISABLE_PRINTF
530e27dfd5SMichael Jones   char buf[100];
54*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(
55*b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::sprintf(buf, argv[1], argv[2], argv[3][0], argv[4][0]),
560e27dfd5SMichael Jones       14);
570e27dfd5SMichael Jones   ASSERT_TRUE(my_streq(buf, "First arg a 48"));
580e27dfd5SMichael Jones #endif
590e27dfd5SMichael Jones 
600e27dfd5SMichael Jones   return 0;
610e27dfd5SMichael Jones }
62