def run_test_product_of_some_radii(concept_map1, concept_map2):
    """
    What comes in: two ConceptMap objects.
    Side effects:  Tests the   product_of_some_radii   method twice:
      once using the given concept_map1 and then using the given concept_map2.
    """
    print()
    print("--------------------------------------------------")
    print("Testing the  product_of_some_radii   method:")
    print("--------------------------------------------------")

    # Test using concept_map1:
    expected = 96
    actual = concept_map1.product_of_some_radii()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 40
    actual = concept_map2.product_of_some_radii()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")
def run_test_sum_of_distances(concept_map1, concept_map2):
    """
    What comes in: two ConceptMap objects.
    Side effects:  Tests the   sum_of_distances   method twice:
      once using the given concept_map1 and then using the given concept_map2.
    """
    print()
    print("--------------------------------------------------")
    print("Testing the  sum_of_distances   method:")
    print("--------------------------------------------------")

    # Test using concept_map1:
    expected = 71.943111
    actual = concept_map1.sum_of_distances(Point(14, 12))
    print("Expected:", expected, "(approximately)")
    print("Actual:  ", actual)
    if actual is None or abs(actual - expected) > 0.00001:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 398.519518
    actual = concept_map2.sum_of_distances(Point(100, 200))
    print("Expected:", expected, "(approximately)")
    print("Actual:  ", actual)
    if actual is None or abs(actual - expected) > 0.00001:
        testing_helper.print_colored("FAILED the above test", color="red")
def run_test_number_of_small_circles(concept_map1, concept_map2):
    """
    What comes in: two ConceptMap objects.
    Side effects:  Tests the   number_of_small_circles   method twice:
      once using the given concept_map1 and then using the given concept_map2.
    """
    print()
    print("--------------------------------------------------")
    print("Testing the  number_of_small_circles   method:")
    print("--------------------------------------------------")

    # Test using concept_map1:
    expected = 4
    actual = concept_map1.number_of_small_circles(15)
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 0
    actual = concept_map2.number_of_small_circles(5)
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")
def run_test_product_of_sums_of_x_and_y_coordinates(concept_map1, concept_map2):
    """
    What comes in: two ConceptMap objects.
    Side effects:  Tests the   product_of_sums_of_x_and_y_coordinates   method
      twice: once using the given concept_map1
      and then again using the given concept_map2.
    """
    print()
    print("--------------------------------------------------")
    print("Testing the method:")
    print("   product_of_sums_of_x_and_y_coordinates")
    print("--------------------------------------------------")

    # Test using concept_map1:
    expected = 71259750
    actual = concept_map1.product_of_sums_of_x_and_y_coordinates()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 480000
    actual = concept_map2.product_of_sums_of_x_and_y_coordinates()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")
コード例 #5
0
    def run_test(self):
        """
        Runs this test, printing appropriate messages.
        Returns True if your code passed the test, else False.
        Does not attempt to catch Exceptions.
        """
        your_answer = self.function_to_test(*(self.arguments_to_use))

        print()

        if your_answer == self.correct_returned_value:
            result = 'PASSED'
            testing_helper.print_colored(
                'Your code {:6} this test'.format(result), color='blue')
        else:
            result = 'FAILED'
            testing_helper.print_colored(
                'Your code {:6} this test'.format(result), color='red')

        if len(self.arguments_to_use) == 0:
            format_string = '  ( )'
        else:
            f_beginning = '  {}( {} '
            f_args = ', {}' * (len(self.arguments_to_use) - 1)
            format_string = f_beginning + f_args + ' )'
        print(
            format_string.format(self.function_to_test.__name__,
                                 *(self.arguments_to_use)))

        print('  The correct returned value is:', self.correct_returned_value)
        print('  Your code returned ..........:', your_answer)

        return (your_answer == self.correct_returned_value)
