1 //===-- Unittests for strlcpy ---------------------------------------------===// 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 "src/string/strlcpy.h" 10 #include "utils/UnitTest/Test.h" 11 #include <stdlib.h> 12 13 TEST(LlvmLibcStrlcpyTest, TooBig) { 14 const char *str = "abc"; 15 char buf[2]; 16 EXPECT_EQ(__llvm_libc::strlcpy(buf, str, 2), size_t(3)); 17 EXPECT_STREQ(buf, "a"); 18 19 EXPECT_EQ(__llvm_libc::strlcpy(nullptr, str, 0), size_t(3)); 20 } 21 22 TEST(LlvmLibcStrlcpyTest, Smaller) { 23 const char *str = "abc"; 24 char buf[7]{"111111"}; 25 26 EXPECT_EQ(__llvm_libc::strlcpy(buf, str, 7), size_t(3)); 27 EXPECT_STREQ(buf, "abc"); 28 for (const char *p = buf + 3; p < buf + 7; p++) 29 EXPECT_EQ(*p, '\0'); 30 } 31