Skip to content

Commit 73a5cce

Browse files
committed
Port old tests into new test system
1 parent 4c0579f commit 73a5cce

9 files changed

Lines changed: 356 additions & 222 deletions

File tree

CMakeLists.txt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ set(CORE_SRCS
3636

3737
# Put here sources needed for tests only
3838
set(TEST_SRCS
39+
src/main_test.c
40+
41+
src/ceda_string.c
42+
src/conf.c
43+
src/fdc.c
44+
src/hexdump.c
45+
src/tokenizer.c
46+
47+
src/tests/test_ceda_string.c
48+
src/tests/test_conf.c
3949
src/tests/test_fdc.c
50+
src/tests/test_tokenizer.c
4051
)
4152

4253
# External dependencies
@@ -58,10 +69,6 @@ set_source_files_properties(src/3rd/disassembler.c PROPERTIES COMPILE_FLAGS -Wno
5869
# Automatically add targets, with same properties
5970
function(add_ceda_target target)
6071

61-
add_executable(${target}
62-
${CORE_SRCS}
63-
)
64-
6572
target_compile_options(${target} PRIVATE -W -Wformat -Wall -Wundef -Wpointer-arith -Wcast-qual -Wwrite-strings -Wsign-compare -Wmissing-noreturn -Wextra -Wconversion)
6673

6774
if(CMAKE_BUILD_TYPE MATCHES "Release")
@@ -81,12 +88,24 @@ function(add_ceda_target target)
8188

8289
endfunction()
8390

84-
# core target and tests target
91+
# Options related to core target only
92+
# -----------------------------------
93+
add_executable(ceda ${CORE_SRCS})
8594
add_ceda_target(ceda)
86-
add_ceda_target(ceda-test)
95+
target_sources(ceda
96+
PRIVATE
97+
${CORE_SRC}
98+
)
8799

88100
# Options related to test target only
89-
target_compile_options(ceda-test PRIVATE -DCEDA_TEST=1)
101+
# -----------------------------------
102+
include(CTest)
103+
add_executable(ceda-test ${TEST_SRCS})
104+
add_ceda_target(ceda-test)
105+
add_test(NAME ceda-test
106+
COMMAND ceda-test
107+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
108+
)
90109

91110
target_link_libraries(ceda-test
92111
criterion
@@ -96,3 +115,4 @@ target_sources(ceda-test
96115
PRIVATE
97116
${TEST_SRCS}
98117
)
118+

script/build

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,10 @@
22

33
set -euo pipefail
44

5-
function banner() {
6-
echo "================================================================================"
7-
echo
8-
echo -n -e "\t"
9-
echo $1
10-
echo
11-
echo "================================================================================"
12-
}
5+
cmake -B build.release -DCMAKE_BUILD_TYPE=Release
6+
make -C build.release
7+
make -C build.release test
138

14-
case ${1-help} in
15-
release)
16-
banner "Building RELEASE"
17-
mkdir -p build/release
18-
cmake -B build/release -DCMAKE_BUILD_TYPE=Release
19-
make -C build/release -j
20-
;;
21-
22-
debug)
23-
banner "Building DEBUG"
24-
mkdir -p build/debug
25-
cmake -B build/debug -DCMAKE_BUILD_TYPE=Debug
26-
make -C build/debug
27-
;;
28-
29-
help|*)
30-
echo Choose one among: [release, debug]
31-
exit 1
32-
;;
33-
esac
9+
cmake -B build.debug -DCMAKE_BUILD_TYPE=Debug
10+
make -C build.debug
3411

