2017-12-11 12:52:06 -05:00
|
|
|
#!/usr/bin/env python3
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
import os, sys
|
|
|
|
from glob import glob
|
|
|
|
from re import sub
|
2017-12-13 11:32:25 -05:00
|
|
|
import argparse
|
|
|
|
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
def escape(s):
|
2017-12-11 12:52:06 -05:00
|
|
|
s = s.decode()
|
|
|
|
lookup = {
|
2020-02-26 23:36:53 -05:00
|
|
|
"\0": "\\0",
|
|
|
|
"\t": "\\t",
|
|
|
|
"\n": '\\n"\n"',
|
|
|
|
"\r": "\\r",
|
|
|
|
"\\": "\\\\",
|
|
|
|
'"': '\\"',
|
2017-12-11 12:52:06 -05:00
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
return '""\n"{}"'.format("".join([lookup[x] if x in lookup else x for x in s]))
|
|
|
|
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
def chew_filename(t):
|
2020-02-26 23:36:53 -05:00
|
|
|
return {"func": "test_{}_fn".format(sub(r"/|\.|-", "_", t)), "desc": t}
|
|
|
|
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2017-12-11 12:52:06 -05:00
|
|
|
def script_to_map(test_file):
|
|
|
|
r = {"name": chew_filename(test_file)["func"]}
|
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
r["script"] = escape(f.read())
|
|
|
|
with open(test_file + ".exp", "rb") as f:
|
|
|
|
r["output"] = escape(f.read())
|
|
|
|
return r
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2014-05-05 11:49:52 -04:00
|
|
|
test_function = (
|
|
|
|
"void {name}(void* data) {{\n"
|
2017-12-11 12:52:06 -05:00
|
|
|
" static const char pystr[] = {script};\n"
|
|
|
|
" static const char exp[] = {output};\n"
|
2019-12-18 13:59:48 -05:00
|
|
|
' printf("\\n");\n'
|
2017-12-11 12:52:06 -05:00
|
|
|
" upytest_set_expected_output(exp, sizeof(exp) - 1);\n"
|
|
|
|
" upytest_execute_test(pystr);\n"
|
2019-12-18 13:59:48 -05:00
|
|
|
' printf("result: ");\n'
|
2014-05-05 11:49:52 -04:00
|
|
|
"}}"
|
|
|
|
)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
testcase_struct = "struct testcase_t {name}_tests[] = {{\n{body}\n END_OF_TESTCASES\n}};"
|
|
|
|
testcase_member = ' {{ "{desc}", {func}, TT_ENABLED_, 0, 0 }},'
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
testgroup_struct = "struct testgroup_t groups[] = {{\n{body}\n END_OF_GROUPS\n}};"
|
|
|
|
testgroup_member = ' {{ "{name}", {name}_tests }},'
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
## XXX: may be we could have `--without <groups>` argument...
|
2015-01-08 19:03:21 -05:00
|
|
|
# currently these tests are selected because they pass on qemu-arm
|
2020-02-26 23:36:53 -05:00
|
|
|
test_dirs = (
|
|
|
|
"basics",
|
|
|
|
"micropython",
|
|
|
|
"misc",
|
|
|
|
"extmod",
|
|
|
|
"float",
|
|
|
|
"inlineasm",
|
|
|
|
"qemu-arm",
|
|
|
|
) # 'import', 'io',)
|
2016-10-13 09:08:19 -04:00
|
|
|
exclude_tests = (
|
2017-12-11 13:00:25 -05:00
|
|
|
# pattern matching in .exp
|
2020-02-26 23:36:53 -05:00
|
|
|
"basics/bytes_compare3.py",
|
|
|
|
"extmod/ticks_diff.py",
|
|
|
|
"extmod/time_ms_us.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# unicode char issue
|
2022-08-18 02:57:45 -04:00
|
|
|
"extmod/json_loads.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# doesn't output to python stdout
|
2022-08-18 02:57:45 -04:00
|
|
|
"extmod/re_debug.py",
|
2020-02-26 23:36:53 -05:00
|
|
|
"extmod/vfs_basic.py",
|
|
|
|
"extmod/vfs_fat_ramdisk.py",
|
|
|
|
"extmod/vfs_fat_fileio.py",
|
|
|
|
"extmod/vfs_fat_fsusermount.py",
|
|
|
|
"extmod/vfs_fat_oldproto.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# rounding issues
|
2020-02-26 23:36:53 -05:00
|
|
|
"float/float_divmod.py",
|
2017-12-11 12:59:11 -05:00
|
|
|
# requires double precision floating point to work
|
2020-02-26 23:36:53 -05:00
|
|
|
"float/float2int_doubleprec_intbig.py",
|
2022-07-12 09:48:38 -04:00
|
|
|
"float/float_format_ints_doubleprec.py",
|
2020-02-26 23:36:53 -05:00
|
|
|
"float/float_parse_doubleprec.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# inline asm FP tests (require Cortex-M4)
|
2020-02-26 23:36:53 -05:00
|
|
|
"inlineasm/asmfpaddsub.py",
|
|
|
|
"inlineasm/asmfpcmp.py",
|
|
|
|
"inlineasm/asmfpldrstr.py",
|
|
|
|
"inlineasm/asmfpmuldiv.py",
|
|
|
|
"inlineasm/asmfpsqrt.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# different filename in output
|
2020-02-26 23:36:53 -05:00
|
|
|
"micropython/emg_exc.py",
|
|
|
|
"micropython/heapalloc_traceback.py",
|
2020-01-28 22:27:33 -05:00
|
|
|
# don't have emergency exception buffer
|
|
|
|
"micropython/heapalloc_exc_compressed_emg_exc.py",
|
2017-12-11 13:00:25 -05:00
|
|
|
# pattern matching in .exp
|
2020-02-26 23:36:53 -05:00
|
|
|
"micropython/meminfo.py",
|
2019-08-18 20:59:27 -04:00
|
|
|
# needs sys stdfiles
|
2020-02-26 23:36:53 -05:00
|
|
|
"misc/print_exception.py",
|
2019-08-14 10:11:25 -04:00
|
|
|
# settrace .exp files are too large
|
2020-02-26 23:36:53 -05:00
|
|
|
"misc/sys_settrace_loop.py",
|
|
|
|
"misc/sys_settrace_generator.py",
|
|
|
|
"misc/sys_settrace_features.py",
|
2021-08-12 11:44:08 -04:00
|
|
|
# don't have f-string
|
|
|
|
"basics/string_fstring.py",
|
2021-08-26 09:27:46 -04:00
|
|
|
"basics/string_fstring_debug.py",
|
2016-10-13 09:08:19 -04:00
|
|
|
)
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
output = []
|
2017-12-13 11:32:25 -05:00
|
|
|
tests = []
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
description="Convert native MicroPython tests to tinytest/upytesthelper C code"
|
|
|
|
)
|
|
|
|
argparser.add_argument("--stdin", action="store_true", help="read list of tests from stdin")
|
2021-05-25 08:16:11 -04:00
|
|
|
argparser.add_argument("--exclude", action="append", help="exclude test by name")
|
2017-12-13 11:32:25 -05:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2017-12-14 05:26:59 -05:00
|
|
|
if not args.stdin:
|
2021-05-25 08:16:11 -04:00
|
|
|
if args.exclude:
|
|
|
|
exclude_tests += tuple(args.exclude)
|
2017-12-13 11:32:25 -05:00
|
|
|
for group in test_dirs:
|
2020-02-26 23:36:53 -05:00
|
|
|
tests += [test for test in glob("{}/*.py".format(group)) if test not in exclude_tests]
|
2017-12-13 11:32:25 -05:00
|
|
|
else:
|
2017-12-14 05:26:59 -05:00
|
|
|
for l in sys.stdin:
|
2017-12-13 11:32:25 -05:00
|
|
|
tests.append(l.rstrip())
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2017-12-13 11:32:25 -05:00
|
|
|
output.extend([test_function.format(**script_to_map(test)) for test in tests])
|
|
|
|
testcase_members = [testcase_member.format(**chew_filename(test)) for test in tests]
|
2020-02-26 23:36:53 -05:00
|
|
|
output.append(testcase_struct.format(name="", body="\n".join(testcase_members)))
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2017-12-13 11:32:25 -05:00
|
|
|
testgroup_members = [testgroup_member.format(name=group) for group in [""]]
|
2014-05-05 11:49:52 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
output.append(testgroup_struct.format(body="\n".join(testgroup_members)))
|
2014-05-05 11:49:52 -04:00
|
|
|
|
|
|
|
## XXX: may be we could have `--output <filename>` argument...
|
2017-12-15 04:37:32 -05:00
|
|
|
# Don't depend on what system locale is set, use utf8 encoding.
|
2020-02-26 23:36:53 -05:00
|
|
|
sys.stdout.buffer.write("\n\n".join(output).encode("utf8"))
|