Skip to content

Commit 9b32c11

Browse files
committed
Port old tests into new test system
1 parent 5dad52f commit 9b32c11

8 files changed

Lines changed: 347 additions & 194 deletions

File tree

CMakeLists.txt

Lines changed: 23 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,13 +88,21 @@ function(add_ceda_target target)
8188

8289
endfunction()
8390

84-
# core target and tests target
91+
# Add core and tests target
92+
add_executable(ceda ${CORE_SRCS})
93+
add_executable(ceda-test ${TEST_SRCS})
8594
add_ceda_target(ceda)
8695
add_ceda_target(ceda-test)
8796

88-
# Options related to test target only
89-
target_compile_options(ceda-test PRIVATE -DCEDA_TEST=1)
97+
# Options related to core target only
98+
# -----------------------------------
99+
target_sources(ceda
100+
PRIVATE
101+
${CORE_SRC}
102+
)
90103

104+
# Options related to test target only
105+
# -----------------------------------
91106
target_link_libraries(ceda-test
92107
criterion
93108
)
@@ -96,3 +111,4 @@ target_sources(ceda-test
96111
PRIVATE
97112
${TEST_SRCS}
98113
)
114+

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+
}

src/tests/test_ceda_string.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "../ceda_string.h"
2+
3+
#include <criterion/criterion.h>
4+
5+
Test(ceda_string, new) {
6+
ceda_string_t *str = ceda_string_new(0);
7+
ceda_string_cat(str, "hello world");
8+
cr_assert_str_eq(ceda_string_data(str), "hello world");
9+
ceda_string_delete(str);
10+
}
11+
12+
Test(ceda_string, concat) {
13+
ceda_string_t *str = ceda_string_new(0);
14+
ceda_string_cat(str, "hello ");
15+
ceda_string_cat(str, "world");
16+
cr_assert_str_eq(ceda_string_data(str), "hello world");
17+
ceda_string_delete(str);
18+
}
19+
20+
Test(ceda_string, auto_alloc) {
21+
ceda_string_t *str = ceda_string_new(4);
22+
ceda_string_cat(str, "hello world ");
23+
ceda_string_cat(str, "everybody!");
24+
cr_assert_str_eq(ceda_string_data(str), "hello world everybody!");
25+
ceda_string_delete(str);
26+
}
27+
28+
Test(ceda_string, printf) {
29+
const int magic = 0x55;
30+
ceda_string_t *str = ceda_string_new(0);
31+
ceda_string_printf(str, "%s %d ", "hello world", magic);
32+
ceda_string_printf(str, "%u %x %X", magic, magic, magic);
33+
cr_assert_str_eq(ceda_string_data(str), "hello world 85 85 55 55");
34+
ceda_string_delete(str);
35+
}
36+
37+
Test(ceda_string, cpy) {
38+
ceda_string_t *str = ceda_string_new(0);
39+
ceda_string_cat(str, "hello");
40+
ceda_string_cpy(str, "world");
41+
cr_assert_str_eq(ceda_string_data(str), "world");
42+
ceda_string_delete(str);
43+
}
44+
45+
Test(ceda_string, len) {
46+
ceda_string_t *str = ceda_string_new(0);
47+
cr_assert_eq(ceda_string_len(str), 0);
48+
ceda_string_delete(str);
49+
50+
ceda_string_t *str2 = ceda_string_new(4);
51+
ceda_string_cpy(str2, "");
52+
cr_assert_eq(ceda_string_len(str2), 0);
53+
ceda_string_delete(str2);
54+
}
55+
56+
Test(ceda_string, eq) {
57+
ceda_string_t *str0 = ceda_string_new(0);
58+
ceda_string_cpy(str0, "hello world");
59+
ceda_string_t *str1 = ceda_string_new(0);
60+
ceda_string_cpy(str1, "hello world");
61+
ceda_string_t *str2 = ceda_string_new(0);
62+
ceda_string_cpy(str2, "hello Earth");
63+
64+
cr_assert(ceda_string_eq(str0, str1));
65+
cr_assert(!ceda_string_eq(str0, str2));
66+
67+
ceda_string_delete(str0);
68+
ceda_string_delete(str1);
69+
ceda_string_delete(str2);
70+
}

0 commit comments

Comments
 (0)