1 #include <gtest/gtest.h> 2 #include <string.h> 3 4 using namespace testing::ext; 5 6 class StringMemcmpTest : public testing::Test { SetUp()7 void SetUp() override {} TearDown()8 void TearDown() override {} 9 }; 10 11 /** 12 * @tc.name: memcmp_001 13 * @tc.desc: Validate the behavior of the memcmp function when comparing two identical strings, ensuring that memcmp 14 * correctly compares the strings and returns the expected result of 0. 15 * @tc.type: FUNC 16 */ 17 HWTEST_F(StringMemcmpTest, memcmp_001, TestSize.Level1) 18 { 19 const char* str1 = "Hello, world!"; 20 const char* str2 = "Hello, world!"; 21 int result = memcmp(str1, str2, strlen(str1)); 22 EXPECT_EQ(result, 0); 23 } 24 25 /** 26 * @tc.name: memcmp_002 27 * @tc.desc: Validate the behavior of the memcmp function when comparing two strings of different lengths, ensuring 28 * that memcmp correctly compares the strings and returns a result greater than 0. 29 * @tc.type: FUNC 30 */ 31 HWTEST_F(StringMemcmpTest, memcmp_002, TestSize.Level1) 32 { 33 const char* str1 = "Hello, world!"; 34 const char* str2 = "Hello"; 35 int result = memcmp(str1, str2, strlen(str1)); 36 EXPECT_GT(result, 0); 37 } 38 39 /** 40 * @tc.name: memcmp_003 41 * @tc.desc: Validate the behavior of the memcmp function when comparing strings of different lengths, ensuring that it 42 * compares according to the specified length and returns the expected result of 0. 43 * @tc.type: FUNC 44 */ 45 HWTEST_F(StringMemcmpTest, memcmp_003, TestSize.Level1) 46 { 47 const char* str1 = "Hello, world!"; 48 const char* str2 = "Hello"; 49 int result = memcmp(str1, str2, strlen(str2)); 50 EXPECT_EQ(result, 0); 51 } 52 53 /** 54 * @tc.name: memcmp_004 55 * @tc.desc: Validate the behavior of the memcmp function when comparing null pointers or strings with a length of 0, 56 * ensuring that memcmp correctly handles this scenario and returns the expected result of 0. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(StringMemcmpTest, memcmp_004, TestSize.Level1) 60 { 61 const char* str1 = nullptr; 62 const char* str2 = nullptr; 63 int result = memcmp(str1, str2, 0); 64 EXPECT_EQ(result, 0); 65 }