This commit is contained in:
2025-07-18 22:23:57 +08:00
parent 2a7d5ed0cc
commit 6df3e30818
3 changed files with 79 additions and 0 deletions

51
tests/test_387.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include <gtest/gtest.h>
#include <solution/387.h>
class FirstUniqCharTest : public ::testing::Test
{
protected:
void AssertFirstUniqChar(const std::string &input, int expected)
{
char buf[1001];
strncpy(buf, input.c_str(), sizeof(buf));
buf[1000] = '\0'; // 防止越界
int result = firstUniqChar(buf);
EXPECT_EQ(result, expected);
}
};
// 示例 1leetcode → 'l' 是第一个唯一字符,索引 0
TEST_F(FirstUniqCharTest, Example1)
{
AssertFirstUniqChar("leetcode", 0);
}
// 示例 2loveleetcode → 'v' 是第一个唯一字符,索引 2
TEST_F(FirstUniqCharTest, Example2)
{
AssertFirstUniqChar("loveleetcode", 2);
}
// 示例 3aabb → 没有唯一字符,返回 -1
TEST_F(FirstUniqCharTest, Example3)
{
AssertFirstUniqChar("aabb", -1);
}
// 边界测试:空字符串
TEST_F(FirstUniqCharTest, EmptyString)
{
AssertFirstUniqChar("", -1);
}
// 边界测试:单字符
TEST_F(FirstUniqCharTest, SingleChar)
{
AssertFirstUniqChar("z", 0);
}
// 更多测试:末尾唯一字符
TEST_F(FirstUniqCharTest, UniqueAtEnd)
{
AssertFirstUniqChar("aabbc", 4); // 'c' 是唯一字符
}