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
14 changes: 7 additions & 7 deletions leetcode/0001/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
using SolutionList = TypeList<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["nums"], input.nums);
Expand All @@ -18,11 +18,11 @@ struct TestCase {
};

template <typename Solution>
void Expect(const std::vector<int> &nums, const int target, const std::vector<int> &expected) {
void RunTest(const std::vector<int> &nums, const int target, const std::vector<int> &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): 补充检查

Expand All @@ -37,8 +37,8 @@ void Expect(const std::vector<int> &nums, const int target, const std::vector<in
}

template <typename Solution>
void Expect(const TestCase &tc) {
Expect<Solution>(tc.input.nums, tc.input.target, tc.expected);
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.nums, tc.input.target, tc.expected);
}

TEST_Y(LeetCode1, Example1);
Expand Down
7 changes: 4 additions & 3 deletions leetcode/0392/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 8 additions & 12 deletions leetcode/0392/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
using SolutionList = TypeList<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["s"], input.s);
Expand All @@ -18,25 +18,21 @@ struct TestCase {
};

template <typename Solution>
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;
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);
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.s, tc.input.t, tc.expected);
}

TEST_Y(LeetCode392, Example1);
Expand Down
13 changes: 6 additions & 7 deletions leetcode/2011/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace testing;
namespace {
using SolutionList = std::tuple<approach1::Solution, approach2::Solution>;
using SolutionList = TypeList<approach1::Solution, approach2::Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["operations"], input.operations);
Expand All @@ -16,21 +16,20 @@ struct TestCase {
};

template <typename Solution>
void Expect(const std::vector<std::string> &operations, const int expected) {
void RunTest(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--")));
}
// 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;
EXPECT_EQ(solution.finalValueAfterOperations(operations), expected);
}

template <typename Solution>
void Expect(const TestCase &tc) {
Expect<Solution>(tc.input.operations, tc.expected);
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.operations, tc.expected);
}

TEST_Y(LeetCode2011, Example1);
Expand Down
16 changes: 7 additions & 9 deletions leetcode/3346/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
using SolutionList = TypeList<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["nums"], input.nums);
Expand All @@ -20,13 +20,11 @@ struct TestCase {
};

template <typename Solution>
void Expect(const std::vector<int> &nums, const int k, const int numOperations, const int expected) {
void RunTest(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(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

// 测试目标函数
Expand All @@ -35,8 +33,8 @@ void Expect(const std::vector<int> &nums, const int k, const int numOperations,
}

template <typename Solution>
void Expect(const TestCase &tc) {
Expect<Solution>(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected);
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected);
}

TEST_Y(LeetCode3346, Example1);
Expand Down
16 changes: 7 additions & 9 deletions leetcode/3347/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace testing;
namespace {
using SolutionList = std::tuple<Solution>;
using SolutionList = TypeList<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["nums"], input.nums);
Expand All @@ -20,13 +20,11 @@ struct TestCase {
};

template <typename Solution>
void Expect(const std::vector<int> &nums, const int k, const int numOperations, const int expected) {
void RunTest(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(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

// 测试目标函数
Expand All @@ -35,8 +33,8 @@ void Expect(const std::vector<int> &nums, const int k, const int numOperations,
}

template <typename Solution>
void Expect(const TestCase &tc) {
Expect<Solution>(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected);
void RunTest(const TestCase &tc) {
RunTest<Solution>(tc.input.nums, tc.input.k, tc.input.numOperations, tc.expected);
}

TEST_Y(LeetCode3347, Example1);
Expand Down
2 changes: 1 addition & 1 deletion leetcode/3461/solution.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>

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';
Expand Down
27 changes: 22 additions & 5 deletions leetcode/test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "gtest/gtest.h"

#define TEST_Y(suite, name) \
TEST(suite, name) { ::Expect(SolutionList{}, ::GetTestCase<TestCase>()); }
TEST(suite, name) { ::RunTestImpl<SolutionList>::F(::GetTestCase<TestCase>()); }

std::string GetTestSuitePath();
std::string GetTestCaseName();
Expand Down Expand Up @@ -32,7 +32,24 @@ void Decode(const YAML::Node &node, T &t) {
t = node.as<std::decay_t<T>>();
}

template <template <typename...> typename Ts, typename... Solutions, typename... Args>
void Expect(Ts<Solutions...> &&, Args &&...args) {
(Expect<Solutions>(std::forward<Args>(args)...), ...);
}
template <typename...>
struct TypeList;

template <typename>
struct RunTestImpl;
template <template <typename...> typename TypeList, typename... Solutions>
struct RunTestImpl<TypeList<Solutions...>> {
template <typename... Args>
static void F(const Args &...args) {
(RunTest<Solutions>(args...), ...);
}
};

// 用例约束检查
// 编译期计算10^N,简明定义数据范围
template <unsigned N>
constexpr std::intmax_t E{10 * E<N - 1>};
template <>
constexpr std::intmax_t E<0>{1};

constexpr bool IsLower(char c) noexcept { return 'a' <= c && c <= 'z'; }