From 7999e3f95e96fc15df464d1758440b59d4c22d5b Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Fri, 27 Sep 2013 23:11:00 -0500 Subject: [PATCH] Refactor and add error tests --- skimage/novice/tests/test_novice.py | 116 +++++++++++++++++----------- 1 file changed, 73 insertions(+), 43 deletions(-) diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 1e455767..983b7e00 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -2,7 +2,7 @@ import os import tempfile import numpy as np -from numpy.testing import assert_equal, assert_raises, raises, assert_allclose +from numpy.testing import assert_equal, raises, assert_allclose from skimage import novice from skimage import data_dir @@ -76,30 +76,6 @@ def test_pixel_rgb_float(): assert_equal(pixel.rgb, (1, 1, 1)) -@raises(ValueError) -def test_pixel_rgb_raises(): - pixel = novice.Picture.from_size((1, 1))[0, 0] - pixel.rgb = (-1, -1, -1) - - -@raises(ValueError) -def test_pixel_red_raises(): - pixel = novice.Picture.from_size((1, 1))[0, 0] - pixel.red = 256 - - -@raises(ValueError) -def test_pixel_green_raises(): - pixel = novice.Picture.from_size((1, 1))[0, 0] - pixel.green = 256 - - -@raises(ValueError) -def test_pixel_blue_raises(): - pixel = novice.Picture.from_size((1, 1))[0, 0] - pixel.blue = 256 - - def test_modified_on_set(): pic = novice.Picture(SMALL_IMAGE_PATH) pic[0, 0] = (1, 1, 1) @@ -175,24 +151,6 @@ def test_pixel_group(): assert_allclose(pixel_group.array, array[index]) -def test_indexing_bounds(): - pic = novice.open(SMALL_IMAGE_PATH) - - # Outside bounds - assert_raises(IndexError, lambda: pic[pic.width, pic.height]) - - # Negative indexing not supported - assert_raises(IndexError, lambda: pic[-1, -1]) - assert_raises(IndexError, lambda: pic[-1:, -1:]) - - # Step sizes > 1 not supported - assert_raises(IndexError, lambda: pic[::2, ::2]) - - # Only 2D indexes supported - assert_raises(IndexError, lambda: pic[1]) - assert_raises(IndexError, lambda: pic[1, 2, 3]) - - def test_slicing(): cut = 40 pic = novice.open(IMAGE_PATH) @@ -214,6 +172,78 @@ def test_slicing(): assert p1.rgb == p2.rgb +@raises(IndexError) +def test_1d_getitem_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[1] + + +@raises(IndexError) +def test_3d_getitem_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[1, 2, 3] + + +@raises(IndexError) +def test_1d_setitem_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[1] = 0 + + +@raises(IndexError) +def test_3d_setitem_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[1, 2, 3] = 0 + + +@raises(IndexError) +def test_getitem_with_step_raises(): + pic = novice.Picture.from_size((3, 3)) + pic[::2, ::2] + + +@raises(IndexError) +def test_negative_index_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[-1, -1] + + +@raises(IndexError) +def test_negative_slice_raises(): + pic = novice.Picture.from_size((1, 1)) + pic[-1:, -1:] + + +@raises(IndexError) +def test_out_of_bounds_indexing(): + pic = novice.open(SMALL_IMAGE_PATH) + pic[pic.width, pic.height] + + +@raises(ValueError) +def test_pixel_rgb_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.rgb = (-1, -1, -1) + + +@raises(ValueError) +def test_pixel_red_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.red = 256 + + +@raises(ValueError) +def test_pixel_green_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.green = 256 + + +@raises(ValueError) +def test_pixel_blue_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.blue = 256 + + if __name__ == '__main__': from numpy import testing testing.run_module_suite()