From c8f5b9a37174f7184db1e3e57c0f588a59306c90 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 29 Jul 2018 10:10:51 +0100 Subject: fix tests --- .gitignore | 1 + Makefile | 6 ++ test-data/32bpp-alpha-test | Bin 30356 -> 0 bytes test-data/primary-color-16bpp | Bin 180144 -> 0 bytes test-data/small | Bin 224 -> 0 bytes test/Makefile | 3 + test/decode_rosprite.c | 135 ++++++++++++++++++++++++++++++++++++ test/runtest.sh | 58 ++++++++++++++++ test/sprite/32bpp-alpha-test.spr | Bin 0 -> 30356 bytes test/sprite/primary-color-16bpp.spr | Bin 0 -> 180144 bytes test/sprite/small.spr | Bin 0 -> 224 bytes 11 files changed, 203 insertions(+) delete mode 100644 test-data/32bpp-alpha-test delete mode 100644 test-data/primary-color-16bpp delete mode 100644 test-data/small create mode 100644 test/Makefile create mode 100644 test/decode_rosprite.c create mode 100755 test/runtest.sh create mode 100644 test/sprite/32bpp-alpha-test.spr create mode 100644 test/sprite/primary-color-16bpp.spr create mode 100644 test/sprite/small.spr diff --git a/.gitignore b/.gitignore index dd07bff..015f2f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build-* +*~ diff --git a/Makefile b/Makefile index b82e633..d432327 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ PREFIX ?= /opt/netsurf NSSHARED ?= $(PREFIX)/share/netsurf-buildsystem include $(NSSHARED)/makefiles/Makefile.tools +# Reevaluate when used, as BUILDDIR won't be defined yet +TESTRUNNER = test/runtest.sh $(BUILDDIR) $(EXEEXT) + # Toolchain flags WARNFLAGS := -Wall -Wextra -Wundef -Wpointer-arith -Wcast-align \ -Wwrite-strings -Wstrict-prototypes \ @@ -36,6 +39,9 @@ else CFLAGS := $(CFLAGS) -Dinline="__inline__" endif +TESTCFLAGS := -g -O2 +TESTLDFLAGS := -lm -l$(COMPONENT) $(TESTLDFLAGS) + include $(NSBUILD)/Makefile.top # Extra installation rules diff --git a/test-data/32bpp-alpha-test b/test-data/32bpp-alpha-test deleted file mode 100644 index 92ebd63..0000000 Binary files a/test-data/32bpp-alpha-test and /dev/null differ diff --git a/test-data/primary-color-16bpp b/test-data/primary-color-16bpp deleted file mode 100644 index fba7a08..0000000 Binary files a/test-data/primary-color-16bpp and /dev/null differ diff --git a/test-data/small b/test-data/small deleted file mode 100644 index 5552782..0000000 Binary files a/test-data/small and /dev/null differ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..1402632 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,3 @@ +DIR_TEST_ITEMS := decode_rosprite:decode_rosprite.c + +include $(NSBUILD)/Makefile.subdir diff --git a/test/decode_rosprite.c b/test/decode_rosprite.c new file mode 100644 index 0000000..c038b1f --- /dev/null +++ b/test/decode_rosprite.c @@ -0,0 +1,135 @@ +/* + * Copyright 2018 Vincent Sanders + * + * This file is part of NetSurf's librosprite, http://www.netsurf-browser.org/ + * Licenced under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../include/librosprite.h" + + +static void write_ppm(FILE* fh, const char *srcname, struct rosprite* sprite) +{ + fprintf(fh, "P3\n"); + fprintf(fh, "# %s\n", srcname); + + fprintf(fh, "# name %s\n", sprite->name); + fprintf(fh, "# color_model %s\n", + sprite->mode.color_model == ROSPRITE_RGB ? "RGB" : "CMYK"); + fprintf(fh, "# colorbpp %u\n", sprite->mode.colorbpp); + fprintf(fh, "# xdpi %u\n", sprite->mode.xdpi); + fprintf(fh, "# ydpi %u\n", sprite->mode.ydpi); + fprintf(fh, "# width %u px\n", sprite->width); + fprintf(fh, "# height %u px\n", sprite->height); + + fprintf(fh, "# hasPalette %s\n", sprite->has_palette ? "YES" : "NO"); + if (sprite->has_palette) { + fprintf(fh, "# paletteSize %u\n", sprite->palettesize); + } + fprintf(fh, "# hasMask %s\n", sprite->has_mask ? "YES" : "NO"); + if (sprite->has_mask) { + fprintf(fh, "# mask_width %u\n", sprite->mode.mask_width); + } + if (sprite->has_mask) { + fprintf(fh, "# maskbpp %u\n", sprite->mode.maskbpp); + } + + fprintf(fh, "%u %u 256\n", sprite->width, sprite->height); + + + for (uint32_t y = 0; y < sprite->height; y++) { + for (uint32_t x = 0; x < sprite->width; x++) { + uint32_t color; /* color is 0xrrggbbaa */ + + color = sprite->image[y*sprite->width + x]; + fprintf(fh, "%u %u %u ", + (color & 0xff000000) >> 24, + (color & 0x00ff0000) >> 16, + (color & 0x0000ff00) >> 8); + + } + fprintf(fh, "\n"); + } + +} + + +int main(int argc, char *argv[]) +{ + + int res = 0; + FILE *inf; + FILE *outf = stdout; + struct rosprite_file_context *ctx; + struct rosprite_area *sprite_area; + unsigned int sprite_number = 0;/* number of sprite in sprite area to convert */ + + if (argc < 2) { + fprintf(stderr, "Usage: %s image.spr [out]\n", argv[0]); + return 1; + } + + inf = fopen(argv[1], "rb"); + if (inf == NULL) { + fprintf(stderr, "Unable to open %s for reading\n", argv[1]); + return 3; + } + + if (argc > 2) { + outf = fopen(argv[2], "w+"); + if (outf == NULL) { + fprintf(stderr, + "Unable to open %s for writing\n", argv[2]); + fclose(inf); + return 2; + } + } + + if (rosprite_create_file_context(inf, &ctx) != ROSPRITE_OK) { + fprintf(stderr, "Unable to create file context\n"); + res = 4; + goto cleanup; + } + + /* load sprites into sprite area */ + if (rosprite_load(rosprite_file_reader, + ctx, + &sprite_area) != ROSPRITE_OK) { + fprintf(stderr, "Error loading spritefile\n"); + res = 5; + goto cleanup; + }; + + if (sprite_number >= sprite_area->sprite_count) { + fprintf(stderr, + "Sprite %d of %d is not present in sprite pool\n", + sprite_number, sprite_area->sprite_count); + res = 6; + goto cleanup; + } + + /* write out sprite */ + write_ppm(outf, argv[1], sprite_area->sprites[sprite_number]); + + rosprite_destroy_file_context(ctx); + rosprite_destroy_sprite_area(sprite_area); + +cleanup: + + fclose(inf); + + if (argc > 2) { + fclose(outf); + } + + return res; +} diff --git a/test/runtest.sh b/test/runtest.sh new file mode 100755 index 0000000..c5535d9 --- /dev/null +++ b/test/runtest.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +# run test images through librosprite and count results + +TEST_PATH=$1 +TEST_OUT=${TEST_PATH}/ppm +TEST_LOG=${TEST_PATH}/test.log + +mkdir -p ${TEST_OUT} + +echo "RO Sprite tests" > ${TEST_LOG} + +# netsurf test sprites +SPRTESTS="test/sprite/*.spr" + + +rospritedecode() +{ + OUTF=$(basename ${1} .spr) + CMPF=$(dirname ${1})/${OUTF}.ppm + echo "Icon:${1}" >> ${TEST_LOG} + ${TEST_PATH}/test_decode_rosprite ${1} ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG} + if [ -f "${CMPF}" ]; then + cmp ${CMPF} ${TEST_OUT}/${OUTF}.ppm >> ${TEST_LOG} 2>> ${TEST_LOG} + if [ "$?" -ne 0 ]; then + return 128 + fi + fi +} + +# sprite tests + +SPRTESTTOTC=0 +SPRTESTPASSC=0 +SPRTESTERRC=0 + +# netsurf test sprites +for SPR in $(ls ${SPRTESTS});do + SPRTESTTOTC=$((SPRTESTTOTC+1)) + rospritedecode ${SPR} + ECODE=$? + if [ \( "${ECODE}" -gt 127 \) -o \( "${ECODE}" -eq 1 \) ];then + SPRTESTERRC=$((SPRTESTERRC+1)) + else + SPRTESTPASSC=$((SPRTESTPASSC+1)) + fi +done + +echo "Test sprite decode" +echo "Tests:${SPRTESTTOTC} Pass:${SPRTESTPASSC} Error:${SPRTESTERRC}" + + +# exit code +if [ "${SPRTESTERRC}" -gt 0 ]; then + exit 1 +fi + +exit 0 diff --git a/test/sprite/32bpp-alpha-test.spr b/test/sprite/32bpp-alpha-test.spr new file mode 100644 index 0000000..92ebd63 Binary files /dev/null and b/test/sprite/32bpp-alpha-test.spr differ diff --git a/test/sprite/primary-color-16bpp.spr b/test/sprite/primary-color-16bpp.spr new file mode 100644 index 0000000..fba7a08 Binary files /dev/null and b/test/sprite/primary-color-16bpp.spr differ diff --git a/test/sprite/small.spr b/test/sprite/small.spr new file mode 100644 index 0000000..5552782 Binary files /dev/null and b/test/sprite/small.spr differ -- cgit v1.2.1