コード例 #1
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)
コード例 #2
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)
コード例 #3
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)
コード例 #4
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)
コード例 #5
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
        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