def run_test_sum_of_x_coordinates(concept_map1, concept_map2):
    """
    What comes in: two ConceptMap objects.
    Side effects:  Tests the   sum_of_x_coordinates   method twice:
      once using the given concept_map1 and then using the given concept_map2.
    """
    print()
    print("--------------------------------------------------")
    print("Testing the  sum_of_x_coordinates   method:")
    print("--------------------------------------------------")

    # Test using concept_map1:
    expected = 115
    actual = concept_map1.sum_of_x_coordinates()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 152
    actual = concept_map2.sum_of_x_coordinates()
    print("Expected:", expected)
    print("Actual:  ", actual)
    if actual != expected:
        testing_helper.print_colored("FAILED the above test", color="red")
コード例 #7
0
def run_test_problem4b():
    """ Tests the   problem4b   function. """
    ###########################################################################
    # THESE TESTS ARE ALREADY DONE.  DO NOT CHANGE THEM.
    # You may add more tests if you wish, but you are not required to do so.
    ###########################################################################
    print()
    print('--------------------------------------------------')
    print('Testing the  problem4b  function:')
    print('--------------------------------------------------')

    format_string = '    problem4b( {} )'
    passed_tests = 0

    # Test 1:
    print()
    print('Test 1:')
    seq = [('SixSix', 'I am nine', 'This', 'is definitely fun!'),
           ('Nope', '', 'Hello', 'Goodbye', 'More stuff'),
           ('', 'This is seventeen', 'abc'), ('none', 'here')]

    expected = True
    print('  This test case calls:')
    print(format_string.format(seq))
    print("  Expected:", expected)

    actual = problem4b(seq)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 2:
    print()
    print('Test 2:')
    seq = [('SixSix', 'I am nine', 'This', 'is definitely fun!'),
           ('Nope', 'even', 'not prime'),
           ('', 'This is eighteen!!', '1234567890'), ('none', 'here')]

    expected = False
    print('  This test case calls:')
    print(format_string.format(seq))
    print("  Expected:", expected)

    actual = problem4b(seq)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    if passed_tests == 2:
        testing_helper.print_colored('\n*** PASSED all 2 tests! Good! ***',
                                     color='blue')
    else:
        testing_helper.print_colored('\n*** FAILED at least one test! ***',
                                     color='red')
コード例 #8
0
def main():
    """
    Tests the methods of the ConceptMap class defined above.
    Each test uses the same two ConceptMap examples as test cases.
    """
    circles1 = [
        Circle(Point(11, 12), 10),
        Circle(Point(14, 13), 8),
        Circle(Point(10, 20), 5),
        Circle(Point(30, 15), 12),
        Circle(Point(50, 35), 33)
    ]
    concept_map1 = ConceptMap(circles1)

    circles2 = [
        Circle(Point(30, 20), 10),
        Circle(Point(100, 200), 40),
        Circle(Point(22, 10), 5)
    ]
    concept_map2 = ConceptMap(circles2)

    print()
    print("The TESTS all use the same two ConceptMap examples as test cases:")
    testing_helper.print_colored("""
    circles1 = [Circle(Point(11, 12), 10),
                Circle(Point(14, 13), 8),
                Circle(Point(10, 20), 5),
                Circle(Point(30, 15), 12),
                Circle(Point(50, 35), 33)]
    concept_map1 = ConceptMap(circles1)

    circles2 = [Circle(Point(30, 20), 10),
                Circle(Point(100, 200), 40),
                Circle(Point(22, 10), 5)]
    concept_map2 = ConceptMap(circles2)
    """,
                                 color="blue")

    run_test_sum_of_x_coordinates(concept_map1, concept_map2)
    run_test_number_of_small_circles(concept_map1, concept_map2)
    run_test_product_of_sums_of_x_and_y_coordinates(concept_map1, concept_map2)
    run_test_sum_of_some_indices(concept_map1, concept_map2)
    run_test_product_of_some_radii(concept_map1, concept_map2)
    run_test_sum_of_distances(concept_map1, concept_map2)