src/ceda_string.c

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -134,76 +134,3 @@ void ceda_string_delete(ceda_string_t *str) {
134134
free(str->data);
135135
str->valid = false;
136136
}
137-
138-
#ifdef CEDA_TEST
139-
140-
#include <criterion/criterion.h>
141-
142-
Test(ceda_string, new) {
143-
ceda_string_t *str = ceda_string_new(0);
144-
ceda_string_cat(str, "hello world");
145-
cr_assert_str_eq(ceda_string_data(str), "hello world");
146-
ceda_string_delete(str);
147-
}
148-
149-
Test(ceda_string, concat) {
150-
ceda_string_t *str = ceda_string_new(0);
151-
ceda_string_cat(str, "hello ");
152-
ceda_string_cat(str, "world");
153-
cr_assert_str_eq(ceda_string_data(str), "hello world");
154-
ceda_string_delete(str);
155-
}
156-
157-
Test(ceda_string, auto_alloc) {
158-
ceda_string_t *str = ceda_string_new(4);
159-
ceda_string_cat(str, "hello world ");
160-
ceda_string_cat(str, "everybody!");
161-
cr_assert_str_eq(ceda_string_data(str), "hello world everybody!");
162-
ceda_string_delete(str);
163-
}
164-
165-
Test(ceda_string, printf) {
166-
const int magic = 0x55;
167-
ceda_string_t *str = ceda_string_new(0);
168-
ceda_string_printf(str, "%s %d ", "hello world", magic);
169-
ceda_string_printf(str, "%u %x %X", magic, magic, magic);
170-
cr_assert_str_eq(ceda_string_data(str), "hello world 85 85 55 55");
171-
ceda_string_delete(str);
172-
}
173-
174-
Test(ceda_string, cpy) {
175-
ceda_string_t *str = ceda_string_new(0);
176-
ceda_string_cat(str, "hello");
177-
ceda_string_cpy(str, "world");
178-
cr_assert_str_eq(ceda_string_data(str), "world");
179-
ceda_string_delete(str);
180-
}
181-
182-
Test(ceda_string, len) {
183-
ceda_string_t *str = ceda_string_new(0);
184-
cr_assert_eq(ceda_string_len(str), 0);
185-
ceda_string_delete(str);
186-
187-
ceda_string_t *str2 = ceda_string_new(4);
188-
ceda_string_cpy(str2, "");
189-
cr_assert_eq(ceda_string_len(str2), 0);
190-
ceda_string_delete(str2);
191-
}
192-
193-
Test(ceda_string, eq) {
194-
ceda_string_t *str0 = ceda_string_new(0);
195-
ceda_string_cpy(str0, "hello world");
196-
ceda_string_t *str1 = ceda_string_new(0);
197-
ceda_string_cpy(str1, "hello world");
198-
ceda_string_t *str2 = ceda_string_new(0);
199-
ceda_string_cpy(str2, "hello Earth");
200-
201-
cr_assert(ceda_string_eq(str0, str1));
202-
cr_assert(!ceda_string_eq(str0, str2));
203-
204-
ceda_string_delete(str0);
205-
ceda_string_delete(str1);
206-
ceda_string_delete(str2);
207-
}
208-
209-
#endif

