gtest中字符串比较是否相等

1.EXPECT_EQ(val1,val2)

class StringCmpTest:public testing::Test{

};

TEST_F(StringCmpTest, Test_case1){
    string szJC = "JC";
    string szJC1 = "JC";
    EXPECT_EQ(szJC,szJC1);

    char* cJC = "JC";
    char* cJC1 = "JC";
    EXPECT_EQ(cJC,cJC1);

    //前面加L就是将其简单转换成unicode码的字串,要用对应的wcout才能输出
    wchar_t* cTQ = L"TQ";
    wchar_t* cTQ1 = L"TQ";
    EXPECT_EQ(cTQ,cTQ1);
}
  1. EXPECT_STREQ(expected_str, actual_str)

只支持C字符串,char*,wchar_t*,不支持string和wstring,

TEST_F(StringCmpTest,Test_case2){
    char* cJC = "JC";
    char* cJC1 = "JC";

    wchar_t* cTQ = L"TQ";
    wchar_t* cTQ1 = L"TQ";

    EXPECT_STREQ(cJC,cJC1);
    EXPECT_STREQ(cTQ,cTQ1);
}
  1. EXPECT_STRCASEEQ(expected_str, actual_str)

只支持C字符串,char*,不支持string和wstring,不支持wchar_t*类型的

TEST_F(StringCmpTest,Test_case3){
    char* cJC = "JC";
    char* cJC1 = "JC";
    EXPECT_STRCASEEQ(cJC,cJC1);
}

4.使用字符串比较函数,再用EXPECT_EQ()判断返回值

TEST_F(StringCmpTest, Test_case4){
    char* cAB = "AB";
    char* cAB1 = "AB";
    int ret = strcmp(cAB,cAB1);
    EXPECT_EQ(ret,0);
}

TEST_F(StringCmpTest,Test_case5){
    string s1 = "ABC";
    string s2 = "ABC";
    int ret = s1.compare(s2);
    EXPECT_EQ(ret,0);

}

你可能感兴趣的:(gtest)