-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
143 lines (116 loc) · 4.42 KB
/
CMakeLists.txt
File metadata and controls
143 lines (116 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
cmake_minimum_required(VERSION 3.7)
project(sort_strings LANGUAGES C)
set(CMAKE_C_STANDARD 99)
set(ALGORITHMS bubble insertion merge radix quick)
enable_testing()
option(USE_MMAP "Use memory-mapped file I/O" OFF)
set(SOURCES src/file_reader_api.c)
if (USE_MMAP)
message(STATUS "Using memory-mapped file I/O")
set(SOURCES src/file_reader_mmap.c)
else()
message(STATUS "Using standard file I/O")
endif()
set(PROJECT_SOURCES
src/sort_strings.c
src/arg_parser.c
src/sortings.c
src/comparators.c
${SOURCES}
)
add_executable(sort_strings ${PROJECT_SOURCES})
target_include_directories(sort_strings PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_compile_options(sort_strings PRIVATE
-Wall
-Wextra
-Wpedantic
-Werror
-fsanitize=address
-fno-omit-frame-pointer
)
target_link_options(sort_strings PRIVATE -fsanitize=address)
message(STATUS "Build...")
add_custom_target(clean-build
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}
COMMENT "Cleaning build"
)
function(add_invalid_parameters_tests)
add_test(
NAME not_enough_parameters
COMMAND bash -c "./sort_strings 5 ${CMAKE_SOURCE_DIR}/test/1.in bubble > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_test(
NAME too_much_parameters
COMMAND bash -c "./sort_strings 5 ${CMAKE_SOURCE_DIR}/test/1.in bubble asc des > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_test(
NAME incorrect_parameter_file
COMMAND bash -c "./sort_strings 5 chuchumba.in bubble asc > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_test(
NAME incorrect_parameter_number_of_strings_equal_minus
COMMAND bash -c "./sort_strings -1 ${CMAKE_SOURCE_DIR}/test/1.in bubble asc > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_test(
NAME incorrect_parameter_number_of_strings_0
COMMAND bash -c "./sort_strings 0 ${CMAKE_SOURCE_DIR}/test/1.in bubble asc > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_test(
NAME incorrect_algo
COMMAND bash -c "./sort_strings 5 ${CMAKE_SOURCE_DIR}/test/1.in mamba asc > /dev/null 2>&1; test $? -eq 255"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_tests_properties(
not_enough_parameters
too_much_parameters
incorrect_parameter_file
incorrect_parameter_number_of_strings_equal_minus
incorrect_parameter_number_of_strings_0
incorrect_algo
PROPERTIES LABELS "Invalid Parameters"
)
endfunction(add_invalid_parameters_tests)
add_invalid_parameters_tests()
function(add_sort_test num_of_str input_file algorithm expected_output)
get_filename_component(test_name ${input_file} NAME_WE)
set(run_program_cmd "./sort_strings ${num_of_str} ${input_file} ${algorithm} asc > ${CMAKE_BINARY_DIR}/${test_name}_${algorithm}.out")
set(compare_cmd "cmp ${CMAKE_BINARY_DIR}/${test_name}_${algorithm}.out ${expected_output}")
add_test(
NAME ${test_name}_${algorithm}
COMMAND bash -c "${run_program_cmd} && ${compare_cmd}"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_tests_properties(
${test_name}_${algorithm}
PROPERTIES LABELS "Basic Asc Sorting"
)
endfunction()
file(GLOB input_files "${CMAKE_SOURCE_DIR}/test/input/*.in")
foreach(input_file ${input_files})
get_filename_component(test_name ${input_file} NAME_WE)
execute_process(
COMMAND wc -l ${input_file}
OUTPUT_VARIABLE num_of_str
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^[ ]*([0-9]+).*" "\\1" num_of_str "${num_of_str}")
foreach(algorithm ${ALGORITHMS})
set(expected_output "${CMAKE_SOURCE_DIR}/test/output/${test_name}.out")
add_sort_test(${num_of_str} ${input_file} ${algorithm} ${expected_output})
endforeach()
endforeach()
add_test(
NAME test_comp_des
COMMAND bash -c "./sort_strings 5 ${CMAKE_SOURCE_DIR}/test/input/1.in bubble des > ${CMAKE_BINARY_DIR}/${test_name}.out && cmp ${CMAKE_BINARY_DIR}/${test_name}.out ${CMAKE_SOURCE_DIR}/test/descending_output/1.out"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(run_all_tests
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running all tests"
)