Ejemplo n.º 1
0
def test_wild_code():
    """
    Tests that setCode (and the initializer) works properly for WildCard.
    """
    import wild

    # This will create an empty card if the initializer is not defined
    card = wild.WildCard()
    card.setCode('QD')
    introcs.assert_equals(1, card.getSuit())
    introcs.assert_equals(12, card.getRank())
    introcs.assert_equals('QD', card.getCode())
    introcs.assert_false(card.isWild())

    card.setCode('WC')
    introcs.assert_equals(3, card.getSuit())
    introcs.assert_equals(1, card.getRank())
    introcs.assert_equals('WC', card.getCode())
    introcs.assert_true(card.isWild())

    try:
        card.setCode(23)
        introcs.quit_with_error('setCode does not enforce preconditions')
    except:
        pass

    try:
        card.setCode('WD')
        introcs.quit_with_error('setCode does not enforce preconditions')
    except:
        pass

    card = wild.WildCard(code='QD')
    introcs.assert_equals(1, card.getSuit())
    introcs.assert_equals(12, card.getRank())
    introcs.assert_equals('QD', card.getCode())
    introcs.assert_false(card.isWild())

    card = wild.WildCard(code='WC')
    introcs.assert_equals(3, card.getSuit())
    introcs.assert_equals(1, card.getRank())
    introcs.assert_equals('WC', card.getCode())
    introcs.assert_true(card.isWild())

    card = wild.WildCard(2, 11, True)
    introcs.assert_equals(2, card.getSuit())
    introcs.assert_equals(11, card.getRank())
    introcs.assert_equals('WC', card.getCode())
    introcs.assert_true(card.isWild())

    print('The wild setCode tests passed')
Ejemplo n.º 2
0
def test_pair_sum():
    """
    Test procedure for the sum method in the Pair class
    """
    print('Testing class Pair (sum)')
    try:
        obj = pair.Pair(1,2)
    except:
        introcs.quit_with_error('The initializer for Pair has the wrong number of parameters')

    introcs.assert_true(hasattr(obj,'sum'))
    result = 0
    try:
        result = obj.sum()
    except:
        introcs.quit_with_error('The sum method has the wrong number of parameters')
    introcs.assert_equals(3,result)
    introcs.assert_equals(7,pair.Pair(3,4).sum())
Ejemplo n.º 3
0
def test_pair_init():
    """
    Test procedure for the initializer in the Pair class
    """
    print('Testing class Pair (__init__)')
    try:
        result = pair.Pair(1,2)
    except:
        introcs.quit_with_error('The initializer for Pair has the wrong number of parameters')

    introcs.assert_equals(pair.Pair, type(result))
    introcs.assert_true(hasattr(result,'first'))
    introcs.assert_true(hasattr(result,'second'))
    introcs.assert_equals(1, result.first )
    introcs.assert_equals(2, result.second)

    result = pair.Pair(3,5)
    introcs.assert_equals(pair.Pair, type(result))
    introcs.assert_equals(3, result.first )
    introcs.assert_equals(5, result.second)
Ejemplo n.º 4
0
def test_wild_setters():
    """
    Tests the setters for the WildCard objects

    This test does not require that the initializer work yet.  If you still
    have pass in the initilizer, these tests should be fine.

    This test does not require that setCode support the 'WC' code.  This is
    an optional exercise.
    """
    import wild

    # This will create an empty card if the initializer is not defined
    card = wild.WildCard()

    card.setSuit(1)
    introcs.assert_equals(1, card.getSuit())
    card.setRank(3)
    introcs.assert_equals(3, card.getRank())

    card.setWild(True)
    introcs.assert_true(card.isWild())
    card.setWild(False)
    introcs.assert_false(card.isWild())

    try:
        card.setWild(5)
        introcs.quit_with_error('setWild does not enforce preconditions')
    except:
        pass

    # Check that setCode works on codes OTHER than 'WC'
    card.setCode('AS')
    introcs.assert_equals(1, card.getRank())
    introcs.assert_equals(3, card.getSuit())
    introcs.assert_equals('AS', card.getCode())

    print('The wild setter tests passed')
