From 9385e735866f71892c566c0c788f307557820cb1 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Fri, 5 Jul 2013 23:05:13 -0500 Subject: [PATCH] Clean up testing --- skimage/novice/tests/test_novice.py | 76 +++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 19a2d34c..73aba8e8 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 +from numpy.testing import assert_equal, assert_raises, raises from skimage import novice from skimage import data_dir @@ -18,7 +18,7 @@ def test_pic_info(): assert_equal(pic.size, (665, 500)) assert_equal(pic.width, 665) assert_equal(pic.height, 500) - assert_equal(pic.modified, False) + assert not pic.modified assert_equal(pic.scale, 1) @@ -40,23 +40,65 @@ def test_modify(): for p in pic: if p.x < (pic.width / 2): - assert_equal(p.red <= 128, True) - assert_equal(p.green <= 128, True) - assert_equal(p.blue <= 128, True) + assert p.red <= 128 + assert p.green <= 128 + assert p.blue <= 128 s = pic.size pic.size = (pic.width / 2, pic.height / 2) assert_equal(pic.size, (int(s[0] / 2), int(s[1] / 2))) - assert_equal(pic.modified, True) - assert_equal(pic.path, None) + assert pic.modified + assert pic.path is None + + +def test_pixel_rgb(): + pic = novice.Picture(size=(3, 3), color=(10, 10, 10)) + pixel = pic[0, 0] + pixel.rgb = range(3) + + assert_equal(pixel.rgb, range(3)) + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue)): + assert_equal(channel, i) + + pixel.red = 3 + pixel.green = 4 + pixel.blue = 5 + assert_equal(pixel.rgb, np.arange(3) + 3) + + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue)): + assert_equal(channel, i + 3) + + +@raises(ValueError) +def test_pixel_rgb_raises(): + pixel = novice.Picture(size=(1, 1))[0, 0] + pixel.rgb = (-1, -1, -1) + + +@raises(ValueError) +def test_pixel_red_raises(): + pixel = novice.Picture(size=(1, 1))[0, 0] + pixel.red = 256 + + +@raises(ValueError) +def test_pixel_green_raises(): + pixel = novice.Picture(size=(1, 1))[0, 0] + pixel.green = 256 + + +@raises(ValueError) +def test_pixel_blue_raises(): + pixel = novice.Picture(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) - assert_equal(pic.modified, True) - assert_equal(pic.path, None) + assert pic.modified + assert pic.path is None def test_modified_on_set_pixel(): @@ -65,27 +107,27 @@ def test_modified_on_set_pixel(): pixel = pic[0, 0] pixel.green = 1 - assert_equal(pic.modified, True) + assert pic.modified def test_update_on_save(): pic = novice.Picture(image=np.zeros((3, 3))) pic.size = (6, 6) - assert_equal(pic.modified, True) - assert_equal(pic.path, None) + assert pic.modified + assert pic.path is None with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: pic.save(tmp.name) - assert_equal(pic.modified, False) + assert not pic.modified assert_equal(pic.path, os.path.abspath(tmp.name)) assert_equal(pic.format, "jpeg") def test_indexing(): - pic = novice.open(SMALL_IMAGE_PATH) + image = 128 * np.ones((10, 10, 3), dtype=np.uint8) + pic = novice.Picture(image=image) - # Slicing pic[0:5, 0:5] = (0, 0, 0) for p in pic: if (p.x < 5) and (p.y < 5): @@ -118,6 +160,10 @@ def test_indexing(): assert_equal(p.green, 0) assert_equal(p.blue, 255) + +def test_indexing_bounds(): + pic = novice.open(SMALL_IMAGE_PATH) + # Outside bounds assert_raises(IndexError, lambda: pic[pic.width, pic.height])