Skip to content

Instantly share code, notes, and snippets.

@talvik
Created March 16, 2020 02:11
Show Gist options
  • Save talvik/0a59f229e84bb090545c1da7fab8254e to your computer and use it in GitHub Desktop.
Save talvik/0a59f229e84bb090545c1da7fab8254e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import subprocess
import sys
import time
from dataclasses import dataclass
docopt = """
Testerator, a python exercise tester
Usage:
testerator [target file] (opts)
testerator 'py.py' --watch # watch file for changes and execute test
testerator './basic algs/lista2.6.py' # execute tests in 'lista2.6.py'
Options:
--help Show this screen.
--watch Watch for changes and run test
"""
@dataclass
class TestCase:
name: str
inputs: []
outputs: []
def test(file):
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
print("Testing file '{}'...".format(file))
if not check_syntax(file):
print("Syntax invalid for '{}'".format(file))
return 1
test_cases = parse_src_file(file)
for test_case in test_cases:
run_test(file, test_case)
def run_test(file, test_case):
p = subprocess.Popen(["python3", file],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for input in test_case.inputs:
p.stdin.write(input.encode())
p.stdin.write(b"\n")
p.stdin.close()
while p.returncode is None:
p.poll()
result = p.stdout.read()
result = str(result).split("\\n")[-len(test_case.outputs)-1:-1]
result[0] = result[0].split(" ")[-1]
if test_case.outputs != result:
print("Test case:", test_case.name, "ERROR")
print("Expected:", test_case.outputs)
print(" Actual:", result)
if (len(test_case.outputs) != len(result)):
print("Expected lenght", len(test_case.outputs), "actual", len(result))
else:
print("Test case:", test_case.name, "OK")
return test_case.outputs == result
def check_syntax(file):
p = subprocess.Popen(["python3", "-m", "py_compile", file])
while p.returncode is None:
p.poll()
return p.returncode == 0
def parse_src_file(file):
py_src = open(file, "r")
lines = py_src.readlines()
line_iter = iter(lines)
test_cases = []
for line in line_iter:
if line.startswith("# ex: "):
testName = line[len("# ex: "):-1]
input = next(line_iter)[len("# in: "):-1].split(" ")
output = next(line_iter)[len("# out: "):-1].split(" ")
test_cases.append(TestCase(name=testName, inputs=input, outputs=output))
return test_cases
print(sys.argv)
if len(sys.argv) < 2:
print(docopt)
exit(1)
target = sys.argv[1]
if not os.path.exists(target):
print(docopt)
print("'{}' does not exist".format(target))
exit(1)
test(target)
if "--watch" in sys.argv:
last_update = os.stat(target).st_mtime
while True:
time.sleep(0.50)
current = os.stat(target).st_mtime
if current > last_update:
test(target)
last_update = current
'''
Print n multiples of input i and j
'''
i = int(input("i = "))
j = int(input("j = "))
n = int(input("n = "))
mult_i = 0
mult_j = 0
for _ in range(0, n):
mult_i += i
mult_j += j
if mult_j == mult_i:
print(mult_i)
elif mult_i < mult_j:
print(mult_i)
mult_j -= j
else:
mult_i -= i
print(mult_j)
# ex: Simple
# in: 2 3 10
# out: 2 3 4 6 8 9 10 12 14 15
# ex: Big prime
# in: 13 59 11
# out: 13 26 39 52 59 65 78 91 104 117 118
# ex: Huge nums
# in: 1000000000 1000000001 4
# out: 1000000000 1000000001 2000000000 2000000002
# ex: OK
# in: 2 3 10
# out: 2 3 4 6 8 9 10 12 14 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment