• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <string.h>
3 
4 using namespace testing::ext;
5 
6 constexpr size_t BUF_SIZE = 10;
7 
8 class StringStrcpyTest : public testing::Test {
SetUp()9     void SetUp() override {}
TearDown()10     void TearDown() override {}
11 };
12 
13 /**
14  * @tc.name: strcpy_001
15  * @tc.desc: Verify the behavior of the strcpy interface under normal conditions. It asserts that when the strcpy
16  *           function successfully copies the source string to the destination string, the last character of the
17  *           destination string should be the null character \0, and all the characters after it should remain unchanged
18  *           from the original destination string.
19  * @tc.type: FUNC
20  */
21 HWTEST_F(StringStrcpyTest, strcpy_001, TestSize.Level1)
22 {
23     char buffer[BUF_SIZE];
24     char* ptr = strdup("12345");
25     memset(buffer, 'B', sizeof(buffer));
26     EXPECT_TRUE(strcpy(buffer, ptr) == buffer);
27     EXPECT_TRUE(buffer[9] == 'B');
28     EXPECT_TRUE(buffer[8] == 'B');
29     EXPECT_TRUE(buffer[7] == 'B');
30     EXPECT_TRUE(buffer[6] == 'B');
31     free(ptr);
32 }
33 
34 /**
35  * @tc.name: strcpy_002
36  * @tc.desc: Verify the behavior of the strcpy interface when the source string is an empty string. It asserts that when
37  *           the source string is an empty string, the strcpy function should set the destination string to be the null
38  *           character \0, and the returned pointer should point to the starting address of the destination string.
39  * @tc.type: FUNC
40  */
41 HWTEST_F(StringStrcpyTest, strcpy_002, TestSize.Level1)
42 {
43     char buffer[BUF_SIZE];
44     char* ptr = strdup("");
45     EXPECT_TRUE(buffer[0] == '\0');
46     EXPECT_TRUE(strcpy(buffer, ptr) == buffer);
47     free(ptr);
48 }
49 
50 /**
51  * @tc.name: strcpy_003
52  * @tc.desc: Test the behavior with a non-empty source string and verifies that the function can correctly copy the
53  *           source string to the destination string and return the correct pointer.
54  * @tc.type: FUNC
55  */
56 HWTEST_F(StringStrcpyTest, strcpy_003, TestSize.Level1)
57 {
58     char buffer[BUF_SIZE];
59     char* ptr = strdup("147852369");
60     memset(buffer, 'B', sizeof(buffer));
61     EXPECT_TRUE(strcpy(buffer, ptr) == buffer);
62     free(ptr);
63 }