コード例 #1
0
ファイル: tests.py プロジェクト: valeriehk/course
def test_stubborn_asker(low, high):
    """Test the stubborn asker function."""
    try:
        exercise1 = loadExerciseFile(weekNumber=WEEK_NUMBER, exerciseNumber=1)
    except Exception as e:
        return syntax_error_message(4, e)

    mockInputs = list(range(low - 25, high + 20, 5))
    try:
        with mock.patch("builtins.input", side_effect=mockInputs):
            try:
                x = exercise1.stubborn_asker(low, high)
                if x is not None:
                    result = low <= x <= high
                    return result
                else:
                    print(
                        "Trouble testing the test_stubborn_asker.",
                        "Maybe you haven't started yet?",
                    )
            except Exception as e:
                print(e)
            return
    except Exception as e:
        print("exception:", e)
コード例 #2
0
ファイル: tests.py プロジェクト: valeriehk/course
def test_super_asker(low, high):
    """Test the super asker function."""
    try:
        exercise1 = loadExerciseFile(weekNumber=WEEK_NUMBER, exerciseNumber=1)
    except Exception as e:
        return syntax_error_message(1, e)

    dirty_things = ["aword", [1, 2, 3], {"an": "object"}]
    neat_range = list(range(low - 25, high + 20, 5))
    mockInputs = dirty_things + neat_range
    with mock.patch("builtins.input", side_effect=mockInputs):
        try:
            my_args = (low, high)
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise1.super_asker,
                                   args=my_args)
            return message
        except FunctionTimedOut:
            print(
                "{f}({args}) could not complete within {t} seconds and was killed."
                .format(f=sys._getframe().f_code.co_name,
                        args=my_args,
                        t=TIMEOUT_IN_SECONDS))
        except Exception as e:
            print(e)
コード例 #3
0
def vis_binary_search_performance(path="."):
    """Provide a visualisation of the performance of the binary search."""
    try:
        path = "exercise4.py"
        exercise4 = imp.load_source("exercise4", path)
    except Exception as e:
        return syntax_error_message(4, e)

    import matplotlib.pyplot as plt
    BASE2 = 2
    results = []
    testRuns = 1000
    for _ in range(testRuns):
        low = random.randint(-100, 100)
        high = random.randint(low + 2, 200)
        guess = random.randint(low + 1, high - 1)
        bs = exercise4.binary_search(low, high, guess)
        # print bs, low, high, guess
        tries = bs['tries']
        worst = math.log(high - low, BASE2)
        ratio = tries/worst
        results.append(ratio)
    plt.hist(results)
    plt.title("Proportion of worst case performance "
              "over {} iterations".format(testRuns))
    print("""
This histogram shows the number of guesses that it took the search to
find the answer. The big O worst case is the base 2 log of the range that
you're guessing within. In other words, what power of two fills that space?
E.g. if your range is 16, then the worst case is 4 guesses: 2×2×2×2 = 16
Think back to when you were playing the game with your brain, sometimes
you'd go over the worst case because you aren't a perfect arithmatic
machine but the computer is, so it's always below that worst case limit.
            Close the histogram to finish running the tests.""")
    plt.show()
コード例 #4
0
ファイル: tests.py プロジェクト: valeriehk/course
def test_example_guessingGame():
    """Test the example_guessingGame function.

    This should always pass becasue it's provided code
    """
    try:
        exercise2 = loadExerciseFile(weekNumber=WEEK_NUMBER, exerciseNumber=2)
    except Exception as e:
        return syntax_error_message(2, e)
    upperBound = 5
    guesses = list(range(5 + 1))
    mockInputs = [upperBound] + guesses
    with mock.patch("builtins.input", side_effect=mockInputs):
        try:
            my_args = None
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise2.exampleGuessingGame,
                                   args=my_args)

            return message == "You got it!"
        except FunctionTimedOut:
            print(
                "{f}({args}) could not complete within {t} seconds and was killed."
                .format(f=sys._getframe().f_code.co_name,
                        args=my_args,
                        t=TIMEOUT_IN_SECONDS))
        except Exception as e:
            print(e)
