From 265c90e7d2e4cbc1e88e8d6ed7e7e19ca90e1ff6 Mon Sep 17 00:00:00 2001 From: resserops Date: Thu, 30 Oct 2025 15:22:07 +0800 Subject: [PATCH] test(leetcode): simplify input validation --- leetcode/0001/test.cpp | 14 +++++++------- leetcode/0392/solution.hpp | 7 ++++--- leetcode/0392/test.cpp | 20 ++++++++------------ leetcode/2011/test.cpp | 13 ++++++------- leetcode/3346/test.cpp | 16 +++++++--------- leetcode/3347/test.cpp | 16 +++++++--------- leetcode/3461/solution.hpp | 2 +- leetcode/test/test.hpp | 27 ++++++++++++++++++++++----- 8 files changed, 62 insertions(+), 53 deletions(-) diff --git a/leetcode/0001/test.cpp b/leetcode/0001/test.cpp index 3fb2918..e30cf4a 100644 --- a/leetcode/0001/test.cpp +++ b/leetcode/0001/test.cpp @@ -3,7 +3,7 @@ using namespace testing; namespace { -using SolutionList = std::tuple; +using SolutionList = TypeList; struct TestCase { TestCase(const YAML::Node &node) { Decode(node["input"]["nums"], input.nums); @@ -18,11 +18,11 @@ struct TestCase { }; template -void Expect(const std::vector &nums, const int target, const std::vector &expected) { +void RunTest(const std::vector &nums, const int target, const std::vector &expected) { // 检查约束条件 - ASSERT_THAT(nums.size(), AllOf(Ge(2), Le(10000))); // 2 <= nums.length <= 10^4 - ASSERT_THAT(nums, Each(AllOf(Ge(-1000000000), Le(1000000000)))); // -10^9 <= nums[i] <= 10^9 - ASSERT_THAT(target, AllOf(Ge(-1000000000), Le(1000000000))); // -10^9 <= target <= 10^9 + ASSERT_THAT(nums.size(), AllOf(Ge(2), Le(E<4>))); // 2 <= nums.length <= 10^4 + ASSERT_THAT(nums, Each(AllOf(Ge(-E<9>), Le(E<9>)))); // -10^9 <= nums[i] <= 10^9 + ASSERT_THAT(target, AllOf(Ge(-E<9>), Le(E<9>))); // -10^9 <= target <= 10^9 // Only one valid answer exists // TODO(resserops): 补充检查 @@ -37,8 +37,8 @@ void Expect(const std::vector &nums, const int target, const std::vector -void Expect(const TestCase &tc) { - Expect(tc.input.nums, tc.input.target, tc.expected); +void RunTest(const TestCase &tc) { + RunTest(tc.input.nums, tc.input.target, tc.expected); } TEST_Y(LeetCode1, Example1); diff --git a/leetcode/0392/solution.hpp b/leetcode/0392/solution.hpp index f052937..8341eeb 100644 --- a/leetcode/0392/solution.hpp +++ b/leetcode/0392/solution.hpp @@ -4,14 +4,15 @@ struct Solution { static bool isSubsequence(const std::string &s, const std::string &t) { std::size_t ss{s.size()}, ts{t.size()}; - if (s.size() > t.size()) { + if (ss > ts) { return false; } - std::size_t si{0}; - for (std::size_t ti{0}; si < ss && ti < ts; ++ti) { + std::size_t si{0}, ti{0}; + while (si < ss && ti < ts) { if (s[si] == t[ti]) { ++si; } + ++ti; } return si == ss; } diff --git a/leetcode/0392/test.cpp b/leetcode/0392/test.cpp index 4bb8934..b57c67d 100644 --- a/leetcode/0392/test.cpp +++ b/leetcode/0392/test.cpp @@ -3,7 +3,7 @@ using namespace testing; namespace { -using SolutionList = std::tuple; +using SolutionList = TypeList; struct TestCase { TestCase(const YAML::Node &node) { Decode(node["input"]["s"], input.s); @@ -18,16 +18,12 @@ struct TestCase { }; template -void Expect(const std::string &s, const std::string &t, const bool expected) { +void RunTest(const std::string &s, const std::string &t, const bool expected) { // 检查约束条件 - ASSERT_THAT(s.size(), AllOf(Ge(0), Le(100))); // 0 <= s.length <= 100 - ASSERT_THAT(t.size(), AllOf(Ge(0), Le(10000))); // 0 <= t.length <= 10^4 - for (auto ch : s) { // s and t consist only of lowercase English letters - ASSERT_TRUE(std::islower(ch)); - } - for (auto ch : t) { - ASSERT_TRUE(std::islower(ch)); - } + ASSERT_THAT(s.size(), AllOf(Ge(0), Le(100))); // 0 <= s.length <= 100 + ASSERT_THAT(t.size(), AllOf(Ge(0), Le(E<4>))); // 0 <= t.length <= 10^4 + ASSERT_THAT(s, Each(Truly(IsLower))); // s and t consist only of lowercase English letters + ASSERT_THAT(t, Each(Truly(IsLower))); // 测试目标函数 Solution solution; @@ -35,8 +31,8 @@ void Expect(const std::string &s, const std::string &t, const bool expected) { } template -void Expect(const TestCase &tc) { - Expect(tc.input.s, tc.input.t, tc.expected); +void RunTest(const TestCase &tc) { + RunTest(tc.input.s, tc.input.t, tc.expected); } TEST_Y(LeetCode392, Example1); diff --git a/leetcode/2011/test.cpp b/leetcode/2011/test.cpp index 68f525f..67b4bc1 100644 --- a/leetcode/2011/test.cpp +++ b/leetcode/2011/test.cpp @@ -3,7 +3,7 @@ using namespace testing; namespace { -using SolutionList = std::tuple; +using SolutionList = TypeList; struct TestCase { TestCase(const YAML::Node &node) { Decode(node["input"]["operations"], input.operations); @@ -16,12 +16,11 @@ struct TestCase { }; template -void Expect(const std::vector &operations, const int expected) { +void RunTest(const std::vector &operations, const int expected) { // 检查约束条件 ASSERT_THAT(operations.size(), AllOf(Ge(1), Le(100))); // 1 <= operations.length <= 100 - for (const auto &op : operations) { // operations[i] will be either "++X", "X++", "--X", or "X--" - ASSERT_THAT(op, AnyOf(Eq("++X"), Eq("X++"), Eq("--X"), Eq("X--"))); - } + // operations[i] will be either "++X", "X++", "--X", or "X--" + ASSERT_THAT(operations, Each(AnyOf(Eq("++X"), Eq("X++"), Eq("--X"), Eq("X--")))); // 测试目标函数 Solution solution; @@ -29,8 +28,8 @@ void Expect(const std::vector &operations, const int expected) { } template -void Expect(const TestCase &tc) { - Expect(tc.input.operations, tc.expected); +void RunTest(const TestCase &tc) { + RunTest(tc.input.operations, tc.expected); } TEST_Y(LeetCode2011, Example1); diff --git a/leetcode/3346/test.cpp b/leetcode/3346/test.cpp index b6d185b..71117b5 100644 --- a/leetcode/3346/test.cpp +++ b/leetcode/3346/test.cpp @@ -3,7 +3,7 @@ using namespace testing; namespace { -using SolutionList = std::tuple; +using SolutionList = TypeList; struct TestCase { TestCase(const YAML::Node &node) { Decode(node["input"]["nums"], input.nums); @@ -20,13 +20,11 @@ struct TestCase { }; template -void Expect(const std::vector &nums, const int k, const int numOperations, const int expected) { +void RunTest(const std::vector &nums, const int k, const int numOperations, const int expected) { // 检查约束条件 - ASSERT_THAT(nums.size(), AllOf(Ge(1), Le(100000))); // 1 <= nums.length <= 10^5 - for (int n : nums) { // 1 <= nums[i] <= 10^5 - ASSERT_THAT(n, AllOf(Ge(1), Le(100000))); - } - ASSERT_THAT(k, AllOf(Ge(0), Le(100000))); // 0 <= k <= 10^5 + ASSERT_THAT(nums.size(), AllOf(Ge(1), Le(E<5>))); // 1 <= nums.length <= 10^5 + ASSERT_THAT(nums, Each(AllOf(Ge(1), Le(E<5>)))); // 1 <= nums[i] <= 10^5 + ASSERT_THAT(k, AllOf(Ge(0), Le(E<5>))); // 0 <= k <= 10^5 ASSERT_THAT(numOperations, AllOf(Ge(0), Le(nums.size()))); // 0 <= numOperations <= nums.length // 测试目标函数 @@ -35,8 +33,8 @@ void Expect(const std::vector &nums, const int k, const int numOperations, } template -void Expect(const TestCase &tc) { - Expect(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected); +void RunTest(const TestCase &tc) { + RunTest(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected); } TEST_Y(LeetCode3346, Example1); diff --git a/leetcode/3347/test.cpp b/leetcode/3347/test.cpp index 062ea9b..1eeb6fa 100644 --- a/leetcode/3347/test.cpp +++ b/leetcode/3347/test.cpp @@ -3,7 +3,7 @@ using namespace testing; namespace { -using SolutionList = std::tuple; +using SolutionList = TypeList; struct TestCase { TestCase(const YAML::Node &node) { Decode(node["input"]["nums"], input.nums); @@ -20,13 +20,11 @@ struct TestCase { }; template -void Expect(const std::vector &nums, const int k, const int numOperations, const int expected) { +void RunTest(const std::vector &nums, const int k, const int numOperations, const int expected) { // 检查约束条件 - ASSERT_THAT(nums.size(), AllOf(Ge(1), Le(100000))); // 1 <= nums.length <= 10^5 - for (int n : nums) { // 1 <= nums[i] <= 10^5 - ASSERT_THAT(n, AllOf(Ge(1), Le(1000000000))); - } - ASSERT_THAT(k, AllOf(Ge(0), Le(1000000000))); // 0 <= k <= 10^5 + ASSERT_THAT(nums.size(), AllOf(Ge(1), Le(E<5>))); // 1 <= nums.length <= 10^5 + ASSERT_THAT(nums, Each(AllOf(Ge(1), Le(E<9>)))); // 1 <= nums[i] <= 10^9 + ASSERT_THAT(k, AllOf(Ge(0), Le(E<9>))); // 0 <= k <= 10^9 ASSERT_THAT(numOperations, AllOf(Ge(0), Le(nums.size()))); // 0 <= numOperations <= nums.length // 测试目标函数 @@ -35,8 +33,8 @@ void Expect(const std::vector &nums, const int k, const int numOperations, } template -void Expect(const TestCase &tc) { - Expect(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected); +void RunTest(const TestCase &tc) { + RunTest(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected); } TEST_Y(LeetCode3347, Example1); diff --git a/leetcode/3461/solution.hpp b/leetcode/3461/solution.hpp index d53be6f..e52db2a 100644 --- a/leetcode/3461/solution.hpp +++ b/leetcode/3461/solution.hpp @@ -1,7 +1,7 @@ #include struct Solution { - bool hasSameDigits(std::string s) { + static bool hasSameDigits(std::string s) { while (s.size() > 2) { for (std::size_t i{0}; i < s.size() - 1; ++i) { s[i] = ((s[i] - '0') + (s[i + 1] - '0')) % 10 + '0'; diff --git a/leetcode/test/test.hpp b/leetcode/test/test.hpp index 2fa8d3a..c13e621 100644 --- a/leetcode/test/test.hpp +++ b/leetcode/test/test.hpp @@ -3,7 +3,7 @@ #include "gtest/gtest.h" #define TEST_Y(suite, name) \ - TEST(suite, name) { ::Expect(SolutionList{}, ::GetTestCase()); } + TEST(suite, name) { ::RunTestImpl::F(::GetTestCase()); } std::string GetTestSuitePath(); std::string GetTestCaseName(); @@ -32,7 +32,24 @@ void Decode(const YAML::Node &node, T &t) { t = node.as>(); } -template