From 48e931e1d33d008c2622d3fbc5ab7e4a49296bf2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 11 Dec 2017 19:52:06 +0200 Subject: tools/tinytest-codegen.py: Generate code for 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, and so test generator is now switched to generate test code for it. Also, fix PEP8 and other codestyle issues. --- tools/tinytest-codegen.py | 48 ++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index dadfea1cc..062a00935 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -1,32 +1,38 @@ -#! /usr/bin/env python3 +#!/usr/bin/env python3 import os, sys from glob import glob from re import sub def escape(s): - lookup = { - '\0': '\\0', - '\t': '\\t', - '\n': '\\n\"\n\"', - '\r': '\\r', - '\\': '\\\\', - '\"': '\\\"', - } - return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s])) + s = s.decode() + lookup = { + '\0': '\\0', + '\t': '\\t', + '\n': '\\n\"\n\"', + '\r': '\\r', + '\\': '\\\\', + '\"': '\\\"', + } + return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s])) def chew_filename(t): - return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] } + return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] } -def script_to_map(t): - r = { 'name': chew_filename(t)['func'] } - with open(t) as f: r['script'] = escape(''.join(f.readlines())) - return r +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 test_function = ( "void {name}(void* data) {{\n" - " const char * pystr = {script};\n" - " do_str(pystr);\n" + " static const char pystr[] = {script};\n" + " static const char exp[] = {output};\n" + " upytest_set_expected_output(exp, sizeof(exp) - 1);\n" + " upytest_execute_test(pystr);\n" "}}" ) @@ -57,10 +63,10 @@ exclude_tests = ( output = [] for group in test_dirs: - tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] - 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] - output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members))) + tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] + 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] + output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members))) testgroup_members = [testgroup_member.format(name=group) for group in test_dirs] -- cgit v1.2.3 From 4db6a7adbe25ba2a096cd70ec4072c168fdb4bd4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 11 Dec 2017 19:59:11 +0200 Subject: tools/tinytest-codegen: Wrap lines of exclude_tests. So it was manageable and extensible. --- tools/tinytest-codegen.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index 062a00935..6744446ea 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -54,10 +54,16 @@ testgroup_member = ( # currently these tests are selected because they pass on qemu-arm test_dirs = ('basics', 'micropython', 'float', 'extmod', 'inlineasm') # 'import', 'io', 'misc') exclude_tests = ( - 'float/float2int_doubleprec_intbig.py', # requires double precision floating point to work - 'inlineasm/asmfpaddsub.py', 'inlineasm/asmfpcmp.py', 'inlineasm/asmfpldrstr.py', 'inlineasm/asmfpmuldiv.py', 'inlineasm/asmfpsqrt.py', - 'extmod/ticks_diff.py', 'extmod/time_ms_us.py', 'extmod/uheapq_timeq.py', - 'extmod/vfs_fat_ramdisk.py', 'extmod/vfs_fat_fileio.py', 'extmod/vfs_fat_fsusermount.py', 'extmod/vfs_fat_oldproto.py', + 'extmod/ticks_diff.py', + 'extmod/time_ms_us.py', + 'extmod/uheapq_timeq.py', + 'extmod/vfs_fat_ramdisk.py', 'extmod/vfs_fat_fileio.py', + 'extmod/vfs_fat_fsusermount.py', 'extmod/vfs_fat_oldproto.py', + # requires double precision floating point to work + 'float/float2int_doubleprec_intbig.py', + # inline asm tests + 'inlineasm/asmfpaddsub.py', 'inlineasm/asmfpcmp.py', 'inlineasm/asmfpldrstr.py', + 'inlineasm/asmfpmuldiv.py','inlineasm/asmfpsqrt.py', ) output = [] -- cgit v1.2.3 From e6f0d547ab16096d7a2c24824c6e398c082c0d50 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 11 Dec 2017 20:00:25 +0200 Subject: tools/tinytest-codegen: More excludes after enabling expected output match. --- tools/tinytest-codegen.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index 6744446ea..d7761cd5a 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -54,16 +54,31 @@ testgroup_member = ( # currently these tests are selected because they pass on qemu-arm test_dirs = ('basics', 'micropython', 'float', 'extmod', 'inlineasm') # 'import', 'io', 'misc') exclude_tests = ( + # pattern matching in .exp + 'basics/bytes_compare3.py', 'extmod/ticks_diff.py', 'extmod/time_ms_us.py', 'extmod/uheapq_timeq.py', + # unicode char issue + 'extmod/ujson_loads.py', + # doesn't output to python stdout + 'extmod/ure_debug.py', + 'extmod/vfs_basic.py', 'extmod/vfs_fat_ramdisk.py', 'extmod/vfs_fat_fileio.py', 'extmod/vfs_fat_fsusermount.py', 'extmod/vfs_fat_oldproto.py', + # rounding issues + 'float/float_divmod.py', # requires double precision floating point to work 'float/float2int_doubleprec_intbig.py', - # inline asm tests + 'float/float_parse_doubleprec.py', + # inline asm FP tests (require Cortex-M4) 'inlineasm/asmfpaddsub.py', 'inlineasm/asmfpcmp.py', 'inlineasm/asmfpldrstr.py', 'inlineasm/asmfpmuldiv.py','inlineasm/asmfpsqrt.py', + # different filename in output + 'micropython/emg_exc.py', + 'micropython/heapalloc_traceback.py', + # pattern matching in .exp + 'micropython/meminfo.py', ) output = [] -- cgit v1.2.3 From 43141ddb55c715e90b60d2fa2d9d5e01cc66b511 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 13 Dec 2017 18:32:25 +0200 Subject: tools/tinytest-codegen: Take --target= option for test set selection. Gets passed to run-tests --list-tests to get actual list of tests to use. If --target= is not given, legacy set hardcoded in tinytest-codegen itself is used. Also, get rid of tinytest test groups - they aren't really used for anything, and only complicate processing. Besides, one of the next step is to limit number of tests per a generated file to control the binary size, which also will require "flat" list of tests. --- tools/tinytest-codegen.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index d7761cd5a..c6ba1867a 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -3,6 +3,8 @@ import os, sys from glob import glob from re import sub +import argparse + def escape(s): s = s.decode() @@ -17,7 +19,7 @@ def escape(s): return "\"\"\n\"{}\"".format(''.join([lookup[x] if x in lookup else x for x in s])) def chew_filename(t): - return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t.split('/')[1] } + return { 'func': "test_{}_fn".format(sub(r'/|\.|-', '_', t)), 'desc': t } def script_to_map(test_file): r = {"name": chew_filename(test_file)["func"]} @@ -47,7 +49,7 @@ testgroup_struct = ( "struct testgroup_t groups[] = {{\n{body}\n END_OF_GROUPS\n}};" ) testgroup_member = ( - " {{ \"{name}/\", {name}_tests }}," + " {{ \"{name}\", {name}_tests }}," ) ## XXX: may be we could have `--without ` argument... @@ -82,14 +84,24 @@ exclude_tests = ( ) output = [] +tests = [] + +argparser = argparse.ArgumentParser(description='Convert native MicroPython tests to tinytest/upytesthelper C code') +argparser.add_argument('--target', help='the target platform') +args = argparser.parse_args() + +if not args.target: + for group in test_dirs: + tests += [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] +else: + for l in os.popen("./run-tests --list-tests --target=%s" % args.target, "r"): + tests.append(l.rstrip()) -for group in test_dirs: - tests = [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] - 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] - output.append(testcase_struct.format(name=group, body='\n'.join(testcase_members))) +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] +output.append(testcase_struct.format(name="", body='\n'.join(testcase_members))) -testgroup_members = [testgroup_member.format(name=group) for group in test_dirs] +testgroup_members = [testgroup_member.format(name=group) for group in [""]] output.append(testgroup_struct.format(body='\n'.join(testgroup_members))) -- cgit v1.2.3 From 325d0fc74ba9634f89f6535210944ccdd635a995 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 14 Dec 2017 12:26:59 +0200 Subject: tools/tinytest-codegen: Add --stdin switch instead of recently added --target. Instead of passing thru more and more options from tinytest-codegen to run-tests --list-tests, pipe output of run-tests --list-tests into tinytest-codegen. --- tools/tinytest-codegen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index c6ba1867a..5339972cd 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -87,14 +87,14 @@ output = [] tests = [] argparser = argparse.ArgumentParser(description='Convert native MicroPython tests to tinytest/upytesthelper C code') -argparser.add_argument('--target', help='the target platform') +argparser.add_argument('--stdin', action="store_true", help='read list of tests from stdin') args = argparser.parse_args() -if not args.target: +if not args.stdin: for group in test_dirs: tests += [test for test in glob('{}/*.py'.format(group)) if test not in exclude_tests] else: - for l in os.popen("./run-tests --list-tests --target=%s" % args.target, "r"): + for l in sys.stdin: tests.append(l.rstrip()) output.extend([test_function.format(**script_to_map(test)) for test in tests]) -- cgit v1.2.3 From 4475f324202e15b98f2f365e33078aebcf0bc5a5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 15 Dec 2017 11:37:32 +0200 Subject: tools/tinytest-codegen: Ignore system locale, write output in UTF-8. Way to reproduce a UnicodeEncodeError without this patch: LC_ALL=C tinytest-codegen.py ... --- tools/tinytest-codegen.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tools/tinytest-codegen.py') diff --git a/tools/tinytest-codegen.py b/tools/tinytest-codegen.py index 5339972cd..ad3b3bbec 100755 --- a/tools/tinytest-codegen.py +++ b/tools/tinytest-codegen.py @@ -106,4 +106,5 @@ testgroup_members = [testgroup_member.format(name=group) for group in [""]] output.append(testgroup_struct.format(body='\n'.join(testgroup_members))) ## XXX: may be we could have `--output ` argument... -print('\n\n'.join(output)) +# Don't depend on what system locale is set, use utf8 encoding. +sys.stdout.buffer.write('\n\n'.join(output).encode('utf8')) -- cgit v1.2.3