32
Gitea CTest Workflow / test (push) Waiting to run

This commit is contained in:
2026-06-26 15:04:19 +08:00
parent 4e0f032e14
commit b63fdb775d
3 changed files with 157 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
// This file is generated by mkfile.py
// Date: 2026-06-26
#include <gtest/gtest.h>
#include <solution/32.h>
#include <string>
#include <vector>
class LongestValidParenthesesTest : public ::testing::Test
{
protected:
void AssertLongestValidParentheses(const std::string &input, int expected)
{
std::vector<char> buffer(input.begin(), input.end());
buffer.push_back('\0');
EXPECT_EQ(longestValidParentheses(buffer.data()), expected);
}
};
TEST_F(LongestValidParenthesesTest, Example1)
{
AssertLongestValidParentheses("(()", 2);
}
TEST_F(LongestValidParenthesesTest, Example2)
{
AssertLongestValidParentheses(")()())", 4);
}
TEST_F(LongestValidParenthesesTest, Example3)
{
AssertLongestValidParentheses("", 0);
}
TEST_F(LongestValidParenthesesTest, ReturnsZeroWhenThereIsNoValidPair)
{
AssertLongestValidParentheses("(", 0);
AssertLongestValidParentheses(")", 0);
AssertLongestValidParentheses("((((", 0);
AssertLongestValidParentheses("))))", 0);
AssertLongestValidParentheses(")(", 0);
}
TEST_F(LongestValidParenthesesTest, EntireStringIsValid)
{
AssertLongestValidParentheses("()", 2);
AssertLongestValidParentheses("()()", 4);
AssertLongestValidParentheses("(())", 4);
AssertLongestValidParentheses("(()())", 6);
}
TEST_F(LongestValidParenthesesTest, FindsValidSubstringAfterInvalidPrefix)
{
AssertLongestValidParentheses(")()", 2);
AssertLongestValidParentheses("())(())", 4);
AssertLongestValidParentheses("())()()", 4);
}
TEST_F(LongestValidParenthesesTest, FindsLongestContiguousSubstring)
{
AssertLongestValidParentheses("()(()", 2);
AssertLongestValidParentheses("(()(((()", 2);
AssertLongestValidParentheses("(()())())", 8);
AssertLongestValidParentheses("()(())", 6);
}
TEST_F(LongestValidParenthesesTest, HandlesMultipleSeparatedValidBlocks)
{
AssertLongestValidParentheses("()(()())", 8);
AssertLongestValidParentheses("())(()())", 6);
AssertLongestValidParentheses("(()())())(()(()))", 8);
}