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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_compile_options(-Werror -Wall -Wextra)
set(EXECUTABLE_OUTPUT_PATH ${project_dir}/build/bin)

if(ENABLE_ASAN)
Expand All @@ -26,4 +25,5 @@ if(ENABLE_ASAN)
endif()

include(${3rd_cmake_dir}/googletest.cmake)
include(${3rd_cmake_dir}/yaml-cpp.cmake)
add_subdirectory(leetcode)

This file was deleted.

41 changes: 41 additions & 0 deletions leetcode/3346/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "test.hpp"
#include "solution.hpp"

namespace {
using SolutionList = std::tuple<Solution>;
struct TestCase {
TestCase(const YAML::Node &node) {
input.nums = node["input"]["nums"].as<std::vector<int>>();
input.k = node["input"]["k"].as<int>();
input.numOperations = node["input"]["numOperations"].as<int>();
expected = node["expected"].as<int>();
}
struct {
std::vector<int> nums;
int k;
int numOperations;
} input;
int expected;
};

template <typename Solution>
void Expect(const std::vector<int> &nums, const int k, const int numOperations, const int expected) {
Solution solution;
EXPECT_EQ(solution.maxFrequency(nums, k, numOperations), expected);
}

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

TEST_Y(LeetCode3346, Example1);
TEST_Y(LeetCode3346, Example2);
TEST_Y(LeetCode3346, Regression1);
TEST_Y(LeetCode3346, Regression2);
TEST_Y(LeetCode3346, Regression3);
TEST_Y(LeetCode3346, Regression4);
TEST_Y(LeetCode3346, Regression5);
TEST_Y(LeetCode3346, Regression6);
TEST_Y(LeetCode3346, Regression7);
} // namespace

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions leetcode/3346/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maximum-frequency-of-an-element-after-performing-operations-i
2 changes: 1 addition & 1 deletion leetcode/3347/case.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ regression2:
nums: [999999997,999999999,999999999]
k: 999999999
numOperations: 2
expected:
expected: 2

regression3:
input:
Expand Down
15 changes: 14 additions & 1 deletion leetcode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# 构建测试对象库
file(GLOB_RECURSE TEST_SRC "*/test.cpp")
add_library(leetcode_test_o OBJECT ${TEST_SRC})
target_include_directories(leetcode_test_o PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test)

# 启用 LTO(关键!)
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_supported OUTPUT error_msg)
if(lto_supported)
message(STATUS "IPO/LTO supported, enabling -Wodr")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO/LTO not supported: ${error_msg}")
endif()

# 构建测试二进制
add_executable(leetcode_test)
target_link_libraries(leetcode_test PRIVATE leetcode_test_o gtest_main pthread)
target_compile_options(leetcode_test PRIVATE -Werror -Wall -Wextra -Wodr)
target_link_libraries(leetcode_test PRIVATE leetcode_test_o pthread gtest_main yaml-cpp)
15 changes: 15 additions & 0 deletions leetcode/test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "test.hpp"

#include <filesystem>
#include <ranges>

std::string GetTestSuitePath(const char *file) {
std::filesystem::path path{file};
return path.parent_path() / "test_suite.yaml";
}

std::string GetTestCaseName() {
std::string name{testing::UnitTest::GetInstance()->current_test_info()->name()};
std::ranges::transform(name, name.begin(), [](unsigned char c) { return std::tolower(c); });
return name;
}
40 changes: 40 additions & 0 deletions leetcode/test/test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "yaml-cpp/yaml.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#define GET_TEST_CASE() ::GetTestCase<TestCase>(::GetTestSuitePath(__FILE__), ::GetTestCaseName())
#define TEST_Y(suite, name) \
TEST(suite, name) { ::Expect(SolutionList{}, GET_TEST_CASE()); }

std::string GetTestSuitePath(const char *file);
std::string GetTestCaseName();

template <typename TestCase>
auto GetTestSuite(const std::string &testSuitePath) {
std::cout << "Get path: " << testSuitePath << std::endl;
std::unordered_map<std::string, TestCase> testSuite;
auto testCaseFile{YAML::LoadFile(testSuitePath)};
for (const auto &p : testCaseFile) {
std::string key{p.first.template as<std::string>()};
testSuite.emplace(key, p.second);
}
return testSuite;
}

template <typename TestCase>
const TestCase &GetTestCase(const std::string &testSuitePath, const std::string &key) {
static const std::string &path{testSuitePath};
static const auto testSuite{GetTestSuite<TestCase>(path)};
assert(testSuitePath == path);
return testSuite.at(key);
}

template <typename T>
void GetNode(T &t, const YAML::Node &node) {
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)...), ...);
}
9 changes: 9 additions & 0 deletions third_party/cmake/yaml-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if(NOT TARGET yaml-cpp)
execute_process(COMMAND git submodule update --init --recursive ${3rd_dir}/yaml-cpp)

set(YAML_CPP_BUILD_CONTRIB OFF CACHE BOOL "Enable yaml-cpp contrib in library" FORCE)
set(YAML_CPP_BUILD_TOOLS OFF CACHE BOOL "Enable parse tools" FORCE)

include_directories(SYSTEM ${3rd_dir}/yaml-cpp/include)
add_subdirectory(${3rd_dir}/yaml-cpp)
endif()