コード例 #5
0
ファイル: tests.py プロジェクト: YanniWang0522/course
def test_example_guessingGame(repo_path):
    """Test the example_guessingGame function.

    This should always pass becasue it's provided code
    """
    try:
        exercise2 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=2)
    except Exception as e:
        return syntax_error_message(2, e)
    upperBound = 5
    guesses = list(range(5 + 1))
    mockInputs = [upperBound] + guesses
    with mock.patch("builtins.input", side_effect=mockInputs):
        my_args = None
        try:
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise2.exampleGuessingGame,
                                   args=my_args)

            return message == "You got it!"
        except FunctionTimedOut:
            timeout_message(
                function_name=sys._getframe().f_code.co_name,
                args=my_args,
                timeout_in_seconds=TIMEOUT_IN_SECONDS,
            )
        except Exception as e:
            print(e)
コード例 #6
0
ファイル: tests.py プロジェクト: YanniWang0522/course
def test_not_number_rejector(repo_path):
    """Test the not number rejector function."""
    try:
        exercise1 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=1)
    except Exception as e:
        return syntax_error_message(1, e)

    mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40]
    with mock.patch("builtins.input", side_effect=mockInputs):
        my_args = "Testing some values:"
        try:
            r = func_timeout(TIMEOUT_IN_SECONDS,
                             exercise1.not_number_rejector,
                             args=[my_args])
            return r
        except FunctionTimedOut:
            timeout_message(
                function_name=sys._getframe().f_code.co_name,
                args=my_args,
                timeout_in_seconds=TIMEOUT_IN_SECONDS,
            )
        except Exception as e:
            print("exception:", e)
コード例 #7
0
ファイル: tests.py プロジェクト: YanniWang0522/course
def test_advanced_guessingGame(repo_path, mockInputs):
    """Test the advanced_guessingGame function."""
    try:
        exercise3 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=3)
    except Exception as e:
        return syntax_error_message(3, e)

    with mock.patch("builtins.input", side_effect=mockInputs):
        my_args = None
        try:
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise3.advancedGuessingGame,
                                   args=my_args)

            return message == "You got it!"
        except FunctionTimedOut:
            timeout_message(
                function_name=sys._getframe().f_code.co_name,
                args=my_args,
                timeout_in_seconds=TIMEOUT_IN_SECONDS,
            )
        except Exception as e:
            print(e)
コード例 #8
0
ファイル: tests.py プロジェクト: shweric/me
def test_advanced_guessingGame(repo_path, mockInputs):
    """Test the advanced_guessingGame function."""
    try:
        exercise3 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=3)
    except Exception as e:
        return syntax_error_message(3, e)

    with mock.patch("builtins.input", side_effect=mockInputs):
        try:
            my_args = None
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise3.advancedGuessingGame,
                                   args=my_args)

            return message == "You got it!"
        except FunctionTimedOut:
            print(
                "{f}({args}) could not complete within {t} seconds and was killed."
                .format(f=sys._getframe().f_code.co_name,
                        args=my_args,
                        t=TIMEOUT_IN_SECONDS))
        except Exception as e:
            print(e)
コード例 #9
0
def test_not_number_rejector(repo_path):
    """Test the not number rejector function."""
    try:
        exercise1 = loadExerciseFile(
            repo_path, weekNumber=WEEK_NUMBER, exerciseNumber=1
        )
    except Exception as e:
        return syntax_error_message(1, e)

    mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40]
    with mock.patch("builtins.input", side_effect=mockInputs):
        try:
            my_args = "Testing some values:"
            r = func_timeout(
                TIMEOUT_IN_SECONDS, exercise1.not_number_rejector, args=my_args
            )
            return r
        except FunctionTimedOut:
            print(
                "{f}({args}) could not complete within {t} seconds and was killed.".format(
                    f=sys._getframe().f_code.co_name, args=my_args, t=TIMEOUT_IN_SECONDS
                )
            )
        except Exception as e:
            print("exception:", e)
コード例 #10
0
ファイル: tests.py プロジェクト: YanniWang0522/course
def test_super_asker(repo_path, low, high):
    """Test the super asker function."""
    try:
        exercise1 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=1)
    except Exception as e:
        return syntax_error_message(1, e)

    dirty_things = ["aword", [1, 2, 3], {"an": "object"}]
    neat_range = list(range(low - 25, high + 20, 5))
    mockInputs = dirty_things + neat_range
    with mock.patch("builtins.input", side_effect=mockInputs):
        my_args = (low, high)
        try:
            message = func_timeout(TIMEOUT_IN_SECONDS,
                                   exercise1.super_asker,
                                   args=my_args)
            return message
        except FunctionTimedOut:
            timeout_message(
                function_name=sys._getframe().f_code.co_name,
                args=my_args,
                timeout_in_seconds=TIMEOUT_IN_SECONDS,
            )
        except Exception as e:
            print(e)
