Skip to content

Commit d6ec7f1

Browse files
authored
migrated to gtest (#376)
* migrated to gtest * CR fixes * macos and warnings fixes * disabled some tests due to abi bug on macos * warning fix * temporary test * disable warning for macos * removed warning suppression * base clang tidy setup
1 parent edb4a09 commit d6ec7f1

17 files changed

Lines changed: 1183 additions & 509 deletions

.clang-tidy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
# Soft-launch check set: high-signal, low-noise.
3+
# Add modernize-*/cppcoreguidelines-*/readability-* later once the baseline is clean.
4+
Checks: >
5+
-*,
6+
bugprone-*,
7+
clang-analyzer-*,
8+
performance-*,
9+
portability-*,
10+
misc-*,
11+
-bugprone-easily-swappable-parameters,
12+
-misc-non-private-member-variables-in-classes,
13+
-misc-no-recursion,
14+
-misc-use-anonymous-namespace
15+
16+
WarningsAsErrors: ''
17+
HeaderFilterRegex: '^(?!.*/(BUILD|build.*|_deps|tests/gtest/_deps)/).*\.(hh|ixx)$'
18+
FormatStyle: file
19+
...

CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ else()
7676
message(WARNING "clang-format not found, source code formatting targets will not be available")
7777
endif()
7878

79+
# enable compile_commands.json so clang-tidy can pick up per-TU flags (ignored by MSVC generator)
80+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
81+
7982
add_executable(redumper)
8083
target_sources(redumper
8184
PUBLIC
@@ -217,6 +220,10 @@ endif()
217220

218221
target_link_libraries(redumper ${libs})
219222

223+
if(REDUMPER_TARGET_LINUX)
224+
target_link_options(redumper PRIVATE "-static")
225+
endif()
226+
220227
install(TARGETS redumper DESTINATION "bin")
221228

222229
# bundle LLVM libc++ for C++20 support (system libc++ is too old, static linking not allowed on macOS)
@@ -254,3 +261,61 @@ endif()
254261
enable_testing()
255262

256263
add_subdirectory("tests")
264+
265+
# static analysis (clang-tidy)
266+
find_program(CLANG_TIDY NAMES clang-tidy clang-tidy-18)
267+
find_program(RUN_CLANG_TIDY NAMES run-clang-tidy run-clang-tidy-18 run-clang-tidy.py)
268+
269+
if(CLANG_TIDY AND CMAKE_EXPORT_COMPILE_COMMANDS)
270+
# only exclude vendored sources pulled in by FetchContent (googletest)
271+
set(TIDY_FILE_FILTER "^(?!.*/(BUILD|build.*|_deps|tests/gtest/_deps)/).*\\.(cc|ixx)$")
272+
273+
# collect every project target so all module BMIs are built before clang-tidy runs;
274+
# otherwise per-TU compile commands fail with "module file not found" / missing modmap files
275+
get_property(_tidy_root_targets DIRECTORY . PROPERTY BUILDSYSTEM_TARGETS)
276+
get_property(_tidy_gtest_targets DIRECTORY tests/gtest PROPERTY BUILDSYSTEM_TARGETS)
277+
set(_tidy_deps redumper)
278+
foreach(_t IN LISTS _tidy_root_targets _tidy_gtest_targets)
279+
if(TARGET ${_t})
280+
get_target_property(_type ${_t} TYPE)
281+
if(_type STREQUAL "EXECUTABLE" OR _type STREQUAL "STATIC_LIBRARY" OR _type STREQUAL "SHARED_LIBRARY" OR _type STREQUAL "MODULE_LIBRARY" OR _type STREQUAL "OBJECT_LIBRARY")
282+
list(APPEND _tidy_deps ${_t})
283+
endif()
284+
endif()
285+
endforeach()
286+
list(REMOVE_DUPLICATES _tidy_deps)
287+
288+
if(RUN_CLANG_TIDY)
289+
add_custom_target(tidy
290+
COMMAND ${RUN_CLANG_TIDY} -p ${CMAKE_BINARY_DIR}
291+
-clang-tidy-binary ${CLANG_TIDY}
292+
-quiet
293+
"${TIDY_FILE_FILTER}"
294+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
295+
COMMENT "Running clang-tidy (report only)"
296+
VERBATIM)
297+
add_custom_target(tidy-fix
298+
COMMAND ${RUN_CLANG_TIDY} -p ${CMAKE_BINARY_DIR}
299+
-clang-tidy-binary ${CLANG_TIDY}
300+
-fix -format
301+
"${TIDY_FILE_FILTER}"
302+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
303+
COMMENT "Running clang-tidy with --fix"
304+
VERBATIM)
305+
else()
306+
message(STATUS "run-clang-tidy not found, falling back to single-process clang-tidy targets")
307+
# fallback: invoke clang-tidy directly per-file via compile_commands.json
308+
# (slower; install run-clang-tidy for parallelism)
309+
add_custom_target(tidy
310+
COMMAND ${CMAKE_COMMAND} -E echo "Run: clang-tidy -p ${CMAKE_BINARY_DIR} <file> for any source listed in compile_commands.json")
311+
endif()
312+
313+
add_dependencies(tidy ${_tidy_deps})
314+
if(TARGET tidy-fix)
315+
add_dependencies(tidy-fix ${_tidy_deps})
316+
endif()
317+
else()
318+
if(NOT CLANG_TIDY)
319+
message(STATUS "clang-tidy not found, static analysis targets will not be available")
320+
endif()
321+
endif()

cmake/toolchains/linux-arm64.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
set(CMAKE_SYSTEM_NAME Linux)
22
set(CMAKE_CXX_COMPILER "clang++-18")
3-
4-
# link libstdc++ statically
5-
set(CMAKE_EXE_LINKER_FLAGS_INIT "-static")

cmake/toolchains/linux-x64.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
set(CMAKE_SYSTEM_NAME Linux)
22
set(CMAKE_CXX_COMPILER "clang++-18")
3-
4-
# link libstdc++ statically
5-
set(CMAKE_EXE_LINKER_FLAGS_INIT "-static")

cmake/toolchains/linux-x86.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@ set(CMAKE_SYSTEM_NAME Linux)
22
set(CMAKE_CXX_COMPILER "clang++-18")
33

44
set(CMAKE_CXX_COMPILER_TARGET "i686-linux-gnu")
5-
6-
# link libstdc++ statically
7-
set(CMAKE_EXE_LINKER_FLAGS_INIT "-static")

filesystem/iso9660/iso9660_defs.ixx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,9 @@ std::string identifier_to_string(const uint16_t (&identifier)[N])
381381
for(size_t i = 0; i < N; i++)
382382
utf16_identifier[i] = endian_swap(identifier[i]);
383383

384-
#ifdef __GNUC__
385-
#pragma GCC diagnostic push
386-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
387-
#endif
388-
389384
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert("", u"");
390-
391-
#ifdef __GNUC__
392-
#pragma GCC diagnostic pop
393-
#endif
394-
395385
std::string utf8_identifier = convert.to_bytes(utf16_identifier, &utf16_identifier[N - 1]);
386+
396387
return trim(utf8_identifier).c_str();
397388
}
398389

tests/CMakeLists.txt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,3 @@ endif()
1010
FetchContent_MakeAvailable(googletest)
1111

1212
add_subdirectory("gtest")
13-
14-
# DEPRECATED: will migrate to googletest
15-
add_executable(tests)
16-
target_include_directories(tests PUBLIC ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/utils")
17-
target_sources(tests
18-
PUBLIC
19-
"tests.cc"
20-
PUBLIC
21-
FILE_SET cxx_modules
22-
TYPE CXX_MODULES
23-
BASE_DIRS
24-
"${CMAKE_SOURCE_DIR}"
25-
FILES
26-
"${CMAKE_SOURCE_DIR}/range.ixx"
27-
"${CMAKE_SOURCE_DIR}/cd/cd.ixx"
28-
"${CMAKE_SOURCE_DIR}/cd/cdrom.ixx"
29-
"${CMAKE_SOURCE_DIR}/cd/cd_edc.ixx"
30-
"${CMAKE_SOURCE_DIR}/cd/cd_scrambler.ixx"
31-
"${CMAKE_SOURCE_DIR}/crc/crc.ixx"
32-
"${CMAKE_SOURCE_DIR}/crc/crc16_gsm.ixx"
33-
"${CMAKE_SOURCE_DIR}/crc/crc32.ixx"
34-
"${CMAKE_SOURCE_DIR}/dvd/dvd_edc.ixx"
35-
"${CMAKE_SOURCE_DIR}/utils/file_io.ixx"
36-
"${CMAKE_SOURCE_DIR}/utils/misc.ixx"
37-
"${CMAKE_SOURCE_DIR}/utils/strings.ixx"
38-
)
39-
add_test(NAME tests COMMAND tests WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

tests/gtest/CMakeLists.txt

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,89 @@
1-
add_executable(gtest_interval_set)
1+
include(GoogleTest)
2+
3+
4+
# add_gtest(<target> SOURCE <test.cc> MODULES <module1.ixx> [module2.ixx ...] [WORKING_DIRECTORY <dir>])
5+
function(add_gtest target)
6+
cmake_parse_arguments(ARG "" "SOURCE;WORKING_DIRECTORY" "MODULES" ${ARGN})
7+
8+
add_executable(${target})
9+
target_include_directories(${target} PRIVATE ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/utils")
10+
target_sources(${target}
11+
PRIVATE
12+
${ARG_SOURCE}
13+
PRIVATE
14+
FILE_SET cxx_modules
15+
TYPE CXX_MODULES
16+
BASE_DIRS
17+
"${CMAKE_SOURCE_DIR}"
18+
FILES
19+
${ARG_MODULES}
20+
)
21+
target_link_libraries(${target} PRIVATE GTest::gtest_main)
22+
23+
if(ARG_WORKING_DIRECTORY)
24+
gtest_discover_tests(${target} WORKING_DIRECTORY "${ARG_WORKING_DIRECTORY}" DISCOVERY_MODE PRE_TEST)
25+
else()
26+
gtest_discover_tests(${target} DISCOVERY_MODE PRE_TEST)
27+
endif()
28+
endfunction()
29+
230

3-
target_include_directories(gtest_interval_set PUBLIC ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/utils")
4-
target_sources(gtest_interval_set
5-
PUBLIC
6-
"test_interval_set.cc"
7-
PUBLIC
8-
FILE_SET cxx_modules
9-
TYPE CXX_MODULES
10-
BASE_DIRS
11-
"${CMAKE_SOURCE_DIR}"
12-
FILES
13-
"${CMAKE_SOURCE_DIR}/interval_set.ixx"
31+
add_gtest(gtest_interval_set
32+
SOURCE "test_interval_set.cc"
33+
MODULES "${CMAKE_SOURCE_DIR}/interval_set.ixx"
1434
)
1535

16-
target_link_libraries(gtest_interval_set GTest::gtest_main)
36+
add_gtest(gtest_scale
37+
SOURCE "test_scale.cc"
38+
MODULES "${CMAKE_SOURCE_DIR}/utils/misc.ixx"
39+
)
1740

18-
include(GoogleTest)
19-
gtest_discover_tests(gtest_interval_set DISCOVERY_MODE PRE_TEST)
41+
add_gtest(gtest_lbamsf
42+
SOURCE "test_lbamsf.cc"
43+
MODULES
44+
"${CMAKE_SOURCE_DIR}/cd/cd.ixx"
45+
)
46+
47+
add_gtest(gtest_bcd
48+
SOURCE "test_bcd.cc"
49+
MODULES
50+
"${CMAKE_SOURCE_DIR}/cd/cd.ixx"
51+
)
52+
53+
add_gtest(gtest_unscramble
54+
SOURCE "test_unscramble.cc"
55+
MODULES
56+
"${CMAKE_SOURCE_DIR}/cd/cd.ixx"
57+
"${CMAKE_SOURCE_DIR}/cd/cdrom.ixx"
58+
"${CMAKE_SOURCE_DIR}/cd/cd_scrambler.ixx"
59+
"${CMAKE_SOURCE_DIR}/utils/file_io.ixx"
60+
"${CMAKE_SOURCE_DIR}/utils/misc.ixx"
61+
"${CMAKE_SOURCE_DIR}/utils/strings.ixx"
62+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.."
63+
)
64+
65+
add_gtest(gtest_crc
66+
SOURCE "test_crc.cc"
67+
MODULES
68+
"${CMAKE_SOURCE_DIR}/crc/crc.ixx"
69+
"${CMAKE_SOURCE_DIR}/crc/crc16_gsm.ixx"
70+
"${CMAKE_SOURCE_DIR}/crc/crc32.ixx"
71+
"${CMAKE_SOURCE_DIR}/cd/cd_edc.ixx"
72+
"${CMAKE_SOURCE_DIR}/dvd/dvd_edc.ixx"
73+
"${CMAKE_SOURCE_DIR}/utils/misc.ixx"
74+
)
75+
76+
add_gtest(gtest_range
77+
SOURCE "test_range.cc"
78+
MODULES "${CMAKE_SOURCE_DIR}/range.ixx"
79+
)
80+
81+
add_gtest(gtest_misc
82+
SOURCE "test_misc.cc"
83+
MODULES "${CMAKE_SOURCE_DIR}/utils/misc.ixx"
84+
)
85+
86+
add_gtest(gtest_bit_copy
87+
SOURCE "test_bit_copy.cc"
88+
MODULES "${CMAKE_SOURCE_DIR}/utils/misc.ixx"
89+
)

tests/gtest/test_bcd.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <cstdint>
2+
#include <gtest/gtest.h>
3+
4+
import cd.cd;
5+
6+
using namespace gpsxre;
7+
8+
9+
struct BcdCase
10+
{
11+
uint8_t encoded;
12+
uint8_t decoded;
13+
};
14+
15+
16+
class Bcd : public ::testing::TestWithParam<BcdCase>
17+
{
18+
};
19+
20+
21+
TEST_P(Bcd, Decode)
22+
{
23+
const auto &c = GetParam();
24+
EXPECT_EQ(bcd_decode(c.encoded), c.decoded);
25+
}
26+
27+
28+
TEST_P(Bcd, Encode)
29+
{
30+
const auto &c = GetParam();
31+
EXPECT_EQ(bcd_encode(c.decoded), c.encoded);
32+
}
33+
34+
35+
TEST_P(Bcd, RoundTripDecodeEncode)
36+
{
37+
const auto &c = GetParam();
38+
EXPECT_EQ(bcd_encode(bcd_decode(c.encoded)), c.encoded);
39+
}
40+
41+
42+
TEST_P(Bcd, RoundTripEncodeDecode)
43+
{
44+
const auto &c = GetParam();
45+
EXPECT_EQ(bcd_decode(bcd_encode(c.decoded)), c.decoded);
46+
}
47+
48+
49+
INSTANTIATE_TEST_SUITE_P(Cases, Bcd,
50+
::testing::Values(BcdCase{ 0x00, 0 }, BcdCase{ 0x01, 1 }, BcdCase{ 0x09, 9 }, BcdCase{ 0x10, 10 }, BcdCase{ 0x11, 11 }, BcdCase{ 0x15, 15 }, BcdCase{ 0x19, 19 }, BcdCase{ 0x55, 55 },
51+
BcdCase{ 0x99, 99 }, BcdCase{ 0xA0, 100 }, BcdCase{ 0xA1, 101 }, BcdCase{ 0xA6, 106 }, BcdCase{ 0xA9, 109 }, BcdCase{ 0xB0, 110 }, BcdCase{ 0xF0, 150 }, BcdCase{ 0xF9, 159 }));

0 commit comments

Comments
 (0)