Beispiel #1
0
def test_complement():
    """
    Test function complement
    """
    introcs.assert_equals(introcs.RGB(255-250, 255-0, 255-71),
                          a2.complement_rgb(introcs.RGB(250, 0, 71)))
    introcs.assert_equals(introcs.RGB(255-92, 255-128, 255-255),
                          a2.complement_rgb(introcs.RGB(92, 128, 255)))

    # Make sure we are not modifying the color
    rgb = introcs.RGB(128,128,128)
    introcs.assert_not_equals(id(rgb),id(a2.complement_rgb(rgb)))
Beispiel #2
0
def test_cluster_a():
    """
    Tests Part A of the Cluster class assignment.
    """
    print('  Testing Part A of class Cluster')

    # TEST CASE 1
    # Create and test a cluster (always empty)
    dset = a6dataset.Dataset(3)
    point = [0.0, 1.0, 0.0]
    cluster1 = a6cluster.Cluster(dset, point)

    # Compare centroid and contents
    introcs.assert_float_lists_equal(point, cluster1.getCentroid())
    introcs.assert_equals([], cluster1.getIndices())
    # Make sure centroid COPIED
    introcs.assert_not_equals(id(point), id(cluster1.getCentroid()))

    print('    Basic cluster methods look okay')

    # Add something to cluster (and check it was added)
    extra = [[0.0, 0.5, 4.2], [0.0, 1.0, 0.0]]
    dset.addPoint(extra[0])
    dset.addPoint(extra[1])
    cluster1.addIndex(1)
    introcs.assert_equals([1], cluster1.getIndices())
    cluster1.addIndex(0)
    introcs.assert_equals([1, 0], cluster1.getIndices())
    # Make sure we can handle duplicates!
    cluster1.addIndex(1)
    introcs.assert_equals([1, 0], cluster1.getIndices())

    print('    Method Cluster.addIndex look okay')

    # And clear it
    contents = cluster1.getContents()
    introcs.assert_equals(2, len(contents))
    introcs.assert_float_lists_equal(extra[1], contents[0])
    introcs.assert_float_lists_equal(extra[0], contents[1])

    print('    Method Cluster.getContents look okay')

    # And clear it
    cluster1.clear()
    introcs.assert_equals([], cluster1.getIndices())

    print('    Method Cluster.clear look okay')
    print('  Part A of class Cluster appears correct')
    print()
Beispiel #3
0
def test_image_other():
    """
    Tests the copy and swapPixel methods in class Image
    """
    print('Testing image extra methods')
    p = [(255, 64, 0),(0, 255, 64),(64, 0, 255),(64, 255, 128),(128, 64, 255),(255, 128, 64)]
    q = p[:]  # Need to copy this
    
    # Test the copy
    image = a6image.Image(p,2)
    copy  = image.copy()
    introcs.assert_equals(len(image),len(copy))
    introcs.assert_equals(image.getWidth(),copy.getWidth())
    introcs.assert_not_equals(id(image), id(copy))
    introcs.assert_not_equals(id(image._data), id(copy._data))
    for pos in range(len(copy)):
        introcs.assert_equals(image[pos],copy[pos])
    
    # Test swap pixels
    image.swapPixels(0,0,2,1)
    introcs.assert_equals(q[5],image.getPixel(0,0))
    introcs.assert_equals(q[0],image.getPixel(2,1))
    image.swapPixels(0,0,2,1)
    introcs.assert_equals(q[0],image.getPixel(0,0))
    introcs.assert_equals(q[5],image.getPixel(2,1))
    image.swapPixels(0,1,2,0)
    introcs.assert_equals(q[4],image.getPixel(0,1))
    introcs.assert_equals(q[1],image.getPixel(2,0))
    image.swapPixels(0,1,2,0)
    introcs.assert_equals(q[1],image.getPixel(0,1))
    introcs.assert_equals(q[4],image.getPixel(2,0))
    image.swapPixels(0,0,0,0)
    introcs.assert_equals(q[0],image.getPixel(0,0))
    
    # Test enforcement
    introcs.assert_error(image.swapPixels, 'a', 1, 0, 0, message='swapPixels does not enforce the precondition on row type')
    introcs.assert_error(image.swapPixels, 8, 1, 0, 0,   message='swapPixels does not enforce the precondition on row value')
    introcs.assert_error(image.swapPixels, 0, 1, 'a', 0, message='swapPixels does not enforce the precondition on row type')
    introcs.assert_error(image.swapPixels, 0, 1, 8, 0,   message='swapPixels does not enforce the precondition on row value')
    introcs.assert_error(image.swapPixels, 0, 'a', 0, 0, message='swapPixels does not enforce the precondition on column type')
    introcs.assert_error(image.swapPixels, 0, 8, 0, 0,   message='swapPixels does not enforce the precondition on column value')
    introcs.assert_error(image.swapPixels, 0, 1, 0, 'a', message='swapPixels does not enforce the precondition on column type')
    introcs.assert_error(image.swapPixels, 0, 1, 0, 8,   message='swapPixels does not enforce the precondition on column value')
Beispiel #4
0
def test_image_init():
    """
    Tests the __init__ method and getters for class Image
    """
    print('Testing image initializer')
    p = [(0,0,0)]*6
    
    image = a6image.Image(p,3)
    # Normally it is bad to test things that are hidden
    # But without this you will not find the error until test_image_operators
    introcs.assert_equals(id(p),id(image._data))
    introcs.assert_not_equals(id(p),id(image.getData()))
    introcs.assert_equals(p,image.getData())
    introcs.assert_equals(3,image.getWidth())
    introcs.assert_equals(2,image.getHeight())

    image = a6image.Image(p,2)
    introcs.assert_equals(id(p),id(image._data))
    introcs.assert_not_equals(id(p),id(image.getData()))
    introcs.assert_equals(p,image.getData())
    introcs.assert_equals(2,image.getWidth())
    introcs.assert_equals(3,image.getHeight())

    image = a6image.Image(p,1)
    introcs.assert_equals(id(p),id(image._data))
    introcs.assert_not_equals(id(p),id(image.getData()))
    introcs.assert_equals(p,image.getData())
    introcs.assert_equals(1,image.getWidth())
    introcs.assert_equals(6,image.getHeight())
    
    # Test enforcement
    introcs.assert_error(a6image.Image,'aaa',3,message='Image does not enforce the precondition on data')
    introcs.assert_error(a6image.Image,p,'a',  message='Image does not enforce the precondition width type')
    introcs.assert_error(a6image.Image,p,5,    message='Image does not enforce the precondition width validity')
Beispiel #5
0
def test_wild_eq():
    """
    Tests that __eq__ works properly for WildCard.
    """
    import wild

    card1 = wild.WildCard(1, 12, False)
    card2 = card.Card(1, 12)
    introcs.assert_equals(card1, card1)
    introcs.assert_not_equals(card1, card2)

    card2 = wild.WildCard(1, 12)
    introcs.assert_equals(card1, card1)

    card2 = wild.WildCard(1, 12, True)
    introcs.assert_not_equals(card1, card2)
    card2 = wild.WildCard(3, 12)
    introcs.assert_not_equals(card1, card2)
    card2 = wild.WildCard(1, 1)
    introcs.assert_not_equals(card1, card2)

    print('The wild __eq__ tests passed')