• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <stdint.h>
3 
4 using namespace testing::ext;
5 
6 constexpr int SIZE = 128;
7 constexpr int ILLEGAL_SIZE = -128;
8 
9 class HookReallocTest : public testing::Test {
SetUp()10     void SetUp() override {}
TearDown()11     void TearDown() override {}
12 };
13 
14 /**
15  * @tc.name: realloc_001
16  * @tc.desc: Validate the behavior of the realloc function when given a null pointer and a size of 0, ensuring that
17  *           realloc behaves correctly in boundary cases and returns a valid pointer.
18  * @tc.type: FUNC
19  */
20 HWTEST_F(HookReallocTest, realloc_001, TestSize.Level1)
21 {
22     void* ptr = realloc(nullptr, 0);
23     ASSERT_NE(ptr, nullptr);
24     free(ptr);
25 }
26 /**
27  * @tc.name: realloc_002
28  * @tc.desc: Validate the behavior of the realloc function when given a null pointer and a non-zero size, ensuring that
29  *           realloc can allocate memory correctly and return a valid pointer.
30  * @tc.type: FUNC
31  */
32 HWTEST_F(HookReallocTest, realloc_002, TestSize.Level1)
33 {
34     void* ptr = realloc(nullptr, SIZE);
35     ASSERT_NE(ptr, nullptr);
36     free(ptr);
37 }
38 /**
39  * @tc.name: realloc_003
40  * @tc.desc: Validate the behavior of the realloc function when attempting to allocate memory with an illegal size,
41  *           ensuring that realloc correctly handles illegal size scenarios and returns a null pointer.
42  * @tc.type: FUNC
43  */
44 HWTEST_F(HookReallocTest, realloc_003, TestSize.Level1)
45 {
46     void* ptr = realloc(nullptr, ILLEGAL_SIZE);
47     EXPECT_EQ(ptr, nullptr);
48 }
49 /**
50  * @tc.name: realloc_004
51  * @tc.desc: Validate the behavior of the realloc function when reallocation is performed on an already allocated memory
52  *           block, ensuring that realloc correctly handles the situation and returns a valid pointer.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(HookReallocTest, realloc_004, TestSize.Level1)
56 {
57     void* ptr = malloc(SIZE);
58     ptr = realloc(ptr, SIZE);
59     ASSERT_NE(ptr, nullptr);
60     free(ptr);
61 }
62 
63 /**
64  * @tc.name: realloc_005
65  * @tc.desc: Verify that the realloc function correctly returns a non-null pointer when expanding the size of the
66  *           original memory block to twice its original size, ensuring its proper functionality.
67  * @tc.type: FUNC
68  */
69 HWTEST_F(HookReallocTest, realloc_005, TestSize.Level1)
70 {
71     void* ptr = malloc(SIZE);
72     ptr = realloc(ptr, SIZE * 2);
73     ASSERT_NE(ptr, nullptr);
74     free(ptr);
75 }
76 
77 /**
78  * @tc.name: realloc_006
79  * @tc.desc: Verify that the realloc function correctly returns a non-null pointer when reducing the size of the
80  *           original memory block by half, ensuring its proper functionality.
81  * @tc.type: FUNC
82  */
83 HWTEST_F(HookReallocTest, realloc_006, TestSize.Level1)
84 {
85     void* ptr = malloc(SIZE);
86     ptr = realloc(ptr, SIZE / 2);
87     ASSERT_NE(ptr, nullptr);
88     free(ptr);
89 }
90