コード例 #9
0
    def run_tests(function_name, tests):
        print()
        print('--------------------------------------------------')
        print('Testing the   {}   function:'.format(function_name))
        print('--------------------------------------------------')

        failures = 0
        for k in range(len(tests)):
            result = tests[k].run_test()
            if result is False:
                failures = failures + 1

        if failures > 0:
            print()
            testing_helper.print_colored(
                '************************************', color='red')
            testing_helper.print_colored(
                '*** YOUR CODE FAILED SOME TESTS. ***', color='red')
            testing_helper.print_colored(
                '************************************', color='red')
    actual = concept_map1.sum_of_distances(Point(14, 12))
    print("Expected:", expected, "(approximately)")
    print("Actual:  ", actual)
    if actual is None or abs(actual - expected) > 0.00001:
        testing_helper.print_colored("FAILED the above test", color="red")

    # Test using concept_map2:
    expected = 398.519518
    actual = concept_map2.sum_of_distances(Point(100, 200))
    print("Expected:", expected, "(approximately)")
    print("Actual:  ", actual)
    if actual is None or abs(actual - expected) > 0.00001:
        testing_helper.print_colored("FAILED the above test", color="red")


# -----------------------------------------------------------------------------
# Calls  main  to start the ball rolling.
# The   try .. except   prevents error messages on the console from being
# intermingled with ordinary output to the console.
# -----------------------------------------------------------------------------
try:
    main()
except Exception:
    testing_helper.print_colored("ERROR - While running this test,",
                                 color="red")
    testing_helper.print_colored("your code raised the following exception:",
                                 color="red")
    print()
    time.sleep(1)
    raise
