Skip to content

Commit ec38be8

Browse files
committed
update tests and examples
1 parent 0d28a67 commit ec38be8

17 files changed

Lines changed: 451 additions & 72 deletions

CMakeLists.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ project(c_traceback
66
DESCRIPTION "Colorful, lightweight tracebacks in C"
77
)
88

9-
# --- Dependencies & Modules ---
9+
### Dependencies & Modules ###
1010
include(GNUInstallDirs)
1111

12-
# --- Options ---
12+
### Options ###
1313
option(ENABLE_SANITIZERS "Enable address and undefined sanitizers" OFF)
1414
option(BUILD_EXAMPLES "Build example executables" OFF)
1515

16-
# --- Library ---
16+
### Library ###
1717
add_library(c_traceback STATIC
1818
src/error.c
1919
src/error_codes.c
@@ -25,14 +25,14 @@ add_library(c_traceback STATIC
2525
)
2626
add_library(c_traceback::c_traceback ALIAS c_traceback)
2727

28-
# --- Standard (c99) ---
28+
### Standard (c99) ###
2929
set_target_properties(c_traceback PROPERTIES
3030
C_STANDARD 99
3131
C_STANDARD_REQUIRED ON
3232
POSITION_INDEPENDENT_CODE ON
3333
)
3434

35-
# --- Includes ---
35+
### Includes ###
3636
target_include_directories(c_traceback
3737
PUBLIC
3838
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@@ -41,7 +41,7 @@ target_include_directories(c_traceback
4141
${CMAKE_CURRENT_SOURCE_DIR}/src/internal
4242
)
4343

44-
# --- Warnings ---
44+
### Warnings ###
4545
if(MSVC)
4646
target_compile_options(c_traceback PRIVATE /W4)
4747
else()
@@ -51,18 +51,18 @@ else()
5151
)
5252
endif()
5353

54-
# --- Sanitizers ---
54+
### Sanitizers ###
5555
if(ENABLE_SANITIZERS)
5656
if(NOT MSVC)
5757
target_compile_options(c_traceback PRIVATE -fsanitize=address,undefined)
5858
target_link_options(c_traceback PRIVATE -fsanitize=address,undefined)
5959
endif()
6060
endif()
6161

62-
# --- Definitions ---
62+
### Definitions ###
6363
target_compile_definitions(c_traceback PRIVATE CTB_VERSION="${PROJECT_VERSION}")
6464

65-
# --- Installation ---
65+
### Installation ###
6666
if(PROJECT_IS_TOP_LEVEL)
6767
include(CMakePackageConfigHelpers)
6868

@@ -78,7 +78,7 @@ if(PROJECT_IS_TOP_LEVEL)
7878
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
7979
)
8080

81-
# --- Package config & version ---
81+
### Package config & version ###
8282
write_basic_package_version_file(
8383
"${CMAKE_CURRENT_BINARY_DIR}/c_tracebackConfigVersion.cmake"
8484
VERSION ${PROJECT_VERSION}
@@ -97,7 +97,7 @@ if(PROJECT_IS_TOP_LEVEL)
9797
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/c_traceback"
9898
)
9999

100-
# --- Export targets / install cmake files ---
100+
### Export targets / install cmake files ###
101101
install(EXPORT c_tracebackTargets
102102
FILE c_tracebackTargets.cmake
103103
NAMESPACE c_traceback::
@@ -110,13 +110,17 @@ if(PROJECT_IS_TOP_LEVEL)
110110
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c_traceback
111111
)
112112

113-
# --- Build Examples ---
113+
### Build Examples ###
114114
if(BUILD_EXAMPLES)
115-
enable_testing()
116-
117115
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/examples/CMakeLists.txt")
118116
add_subdirectory(examples)
119117
endif()
120118
endif()
121119

122-
endif()
120+
endif()
121+
122+
### Testing ###
123+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
124+
enable_testing()
125+
add_subdirectory(tests)
126+
endif()

examples/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ file(GLOB EXAMPLE_SOURCES CONFIGURE_DEPENDS "example*.c")
33
foreach(SOURCE_FILE ${EXAMPLE_SOURCES})
44

55
get_filename_component(TARGET_NAME ${SOURCE_FILE} NAME_WE)
6-
76
add_executable(${TARGET_NAME} ${SOURCE_FILE})
87
target_link_libraries(${TARGET_NAME} PRIVATE c_traceback::c_traceback)
9-
108
if(ENABLE_SANITIZERS AND NOT MSVC)
119
target_compile_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
1210
target_link_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
1311
endif()
1412

15-
# Test
16-
add_test(
17-
NAME ${TARGET_NAME}
18-
COMMAND ${CMAKE_COMMAND}
19-
-DTEST_PROG=$<TARGET_FILE:${TARGET_NAME}>
20-
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_test.cmake
21-
)
22-
23-
endforeach()
13+
endforeach()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
#include "c_traceback.h"
4+
5+
int main(void)
6+
{
7+
ctb_clear_context();
8+
ctb_install_signal_handler();
9+
10+
printf("Press Ctrl+C to trigger a keyboard interrupt...\n");
11+
while (1)
12+
{
13+
/* Do nothing */
14+
}
15+
16+
return 0;
17+
}

