libgimp: also update unit testing for GimpColorArray.

Making sure not only that the PDB passes correctly the number of colors, but
taking one random color in the array and verifying it is correct.
This commit is contained in:
Jehan 2024-04-18 15:58:43 +02:00
parent 9f149ef3a2
commit 94853968b3
2 changed files with 82 additions and 28 deletions

View file

@ -1,5 +1,10 @@
#define GIMP_TEST_PALETTE "Bears" #define GIMP_TEST_PALETTE "Bears"
#define GIMP_TEST_PALETTE_SIZE 256 #define GIMP_TEST_PALETTE_SIZE 256
#define GIMP_TEST_COLOR_IDX 3
#define GIMP_TEST_COLOR_FORMAT "R'G'B' u8"
#define GIMP_TEST_COLOR_R_U8 72
#define GIMP_TEST_COLOR_G_U8 56
#define GIMP_TEST_COLOR_B_U8 56
static GimpValueArray * static GimpValueArray *
gimp_c_test_run (GimpProcedure *procedure, gimp_c_test_run (GimpProcedure *procedure,
@ -14,6 +19,9 @@ gimp_c_test_run (GimpProcedure *procedure,
GimpPalette *palette2; GimpPalette *palette2;
GeglColor **colors; GeglColor **colors;
gint n_colors; gint n_colors;
const Babl *format;
GeglColor *color;
guint8 rgb[3];
GimpValueArray *retvals; GimpValueArray *retvals;
GIMP_TEST_START("gimp_palette_get_by_name()") GIMP_TEST_START("gimp_palette_get_by_name()")
@ -24,13 +32,22 @@ gimp_c_test_run (GimpProcedure *procedure,
palette2 = gimp_palette_get_by_name (GIMP_TEST_PALETTE); palette2 = gimp_palette_get_by_name (GIMP_TEST_PALETTE);
GIMP_TEST_END(GIMP_IS_PALETTE (palette2) && palette == palette2) GIMP_TEST_END(GIMP_IS_PALETTE (palette2) && palette == palette2)
GIMP_TEST_START("gimp_palette_get_color_count()")
n_colors = gimp_palette_get_color_count (palette);
GIMP_TEST_END(n_colors == GIMP_TEST_PALETTE_SIZE)
GIMP_TEST_START("gimp_palette_get_colors()") GIMP_TEST_START("gimp_palette_get_colors()")
colors = gimp_palette_get_colors (palette); colors = gimp_palette_get_colors (palette);
GIMP_TEST_END(colors != NULL && gimp_color_array_get_length (colors) == GIMP_TEST_PALETTE_SIZE) GIMP_TEST_END(colors != NULL && gimp_color_array_get_length (colors) == GIMP_TEST_PALETTE_SIZE)
GIMP_TEST_START("gimp_palette_get_color_count()") GIMP_TEST_START("Checking fourth palette color's format")
n_colors = gimp_palette_get_color_count (palette); color = colors[GIMP_TEST_COLOR_IDX];
GIMP_TEST_END(n_colors == gimp_color_array_get_length (colors)) format = gegl_color_get_format (color);
GIMP_TEST_END(format == babl_format (GIMP_TEST_COLOR_FORMAT))
GIMP_TEST_START("Checking fourth palette color's RGB components")
gegl_color_get_pixel (color, format, rgb);
GIMP_TEST_END(rgb[0] == GIMP_TEST_COLOR_R_U8 && rgb[1] == GIMP_TEST_COLOR_G_U8 && rgb[2] == GIMP_TEST_COLOR_B_U8)
/* Run the same tests through PDB. */ /* Run the same tests through PDB. */
@ -45,6 +62,17 @@ gimp_c_test_run (GimpProcedure *procedure,
gimp_value_array_unref (retvals); gimp_value_array_unref (retvals);
GIMP_TEST_START("gimp-palette-get-color-count")
retvals = gimp_procedure_run (gimp_pdb_lookup_procedure (gimp_get_pdb (), "gimp-palette-get-color-count"),
"palette", palette, NULL);
GIMP_TEST_END(g_value_get_enum (gimp_value_array_index (retvals, 0)) == GIMP_PDB_SUCCESS)
GIMP_TEST_START("gimp-palette-get-color-count returns the right number of colors")
n_colors = g_value_get_int (gimp_value_array_index (retvals, 1));
GIMP_TEST_END(n_colors == GIMP_TEST_PALETTE_SIZE)
gimp_value_array_unref (retvals);
GIMP_TEST_START("gimp-palette-get-colors") GIMP_TEST_START("gimp-palette-get-colors")
retvals = gimp_procedure_run (gimp_pdb_lookup_procedure (gimp_get_pdb (), "gimp-palette-get-colors"), retvals = gimp_procedure_run (gimp_pdb_lookup_procedure (gimp_get_pdb (), "gimp-palette-get-colors"),
"palette", palette, NULL); "palette", palette, NULL);
@ -58,16 +86,14 @@ gimp_c_test_run (GimpProcedure *procedure,
colors = gimp_value_array_get_color_array (retvals, 1); colors = gimp_value_array_get_color_array (retvals, 1);
GIMP_TEST_END(colors != NULL && gimp_color_array_get_length (colors) == GIMP_TEST_PALETTE_SIZE) GIMP_TEST_END(colors != NULL && gimp_color_array_get_length (colors) == GIMP_TEST_PALETTE_SIZE)
gimp_value_array_unref (retvals); GIMP_TEST_START("Checking fourth palette color's format (returned by PDB)")
color = colors[GIMP_TEST_COLOR_IDX];
format = gegl_color_get_format (color);
GIMP_TEST_END(format == babl_format (GIMP_TEST_COLOR_FORMAT))
GIMP_TEST_START("gimp-palette-get-color-count") GIMP_TEST_START("Checking fourth palette color's RGB values (returned by PDB)")
retvals = gimp_procedure_run (gimp_pdb_lookup_procedure (gimp_get_pdb (), "gimp-palette-get-color-count"), gegl_color_get_pixel (color, format, rgb);
"palette", palette, NULL); GIMP_TEST_END(rgb[0] == GIMP_TEST_COLOR_R_U8 && rgb[1] == GIMP_TEST_COLOR_G_U8 && rgb[2] == GIMP_TEST_COLOR_B_U8)
GIMP_TEST_END(g_value_get_enum (gimp_value_array_index (retvals, 0)) == GIMP_PDB_SUCCESS)
GIMP_TEST_START("gimp-palette-get-color-count returns the right number of colors")
n_colors = g_value_get_int (gimp_value_array_index (retvals, 1));
GIMP_TEST_END(n_colors == GIMP_TEST_PALETTE_SIZE)
gimp_value_array_unref (retvals); gimp_value_array_unref (retvals);

View file

@ -1,22 +1,40 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
pal = Gimp.Palette.get_by_name('Bears') GIMP_TEST_PALETTE = "Bears"
GIMP_TEST_PALETTE_SIZE = 256
GIMP_TEST_COLOR_IDX = 3
GIMP_TEST_COLOR_FORMAT = "R'G'B' u8"
GIMP_TEST_COLOR_R_U8 = 72
GIMP_TEST_COLOR_G_U8 = 56
GIMP_TEST_COLOR_B_U8 = 56
pal = Gimp.Palette.get_by_name(GIMP_TEST_PALETTE)
gimp_assert('gimp_palette_get_by_name()', type(pal) == Gimp.Palette) gimp_assert('gimp_palette_get_by_name()', type(pal) == Gimp.Palette)
pal2 = Gimp.Palette.get_by_name('Bears') pal2 = Gimp.Palette.get_by_name(GIMP_TEST_PALETTE)
gimp_assert('gimp_palette_get_by_name() is unique', pal == pal2) gimp_assert('gimp_palette_get_by_name() is unique', pal == pal2)
colors = pal.get_colors()
gimp_assert('gimp_palette_get_colors()', len(colors) == 256 and type(colors[0]) == Gegl.Color)
n_colors = pal.get_color_count() n_colors = pal.get_color_count()
gimp_assert('gimp_palette_get_color_count()', len(colors) == n_colors) gimp_assert('gimp_palette_get_color_count()', GIMP_TEST_PALETTE_SIZE == n_colors)
colors = pal.get_colors()
gimp_assert('gimp_palette_get_colors()',
len(colors) == GIMP_TEST_PALETTE_SIZE and type(colors[0]) == Gegl.Color)
f = colors[GIMP_TEST_COLOR_IDX].get_format()
gimp_assert ("Checking fourth palette color's format",
f == Babl.format (GIMP_TEST_COLOR_FORMAT))
b = colors[GIMP_TEST_COLOR_IDX].get_bytes(f)
rgb = b.get_data()
gimp_assert ("Checking fourth palette color's RGB components",
int(rgb[0]) == GIMP_TEST_COLOR_R_U8 and int(rgb[1]) == GIMP_TEST_COLOR_G_U8 and int(rgb[2]) == GIMP_TEST_COLOR_B_U8)
# Run the same tests through PDB: # Run the same tests through PDB:
proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-by-name') proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-by-name')
config = proc.create_config() config = proc.create_config()
config.set_property('name', 'Bears') config.set_property('name', GIMP_TEST_PALETTE)
result = proc.run(config) result = proc.run(config)
status = result.index(0) status = result.index(0)
gimp_assert('gimp-palette-get-by-name', status == Gimp.PDBStatusType.SUCCESS) gimp_assert('gimp-palette-get-by-name', status == Gimp.PDBStatusType.SUCCESS)
@ -24,6 +42,16 @@ gimp_assert('gimp-palette-get-by-name', status == Gimp.PDBStatusType.SUCCESS)
pal2 = result.index(1) pal2 = result.index(1)
gimp_assert('gimp-palette-get-by-name and gimp_palette_get_by_name() get identical result', pal == pal2) gimp_assert('gimp-palette-get-by-name and gimp_palette_get_by_name() get identical result', pal == pal2)
proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-color-count')
config = proc.create_config()
config.set_property('palette', pal)
result = proc.run(config)
status = result.index(0)
gimp_assert('gimp-palette-get-color-count', status == Gimp.PDBStatusType.SUCCESS)
n_colors = result.index(1)
gimp_assert('gimp_palette_get_color_count()', GIMP_TEST_PALETTE_SIZE == n_colors)
proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-colors') proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-colors')
config = proc.create_config() config = proc.create_config()
config.set_property('palette', pal) config.set_property('palette', pal)
@ -42,14 +70,14 @@ colors = result.index(1)
gimp_assert('gimp-palette-get-colors', type(colors) == GObject.GBoxed) gimp_assert('gimp-palette-get-colors', type(colors) == GObject.GBoxed)
colors = result.get_color_array(1) colors = result.get_color_array(1)
gimp_assert('gimp_palette_get_colors()', type(colors) == list and len(colors) == 256 and type(colors[0]) == Gegl.Color) gimp_assert('gimp_palette_get_colors()',
type(colors) == list and len(colors) == GIMP_TEST_PALETTE_SIZE and type(colors[0]) == Gegl.Color)
proc = Gimp.get_pdb().lookup_procedure('gimp-palette-get-color-count') f = colors[3].get_format()
config = proc.create_config() gimp_assert ("Checking fourth palette color's format",
config.set_property('palette', pal) f == Babl.format (GIMP_TEST_COLOR_FORMAT))
result = proc.run(config)
status = result.index(0)
gimp_assert('gimp-palette-get-color-count', status == Gimp.PDBStatusType.SUCCESS)
n_colors = result.index(1) b = colors[3].get_bytes(f)
gimp_assert('gimp_palette_get_color_count()', len(colors) == n_colors) rgb = b.get_data()
gimp_assert ("Checking fourth palette color's RGB components",
int(rgb[0]) == GIMP_TEST_COLOR_R_U8 and int(rgb[1]) == GIMP_TEST_COLOR_G_U8 and int(rgb[2]) == GIMP_TEST_COLOR_B_U8)