Skip to content

Commit

Permalink
Fixed many bugs with reading. Added testing suite and test driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorks committed Feb 21, 2021
1 parent 7db6647 commit a95decd
Show file tree
Hide file tree
Showing 114 changed files with 563 additions and 209 deletions.
29 changes: 29 additions & 0 deletions tests/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Import of test PNGs from libpng to test tPNG's reading abilities.




Here, each PNG file in this directory has an RGBA data equivalent within the
"rawdata" directory that matches. The test (driver.c) will open
every PNG and compare the raw RGBA values against the raw data
equivalent.

If A test fails, the program will print "TEST FAILED" with more information
to stdout and will exit. If all tests pass, the program returns
"The test is complete.\n".

Every major commit should be checked with these tests, and
further tests should be added using the existing patterns here.




To run the tests:

- Run make in this directory.
- Run the resultant test.exe


Alternatively, driver.c and tpng.c compiled together
with your compiler will be sufficient to produce the
text program in the case that GCC and GNUmake isn't available.
107 changes: 0 additions & 107 deletions tests/README.txt

This file was deleted.

191 changes: 187 additions & 4 deletions tests/driver.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
/**************************************************************************
*
* tPNG:
* 2021, Johnathan Corkery
* TINFL:
* Copyright 2013-2014 RAD Game Tools and Valve Software
* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/



#include "../tpng.h"
#include <stdio.h>
#include <string.h>
Expand All @@ -20,6 +56,7 @@ char * TPNG_ERROR__STRINGS[] = {
"Cannot read file.",
"the PNG file data was not successfully parsed.",
"The width/height of the pixel data does not match the correct size.",
"Incorrect pixel data."
};


Expand All @@ -30,6 +67,7 @@ static void throw_error(int error) {
exit(error);
}


static uint8_t * dump_file_data(const char * filename, uint32_t * size) {
FILE * f = fopen(filename, "rb");
if (!f) {
Expand All @@ -53,7 +91,32 @@ static uint8_t * dump_file_data(const char * filename, uint32_t * size) {
}


static int verify_test(const char * filenamePNG, const char * filenameKey) {
static void integrity_check(const char * filenamePNG) {
printf("checking integrity of %s...\n", filenamePNG);

uint32_t pngsize;
uint8_t * pngdata = dump_file_data(filenamePNG, &pngsize);

uint32_t w;
uint32_t h;

uint8_t * pixels = tpng_get_rgba(
pngdata,
pngsize,
&w,
&h
);

free(pixels);
free(pngdata);
}


static int verify_test(const char * filenamePNG) {
char * filenameKey = malloc(strlen(filenamePNG) + 256);;
sprintf(filenameKey, "rawdata/%s.c.data", filenamePNG);


printf("checking %s against %s...\n", filenamePNG, filenameKey);

uint32_t pngsize;
Expand Down Expand Up @@ -85,7 +148,7 @@ static int verify_test(const char * filenamePNG, const char * filenameKey) {
uint8_t * keyIter = keydata;
uint32_t x, y;
for(y = 0; y < h; ++y) {
for(x = 0; x < h; ++x) {
for(x = 0; x < w; ++x) {
if (pixelIter[0] != keyIter[0] ||
pixelIter[1] != keyIter[1] ||
pixelIter[2] != keyIter[2] ||
Expand All @@ -107,12 +170,132 @@ static int verify_test(const char * filenamePNG, const char * filenameKey) {
free(pixels);
free(keydata);
free(pngdata);
free(filenameKey);
}


int main() {
verify_test("gray-1.png", "gray-1.data");
verify_test("gray-1.png");
verify_test("gray-1-1.8.png");
verify_test("gray-1-1.8-tRNS.png");
verify_test("gray-1-linear.png");
verify_test("gray-1-linear-tRNS.png");
verify_test("gray-1-sRGB.png");
verify_test("gray-1-sRGB-tRNS.png");
verify_test("gray-1-tRNS.png");
verify_test("gray-2.png");
verify_test("gray-2-1.8.png");
verify_test("gray-2-linear.png");
verify_test("gray-2-linear-tRNS.png");
verify_test("gray-2-sRGB.png");
verify_test("gray-2-sRGB-tRNS.png");
verify_test("gray-2-tRNS.png");
verify_test("gray-4.png");
verify_test("gray-4-1.8.png");
verify_test("gray-4-linear.png");
verify_test("gray-4-linear-tRNS.png");
verify_test("gray-8.png");
verify_test("gray-8-1.8.png");
verify_test("gray-8-1.8-tRNS.png");
verify_test("gray-8-linear.png");
verify_test("gray-8-linear-tRNS.png");
verify_test("gray-8-sRGB.png");
verify_test("gray-8-sRGB-tRNS.png");
verify_test("gray-16.png");
verify_test("gray-16-1.8.png");
verify_test("gray-16-1.8-tRNS.png");
verify_test("gray-16-linear.png");
verify_test("gray-16-linear-tRNS.png");
verify_test("gray-16-sRGB.png");
verify_test("gray-16-sRGB-tRNS.png");
verify_test("gray-16-tRNS.png");

verify_test("palette-1-1.8.png");
verify_test("palette-1-1.8-tRNS.png");
verify_test("palette-1-linear.png");
verify_test("palette-1-linear-tRNS.png");
verify_test("palette-1.png");
verify_test("palette-1-sRGB.png");
verify_test("palette-1-sRGB-tRNS.png");
verify_test("palette-2-1.8.png");
verify_test("palette-2-1.8-tRNS.png");
verify_test("palette-2-linear.png");
verify_test("palette-2-linear.png");
verify_test("palette-2.png");
verify_test("palette-2-sRGB.png");
verify_test("palette-2-sRGB-tRNS.png");
verify_test("palette-2-tRNS.png");
verify_test("palette-4-1.8.png");
verify_test("palette-4-1.8-tRNS.png");
verify_test("palette-4-linear.png");
verify_test("palette-4-linear-tRNS.png");
verify_test("palette-4.png");
verify_test("palette-4-sRGB.png");
verify_test("palette-4-sRGB-tRNS.png");
verify_test("palette-4-tRNS.png");
verify_test("palette-8-1.8.png");
verify_test("palette-8-1.8-tRNS.png");
verify_test("palette-8-linear.png");
verify_test("palette-8-linear-tRNS.png");
verify_test("palette-8.png");
verify_test("palette-8-sRGB.png");
verify_test("palette-8-sRGB-tRNS.png");
verify_test("palette-8-tRNS.png");
verify_test("rgb-16-1.8.png");
verify_test("rgb-16-1.8-tRNS.png");
verify_test("rgb-16-linear.png");
verify_test("rgb-16-linear-tRNS.png");
verify_test("rgb-16.png");
verify_test("rgb-16-sRGB.png");
verify_test("rgb-16-sRGB-tRNS.png");
verify_test("rgb-16-tRNS.png");
verify_test("rgb-8-1.8.png");
verify_test("rgb-8-1.8-tRNS.png");
verify_test("rgb-8-linear.png");
verify_test("rgb-8-linear-tRNS.png");
verify_test("rgb-8.png");
verify_test("rgb-8-sRGB.png");
verify_test("rgb-8-sRGB-tRNS.png");
verify_test("rgb-8-tRNS.png");

verify_test("rgb-alpha-16-1.8.png");
verify_test("rgb-alpha-16-linear.png");
verify_test("rgb-alpha-16.png");
verify_test("rgb-alpha-16-sRGB.png");

verify_test("rgb-alpha-8-1.8.png");
verify_test("rgb-alpha-8-linear.png");
verify_test("rgb-alpha-8.png");
verify_test("rgb-alpha-8-sRGB.png");




integrity_check("crashers/badadler.png");
integrity_check("crashers/badcrc.png");
integrity_check("crashers/bad_iCCP.png");
integrity_check("crashers/empty_ancillary_chunks.png");
integrity_check("crashers/huge_bKGD_chunk.png");
integrity_check("crashers/huge_cHRM_chunk.png");
integrity_check("crashers/huge_eXIf_chunk.png");
integrity_check("crashers/huge_gAMA_chunk.png");
integrity_check("crashers/huge_hIST_chunk.png");
integrity_check("crashers/huge_iCCP_chunk.png");
integrity_check("crashers/huge_IDAT.png");
integrity_check("crashers/huge_iTXt_chunk.png");
integrity_check("crashers/huge_juNk_safe_to_copy.png");
integrity_check("crashers/huge_juNK_unsafe_to_copy.png");
integrity_check("crashers/huge_pCAL_chunk.png");
integrity_check("crashers/huge_pHYs_chunk.png");
integrity_check("crashers/huge_sCAL_chunk.png");
integrity_check("crashers/huge_sPLT_chunk.png");
integrity_check("crashers/huge_sRGB_chunk.png");
integrity_check("crashers/huge_sTER_chunk.png");
integrity_check("crashers/huge_tEXt_chunk.png");
integrity_check("crashers/huge_tIME_chunk.png");
integrity_check("crashers/huge_zTXt_chunk.png");


printf("The test is complete.\n");
return 0;
}
}
Empty file removed tests/driver.h
Empty file.
Loading

0 comments on commit a95decd

Please sign in to comment.