From 60c61c41c2fd0943f4c66d28fcfc09096384fba2 Mon Sep 17 00:00:00 2001 From: resserops Date: Wed, 5 Nov 2025 00:39:16 +0800 Subject: [PATCH] test(leetcode): add test for problem #2 --- .../solution.hpp | 11 ++-- leetcode/0002/test.cpp | 51 +++++++++++++++++++ leetcode/0002/test_suite.yaml | 17 +++++++ leetcode/0002/title.txt | 1 + leetcode/list_node.hpp | 19 +++++++ leetcode/test_utility.hpp | 1 + 6 files changed, 92 insertions(+), 8 deletions(-) rename leetcode/{0002-add-two-numbers => 0002}/solution.hpp (74%) create mode 100644 leetcode/0002/test.cpp create mode 100644 leetcode/0002/test_suite.yaml create mode 100644 leetcode/0002/title.txt diff --git a/leetcode/0002-add-two-numbers/solution.hpp b/leetcode/0002/solution.hpp similarity index 74% rename from leetcode/0002-add-two-numbers/solution.hpp rename to leetcode/0002/solution.hpp index 7cce043..e025475 100644 --- a/leetcode/0002-add-two-numbers/solution.hpp +++ b/leetcode/0002/solution.hpp @@ -1,14 +1,9 @@ -struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; +#pragma once +#include "list_node.hpp" class Solution { public: - ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { + ListNode *addTwoNumbers(const ListNode *l1, const ListNode *l2) { ListNode fake, *p{&fake}; int carry = 0; while (l1 != nullptr || l2 != nullptr || carry != 0) { diff --git a/leetcode/0002/test.cpp b/leetcode/0002/test.cpp new file mode 100644 index 0000000..9623367 --- /dev/null +++ b/leetcode/0002/test.cpp @@ -0,0 +1,51 @@ +#include "solution.hpp" +#include "test_utility.hpp" + +#include + +using namespace testing; +namespace { +using SolutionList = TypeList; +struct TestCase { + TestCase(const YAML::Node &node) { + Decode(node["input"]["l1"], input.l1); + Decode(node["input"]["l2"], input.l2); + Decode(node["expected"], expected); + } + struct { + std::vector l1; + std::vector l2; + } input; + std::vector expected; +}; + +template +void RunTest(const std::vector &l1, const std::vector &l2, const std::vector &expceted) { + // 检查约束条件 + auto assertList = [](const std::vector &l) { + // The number of nodes in each linked list is in the range [1, 100] + ASSERT_THAT(l.size(), AllOf(Ge(1), Le(100))); + ASSERT_THAT(l, Each(AllOf(Ge(0), Le(9)))); // 0 <= Node.val <= 9 + // It is guaranteed that the list represents a number that does not have leading zeros + if (l.size() > 1) { + ASSERT_GT(l.back(), 0); + } + }; + assertList(l1); + assertList(l2); + + // 验证目标函数 + Solution solution; + EXPECT_EQ(FromList(solution.addTwoNumbers(ToList(l1), ToList(l2))), expceted); + ListNode::Clear(); +} + +template +void RunTest(const TestCase &tc) { + RunTest(tc.input.l1, tc.input.l2, tc.expected); +} + +TEST_Y(LeetCode2, Example1); +TEST_Y(LeetCode2, Example2); +TEST_Y(LeetCode2, Example3); +} // namespace diff --git a/leetcode/0002/test_suite.yaml b/leetcode/0002/test_suite.yaml new file mode 100644 index 0000000..456eddc --- /dev/null +++ b/leetcode/0002/test_suite.yaml @@ -0,0 +1,17 @@ +example1: + input: + l1: [2,4,3] + l2: [5,6,4] + expected: [7,0,8] + +example2: + input: + l1: [0] + l2: [0] + expected: [0] + +example3: + input: + l1: [9,9,9,9,9,9,9] + l2: [9,9,9,9] + expected: [8,9,9,9,0,0,0,1] diff --git a/leetcode/0002/title.txt b/leetcode/0002/title.txt new file mode 100644 index 0000000..c4679f9 --- /dev/null +++ b/leetcode/0002/title.txt @@ -0,0 +1 @@ +add-two-numbers diff --git a/leetcode/list_node.hpp b/leetcode/list_node.hpp index e47c6b5..c81a422 100644 --- a/leetcode/list_node.hpp +++ b/leetcode/list_node.hpp @@ -1,5 +1,6 @@ #pragma once #include "tracker.hpp" +#include struct ListNode : public Tracker { int val; @@ -8,3 +9,21 @@ struct ListNode : public Tracker { ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; + +ListNode *ToList(const std::vector &vec) { + ListNode *head{nullptr}; + for (const auto &val : vec | std::views::reverse) { + ListNode *n{new ListNode(val, head)}; + head = n; + } + return head; +} + +std::vector FromList(const ListNode *head) { + std::vector vec; + while (head != nullptr) { + vec.push_back(head->val); + head = head->next; + } + return vec; +} diff --git a/leetcode/test_utility.hpp b/leetcode/test_utility.hpp index 4534698..0ab9679 100644 --- a/leetcode/test_utility.hpp +++ b/leetcode/test_utility.hpp @@ -24,6 +24,7 @@ auto GetTestSuite(const std::string &testSuitePath) { template const TestCase &GetTestCase() { static const auto path{GetTestSuitePath()}; + // TODO(resserops): 每个TestSuite结束后清理 static const auto testSuite{GetTestSuite(path)}; assert(path == GetTestSuitePath()); return testSuite.at(GetTestCaseName());