Ejemplo n.º 5
0
def test_wild_init():
    """
    Tests the initializer for the WildCard objects

    This test does not require that setCode support the 'WC' code.  This is
    an optional exercise.
    """
    import wild

    # This will create an empty card if the initializer is not defined
    card = wild.WildCard(1, 12)
    introcs.assert_equals(1, card.getSuit())
    introcs.assert_equals(12, card.getRank())
    introcs.assert_equals('QD', card.getCode())
    introcs.assert_false(card.isWild())

    card = wild.WildCard(2, 11, True)
    introcs.assert_equals(2, card.getSuit())
    introcs.assert_equals(11, card.getRank())
    introcs.assert_true(card.isWild())

    card = wild.WildCard(2, 11, True, 'AS')
    introcs.assert_equals(3, card.getSuit())
    introcs.assert_equals(1, card.getRank())
    introcs.assert_false(card.isWild())

    try:
        card = wild.WildCard(5, 11, True)
        introcs.quit_with_error('initializer does not enforce preconditions')
    except:
        pass

    try:
        card = wild.WildCard(2, 0, True)
        introcs.quit_with_error('initializer does not enforce preconditions')
    except:
        pass

    try:
        card = wild.WildCard(2, 11, 3)
        introcs.quit_with_error('initializer does not enforce preconditions')
    except:
        pass

    print('The wild __init__ tests passed')
Ejemplo n.º 6
0
def assert_point_sets_equal(expected, received):
    """
    Quits if the lists of points ``expected`` and ``received`` differ

    This function takes two lists of points and compares them using functions
    from the numerical  package ``numpy``.  This is a scientific computing
    package that allows us to test if numbers are "close enough".

    :param expected: The value you expect the test to have
    :type expected:  ``list`` or ``tuple``

    :param received: The value the test actually had
    :type received:  ``list`` or ``tuple``
    """
    import numpy
    if not type(expected) in [list, tuple]:
        msg = ('assert_point_sets_equal: first argument %s is not a sequence' %
               repr(expected))
        introcs.quit_with_error(msg)
    elif not type(received) in [list, tuple]:
        msg = (
            'assert_point_sets_equal: second argument %s is not a sequence' %
            repr(received))
        introcs.quit_with_error(msg)
    elif sum(map(lambda x: 0
                 if type(x) in [list, tuple] else 1, expected)) > 0:
        msg = (
            'assert_point_sets_equal: first argument %s is not 2-dimensional' %
            repr(expected))
        introcs.quit_with_error(msg)
    elif sum(map(lambda x: 0
                 if type(x) in [list, tuple] else 1, received)) > 0:
        msg = (
            'assert_point_sets_equal: second argument %s is not 2-dimensional'
            % repr(received))
        introcs.quit_with_error(msg)
    elif any([
            sum(map(lambda x: 0 if type(x) in [int, float] else 1, item)) > 0
            for item in expected
    ]):
        msg = (
            'assert_point_sets_equal: first argument %s has non-numeric values'
            % repr(expected))
        introcs.quit_with_error(msg)
    elif any([
            sum(map(lambda x: 0 if type(x) in [int, float] else 1, item)) > 0
            for item in received
    ]):
        msg = (
            'assert_point_sets_equal: second argument %s has non-numeric values'
            % repr(received))
        introcs.quit_with_error(msg)
    elif len(expected) != len(received):
        msg = (
            'assert_point_sets_equal: sequences %s and %s have different sizes'
            % (repr(expected), repr(received)))
        introcs.quit_with_error(msg)

    test = True
    try:
        test = numpy.allclose(expected, received)
    except Exception as e:
        msg = 'assert_point_sets_equal: sequences %s and %s are not comparable' % (
            repr(expected), repr(received))
        introcs.quit_with_error(msg)
    if (not test):
        msg = 'assert_point_sets_equal: expected %s but instead got %s' % (
            repr(expected), repr(received))
        introcs.quit_with_error(msg)