examples/example_multi_error.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,8 @@
44
#define FILE_PATH1 "../test1.txt"
55
#define FILE_PATH2 "../test2.txt"
66

7-
void open_file(const char *file_name)
8-
{
9-
FILE *file = fopen(file_name, "r");
10-
if (!file)
11-
{
12-
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
13-
return;
14-
}
15-
/* Do something */
16-
fclose(file);
17-
}
18-
19-
void do_something_risky()
20-
{
21-
int x = 0;
22-
if (x == 0)
23-
{
24-
THROW(CTB_RUNTIME_ERROR, "Division by zero attempted");
25-
return;
26-
}
27-
int y = 10 / x;
28-
(void)y;
29-
}
7+
void open_file(const char *file_name);
8+
void do_something_risky();
309

3110
int main(void)
3211
{
@@ -50,3 +29,27 @@ int main(void)
5029
ctb_dump_traceback();
5130
return 1;
5231
}
32+
33+
void open_file(const char *file_name)
34+
{
35+
FILE *file = fopen(file_name, "r");
36+
if (!file)
37+
{
38+
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
39+
return;
40+
}
41+
/* Do something */
42+
fclose(file);
43+
}
44+
45+
void do_something_risky()
46+
{
47+
int x = 0;
48+
if (x == 0)
49+
{
50+
THROW(CTB_RUNTIME_ERROR, "Division by zero attempted");
51+
return;
52+
}
53+
int y = 10 / x;
54+
(void)y;
55+
}

examples/example_open_file.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@
44

55
#define FILE_PATH "../test.txt"
66

7-
void open_file(const char *file_name)
8-
{
9-
FILE *file = fopen(file_name, "r");
10-
if (!file)
11-
{
12-
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
13-
return;
14-
}
15-
/* Do something */
16-
fclose(file);
17-
}
7+
void open_file(const char *file_name);
188

199
int main(void)
2010
{
@@ -29,4 +19,16 @@ int main(void)
2919
error:
3020
ctb_dump_traceback();
3121
return 1;
22+
}
23+
24+
void open_file(const char *file_name)
25+
{
26+
FILE *file = fopen(file_name, "r");
27+
if (!file)
28+
{
29+
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
30+
return;
31+
}
32+
/* Do something */
33+
fclose(file);
3234
}

examples/example_recursion.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44

55
#define N 100
66

7-
void recursion(int count)
8-
{
9-
if (count >= N)
10-
{
11-
THROW_FMT(CTB_RUNTIME_ERROR, "Oh no, some error occurred at depth %d", count);
12-
return;
13-
}
14-
15-
TRACE(recursion(count + 1));
16-
}
7+
void recursion(int count);
178

189
int main(void)
1910
{
@@ -23,7 +14,20 @@ int main(void)
2314
TRY_GOTO(recursion(0), error);
2415
printf("This shouldn't be printed if there is error");
2516

17+
return 0;
18+
2619
error:
2720
ctb_dump_traceback(); // Log traceback and reset error stack
28-
return 0;
21+
return 1;
2922
}
23+
24+
void recursion(int count)
25+
{
26+
if (count >= N)
27+
{
28+
THROW_FMT(CTB_RUNTIME_ERROR, "Oh no, some error occurred at depth %d", count);
29+
return;
30+
}
31+
32+
TRACE(recursion(count + 1));
33+
}

examples/example_seg_fault.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(void)
1313
{
1414
ctb_clear_context();
1515
ctb_install_signal_handler();
16-
THROW(CTB_BUFFER_ERROR, "Hello! This is a test error before segfault.");
16+
THROW(CTB_BUFFER_ERROR, "Hello! This is an error before segfault.");
1717

1818
TRACE(some_function());
1919

examples/example_stack_overflow.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include "c_traceback.h"
5+
6+
void stack_overflow(int depth)
7+
{
8+
char buffer[10240]; // Allocate some stack space
9+
memset(buffer, 0, sizeof(buffer));
10+
printf("Recursion depth: %d\n", depth);
11+
stack_overflow(depth + 1);
12+
}
13+
14+
int main(void)
15+
{
16+
ctb_clear_context();
17+
ctb_install_signal_handler();
18+
19+
// Stack overflow
20+
TRACE(stack_overflow(1));
21+
22+
return 0;
23+
}

tests/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "test*.c")
2+
3+
foreach(SOURCE_FILE ${TEST_SOURCES})
4+
5+
get_filename_component(TARGET_NAME ${SOURCE_FILE} NAME_WE)
6+
7+
add_executable(${TARGET_NAME} ${SOURCE_FILE})
8+
target_link_libraries(${TARGET_NAME} PRIVATE c_traceback::c_traceback)
9+
10+
if(ENABLE_SANITIZERS AND NOT MSVC)
11+
target_compile_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
12+
target_link_options(${TARGET_NAME} PRIVATE -fsanitize=address,undefined)
13+
endif()
14+
15+
# Test
16+
add_test(
17+
NAME ${TARGET_NAME}
18+
COMMAND ${CMAKE_COMMAND}
19+
-DTEST_PROG=$<TARGET_FILE:${TARGET_NAME}>
20+
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_test.cmake
21+
)
22+
23+
endforeach()

0 commit comments

Comments
 (0)