Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions leetcode/0392-is-subsequence/test.cpp

This file was deleted.

File renamed without changes.
44 changes: 44 additions & 0 deletions leetcode/0392/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "test.hpp"
#include "solution.hpp"

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
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 <typename Solution>
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 <typename Solution>
void Expect(const TestCase &tc) {
Expect<Solution>(tc.input.s, tc.input.t, tc.expected);
}

TEST_Y(LeetCode392, Example1);
TEST_Y(LeetCode392, Example2);
} // namespace
11 changes: 11 additions & 0 deletions leetcode/0392/test_suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
example1:
input:
s: "abc"
t: "ahbgdc"
expected: true

example2:
input:
s: "axc"
t: "ahbgdc"
expected: false
1 change: 1 addition & 0 deletions leetcode/0392/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
is-subsequence
8 changes: 8 additions & 0 deletions leetcode/2011/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "test.hpp"
#include "solution.hpp"

using namespace testing;
namespace {
using SolutionList = std::tuple<approach1::Solution, approach2::Solution>;
struct TestCase {
Expand All @@ -16,6 +17,13 @@ struct TestCase {

template <typename Solution>
void Expect(const std::vector<std::string> &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);
}
Expand Down
10 changes: 10 additions & 0 deletions leetcode/3346/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "test.hpp"
#include "solution.hpp"

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
struct TestCase {
Expand All @@ -20,6 +21,15 @@ struct TestCase {

template <typename Solution>
void Expect(const std::vector<int> &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);
}
Expand Down
10 changes: 10 additions & 0 deletions leetcode/3347/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "test.hpp"
#include "solution.hpp"

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
struct TestCase {
Expand All @@ -20,6 +21,15 @@ struct TestCase {

template <typename Solution>
void Expect(const std::vector<int> &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);
}
Expand Down
2 changes: 1 addition & 1 deletion leetcode/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down