diff --git a/leetcode/0392-is-subsequence/test.cpp b/leetcode/0392-is-subsequence/test.cpp deleted file mode 100644 index e89a95b..0000000 --- a/leetcode/0392-is-subsequence/test.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "solution.hpp" -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using namespace ::testing; - -template -void Expect(const bool expected, const std::string &s, const std::string &t) { - // 检查约束条件 - 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 - - // 测试目标函数 - Solution solution; - EXPECT_EQ(expected, solution.isSubsequence(s, t)); -} - -TEST(LeetCode392, Example1) { Expect(true, "abc", "ahbgdc"); } - -TEST(LeetCode392, Example2) { Expect(false, "axc", "ahbgdc"); } diff --git a/leetcode/0392-is-subsequence/solution.hpp b/leetcode/0392/solution.hpp similarity index 100% rename from leetcode/0392-is-subsequence/solution.hpp rename to leetcode/0392/solution.hpp diff --git a/leetcode/0392/test.cpp b/leetcode/0392/test.cpp new file mode 100644 index 0000000..47a1f06 --- /dev/null +++ b/leetcode/0392/test.cpp @@ -0,0 +1,44 @@ +#include "test.hpp" +#include "solution.hpp" + +using namespace testing; +namespace { +using SolutionList = std::tuple; +struct TestCase { + TestCase(const YAML::Node &node) { + Decode(node["input"]["s"], input.s); + Decode(node["input"]["t"], input.t); + Decode(node["expected"], expected); + } + struct { + std::string s; + std::string t; + } input; + bool expected; +}; + +template +void Expect(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)); + } + + // 测试目标函数 + Solution solution; + EXPECT_EQ(solution.isSubsequence(s, t), expected); +} + +template +void Expect(const TestCase &tc) { + Expect(tc.input.s, tc.input.t, tc.expected); +} + +TEST_Y(LeetCode392, Example1); +TEST_Y(LeetCode392, Example2); +} // namespace diff --git a/leetcode/0392/test_suite.yaml b/leetcode/0392/test_suite.yaml new file mode 100644 index 0000000..795a751 --- /dev/null +++ b/leetcode/0392/test_suite.yaml @@ -0,0 +1,11 @@ +example1: + input: + s: "abc" + t: "ahbgdc" + expected: true + +example2: + input: + s: "axc" + t: "ahbgdc" + expected: false diff --git a/leetcode/0392/title.txt b/leetcode/0392/title.txt new file mode 100644 index 0000000..e08c830 --- /dev/null +++ b/leetcode/0392/title.txt @@ -0,0 +1 @@ +is-subsequence diff --git a/leetcode/2011/test.cpp b/leetcode/2011/test.cpp index 0832b17..68f525f 100644 --- a/leetcode/2011/test.cpp +++ b/leetcode/2011/test.cpp @@ -1,6 +1,7 @@ #include "test.hpp" #include "solution.hpp" +using namespace testing; namespace { using SolutionList = std::tuple; struct TestCase { @@ -16,6 +17,13 @@ struct TestCase { template void Expect(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--"))); + } + + // 测试目标函数 Solution solution; EXPECT_EQ(solution.finalValueAfterOperations(operations), expected); } diff --git a/leetcode/3346/test.cpp b/leetcode/3346/test.cpp index 644dcf2..b6d185b 100644 --- a/leetcode/3346/test.cpp +++ b/leetcode/3346/test.cpp @@ -1,6 +1,7 @@ #include "test.hpp" #include "solution.hpp" +using namespace testing; namespace { using SolutionList = std::tuple; struct TestCase { @@ -20,6 +21,15 @@ struct TestCase { template void Expect(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(numOperations, AllOf(Ge(0), Le(nums.size()))); // 0 <= numOperations <= nums.length + + // 测试目标函数 Solution solution; EXPECT_EQ(solution.maxFrequency(nums, k, numOperations), expected); } diff --git a/leetcode/3347/test.cpp b/leetcode/3347/test.cpp index 3d2ee3f..062ea9b 100644 --- a/leetcode/3347/test.cpp +++ b/leetcode/3347/test.cpp @@ -1,6 +1,7 @@ #include "test.hpp" #include "solution.hpp" +using namespace testing; namespace { using SolutionList = std::tuple; struct TestCase { @@ -20,6 +21,15 @@ struct TestCase { template void Expect(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(numOperations, AllOf(Ge(0), Le(nums.size()))); // 0 <= numOperations <= nums.length + + // 测试目标函数 Solution solution; EXPECT_EQ(solution.maxFrequency(nums, k, numOperations), expected); } diff --git a/leetcode/test/test.cpp b/leetcode/test/test.cpp index 49e0a0f..bb1c9ad 100644 --- a/leetcode/test/test.cpp +++ b/leetcode/test/test.cpp @@ -9,7 +9,7 @@ std::string GetTestSuitePath() { std::string_view suite{testing::UnitTest::GetInstance()->current_test_info()->test_suite_name()}; std::string_view num{ suite.substr(std::distance(suite.begin(), std::find_if(suite.begin(), suite.end(), ::isdigit)))}; - return path / num / "test_suite.yaml"; + return path / (std::string(4 - num.size(), '0') + std::string(num)) / "test_suite.yaml"; } std::string GetTestCaseName() {