コード例 #11
0
def test_binary_search(repo_path, low, high, actual):
    # TODO: I don't think this test is working
    """Test the binary search function.

    checks to see that it's searching better than O(log n)
    """
    try:
        exercise4 = loadExerciseFile(
            repo_path, weekNumber=WEEK_NUMBER, exerciseNumber=4
        )
        BASE2 = 2
        b = None
        try:
            my_args = (low, high, actual)
            b = func_timeout(TIMEOUT_IN_SECONDS, exercise4.binary_search, args=my_args)
            b["WorstCaseO"] = math.log(high - low, BASE2)
            if b is not None:
                if b["tries"] is not 0 and b["tries"] < b["WorstCaseO"]:
                    print("b", b)
                    print("snuck it in")
                    return True
                elif b["tries"] is 0:
                    print(
                        "Tries is 0, that probably means that you haven't started yet"
                    )
                else:
                    print(
                        (
                            "That took {t} tries, you "
                            "should get it in under {o} tries"
                        ).format(t=b["tries"], o=b["WorstCaseO"])
                    )
            else:
                return False
        except FunctionTimedOut:
            print(
                "{f}({args}) could not complete within {t} seconds and was killed.".format(
                    f=sys._getframe().f_code.co_name, args=my_args, t=TIMEOUT_IN_SECONDS
                )
            )
            return False
        except Exception as e:
            syntax_error_message(4, e)
            return False
    except Exception as e:
        syntax_error_message(4, e)
        return False
コード例 #12
0
ファイル: tests.py プロジェクト: SijiaPei/code1161
def test_advanced_guessingGame(path, mockInputs):
    """Test the advanced_guessingGame function."""
    try:
        path = "exercise3.py"
        exercise3 = imp.load_source("exercise3", path)
    except Exception as e:
        return syntax_error_message(3, e)

    try:
コード例 #13
0
ファイル: tests.py プロジェクト: YanniWang0522/course
def test_binary_search(repo_path, low, high, actual, label):
    """Test the binary search function.

    checks to see that it's searching better than O(log n)
    """
    try:
        exercise4 = loadExerciseFile(repo_path,
                                     weekNumber=WEEK_NUMBER,
                                     exerciseNumber=4)
        BASE2 = 2
        b = None
        my_args = (low, high, actual)
        try:
            b = func_timeout(TIMEOUT_IN_SECONDS,
                             exercise4.binary_search,
                             args=my_args)
            b["WorstCaseO"] = math.log(high - low, BASE2)
            if b is not None:
                if b["tries"] is not 0 and b["tries"] < b["WorstCaseO"]:
                    print("b", b)
                    print("snuck it in")
                    return True
                elif b["tries"] is 0:
                    print(
                        "Tries is 0, that probably means that you haven't started yet"
                    )
                else:
                    print(f"That took {b['tries']} tries, you "
                          f"should get it in under {b['WorstCaseO']} tries")
            else:
                return False
        except FunctionTimedOut:
            timeout_message(
                function_name=sys._getframe().f_code.co_name,
                args=my_args,
                timeout_in_seconds=TIMEOUT_IN_SECONDS,
            )
            return False
        except Exception as e:
            syntax_error_message(4, e)
            return False
    except Exception as e:
        syntax_error_message(4, e)
        return False
コード例 #14
0
ファイル: tests.py プロジェクト: SijiaPei/code1161
def test_not_number_rejector(path):
    """Test the not number rejector function."""
    try:
        path = "exercise1.py"
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(1, e)

    mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40]
    try:
コード例 #15
0
ファイル: tests.py プロジェクト: SijiaPei/code1161
def test_super_asker(path, low, high):
    """Test the super asker function."""
    try:
        path = "exercise1.py"
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(1, e)

    dirty_things = ["aword", [1, 2, 3], {"an": "object"}]
    neat_range = list(range(low - 25, high + 20, 5))
    mockInputs = dirty_things + neat_range
    try:
