From d96a5b11f7574d7214509c750d94e043a79424d3 Mon Sep 17 00:00:00 2001 From: jgirardet Date: Thu, 4 Apr 2019 21:32:04 +0200 Subject: [PATCH] add resources to snippets --- .gitignore | 1 + tests/snippets/builtin_slice.py | 66 +- tests/snippets/slice_res.py | 2252 ------------------------------- tests/test_snippets.py | 112 +- 4 files changed, 104 insertions(+), 2327 deletions(-) delete mode 100644 tests/snippets/slice_res.py diff --git a/.gitignore b/.gitignore index 967a51a83..e9dd6d220 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ __pycache__ .vscode wasm-pack.log .idea/ +tests/snippets/resources \ No newline at end of file diff --git a/tests/snippets/builtin_slice.py b/tests/snippets/builtin_slice.py index 619356211..d61698034 100644 --- a/tests/snippets/builtin_slice.py +++ b/tests/snippets/builtin_slice.py @@ -78,53 +78,35 @@ def test_all_slices(): """ test all possible slices except big number """ - MODE = None # set to "build" to rebuild slice_res.py - ll = [0, 1, 2, 3] - start = list(range(-7, 7)) - end = list(range(-7, 7)) - step = list(range(-5, 5)) - step.pop(step.index(0)) - for i in [start, end, step]: - i.append(None) + import resources + from resources.cpython_generated_slices import SLICES_RES, START, END, STEP, LL - def build(): - # loop used to build slices_res.py with cpython - with open("slice_res.py", "wt") as f: - for s in start: - for e in end: - for t in step: - f.write(str(ll[s:e:t]) + "\n") + ll = LL + start = START + end = END + step = STEP - def run(): - # test utility - from slice_res import SLICES_RES - - count = 0 - failures = [] - for s in start: - for e in end: - for t in step: - lhs = ll[s:e:t] - try: - assert lhs == SLICES_RES[count] - except AssertionError: - failures.append( - "start: {} ,stop: {}, step {}. Expected: {}, found: {}".format( - s, e, t, lhs, SLICES_RES[count] - ) + count = 0 + failures = [] + for s in start: + for e in end: + for t in step: + lhs = ll[s:e:t] + try: + assert lhs == SLICES_RES[count] + except AssertionError: + failures.append( + "start: {} ,stop: {}, step {}. Expected: {}, found: {}".format( + s, e, t, lhs, SLICES_RES[count] ) - count += 1 + ) + count += 1 - if failures: - for f in failures: - print(f) - print(len(failures), "slices failed") - - if MODE == "build": - build() - else: - run() + if failures: + for f in failures: + print(f) + print(len(failures), "slices failed") test_all_slices() diff --git a/tests/snippets/slice_res.py b/tests/snippets/slice_res.py deleted file mode 100644 index 38eb87729..000000000 --- a/tests/snippets/slice_res.py +++ /dev/null @@ -1,2252 +0,0 @@ -SLICES_RES = [ - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [0], - [0], - [0], - [0], - [0], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [1, 2], - [1], - [1], - [1], - [1, 2], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [1, 2], - [1], - [1], - [1], - [1, 2], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [1], - [1], - [1], - [1], - [1, 0], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2, 1], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2, 1], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [2, 3], - [2], - [2], - [2], - [2, 3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [3], - [3], - [3], - [3], - [3], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [0], - [0], - [0], - [0], - [0], - [], - [], - [], - [], - [], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [0], - [0], - [0], - [0], - [0], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1, 0], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [1, 2], - [1], - [1], - [1], - [1, 2], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [1], - [1], - [1], - [1], - [1], - [], - [], - [], - [], - [], - [1, 2], - [1], - [1], - [1], - [1, 2], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [], - [], - [], - [], - [], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [1], - [1], - [1], - [1], - [1, 0], - [1, 2, 3], - [1, 3], - [1], - [1], - [1, 2, 3], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2, 1], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2], - [2, 1], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [2], - [2], - [2], - [2], - [2], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [], - [], - [], - [], - [], - [2, 3], - [2], - [2], - [2], - [2, 3], - [2], - [2], - [2], - [2, 0], - [2, 1, 0], - [2, 3], - [2], - [2], - [2], - [2, 3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [3], - [3], - [3], - [3], - [3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [0], - [0], - [0], - [0], - [0], - [3], - [3], - [3], - [3], - [3], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [3], - [3], - [3], - [3, 1], - [3, 2, 1], - [], - [], - [], - [], - [], - [3], - [3], - [3], - [3], - [3, 2], - [0], - [0], - [0], - [0], - [0], - [3], - [3], - [3], - [3], - [3], - [0, 1], - [0], - [0], - [0], - [0, 1], - [], - [], - [], - [], - [], - [0, 1, 2], - [0, 2], - [0], - [0], - [0, 1, 2], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [], - [], - [], - [], - [], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], - [3], - [3], - [3, 0], - [3, 1], - [3, 2, 1, 0], - [0, 1, 2, 3], - [0, 2], - [0, 3], - [0], - [0, 1, 2, 3], -] diff --git a/tests/test_snippets.py b/tests/test_snippets.py index 7edbd7b7e..7a888f093 100644 --- a/tests/test_snippets.py +++ b/tests/test_snippets.py @@ -1,4 +1,3 @@ - # This is a python unittest class automatically populating with all tests # in the tests folder. @@ -11,6 +10,8 @@ import logging import subprocess import contextlib import enum +from pathlib import Path +import shutil import compile_code @@ -19,14 +20,15 @@ class _TestType(enum.Enum): functional = 1 -logger = logging.getLogger('tests') -ROOT_DIR = '..' -TEST_ROOT = os.path.abspath(os.path.join(ROOT_DIR, 'tests')) -TEST_DIRS = { - _TestType.functional: os.path.join(TEST_ROOT, 'snippets'), -} -CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'py_code_object')) +logger = logging.getLogger("tests") +ROOT_DIR = ".." +TEST_ROOT = os.path.abspath(os.path.join(ROOT_DIR, "tests")) +TEST_DIRS = {_TestType.functional: os.path.join(TEST_ROOT, "snippets")} +CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, "py_code_object")) RUSTPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR)) +RESOURCES_DIR = os.path.abspath( + os.path.join(TEST_DIRS[_TestType.functional], "resources") +) @contextlib.contextmanager @@ -38,12 +40,12 @@ def pushd(path): def perform_test(filename, method, test_type): - logger.info('Running %s via %s', filename, method) - if method == 'cpython': + logger.info("Running %s via %s", filename, method) + if method == "cpython": run_via_cpython(filename) - elif method == 'cpython_bytecode': + elif method == "cpython_bytecode": run_via_cpython_bytecode(filename, test_type) - elif method == 'rustpython': + elif method == "rustpython": run_via_rustpython(filename, test_type) else: raise NotImplementedError(method) @@ -57,27 +59,27 @@ def run_via_cpython(filename): def run_via_cpython_bytecode(filename, test_type): # Step1: Create bytecode file: - bytecode_filename = filename + '.bytecode' - with open(bytecode_filename, 'w') as f: + bytecode_filename = filename + ".bytecode" + with open(bytecode_filename, "w") as f: compile_code.compile_to_bytecode(filename, out_file=f) # Step2: run cpython bytecode: env = os.environ.copy() - env['RUST_LOG'] = 'info,cargo=error,jobserver=error' - env['RUST_BACKTRACE'] = '1' + env["RUST_LOG"] = "info,cargo=error,jobserver=error" + env["RUST_BACKTRACE"] = "1" with pushd(CPYTHON_RUNNER_DIR): - subprocess.check_call(['cargo', 'run', bytecode_filename], env=env) + subprocess.check_call(["cargo", "run", bytecode_filename], env=env) def run_via_rustpython(filename, test_type): env = os.environ.copy() - env['RUST_LOG'] = 'info,cargo=error,jobserver=error' - env['RUST_BACKTRACE'] = '1' + env["RUST_LOG"] = "info,cargo=error,jobserver=error" + env["RUST_BACKTRACE"] = "1" - target = 'release' - if env.get('CODE_COVERAGE', 'false') == 'true': - target = 'debug' - binary = os.path.abspath(os.path.join(ROOT_DIR, 'target', target, 'rustpython')) + target = "release" + if env.get("CODE_COVERAGE", "false") == "true": + target = "debug" + binary = os.path.abspath(os.path.join(ROOT_DIR, "target", target, "rustpython")) subprocess.check_call([binary, filename], env=env) @@ -85,15 +87,15 @@ def run_via_rustpython(filename, test_type): def create_test_function(cls, filename, method, test_type): """ Create a test function for a single snippet """ core_test_directory, snippet_filename = os.path.split(filename) - test_function_name = 'test_{}_'.format(method) \ - + os.path.splitext(snippet_filename)[0] \ - .replace('.', '_').replace('-', '_') + test_function_name = "test_{}_".format(method) + os.path.splitext(snippet_filename)[ + 0 + ].replace(".", "_").replace("-", "_") def test_function(self): perform_test(filename, method, test_type) if hasattr(cls, test_function_name): - raise ValueError('Duplicate test case {}'.format(test_function_name)) + raise ValueError("Duplicate test case {}".format(test_function_name)) setattr(cls, test_function_name, test_function) @@ -103,25 +105,69 @@ def populate(method): for test_type, filename in get_test_files(): create_test_function(cls, filename, method, test_type) return cls + return wrapper def get_test_files(): """ Retrieve test files """ for test_type, test_dir in TEST_DIRS.items(): - for filepath in sorted(glob.iglob(os.path.join(test_dir, '*.py'))): + for filepath in sorted(glob.iglob(os.path.join(test_dir, "*.py"))): filename = os.path.split(filepath)[1] - if filename.startswith('xfail_'): + if filename.startswith("xfail_"): continue yield test_type, os.path.abspath(filepath) -@populate('cpython') +def generate_slices(path): + # loop used to build slices_res.py with cpython + ll = [0, 1, 2, 3] + start = list(range(-7, 7)) + end = list(range(-7, 7)) + step = list(range(-5, 5)) + step.pop(step.index(0)) + for i in [start, end, step]: + i.append(None) + + slices_res = [] + for s in start: + for e in end: + for t in step: + slices_res.append(ll[s:e:t]) + + path.write_text( + "SLICES_RES={}\nSTART= {}\nEND= {}\nSTEP= {}\nLL={}\n".format( + slices_res, start, end, step, ll + ) + ) + + +@populate("cpython") # @populate('cpython_bytecode') -@populate('rustpython') +@populate("rustpython") class SampleTestCase(unittest.TestCase): @classmethod def setUpClass(cls): - subprocess.check_call(['cargo', 'build']) - subprocess.check_call(['cargo', 'build', '--release']) + # setup resource dir + cls.resources = Path(RESOURCES_DIR) + if cls.resources.exists(): + shutil.rmtree( + RESOURCES_DIR, ignore_errors=True + ) # we don't care if dir doesnt exist + cls.resources.mkdir() + (cls.resources / "__init__.py").touch() + + # Here add resource files + cls.slices_resource_path = Path(RESOURCES_DIR) / "cpython_generated_slices.py" + generate_slices(cls.slices_resource_path) + + # cargo stuff + subprocess.check_call(["cargo", "build"]) + subprocess.check_call(["cargo", "build", "--release"]) + + @classmethod + def tearDownClass(cls): + shutil.rmtree( + RESOURCES_DIR, ignore_errors=True + ) # we don't care if dir doesnt exist