コード例 #11
0
ファイル: problem1.py プロジェクト: bhasinn01/Dino_Game
def run_test_problem1b():
    """ Tests the   problem1b   function. """
    print()
    print("--------------------------------------------------")
    print("Testing the   problem1b   function:")
    print("--------------------------------------------------")

    format_string = "    problem1b( {}, {}, {} )"
    test_results = [0, 0]  # Number of tests passed, failed.

    # Test 1:
    expected = 3
    print_expected_result_of_test([6, 30, 40], expected, test_results,
                                  format_string)
    actual = problem1b(6, 30, 40)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 2:
    expected = 4
    print_expected_result_of_test([36, 20, 32], expected, test_results,
                                  format_string)
    actual = problem1b(36, 20, 32)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 3:
    expected = 5
    print_expected_result_of_test([36, 19, 32], expected, test_results,
                                  format_string)
    actual = problem1b(36, 19, 32)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 4:
    expected = 4
    print_expected_result_of_test([36, 20, 33], expected, test_results,
                                  format_string)
    actual = problem1b(36, 20, 33)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 5:
    expected = 4
    print_expected_result_of_test([36, 20, 34], expected, test_results,
                                  format_string)
    actual = problem1b(36, 20, 34)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 6:
    expected = 5
    print_expected_result_of_test([36, 20, 35], expected, test_results,
                                  format_string)
    actual = problem1b(36, 20, 35)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 7:
    expected = 744
    print_expected_result_of_test([1016, 500, 2000], expected, test_results,
                                  format_string)
    actual = problem1b(1016, 500, 2000)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 8:
    expected = 745
    print_expected_result_of_test([1016, 500, 2001], expected, test_results,
                                  format_string)
    actual = problem1b(1016, 500, 2001)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    # Test 9:
    expected = 746
    print_expected_result_of_test([1016, 499, 2002], expected, test_results,
                                  format_string)
    actual = problem1b(1016, 499, 2002)  # Run the code to test
    print_actual_result_of_test(expected, actual, test_results)

    print()
    print("The next set of tests will take")
    print("up to 10 or so seconds.  Be patient.")
    # More tests: the greatest common divisor of 2 and X is 1
    #   if and only if X is odd.  That forms the basis for the following tests:
    passed_first_set_of_extra_tests = True
    for t in range(1, 3333):
        expected = (t + 1) // 2
        actual = problem1b(2, 1, t)  # Run the code to test
        if expected != actual:
            print_expected_result_of_test([2, 1, t], expected, test_results,
                                          format_string)
            print_actual_result_of_test(expected, actual, test_results)
            passed_first_set_of_extra_tests = False
            break

    if passed_first_set_of_extra_tests:
        message = "  PASSED the FIRST set of extra tests -- good!"
        testing_helper.print_colored(message, color="blue")

    print()
    print("The next set of tests will again take")
    print("up to 10 or so seconds.  Be patient.")
    # Still more tests: a calculation similar to the previous loop
    #   forms the basis for the following tests:
    passed_second_set_of_extra_tests = True
    r = 2 * 2 * 5 * 11
    for t in range(1, 5001):
        expected = (
            t - (t // 2)  # subtract out multiples of 2
            - (t // 5)  # subtract out multiples of 5
            - (t // 11)  # subtract out multiples of 11
            + (t // 10)  # multiples of 10 were double-counted
            + (t // 22)  # multiples of 22 were double-counted
            + (t // 55)  # multiples of 55 were double-counted
            - (t // 110))  # multiples of 110 were triple-counted
        actual = problem1b(r, 1, t)  # Run the code to test
        if expected != actual:
            print_expected_result_of_test([r, 1, t], expected, test_results,
                                          format_string)
            print_actual_result_of_test(expected, actual, test_results)
            passed_second_set_of_extra_tests = False
            break

    if passed_second_set_of_extra_tests:
        message = "  PASSED the SECOND set of extra tests -- good!"
        testing_helper.print_colored(message, color="blue")

    print_summary_of_test_results(test_results)
コード例 #12
0
    #    DIFFICULTY:      5
    #    TIME ESTIMATE:   < 15 minutes.
    ###########################################################################

    list = []
    count = 0
    iterations = start
    while count < n:
        if math.sin(iterations) + math.cos(iterations) > threshold:
            list.append(iterations)
            count += 1
        iterations += 1
    return list


# -----------------------------------------------------------------------------
# Calls  main  to start the ball rolling.
# The   try .. except   prevents error messages on the console from being
# intermingled with ordinary output to the console.
# -----------------------------------------------------------------------------
try:
    main()
except Exception:
    testing_helper.print_colored('ERROR - While running this test,',
                                 color='red')
    testing_helper.print_colored('your code raised the following exception:',
                                 color='red')
    print()
    time.sleep(1)
    raise
コード例 #13
0
def run_test_practice_problem3h():
    """ Tests the    practice_problem3h    function. """
    print()
    print('--------------------------------------------------')
    print('Testing the   practice_problem3h  function:')
    print('--------------------------------------------------')

    format_string = '    practice_problem3h( {} )'
    test_results = [0, 0]  # Number of tests passed, failed.

    # Test 1:
    c = rg.Circle(rg.Point(7, 30), 10)
    expected = rg.Circle(rg.Point(7, 30), 10)
    circles = (rg.Circle(rg.Point(5, 10), 20), rg.Circle(rg.Point(2, 20),
                                                         20), c,
               rg.Circle(rg.Point(10, 40), 20), rg.Circle(rg.Point(2, 50), 10))
    print_expected_result_of_test([circles], expected, test_results,
                                  format_string)
    actual = practice_problem3h(circles)
    print_actual_result_of_test(expected, actual, test_results)

    if actual == c and actual is not c:
        message = ("Technically, *** FAILED the above test. ***\n" +
                   "because you appear to have returned a CLONE\n" +
                   "of the correct rg.Circle instead of\n" +
                   "the rg.Circle itself.")
        testing_helper.print_colored(message, color='red')

    # Test 2:
    c = rg.Circle(rg.Point(58, 10), 20)
    expected = rg.Circle(rg.Point(58, 10), 20)
    circles = (c, )
    print_expected_result_of_test([circles], expected, test_results,
                                  format_string)
    actual = practice_problem3h(circles)
    print_actual_result_of_test(expected, actual, test_results)

    if actual == c and actual is not c:
        message = ("Technically, *** FAILED the above test. ***\n" +
                   "because you appear to have returned a CLONE\n" +
                   "of the correct rg.Circle instead of\n" +
                   "the rg.Circle itself.")
        testing_helper.print_colored(message, color='red')

    # Test 3:
    c = rg.Circle(rg.Point(10005, 300), 100)
    expected = rg.Circle(rg.Point(10005, 300), 100)
    circles = (rg.Circle(rg.Point(84, 100),
                         300), rg.Circle(rg.Point(28, 200), 200), c)
    print_expected_result_of_test([circles], expected, test_results,
                                  format_string)
    actual = practice_problem3h(circles)
    print_actual_result_of_test(expected, actual, test_results)

    if actual == c and actual is not c:
        message = ("Technically, *** FAILED the above test. ***\n" +
                   "because you appear to have returned a CLONE\n" +
                   "of the correct rg.Circle instead of\n" +
                   "the rg.Circle itself.")
        testing_helper.print_colored(message, color='red')

    # Test 4:
    c = rg.Circle(rg.Point(5, 10), 13)
    expected = rg.Circle(rg.Point(5, 10), 13)
    circles = (c, rg.Circle(rg.Point(0, 20),
                            20), rg.Circle(rg.Point(7, 30), 19),
               rg.Circle(rg.Point(10, 40), 14), rg.Circle(rg.Point(2, 50), 14))

    print_expected_result_of_test([circles], expected, test_results,
                                  format_string)
    actual = practice_problem3h(circles)
    print_actual_result_of_test(expected, actual, test_results)

    if actual == c and actual is not c:
        message = ("Technically, *** FAILED the above test. ***\n" +
                   "because you appear to have returned a CLONE\n" +
                   "of the correct rg.Circle instead of\n" +
                   "the rg.Circle itself.")
        testing_helper.print_colored(message, color='red')

    # Test 5:  Perhaps not a valid test since it assumes an rg.Circle
    #          can have a NEGATIVE radius, but it will catch some dubious code.
    small = -9999999999999999999999999999
    c = rg.Circle(rg.Point(7, 30), small)
    expected = rg.Circle(rg.Point(7, 30), small)
    circles = (rg.Circle(rg.Point(0, 20), small + 0.0000001), c,
               rg.Circle(rg.Point(10, 40),
                         small), rg.Circle(rg.Point(2, 50), small + 1))

    print_expected_result_of_test([circles], expected, test_results,
                                  format_string)
    actual = practice_problem3h(circles)
    print_actual_result_of_test(expected, actual, test_results)

    if actual == c and actual is not c:
        message = ("Technically, *** FAILED the above test. ***\n" +
                   "because you appear to have returned a CLONE\n" +
                   "of the correct rg.Circle instead of\n" +
                   "the rg.Circle itself.")
        testing_helper.print_colored(message, color='red')

    # SUMMARY of test results:
    print_summary_of_test_results(test_results)
コード例 #14
0
def run_test_problem4a():
    """ Tests the   problem4a   function. """
    ###########################################################################
    # THESE TESTS ARE ALREADY DONE.  DO NOT CHANGE THEM.
    # You may add more tests if you wish, but you are not required to do so.
    ###########################################################################
    print()
    print('--------------------------------------------------')
    print('Testing the  problem4a  function:')
    print('--------------------------------------------------')

    format_string = '    problem4a( {} )'
    passed_tests = 0

    # Test 1:
    print()
    print('Test 1:')
    strings = ['Nope', '', 'Hello', 'Goodbye', 'More stuff']

    expected = 'Hello'
    print('  This test case calls:')
    print(format_string.format(strings))
    print("  Expected:", expected)

    actual = problem4a(strings)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 2:
    print()
    print('Test 2:')
    strings = ['SixSix', 'I am nine', 'This', 'is definitely fun!']

    expected = -1
    print('  This test case calls:')
    print(format_string.format(strings))
    print("  Expected:", expected)

    actual = problem4a(strings)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 3:
    print()
    print('Test 3:')
    strings = ('01234567', '0123456789', '0123', '0123456')

    expected = '0123456'
    print('  This test case calls:')
    print(format_string.format(strings))
    print("  Expected:", expected)

    actual = problem4a(strings)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    if passed_tests == 3:
        testing_helper.print_colored('\n*** PASSED all 3 tests! Good! ***',
                                     color='blue')
    else:
        testing_helper.print_colored('\n*** FAILED at least one test! ***',
                                     color='red')
コード例 #15
0
def run_test_practice_problem2b():
    # -------------------------------------------------------------------------
    # TODO: 3. Implement this TEST function.
    #   It TESTS the  practice_problem2b  function defined below.
    #   Include at least **   4    ** tests that, taken together,
    #   would form a    ** REASONABLY GOOD test set **
    #   for testing the   practice_problem2b   function defined below.
    #   (We supplied 3 tests, so you need supply only one more.)
    #  ------------------------------------------------------------------------
    #  DIFFICULTY AND TIME RATINGS (see top of this file for explanation)
    #    DIFFICULTY:      4
    #    TIME ESTIMATE:   10 minutes.
    #  --------------------------------------------------------------------
    """ Tests the   practice_problem2b  function. """
    print()
    print('--------------------------------------------------')
    print('Testing the   practice_problem2b  function:')
    print('--------------------------------------------------')

    format_string = '    practice_problem2b( {}, {} )'
    test_results = [0, 0]  # Number of tests passed, failed.

    # Test 1:
    arg1 = [2, 10, 5, -20, 8]
    correct_arg1_after = [8, 16, 11, -14, 14]
    expected = None

    testing_helper.print_function_call_of_test([arg1, 6], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2b(arg1, 6)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # Test 2:
    arg1 = [1, 1, 2, 3, 10, -11, 12, 0, 0, 1]
    correct_arg1_after = [1, 1, 2, 3, 10, -11, 12, 0, 0, 1]
    expected = None

    testing_helper.print_function_call_of_test([arg1, 0], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2b(arg1, 0)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # Test 3:
    arg1 = [795, -795, 0]
    correct_arg1_after = [-1, -1591, -796]
    expected = None

    testing_helper.print_function_call_of_test([arg1, -796], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2b(arg1, -796)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # -------------------------------------------------------------------------
    # TODO: 3 (continued): Add your ADDITIONAL test(s) below here.
    #   You do NOT have to use the "fancy" parts of the above example tests,
    #   but you MUST test BOTH the returned value AND the mutated list.
    # -------------------------------------------------------------------------

    # SUMMARY of test results:
    print_summary_of_test_results(test_results)
コード例 #16
0
def run_test_practice_problem2d():
    """ Tests the   practice_problem2d  function. """
    print()
    print('--------------------------------------------------')
    print('Testing the   practice_problem2d  function:')
    print('--------------------------------------------------')

    format_string = '    practice_problem2d( {}, {} )'
    test_results = [0, 0]  # Number of tests passed, failed.

    print()
    print('--------------------------------------------------')
    print('Testing the   practice_problem2d   function:')
    print('--------------------------------------------------')

    # Test 1:
    arg1 = [10, -3, 20, 4]
    correct_arg1_after = [20, -6, 40, 8]
    arg2 = [5, 0, 8]
    correct_arg2_after = [5, 0, 8]
    expected = [10, 0, 16]

    testing_helper.print_function_call_of_test([arg1, arg2], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2d(arg1, arg2)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('  Argument 2 is:       ', arg2)
    print('  Argument 2 should be:', correct_arg2_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (arg2 == correct_arg2_after)\
                and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # Test 2:
    arg1 = []
    correct_arg1_after = []
    arg2 = []
    correct_arg2_after = []
    expected = []

    testing_helper.print_function_call_of_test([arg1, arg2], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2d(arg1, arg2)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('  Argument 2 is:       ', arg2)
    print('  Argument 2 should be:', correct_arg2_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (arg2 == correct_arg2_after)\
                and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # Test 3:
    arg1 = [1, 2, 3, 0, 1, 2, 3]
    correct_arg1_after = [2, 4, 6, 0, 2, 4, 6]
    arg2 = [0]
    correct_arg2_after = [0]
    expected = [0]

    testing_helper.print_function_call_of_test([arg1, arg2], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2d(arg1, arg2)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('  Argument 2 is:       ', arg2)
    print('  Argument 2 should be:', correct_arg2_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (arg2 == correct_arg2_after)\
                and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # Test 4:
    arg1 = [0]
    correct_arg1_after = [0]
    arg2 = [1, 2, 3, 0, 1, 2, 3]
    correct_arg2_after = [1, 2, 3, 0, 1, 2, 3]
    expected = [2, 4, 6, 0, 2, 4, 6]

    testing_helper.print_function_call_of_test([arg1, arg2], test_results,
                                               format_string)
    print()
    print('BEFORE the function call:')
    print('  Argument 1 is:', arg1)

    answer = practice_problem2d(arg1, arg2)

    print('AFTER the function call:')
    print('  Argument 1 is:       ', arg1)
    print('  Argument 1 should be:', correct_arg1_after)

    print('  Argument 2 is:       ', arg2)
    print('  Argument 2 should be:', correct_arg2_after)

    print('The returned value is:       ', answer)
    print('The returned value should be:', expected)

    if (arg1 == correct_arg1_after) and (arg2 == correct_arg2_after)\
                and (answer == expected):
        testing_helper.print_colored("  PASSED the above test -- good!",
                                     color='blue')
        test_results[0] = test_results[0] + 1
    else:
        testing_helper.print_colored("  *** FAILED the above test. ***",
                                     color='red')
        test_results[1] = test_results[1] + 1

    # SUMMARY of test results:
    print_summary_of_test_results(test_results)
コード例 #17
0
ファイル: problem2.py プロジェクト: geyerje/16-Exam2
def run_test_problem2():
    """ Tests the  problem2   function. """
    ###########################################################################
    # THESE TESTS ARE ALREADY DONE.  DO NOT CHANGE THEM.
    ###########################################################################
    print()
    print('--------------------------------------------------')
    print('Testing the   problem2   function:')
    print('--------------------------------------------------')

    format_string = '    problem2a( {}, {} )'
    passed_tests = 0

    # Test 1:
    print()
    print('Test 1:')
    n = 10
    sequence = [9, 15, 2, 20, 13, 6, 8, 1, 17]

    expected = [9, 2, 6]
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 2:
    print()
    print('Test 2:')
    n = 7
    sequence = [9, 15, 2, 20, 13, 6, 8, 1, 17]

    expected = [2, 6, 1]
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 3:
    print()
    print('Test 3:')
    n = 5
    sequence = [9, 15, 2, 20, 13, 6, 8, 1, 17]

    expected = 'Too few'
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 4:
    print()
    print('Test 4:')
    n = 5
    sequence = [5, 4, 11, 2, 4, 4]

    expected = [4, 2, 4]
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 5:
    print()
    print('Test 5:')
    n = 8
    sequence = [100, 100]

    expected = 'Too few'
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    # Test 6:
    print()
    print('Test 6:')
    n = 10
    sequence = [1, 2]

    expected = 'Too few'
    print('  This test case calls:')
    print(format_string.format(n, sequence))
    print("  Expected:", expected)

    actual = problem2(n, sequence)
    print("  Actual:  ", actual)
    result = print_result_of_test(expected, actual)
    passed_tests = passed_tests + result

    if passed_tests == 6:
        testing_helper.print_colored('\n*** PASSED all 6 tests! Good! ***',
                                     color='blue')
    else:
        testing_helper.print_colored('\n*** FAILED at least one test! ***',
                                     color='red')