| 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 | |
| 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 | |
| 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 | |
| 41 | TEST_MAIN(int argc, char **argv, char **envp) { |
| 42 | ASSERT_EQ(argc, 5); |
| 43 | ASSERT_TRUE(my_streq(lhs: argv[1], rhs: "%s %c %d" )); |
| 44 | ASSERT_EQ(my_strlen(str: argv[1]), 8); |
| 45 | ASSERT_TRUE(my_streq(lhs: argv[2], rhs: "First arg" )); |
| 46 | ASSERT_EQ(my_strlen(str: argv[2]), 9); |
| 47 | ASSERT_TRUE(my_streq(lhs: argv[3], rhs: "a" )); |
| 48 | ASSERT_EQ(my_strlen(str: argv[3]), 1); |
| 49 | ASSERT_TRUE(my_streq(lhs: argv[4], rhs: "0" )); |
| 50 | ASSERT_EQ(my_strlen(str: 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(lhs: buf, rhs: "First arg a 48" )); |
| 58 | #endif |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |