From 7e8a77e6baa5e548038aca8d078aab512bf1beb7 Mon Sep 17 00:00:00 2001 From: Shaariq Chundrigar Date: Thu, 29 Aug 2019 12:52:12 +1000 Subject: [PATCH] Added WIP testing --- .../longest_increasing_subsequence.py | 4 +- .../longest_increasing_subsequence/tests.json | 47 ++++++++++--------- tests.py | 32 +++++++++++++ tests/test_medium.py | 0 4 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 tests.py delete mode 100644 tests/test_medium.py diff --git a/hard/longest_increasing_subsequence/longest_increasing_subsequence.py b/hard/longest_increasing_subsequence/longest_increasing_subsequence.py index 062198c..7b797be 100644 --- a/hard/longest_increasing_subsequence/longest_increasing_subsequence.py +++ b/hard/longest_increasing_subsequence/longest_increasing_subsequence.py @@ -1,6 +1,6 @@ import json -def longestIncreasingSubsequence(nums): +def longest_increasing_subsequence(nums): """ Returns length of longest increasing subsequence given an array of numbers. """ @@ -33,7 +33,7 @@ def longestIncreasingSubsequence(nums): testNumber = d['desc'] args = d['args'][0] expected = d['res'] - actual = longestIncreasingSubsequence(args) + actual = longest_increasing_subsequence(args) if not actual == expected: failedTests.append(testNumber) allTestsPass = actual == expected and allTestsPass diff --git a/hard/longest_increasing_subsequence/tests.json b/hard/longest_increasing_subsequence/tests.json index e353532..5bf6ad7 100644 --- a/hard/longest_increasing_subsequence/tests.json +++ b/hard/longest_increasing_subsequence/tests.json @@ -1,22 +1,25 @@ -[ - { - "desc": 1, - "args": [[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]], - "res": 6 - }, - { - "desc": 2, - "args": [[1, 2, 3, 4, 5]], - "res": 5 - }, - { - "desc": 3, - "args": [[1, 2, 0, 4, 5]], - "res": 4 - }, - { - "desc": 4, - "args": [[1, 2, 0, -4, 5]], - "res": 3 - } -] +{ + "title": "longest_increasing_subsequence", + "tests": [ + { + "desc": 1, + "args": [[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]], + "res": 6 + }, + { + "desc": 2, + "args": [[1, 2, 3, 4, 5]], + "res": 5 + }, + { + "desc": 3, + "args": [[1, 2, 0, 4, 5]], + "res": 4 + }, + { + "desc": 4, + "args": [[1, 2, 0, -4, 5]], + "res": 3 + } + ] +} diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..415e4d5 --- /dev/null +++ b/tests.py @@ -0,0 +1,32 @@ +import os, json, sys +from hard.longest_increasing_subsequence.longest_increasing_subsequence import * +tests_to_run = [] + +# Walk through directories, and look for tests.json files +for root, dirs, files in os.walk('.'): + for file in files: + if file.endswith("tests.json"): + with open(os.path.join(root, file), 'r') as f: + tests_to_run.append(json.load(f)) + +all_tests_passed = True +# Go through parsed tests +for tests in tests_to_run: + function = tests['title'] + for t in tests['tests']: + parameters = ["(", None, ")"] + parameters[1] = ",".join([str(x) for x in t['args']]) + parameters = "".join(parameters) + actual_result = eval(function + parameters) # Build function with parameters and evaluates it + expected_result = t['res'] + + # Prints test details if test failed + if actual_result != expected_result: + print("{}: Failed at test case {}".format(function, t['desc'])) + print("Input: {}".format(t['args'])) + print("Actual: {}".format(actual_result)) + print("Expected: {}".format(expected_result)) + all_tests_passed = False + +if all_tests_passed: + print("All tests passed") \ No newline at end of file diff --git a/tests/test_medium.py b/tests/test_medium.py deleted file mode 100644 index e69de29..0000000