Exemplo n.º 1
0
def test_hist_init():
    """
    Tests the __init__ method and getters in ImageHistory
    """
    print('Testing history initializer')
    import a6image
    import a6history
    p = pixels.Pixels(6)

    p[0] = (255, 0, 0)
    p[1] = (0, 255, 0)
    p[2] = (0, 0, 255)
    p[3] = (0, 255, 255)
    p[4] = (255, 0, 255)
    p[5] = (255, 255, 0)

    image = a6image.Image(p, 2)
    hist = a6history.ImageHistory(image)
    cornell.assert_equals(id(image), id(hist.getOriginal()))
    cornell.assert_equals(type(image), type(hist.getCurrent()))
    cornell.assert_not_equals(id(image), id(hist.getCurrent()))

    current = hist.getCurrent()
    cornell.assert_not_equals(id(p), id(current.getPixels()))
    for pos in range(current.getLength()):
        cornell.assert_equals(p[pos], current.getPixels()[pos])

    cornell.assert_true(hasattr(hist, '_history'))
    cornell.assert_equals(list, type(hist._history))
    cornell.assert_equals(1, len(hist._history))
Exemplo n.º 2
0
def test_hist_edit():
    """
    Tests the edit (increment/undo/clear) methods in ImageHistory
    """
    print('Testing history edit methods')
    import a6image
    import a6history
    p = pixels.Pixels(6)

    p[0] = (255, 0, 0)
    p[1] = (0, 255, 0)
    p[2] = (0, 0, 255)
    p[3] = (0, 255, 255)
    p[4] = (255, 0, 255)
    p[5] = (255, 255, 0)

    image = a6image.Image(p, 2)
    hist = a6history.ImageHistory(image)

    bottom = hist.getCurrent()
    bottom.setPixel(0, 0, (64, 128, 192))
    hist.increment()

    current = hist.getCurrent()
    cornell.assert_equals(bottom.getLength(), current.getLength())
    cornell.assert_equals(bottom.getWidth(), current.getWidth())
    cornell.assert_not_equals(id(bottom), id(current))
    cornell.assert_not_equals(id(bottom.getPixels()), id(current.getPixels()))
    for pos in range(current.getLength()):
        cornell.assert_equals(bottom.getPixels()[pos],
                              current.getPixels()[pos])

    hist.undo()
    cornell.assert_equals(id(bottom), id(hist.getCurrent()))

    hist.increment()
    hist.increment()
    hist.increment()
    cornell.assert_equals(4, len(hist._history))
    current = hist.getCurrent()
    cornell.assert_equals(bottom.getLength(), current.getLength())
    cornell.assert_equals(bottom.getWidth(), current.getWidth())
    cornell.assert_not_equals(id(bottom), id(current))
    cornell.assert_not_equals(id(bottom.getPixels()), id(current.getPixels()))
    for pos in range(current.getLength()):
        cornell.assert_equals(bottom.getPixels()[pos],
                              current.getPixels()[pos])

    hist.clear()
    cornell.assert_not_equals(id(bottom), id(hist.getCurrent()))
    cornell.assert_equals(1, len(hist._history))

    original = hist.getOriginal()
    current = hist.getCurrent()
    cornell.assert_equals(original.getLength(), current.getLength())
    cornell.assert_equals(original.getWidth(), current.getWidth())
    cornell.assert_not_equals(id(original), id(current))
    cornell.assert_not_equals(id(original.getPixels()),
                              id(current.getPixels()))
    for pos in range(current.getLength()):
        cornell.assert_equals(original.getPixels()[pos],
                              current.getPixels()[pos])

    bottom = hist.getCurrent()
    bottom.setPixel(0, 0, (64, 128, 192))
    for step in range(hist.MAX_HISTORY + 1):
        hist.increment()

    cornell.assert_equals(hist.MAX_HISTORY, len(hist._history))
    cornell.assert_not_equals(id(bottom), id(hist._history[0]))
Exemplo n.º 3
0
def test_image_other():
    """
    Tests the copy and swapPixel methods in class Image
    """
    print('Testing image extras')
    import a6image
    p = pixels.Pixels(6)

    p[0] = (255, 64, 0)
    p[1] = (0, 255, 64)
    p[2] = (64, 0, 255)
    p[3] = (64, 255, 128)
    p[4] = (128, 64, 255)
    p[5] = (255, 128, 64)
    q = p[:]  # Need to copy this

    # Test the copy
    image = a6image.Image(p, 2)
    copy = image.copy()
    cornell.assert_equals(image.getLength(), copy.getLength())
    cornell.assert_equals(image.getWidth(), copy.getWidth())
    cornell.assert_not_equals(id(image), id(copy))
    cornell.assert_not_equals(id(image.getPixels()), id(copy.getPixels()))
    for pos in range(copy.getLength()):
        cornell.assert_equals(image.getPixels()[pos], copy.getPixels()[pos])

    # Test swap pixels
    image.swapPixels(0, 0, 2, 1)
    cornell.assert_equals(q[5], image.getPixel(0, 0))
    cornell.assert_equals(q[0], image.getPixel(2, 1))
    image.swapPixels(0, 0, 2, 1)
    cornell.assert_equals(q[0], image.getPixel(0, 0))
    cornell.assert_equals(q[5], image.getPixel(2, 1))
    image.swapPixels(0, 1, 2, 0)
    cornell.assert_equals(q[4], image.getPixel(0, 1))
    cornell.assert_equals(q[1], image.getPixel(2, 0))
    image.swapPixels(0, 1, 2, 0)
    cornell.assert_equals(q[1], image.getPixel(0, 1))
    cornell.assert_equals(q[4], image.getPixel(2, 0))
    image.swapPixels(0, 0, 0, 0)
    cornell.assert_equals(q[0], image.getPixel(0, 0))

    # Test enforcement
    good = test_assert(image.swapPixels, ['a', 1, 0, 0],
                       'You are not enforcing the precondition on row type')
    good = good and test_assert(
        image.swapPixels, [8, 1, 0, 0],
        'You are not enforcing the precondition on row value')
    good = good and test_assert(
        image.swapPixels, [0, 1, 'a', 0],
        'You are not enforcing the precondition on row type')
    good = good and test_assert(
        image.swapPixels, [0, 1, 8, 0],
        'You are not enforcing the precondition on row value')
    good = good and test_assert(
        image.swapPixels, [0, 'a', 0, 0],
        'You are not enforcing the precondition on column type')
    good = good and test_assert(
        image.swapPixels, [0, 8, 0, 0],
        'You are not enforcing the precondition on column value')
    good = good and test_assert(
        image.swapPixels, [0, 1, 0, 'a'],
        'You are not enforcing the precondition on column type')
    good = good and test_assert(
        image.swapPixels, [0, 1, 0, 8],
        'You are not enforcing the precondition on column value')
    if not good:
        exit()