Merge pull request #6870 from hugovk/parametrize

Tests: Convert internal check() functions to use parametrize
This commit is contained in:
Andrew Murray 2023-01-09 09:29:19 +11:00 committed by GitHub
commit 50f7888e3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 50 deletions

View File

@ -158,12 +158,28 @@ def test_optimize():
assert test_bilevel(1) == 799 assert test_bilevel(1) == 799
def test_optimize_correctness(): @pytest.mark.parametrize(
# 256 color Palette image, posterize to > 128 and < 128 levels "colors, size, expected_palette_length",
# Size bigger and smaller than 512x512 (
# These do optimize the palette
(256, 511, 256),
(255, 511, 255),
(129, 511, 129),
(128, 511, 128),
(64, 511, 64),
(4, 511, 4),
# These don't optimize the palette
(128, 513, 256),
(64, 513, 256),
(4, 513, 256),
),
)
def test_optimize_correctness(colors, size, expected_palette_length):
# 256 color Palette image, posterize to > 128 and < 128 levels.
# Size bigger and smaller than 512x512.
# Check the palette for number of colors allocated. # Check the palette for number of colors allocated.
# Check for correctness after conversion back to RGB # Check for correctness after conversion back to RGB.
def check(colors, size, expected_palette_length):
# make an image with empty colors in the start of the palette range # make an image with empty colors in the start of the palette range
im = Image.frombytes( im = Image.frombytes(
"P", (colors, colors), bytes(range(256 - colors, 256)) * colors "P", (colors, colors), bytes(range(256 - colors, 256)) * colors
@ -179,19 +195,6 @@ def test_optimize_correctness():
assert_image_equal(im.convert("RGB"), reloaded.convert("RGB")) assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))
# These do optimize the palette
check(256, 511, 256)
check(255, 511, 255)
check(129, 511, 129)
check(128, 511, 128)
check(64, 511, 64)
check(4, 511, 4)
# These don't optimize the palette
check(128, 513, 256)
check(64, 513, 256)
check(4, 513, 256)
def test_optimize_full_l(): def test_optimize_full_l():
im = Image.frombytes("L", (16, 16), bytes(range(256))) im = Image.frombytes("L", (16, 16), bytes(range(256)))

View File

@ -1,3 +1,5 @@
import pytest
from PIL import Image, ImageMode from PIL import Image, ImageMode
from .helper import hopper from .helper import hopper
@ -49,23 +51,25 @@ def test_sanity():
assert m.typestr == "|u1" assert m.typestr == "|u1"
def test_properties(): @pytest.mark.parametrize(
def check(mode, *result): "mode, expected_base, expected_type, expected_bands, expected_band_names",
signature = ( (
Image.getmodebase(mode), ("1", "L", "L", 1, ("1",)),
Image.getmodetype(mode), ("L", "L", "L", 1, ("L",)),
Image.getmodebands(mode), ("P", "P", "L", 1, ("P",)),
Image.getmodebandnames(mode), ("I", "L", "I", 1, ("I",)),
("F", "L", "F", 1, ("F",)),
("RGB", "RGB", "L", 3, ("R", "G", "B")),
("RGBA", "RGB", "L", 4, ("R", "G", "B", "A")),
("RGBX", "RGB", "L", 4, ("R", "G", "B", "X")),
("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K")),
("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr")),
),
) )
assert signature == result def test_properties(
mode, expected_base, expected_type, expected_bands, expected_band_names
check("1", "L", "L", 1, ("1",)) ):
check("L", "L", "L", 1, ("L",)) assert Image.getmodebase(mode) == expected_base
check("P", "P", "L", 1, ("P",)) assert Image.getmodetype(mode) == expected_type
check("I", "L", "I", 1, ("I",)) assert Image.getmodebands(mode) == expected_bands
check("F", "L", "F", 1, ("F",)) assert Image.getmodebandnames(mode) == expected_band_names
check("RGB", "RGB", "L", 3, ("R", "G", "B"))
check("RGBA", "RGB", "L", 4, ("R", "G", "B", "A"))
check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X"))
check("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K"))
check("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr"))