diff --git a/leetcode/3461/test.cpp b/leetcode/3461/test.cpp new file mode 100644 index 0000000..02ed0cd --- /dev/null +++ b/leetcode/3461/test.cpp @@ -0,0 +1,39 @@ +#include "test.hpp" +#include "solution.hpp" + +using namespace testing; +namespace { +using SolutionList = TypeList; +struct TestCase { + TestCase(const YAML::Node &node) { + Decode(node["input"]["s"], input.s); + Decode(node["expected"], expected); + } + struct { + std::string s; + } input; + bool expected; +}; + +template +void RunTest(const std::string &s, const bool expected) { + // 检查约束条件 + ASSERT_THAT(s.size(), AllOf(Ge(3), Le(100))); // 3 <= s.length <= 100 + ASSERT_THAT(s, Each(Truly(IsDigit))); // s consists of only digits + + // 测试目标函数 + Solution solution; + EXPECT_EQ(solution.hasSameDigits(s), expected); +} + +template +void RunTest(const TestCase &tc) { + RunTest(tc.input.s, tc.expected); +} + +TEST_Y(LeetCode3461, Example1); +TEST_Y(LeetCode3461, Example2); +TEST_Y(LeetCode3461, Regression1); +TEST_Y(LeetCode3461, Regression2); +TEST_Y(LeetCode3461, Regression3); +} // namespace diff --git a/leetcode/3461/case.yaml b/leetcode/3461/test_suite.yaml similarity index 100% rename from leetcode/3461/case.yaml rename to leetcode/3461/test_suite.yaml index 1ca8b90..9370ddb 100644 --- a/leetcode/3461/case.yaml +++ b/leetcode/3461/test_suite.yaml @@ -15,10 +15,10 @@ regression1: regression2: input: - s: "472371665226339635424494604455723348326857778972516313101962164" + s: "4802870269162891409411160836125514611" expected: true regression3: input: - s: "4802870269162891409411160836125514611" + s: "472371665226339635424494604455723348326857778972516313101962164" expected: true diff --git a/leetcode/3461/title.txt b/leetcode/3461/title.txt new file mode 100644 index 0000000..79338d7 --- /dev/null +++ b/leetcode/3461/title.txt @@ -0,0 +1 @@ +check-if-digits-are-equal-in-string-after-operations-i diff --git a/leetcode/test/test.cpp b/leetcode/test/test.cpp index bb1c9ad..dbb9948 100644 --- a/leetcode/test/test.cpp +++ b/leetcode/test/test.cpp @@ -4,6 +4,7 @@ #include #include +namespace detail { std::string GetTestSuitePath() { std::filesystem::path path{PROBLEM_PATH}; std::string_view suite{testing::UnitTest::GetInstance()->current_test_info()->test_suite_name()}; @@ -17,3 +18,4 @@ std::string GetTestCaseName() { name[0] = std::tolower(name[0]); return name; } +} // namespace detail diff --git a/leetcode/test/test.hpp b/leetcode/test/test.hpp index c13e621..157d3b5 100644 --- a/leetcode/test/test.hpp +++ b/leetcode/test/test.hpp @@ -3,8 +3,9 @@ #include "gtest/gtest.h" #define TEST_Y(suite, name) \ - TEST(suite, name) { ::RunTestImpl::F(::GetTestCase()); } + TEST(suite, name) { ::detail::RunTestImpl::F(::detail::GetTestCase()); } +namespace detail { std::string GetTestSuitePath(); std::string GetTestCaseName(); @@ -24,17 +25,9 @@ const TestCase &GetTestCase() { static const auto path{GetTestSuitePath()}; static const auto testSuite{GetTestSuite(path)}; assert(path == GetTestSuitePath()); - return testSuite.at(::GetTestCaseName()); + return testSuite.at(GetTestCaseName()); } -template -void Decode(const YAML::Node &node, T &t) { - t = node.as>(); -} - -template -struct TypeList; - template struct RunTestImpl; template