Skip to content

Commit ea6bddb

Browse files
committed
ports/qemu-arm: Rework "test" target using upytesthelper.
The way tinytest was used in qemu-arm test target is that it didn't test much. MicroPython tests are based on matching the test output against reference output, but qemu-arm's implementation didn't do that, it effectively tested just that there was no exception during test execution. "upytesthelper" wrapper was introduce to fix it, so switch test implementation to use it. This requires passing different CFLAGS when building the firmware, so split out test-related parts to Makefile.test.
1 parent e6f0d54 commit ea6bddb

5 files changed

Lines changed: 37 additions & 48 deletions

File tree

ports/qemu-arm/Makefile

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ SRC_RUN_C = \
4646
SRC_TEST_C = \
4747
test_main.c \
4848

49-
LIB_SRC_C = $(addprefix lib/,\
49+
LIB_SRC_C += $(addprefix lib/,\
5050
libm/math.c \
5151
libm/fmodf.c \
5252
libm/nearbyintf.c \
@@ -91,27 +91,9 @@ all: run
9191
run: $(BUILD)/firmware.elf
9292
qemu-system-arm -machine integratorcp -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -kernel $(BUILD)/firmware.elf
9393

94-
test: $(BUILD)/firmware-test.elf
95-
qemu-system-arm -machine integratorcp -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -kernel $(BUILD)/firmware-test.elf > $(BUILD)/console.out
96-
$(Q)tail -n2 $(BUILD)/console.out
97-
$(Q)tail -n1 $(BUILD)/console.out | grep -q "status: 0"
98-
99-
.PHONY: $(BUILD)/genhdr/tests.h
100-
101-
$(BUILD)/test_main.o: $(BUILD)/genhdr/tests.h
102-
$(BUILD)/genhdr/tests.h:
103-
$(Q)echo "Generating $@";(cd $(TOP)/tests; ../tools/tinytest-codegen.py) > $@
104-
105-
$(BUILD)/tinytest.o:
106-
$(Q)$(CC) $(CFLAGS) -DNO_FORKING -o $@ -c $(TINYTEST)/tinytest.c
107-
10894
## `$(LD)` doesn't seem to like `--specs` for some reason, but we can just use `$(CC)` here.
10995
$(BUILD)/firmware.elf: $(OBJ_COMMON) $(OBJ_RUN)
11096
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
11197
$(Q)$(SIZE) $@
11298

113-
$(BUILD)/firmware-test.elf: $(OBJ_COMMON) $(OBJ_TEST)
114-
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
115-
$(Q)$(SIZE) $@
116-
11799
include $(TOP)/py/mkrules.mk

ports/qemu-arm/Makefile.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
LIB_SRC_C = lib/upytesthelper/upytesthelper.c
2+
3+
include Makefile
4+
5+
CFLAGS += -DTEST
6+
7+
.PHONY: $(BUILD)/genhdr/tests.h
8+
9+
$(BUILD)/test_main.o: $(BUILD)/genhdr/tests.h
10+
$(BUILD)/genhdr/tests.h:
11+
(cd $(TOP)/tests; ./run-tests --write-exp)
12+
$(Q)echo "Generating $@";(cd $(TOP)/tests; ../tools/tinytest-codegen.py) > $@
13+
14+
$(BUILD)/tinytest.o:
15+
$(Q)$(CC) $(CFLAGS) -DNO_FORKING -o $@ -c $(TINYTEST)/tinytest.c
16+
17+
$(BUILD)/firmware-test.elf: $(OBJ_COMMON) $(OBJ_TEST)
18+
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
19+
$(Q)$(SIZE) $@
20+
21+
test: $(BUILD)/firmware-test.elf
22+
qemu-system-arm -machine integratorcp -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -kernel $(BUILD)/firmware-test.elf > $(BUILD)/console.out
23+
$(Q)tail -n2 $(BUILD)/console.out
24+
$(Q)tail -n1 $(BUILD)/console.out | grep -q "status: 0"

ports/qemu-arm/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ toolchain and not with CodeSourcery toolchain. You will need to modify
2121
The difference is that CodeSourcery needs `-T generic-m-hosted.ld` while
2222
ARM's version requires `--specs=nano.specs --specs=rdimon.specs` to be
2323
passed to the linker.
24+
25+
To build and run image with builtin testsuite:
26+
27+
make -f Makefile.test test

ports/qemu-arm/mpconfigport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
1818
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
1919
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
20+
#define MICROPY_WARNINGS (1)
2021
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
2122
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
2223
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
@@ -69,3 +70,9 @@ extern const struct _mp_obj_module_t mp_module_uos;
6970

7071
// We need to provide a declaration/definition of alloca()
7172
#include <alloca.h>
73+
74+
#ifdef TEST
75+
#include "lib/upytesthelper/upytesthelper.h"
76+
#undef MP_PLAT_PRINT_STRN
77+
#define MP_PLAT_PRINT_STRN(str, len) upytest_output(str, len)
78+
#endif

ports/qemu-arm/test_main.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,13 @@
1717
#define HEAP_SIZE (128 * 1024)
1818
STATIC void *heap;
1919

20-
void do_str(const char *src);
21-
inline void do_str(const char *src) {
22-
gc_init(heap, (char*)heap + HEAP_SIZE);
23-
mp_init();
24-
25-
nlr_buf_t nlr;
26-
if (nlr_push(&nlr) == 0) {
27-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
28-
qstr source_name = lex->source_name;
29-
mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
30-
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, false);
31-
mp_call_function_0(module_fun);
32-
nlr_pop();
33-
} else {
34-
mp_obj_t exc = (mp_obj_t)nlr.ret_val;
35-
if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
36-
// Assume that sys.exit() is called to skip the test.
37-
// TODO: That can be always true, we should set up convention to
38-
// use specific exit code as skip indicator.
39-
tinytest_set_test_skipped_();
40-
goto end;
41-
}
42-
mp_obj_print_exception(&mp_plat_print, exc);
43-
tt_abort_msg("Uncaught exception");
44-
}
45-
end:
46-
mp_deinit();
47-
}
48-
4920
#include "genhdr/tests.h"
5021

5122
int main() {
5223
mp_stack_ctrl_init();
5324
mp_stack_set_limit(10240);
5425
heap = malloc(HEAP_SIZE);
26+
upytest_set_heap(heap, (char*)heap + HEAP_SIZE);
5527
int r = tinytest_main(0, NULL, groups);
5628
printf("status: %d\n", r);
5729
return r;

0 commit comments

Comments
 (0)