66 lines
1.6 KiB
C++
66 lines
1.6 KiB
C++
#include <gtest/gtest.h>
|
||
#include <solution/392.h>
|
||
|
||
class IsSubsequenceTest : public ::testing::Test
|
||
{
|
||
protected:
|
||
void AssertIsSubsequence(const std::string &s, const std::string &t, bool expected)
|
||
{
|
||
char s_buf[1001], t_buf[1001];
|
||
strncpy(s_buf, s.c_str(), sizeof(s_buf));
|
||
s_buf[1000] = '\0';
|
||
strncpy(t_buf, t.c_str(), sizeof(t_buf));
|
||
t_buf[1000] = '\0';
|
||
bool result = isSubsequence(s_buf, t_buf);
|
||
EXPECT_EQ(result, expected);
|
||
}
|
||
};
|
||
|
||
// 示例 1:s = "abc", t = "ahbgdc" → true
|
||
TEST_F(IsSubsequenceTest, Example1)
|
||
{
|
||
AssertIsSubsequence("abc", "ahbgdc", true);
|
||
}
|
||
|
||
// 示例 2:s = "axc", t = "ahbgdc" → false
|
||
TEST_F(IsSubsequenceTest, Example2)
|
||
{
|
||
AssertIsSubsequence("axc", "ahbgdc", false);
|
||
}
|
||
|
||
// 边界测试:s 为空 → 总是 true
|
||
TEST_F(IsSubsequenceTest, EmptyS)
|
||
{
|
||
AssertIsSubsequence("", "anystring", true);
|
||
}
|
||
|
||
// 边界测试:t 为空 → 只有 s 也为空才为 true
|
||
TEST_F(IsSubsequenceTest, EmptyT)
|
||
{
|
||
AssertIsSubsequence("a", "", false);
|
||
AssertIsSubsequence("", "", true);
|
||
}
|
||
|
||
// s 比 t 长 → false
|
||
TEST_F(IsSubsequenceTest, SLongerThanT)
|
||
{
|
||
AssertIsSubsequence("abcdefg", "abc", false);
|
||
}
|
||
|
||
// s 与 t 完全相同 → true
|
||
TEST_F(IsSubsequenceTest, SameString)
|
||
{
|
||
AssertIsSubsequence("abcde", "abcde", true);
|
||
}
|
||
|
||
// 非连续但顺序正确 → true
|
||
TEST_F(IsSubsequenceTest, SparseSubsequence)
|
||
{
|
||
AssertIsSubsequence("ace", "abcde", true);
|
||
}
|
||
|
||
// 非连续顺序错误 → false
|
||
TEST_F(IsSubsequenceTest, WrongOrder)
|
||
{
|
||
AssertIsSubsequence("aec", "abcde", false);
|
||
} |