Skip to content

Commit b3f3331

Browse files
committed
Add basic unittests for CInifile
1 parent d62c9b6 commit b3f3331

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

.github/workflows/cibuild.yml

+17
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ jobs:
156156
id: cmake-build
157157
run: cmake --build build --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }}
158158

159+
- name: Run CTest
160+
working-directory: build
161+
run: ctest --config ${{ matrix.configuration }} --parallel ${{ matrix.platform.threads || 4 }} --output-on-failure --schedule-random --no-tests=error
162+
159163
- name: Make package
160164
if: ${{ steps.cmake-build.outcome == 'success' }}
161165
id: make-package
@@ -237,3 +241,16 @@ jobs:
237241
shutdown_vm: false
238242
sync_files: false
239243
run: cmake --build build --config ${{ matrix.Configuration }} --parallel 4
244+
245+
- name: Run CTest
246+
uses: cross-platform-actions/[email protected]
247+
with:
248+
operating_system: ${{ matrix.platform.os }}
249+
architecture: ${{ matrix.platform.arch }}
250+
version: ${{ matrix.platform.os-version }}
251+
cpu_count: 4
252+
memory: 13G
253+
environment_variables: CFLAGS CXXFLAGS
254+
shutdown_vm: false
255+
sync_files: false
256+
run: ctest -C ${{ matrix.Configuration }} --parallel 4 --output-on-failure --schedule-random --no-tests=error

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
[submodule "Externals/sse2rvv"]
2929
path = Externals/sse2rvv
3030
url = https://github.com/pattonkan/sse2rvv.git
31+
[submodule "Externals/doctest"]
32+
path = Externals/doctest
33+
url = https://github.com/doctest/doctest.git
34+
branch = master

CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT XRAY_USE_DEFAULT_CXX_LIB)
139139

140140
if (XRAY_CXX_LIB STREQUAL "libstdc++")
141141
add_compile_options(-stdlib=libstdc++)
142+
add_link_options(-stdlib=libstdc++)
142143
elseif (XRAY_CXX_LIB STREQUAL "libc++")
143144
add_compile_options(-stdlib=libc++)
145+
add_link_options(-stdlib=libc++)
144146
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
145147
add_compile_options(-lcxxrt)
148+
add_link_options(-lcxxrt)
146149
else()
147150
add_compile_options(-lc++abi)
151+
add_link_options(-lc++abi)
148152
endif()
149153
endif()
150154
endif()
@@ -295,6 +299,13 @@ add_subdirectory(src)
295299
add_subdirectory(res)
296300
add_subdirectory(misc)
297301

302+
# Tests
303+
option(BUILD_TESTS "Build tests" ON)
304+
if (BUILD_TESTS)
305+
include(CTest)
306+
add_subdirectory(tests)
307+
endif()
308+
298309
get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
299310

300311
if ("${LIB64}" STREQUAL "TRUE")

Externals/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_subdirectory(GameSpy)
1010
add_subdirectory(OPCODE)
1111
add_subdirectory(ode)
1212
add_subdirectory(imgui-proj)
13+
add_subdirectory(doctest EXCLUDE_FROM_ALL)
1314

1415
if (NOT TARGET xrLuabind)
1516
message(FATAL_ERROR

Externals/doctest

Submodule doctest added at ae7a135

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(xrCore)

tests/xrCore/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_executable(
2+
xrCoreTests
3+
main.cpp
4+
xr_ini_test.cpp)
5+
6+
target_link_libraries(xrCoreTests xrCore doctest::doctest)
7+
target_include_directories(xrCoreTests PRIVATE "${CMAKE_SOURCE_DIR}/src")
8+
add_test(NAME xrCoreTests COMMAND xrCoreTests)
9+
# https://github.com/doctest/doctest/blob/master/doc/markdown/configuration.md
10+
target_compile_definitions(xrCoreTests PRIVATE
11+
DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
12+
DOCTEST_CONFIG_SUPER_FAST_ASSERTS
13+
)

tests/xrCore/main.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT
2+
#include <doctest/doctest.h>
3+
4+
#include <Common/Platform.hpp>
5+
#include <xrCore/xrCore.h>
6+
7+
int main(int argc, char** argv)
8+
{
9+
doctest::Context context;
10+
context.applyCommandLine(argc, argv);
11+
12+
Memory._initialize();
13+
14+
return context.run();
15+
}

tests/xrCore/xr_ini_test.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <doctest/doctest.h>
2+
3+
#include <Common/Platform.hpp>
4+
#include <xrCore/xrCore.h>
5+
#include <xrCore/xr_types.h>
6+
7+
#include <xrCore/xr_ini.h>
8+
9+
CInifile read_from_string(pcstr str, CInifile::allow_include_func_t allow_include = nullptr)
10+
{
11+
IReader reader = IReader(const_cast<pstr>(str), xr_strlen(str));
12+
return CInifile(&reader, "test.ini", allow_include);
13+
}
14+
15+
TEST_CASE("parse empty file")
16+
{
17+
CInifile ini = read_from_string("");
18+
19+
CHECK_EQ(ini.section_count(), 0);
20+
}
21+
22+
TEST_CASE("parse empty section")
23+
{
24+
CInifile ini = read_from_string("[a]");
25+
26+
CHECK_EQ(ini.section_count(), 1);
27+
CHECK_UNARY(ini.section_exist("a"));
28+
}
29+
30+
TEST_CASE("parse simple section")
31+
{
32+
CInifile ini = read_from_string(
33+
R"ini(
34+
[a]
35+
key = value
36+
)ini");
37+
38+
CHECK_UNARY(ini.section_exist("a"));
39+
CHECK_UNARY(ini.line_exist("a", "key"));
40+
CHECK_EQ(ini.read<pcstr>("a", "key"), "value");
41+
}
42+
43+
TEST_CASE("parse integer value")
44+
{
45+
CInifile ini = read_from_string(
46+
R"ini(
47+
[a]
48+
key = 123
49+
)ini");
50+
51+
CHECK_UNARY(ini.section_exist("a"));
52+
CHECK_UNARY(ini.line_exist("a", "key"));
53+
CHECK_EQ(ini.read<u32>("a", "key"), 123);
54+
}
55+
56+
TEST_CASE("Parse float value")
57+
{
58+
CInifile ini = read_from_string(
59+
R"ini(
60+
[a]
61+
key = 123.456
62+
)ini");
63+
64+
CHECK_UNARY(ini.section_exist("a"));
65+
CHECK_UNARY(ini.line_exist("a", "key"));
66+
CHECK_EQ(ini.read<f32>("a", "key"), 123.456f);
67+
}
68+
69+
TEST_CASE("Parse quoted value")
70+
{
71+
CInifile ini = read_from_string(
72+
R"ini(
73+
[a]
74+
key = "value"
75+
)ini");
76+
77+
CHECK_UNARY(ini.section_exist("a"));
78+
CHECK_UNARY(ini.line_exist("a", "key"));
79+
CHECK_EQ(ini.read<pcstr>("a", "key"), "\"value\"");
80+
}
81+
82+
TEST_CASE("Parse multiline value")
83+
{
84+
CInifile ini = read_from_string(
85+
R"ini(
86+
[a]
87+
key = "multiline
88+
value"
89+
)ini");
90+
91+
CHECK_UNARY(ini.section_exist("a"));
92+
CHECK_UNARY(ini.line_exist("a", "key"));
93+
CHECK_EQ(ini.read<pcstr>("a", "key"), "\"multiline\r\nvalue\"");
94+
}

0 commit comments

Comments
 (0)