75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
// 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);
|
|
}
|