1 #include <errno.h> 2 #include <gtest/gtest.h> 3 #include <malloc.h> 4 #include <stdint.h> 5 6 using namespace testing::ext; 7 8 class MallocMallocTest : public testing::Test { SetUp()9 void SetUp() override {} TearDown()10 void TearDown() override {} 11 }; 12 13 constexpr size_t LEN = 50; 14 constexpr size_t SIZE = 200; 15 16 /** 17 * @tc.name: malloc_001 18 * @tc.desc: This test verifies whether the behavior of the malloc function meets expectations, ensuring that the memory 19 * pointer allocated by malloc is not empty and that the allocated memory size meets the expected 20 * requirements. 21 * @tc.type: FUNC 22 */ 23 HWTEST_F(MallocMallocTest, malloc_001, TestSize.Level1) 24 { 25 void* memoryPtr = malloc(LEN); 26 ASSERT_NE(memoryPtr, nullptr); 27 EXPECT_EQ(malloc_usable_size(memoryPtr) >= 50U, true); 28 free(memoryPtr); 29 } 30 31 /** 32 * @tc.name: malloc_002 33 * @tc.desc: This test verifies that after using the malloc function to allocate a memory block of size SIZE, the 34 * available size of the memory block can be correctly obtained to be 200 bytes. 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(MallocMallocTest, malloc_002, TestSize.Level1) 38 { 39 void* block = malloc(SIZE); 40 ASSERT_NE(block, nullptr); 41 EXPECT_LE(SIZE, malloc_usable_size(block)); 42 free(block); 43 } 44 45 /** 46 * @tc.name: malloc_003 47 * @tc.desc: This test verifies that when using the malloc function to allocate a memory block of size 0, the returned 48 * pointer is not empty and the memory block can be successfully released. 49 * @tc.type: FUNC 50 */ 51 HWTEST_F(MallocMallocTest, malloc_003, TestSize.Level1) 52 { 53 void* memoryBlock = malloc(0); 54 ASSERT_NE(memoryBlock, nullptr); 55 free(memoryBlock); 56 }