src/cli.c

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,99 +1110,4 @@ void cli_init(CEDAModule *mod) {
11101110
LOG_INFO("cli ok\n");
11111111
initialized = true;
11121112
}
1113-
1114-
#ifdef CEDA_TEST
1115-
1116-
#include <criterion/criterion.h>
1117-
1118-
struct test {
1119-
bool input; // true => is user input line,
1120-
// false => is emulator expected response
1121-
const char *text;
1122-
};
1123-
1124-
static void cli_test_setup(void) {
1125-
FIFO_INIT(&tx_fifo);
1126-
}
1127-
1128-
static void run_tests(struct test *tests, size_t n) {
1129-
// static bool prompt = false;
1130-
1131-
for (size_t i = 0; i < n; ++i) {
1132-
struct test *tst = &tests[i];
1133-
1134-
#if 0
1135-
LOG_DEBUG("test %s %s\n", tst->input ? "=>" : "<=", tst->text);
1136-
#endif
1137-
1138-
if (tst->input) {
1139-
ceda_string_t *text = ceda_string_new(0);
1140-
ceda_string_cpy(text, tst->text);
1141-
cli_handle_line(text);
1142-
ceda_string_delete(text);
1143-
} else {
1144-
const char *expected = tst->text ? tst->text : USER_PROMPT_STR;
1145-
cr_assert(!FIFO_ISEMPTY(&tx_fifo));
1146-
ceda_string_t *message = FIFO_POP(&tx_fifo);
1147-
cr_assert_str_eq(ceda_string_data(message), expected);
1148-
ceda_string_delete(message);
1149-
}
1150-
}
1151-
}
1152-
1153-
Test(cli, break, .init = cli_test_setup) {
1154-
/* clang-format off */
1155-
struct test tests[] = {
1156-
{true, "break"},
1157-
{false, "no breakpoint set\n"},
1158-
{false, USER_PROMPT_STR},
1159-
{true, "break c000"},
1160-
{false, USER_PROMPT_STR},
1161-
{true, "break"},
1162-
{false, "0\tc000\n"},
1163-
{false, USER_PROMPT_STR},
1164-
{true, "break c030"},
1165-
{false, USER_PROMPT_STR},
1166-
{true, "break"},
1167-
{false, "0\tc000\n1\tc030\n"},
1168-
{false, USER_PROMPT_STR},
1169-
};
1170-
/* clang-format on */
1171-
run_tests(tests, ARRAY_SIZE(tests));
1172-
}
1173-
1174-
Test(cli, delete, .init = cli_test_setup) {
1175-
/* clang-format off */
1176-
struct test tests[] = {
1177-
{true, "break c000"},
1178-
{false, USER_PROMPT_STR},
1179-
{true, "break c030"},
1180-
{false, USER_PROMPT_STR},
1181-
{true, "delete"},
1182-
{false, USER_BAD_ARG_STR "missing delete target\n"},
1183-
{false, USER_PROMPT_STR},
1184-
{true, "delete breakpoint"},
1185-
{false, USER_BAD_ARG_STR "missing index\n"},
1186-
{false, USER_PROMPT_STR},
1187-
{true, "delete brekpoi 1"},
1188-
{false, USER_BAD_ARG_STR "unknown delete target\n"},
1189-
{false, USER_PROMPT_STR},
1190-
{true, "delete breakpoint -1"},
1191-
{false, "can't delete breakpoint\n"},
1192-
{false, USER_PROMPT_STR},
1193-
{true, "delete breakpoint xx"},
1194-
{false, USER_BAD_ARG_STR "bad index format\n"},
1195-
{false, USER_PROMPT_STR},
1196-
{true, "delete breakpoint 0"},
1197-
{false, USER_PROMPT_STR},
1198-
{true, "break"},
1199-
{false, "1\tc030\n"},
1200-
{false, USER_PROMPT_STR},
1201-
};
1202-
/* clang-format on */
1203-
run_tests(tests, ARRAY_SIZE(tests));
1204-
}
1205-
1206-
#endif
1207-
//NOLINTEND
1208-
1113+
// NOLINTEND

src/main.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
#include "ceda.h"
22

3-
#ifdef CEDA_TEST
4-
#include <criterion/criterion.h>
5-
#endif
6-
73
#define LOG_LEVEL LOG_LVL_INFO
84
#include "log.h"
95

106
int main(int argc, char *argv[]) {
117
int ret = 0;
128

13-
#ifdef CEDA_TEST
14-
LOG_INFO("CEDA Test\n");
15-
criterion_options.color = true;
16-
criterion_options.full_stats = true;
17-
criterion_options.timeout = 1;
18-
criterion_options.logging_threshold = CRITERION_INFO;
19-
20-
struct criterion_test_set *set = criterion_initialize();
21-
if (criterion_handle_args(argc, argv, true))
22-
ret = !criterion_run_all_tests(set);
23-
criterion_finalize(set);
24-
25-
#else
269
LOG_INFO("CEDA Emulator\n");
2710

2811
(void)argc;
2912
(void)argv;
3013
ceda_init();
3114
ret = ceda_run();
32-
#endif
3315

3416
return ret;
3517
}

src/main_test.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <criterion/criterion.h>
2+
3+
#include <stdio.h>
4+
5+
int main(int argc, char *argv[]) {
6+
int ret = 0;
7+
8+
printf("CEDA Test\n");
9+
criterion_options.color = true;
10+
criterion_options.full_stats = true;
11+
criterion_options.timeout = 1;
12+
criterion_options.logging_threshold = CRITERION_INFO;
13+
14+
struct criterion_test_set *set = criterion_initialize();
15+
if (criterion_handle_args(argc, argv, true))
16+
ret = !criterion_run_all_tests(set);
17+
criterion_finalize(set);
18+
19+
return ret;
20+
}

0 commit comments

Comments
 (0)