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
39 changes: 39 additions & 0 deletions leetcode/3461/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "test.hpp"
#include "solution.hpp"

using namespace testing;
namespace {
using SolutionList = TypeList<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
Decode(node["input"]["s"], input.s);
Decode(node["expected"], expected);
}
struct {
std::string s;
} input;
bool expected;
};

template <typename Solution>
void RunTest(const std::string &s, const bool expected) {
// 检查约束条件
ASSERT_THAT(s.size(), AllOf(Ge(3), Le(100))); // 3 <= s.length <= 100
ASSERT_THAT(s, Each(Truly(IsDigit))); // s consists of only digits

// 测试目标函数
Solution solution;
EXPECT_EQ(solution.hasSameDigits(s), expected);
}

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

TEST_Y(LeetCode3461, Example1);
TEST_Y(LeetCode3461, Example2);
TEST_Y(LeetCode3461, Regression1);
TEST_Y(LeetCode3461, Regression2);
TEST_Y(LeetCode3461, Regression3);
} // namespace
4 changes: 2 additions & 2 deletions leetcode/3461/case.yaml → leetcode/3461/test_suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ regression1:

regression2:
input:
s: "472371665226339635424494604455723348326857778972516313101962164"
s: "4802870269162891409411160836125514611"
expected: true

regression3:
input:
s: "4802870269162891409411160836125514611"
s: "472371665226339635424494604455723348326857778972516313101962164"
expected: true
1 change: 1 addition & 0 deletions leetcode/3461/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check-if-digits-are-equal-in-string-after-operations-i
2 changes: 2 additions & 0 deletions leetcode/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <filesystem>
#include <ranges>

namespace detail {
std::string GetTestSuitePath() {
std::filesystem::path path{PROBLEM_PATH};
std::string_view suite{testing::UnitTest::GetInstance()->current_test_info()->test_suite_name()};
Expand All @@ -17,3 +18,4 @@ std::string GetTestCaseName() {
name[0] = std::tolower(name[0]);
return name;
}
} // namespace detail
23 changes: 13 additions & 10 deletions leetcode/test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include "gtest/gtest.h"

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

namespace detail {
std::string GetTestSuitePath();
std::string GetTestCaseName();

Expand All @@ -24,17 +25,9 @@ const TestCase &GetTestCase() {
static const auto path{GetTestSuitePath()};
static const auto testSuite{GetTestSuite<TestCase>(path)};
assert(path == GetTestSuitePath());
return testSuite.at(::GetTestCaseName());
return testSuite.at(GetTestCaseName());
}

template <typename T>
void Decode(const YAML::Node &node, T &t) {
t = node.as<std::decay_t<T>>();
}

template <typename...>
struct TypeList;

template <typename>
struct RunTestImpl;
template <template <typename...> typename TypeList, typename... Solutions>
Expand All @@ -44,6 +37,15 @@ struct RunTestImpl<TypeList<Solutions...>> {
(RunTest<Solutions>(args...), ...);
}
};
} // namespace detail

template <typename T>
void Decode(const YAML::Node &node, T &t) {
t = node.as<std::decay_t<T>>();
}

template <typename...>
struct TypeList;

// 用例约束检查
// 编译期计算10^N,简明定义数据范围
Expand All @@ -53,3 +55,4 @@ template <>
constexpr std::intmax_t E<0>{1};

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