コード例 #16
0
def test_advanced_guessingGame(path, mockInputs):
    """Test the advanced_guessingGame function."""
    try:
        path = "exercise3.py"
        exercise3 = imp.load_source("exercise3", path)
    except Exception as e:
        return syntax_error_message(3, e)

    try:
        # with Timeout(3):
        with mock.patch('builtins.input', side_effect=mockInputs):
            return exercise3.advancedGuessingGame() == "You got it!"
    except Exception as e:
        print("exception:", e)
コード例 #17
0
ファイル: tests.py プロジェクト: SijiaPei/code1161
def test_example_guessingGame(path):
    """Test the example_guessingGame function.

    This should always pass becasue it's provided code
    """
    try:
        path = "exercise2.py"
        exercise2 = imp.load_source("exercise2", path)
    except Exception as e:
        return syntax_error_message(2, e)
    upperBound = 5
    guesses = list(range(5+1))
    mockInputs = [upperBound] + guesses
    try:
コード例 #18
0
def test_not_number_rejector(path):
    """Test the not number rejector function."""
    try:
        path = "exercise1.py"
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(1, e)

    mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40]
    try:
        # with Timeout(3):
        with mock.patch('builtins.input', side_effect=mockInputs):
            return exercise1.not_number_rejector("Testing some values:")
    except Exception as e:
        print("exception:", e)
コード例 #19
0
def test_stubborn_asker(path, low, high):
    """Test the stubborn asker function."""
    try:
        path = "exercise1.py"
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(4, e)

    mockInputs = list(range(low - 25, high + 20, 5))
    try:
        # with Timeout(3):
        with mock.patch('builtins.input', side_effect=mockInputs):
            return low <= exercise1.stubborn_asker(low, high) <= high
    except Exception as e:
        print("exception:", e)
コード例 #20
0
def test_stubborn_asker(path, low, high):
    """Test the stubborn asker function."""
    try:
        path = "{}/week{}/exercise1.py".format(path, WEEK_NUMBER)
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(4, e)

    mockInputs = range(low - 25, high + 20, 5)
    try:
        with Timeout(3):
            with mock.patch('__builtin__.raw_input', side_effect=mockInputs):
                return low <= exercise1.stubborn_asker(low, high) <= high
    except Exception as e:
        print("exception:", e)
コード例 #21
0
def test_super_asker(path, low, high):
    """Test the super asker function."""
    try:
        path = "exercise1.py"
        exercise1 = imp.load_source("exercise1", path)
    except Exception as e:
        return syntax_error_message(1, e)

    dirty_things = ["aword", [1, 2, 3], {"an": "object"}]
    neat_range = list(range(low - 25, high + 20, 5))
    mockInputs = dirty_things + neat_range
    try:
        # with Timeout(3):
        with mock.patch('builtins.input', side_effect=mockInputs):
            return exercise1.super_asker(low, high)
    except Exception as e:
        print("exception:", e)
コード例 #22
0
def test_example_guessingGame(path):
    """Test the example_guessingGame function.
    This should always pass becasue it's provided code
    """
    try:
        path = "exercise2.py"
        exercise2 = imp.load_source("exercise2", path)
    except Exception as e:
        return syntax_error_message(2, e)
    upperBound = 5
    guesses = list(range(5+1))
    mockInputs = [upperBound] + guesses
    try:
        # with Timeout(3):
        with mock.patch('builtins.input', side_effect=mockInputs):
            return exercise2.exampleGuessingGame() == "You got it!"
    except Exception as e:
        print("exception:", e)
コード例 #23
0
def test_binary_search(path, low, high, actual):
    """Test the binary search function.
    checks to see that it's searching better than O(log n)
    """
    try:
        path = "exercise4.py"
        exercise4 = imp.load_source("exercise4", path)
        BASE2 = 2
        b = None
        # with Timeout(3):
        b = exercise4.binary_search(low, high, actual)
        b["WorstCaseO"] = math.log(high - low, BASE2)
        print("b", b)
        if b is not None:
            print("snuck it in")
            return b["tries"] < b["WorstCaseO"]
        else:
            return False
    except Exception as e:
        return syntax_error_message(4, e)