コード例 #1
0
def test_image_operators():
    """
    Tests the double-underscore methods for 1-d access in class Image.
    """
    print('Testing image operators for 1-dimensional access')
    p = [(0,0,0)]*4
    
    image = a6image.Image(p,2)
    introcs.assert_equals(4,len(image))

    p = [(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,0,255),(255,255,0)]
    rgb1 = (255,255,255)
    rgb2 = (64,128,192)
    
    image = a6image.Image(p,3)
    introcs.assert_equals(6,len(image))
    for n in range(6):
        introcs.assert_equals(p[n],image[n])
        introcs.assert_equals(id(p[n]),id(image[n]))
    
    image[4] = rgb1
    introcs.assert_equals(rgb1,image[4])
    image[4] = rgb2
    introcs.assert_equals(rgb2,image[4])
    introcs.assert_equals(rgb2,p[4])                # Because image has a reference to p
    
    introcs.assert_error(image.__getitem__,'a', message='__getitem__ does not enforce the precondition on type')
    introcs.assert_error(image.__getitem__,9,   message='__getitem__ does not enforce the precondition on range')
    introcs.assert_error(image.__setitem__,'a',(0,0,255), message='__setitem__ does not enforce the precondition on type')
    introcs.assert_error(image.__setitem__,9,(0,0,255),   message='__setitem__ does not enforce the precondition on range')
    introcs.assert_error(image.__setitem__,9,(0,0,'255'), message='__setitem__ does not enforce the precondition on pixel value')
コード例 #2
0
def test_image_setters():
    """
    Tests the width and height setters for class Image
    """
    print('Testing image setters for width/height')
    p = [(0,0,0)]*6
    
    image = a6image.Image(p,3)
    introcs.assert_equals(3,image.getWidth())
    introcs.assert_equals(2,image.getHeight())
    
    image.setWidth(2)
    introcs.assert_equals(2,image.getWidth())
    introcs.assert_equals(3,image.getHeight())
    
    image.setHeight(1)
    introcs.assert_equals(6,image.getWidth())
    introcs.assert_equals(1,image.getHeight())
    
    image.setWidth(1)
    introcs.assert_equals(1,image.getWidth())
    introcs.assert_equals(6,image.getHeight())
    
    # Test enforcement
    introcs.assert_error(image.setWidth,'a', message='setWidth does not enforce the precondition on width type')
    introcs.assert_error(image.setWidth,5,   message='setWidth does not enforce the precondition on width validity')
    introcs.assert_error(image.setHeight,'a',message='setHeight does not enforce the precondition on height type')
    introcs.assert_error(image.setHeight,5,  message='setHeight does not enforce the precondition on height validity')
コード例 #3
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')
コード例 #4
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')
コード例 #5
0
def test_image_access():
    """
    Tests the methods the two-dimensional get/setPixel methods in class Image
    """
    print('Testing image get/setPixel methods')
    p = [(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,0,255),(255,255,0)]
    rgb1 = (255,255,255)
    rgb2 = (64,128,192)
    
    image = a6image.Image(p,2)
    for n in range(6):
        introcs.assert_equals(p[n],image.getPixel(n // 2, n % 2))
        introcs.assert_equals(id(p[n]),id(image.getPixel(n // 2, n % 2)))
    
    image.setPixel(2,1,rgb1)
    introcs.assert_equals(rgb1,image.getPixel(2,1))
    
    image.setPixel(2,1,rgb2)
    introcs.assert_equals(rgb2,image.getPixel(2,1))
    
    # Test enforcement
    introcs.assert_error(image.getPixel, 'a', 1, message='getPixel does not enforce the precondition on row type')
    introcs.assert_error(image.getPixel, 8, 1,   message='getPixel does not enforce the precondition on row value')
    introcs.assert_error(image.getPixel, 2, 'a', message='getPixel does not enforce the precondition on col value')
    introcs.assert_error(image.getPixel, 2, 8,   message='getPixel does not enforce the precondition on col value')
    introcs.assert_error(image.setPixel, 'a', 1, (0,0,255), message='setPixel does not enforce the precondition on row type')
    introcs.assert_error(image.setPixel, 8, 1, (0,0,255),   message='setPixel does not enforce the precondition on row value')
    introcs.assert_error(image.setPixel, 2, 'a', (0,0,255), message='setPixel does not enforce the precondition on col value')
    introcs.assert_error(image.setPixel, 2, 8, (0,0,255),   message='setPixel does not enforce the precondition on col value')
    introcs.assert_error(image.setPixel, 2, 1, (0,0,'255'), message='setPixel does not enforce the precondition on pixel value')