/*! * regTestCompareFiles() * * Input: rp (regtest parameters) * index1 (of one output file from reg test) * index2 (of another output file from reg test) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This only does something in "compare" mode. * (2) The canonical format of the golden filenames is: * /tmp/golden/<root of main name>_golden.<index>.<ext of localname> * e.g., * /tmp/golden/maze_golden.0.png */ l_int32 regTestCompareFiles(L_REGPARAMS *rp, l_int32 index1, l_int32 index2) { char *name1, *name2; char namebuf[256]; l_int32 same; SARRAY *sa; PROCNAME("regTestCompareFiles"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (index1 < 0 || index2 < 0) { rp->success = FALSE; return ERROR_INT("index1 and/or index2 is negative", procName, 1); } if (index1 == index2) { rp->success = FALSE; return ERROR_INT("index1 must differ from index2", procName, 1); } rp->index++; if (rp->mode != L_REG_COMPARE) return 0; /* Generate the golden file names */ snprintf(namebuf, sizeof(namebuf), "%s_golden.%d.", rp->testname, index1); sa = getSortedPathnamesInDirectory("/tmp/golden", namebuf, 0, 0); if (sarrayGetCount(sa) != 1) { sarrayDestroy(&sa); rp->success = FALSE; L_ERROR("golden file %s not found\n", procName, namebuf); return 1; } name1 = sarrayGetString(sa, 0, L_COPY); sarrayDestroy(&sa); snprintf(namebuf, sizeof(namebuf), "%s_golden.%d.", rp->testname, index2); sa = getSortedPathnamesInDirectory("/tmp/golden", namebuf, 0, 0); if (sarrayGetCount(sa) != 1) { sarrayDestroy(&sa); rp->success = FALSE; FREE(name1); L_ERROR("golden file %s not found\n", procName, namebuf); return 1; } name2 = sarrayGetString(sa, 0, L_COPY); sarrayDestroy(&sa); /* Test and record on failure */ filesAreIdentical(name1, name2, &same); if (!same) { fprintf(rp->fp, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, name1, name2); fprintf(stderr, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, name1, name2); rp->success = FALSE; } FREE(name1); FREE(name2); return 0; }
main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, start, end, same1, same2; size_t size1, size2; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) exit(ERROR_INT("syntax: whatever11", mainName, 1)); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("junk1", "w", lba3->data, lba3->size); l_binaryWrite("junk2", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "junk1", &same1); filesAreIdentical("test24.jpg", "junk2", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("junk3", "w", lba1->data, lba1->size); l_binaryWrite("junk4", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "junk3", &same1); filesAreIdentical("test24.jpg", "junk4", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending */ data1 = l_binaryRead("whatever10.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("junk1.txt", "w", data2, size2); filesAreIdentical("whatever10.c", "junk1.txt", &same1); if (same1) fprintf(stderr, "OK for appended file\n"); else fprintf(stderr, "Error: appended file is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "junk3.pdf", 0, 0, 100, NULL, 0, NULL); lba1 = l_byteaInitFromFile("junk3.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); l_dnaWriteStream(stderr, da); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("junk5", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); l_byteaDestroy(&lba1); return 0; }
/*! * regTestCheckFile() * * Input: rp (regtest parameters) * localname (name of output file from reg test) * Return: 0 if OK, 1 on error (a failure in comparison is not an error) * * Notes: * (1) This function does one of three things, depending on the mode: * * "generate": makes a "golden" file as a copy @localname. * * "compare": compares @localname contents with the golden file * * "display": makes the @localname file but does no comparison * (2) The canonical format of the golden filenames is: * /tmp/golden/<root of main name>_golden.<index>.<ext of localname> * e.g., * /tmp/golden/maze_golden.0.png * It is important to add an extension to the local name, because * the extension is added to the name of the golden file. */ l_int32 regTestCheckFile(L_REGPARAMS *rp, const char *localname) { char *ext; char namebuf[256]; l_int32 ret, same, format; PIX *pix1, *pix2; PROCNAME("regTestCheckFile"); if (!rp) return ERROR_INT("rp not defined", procName, 1); if (!localname) { rp->success = FALSE; return ERROR_INT("local name not defined", procName, 1); } if (rp->mode != L_REG_GENERATE && rp->mode != L_REG_COMPARE && rp->mode != L_REG_DISPLAY) { rp->success = FALSE; return ERROR_INT("invalid mode", procName, 1); } rp->index++; /* If display mode, no generation and no testing */ if (rp->mode == L_REG_DISPLAY) return 0; /* Generate the golden file name; used in 'generate' and 'compare' */ splitPathAtExtension(localname, NULL, &ext); snprintf(namebuf, sizeof(namebuf), "/tmp/golden/%s_golden.%d%s", rp->testname, rp->index, ext); FREE(ext); /* Generate mode. No testing. */ if (rp->mode == L_REG_GENERATE) { /* Save the file as a golden file */ /* fprintf(stderr, "%d: %s\n", rp->index, namebuf); */ ret = fileCopy(localname, namebuf); if (!ret) fprintf(stderr, "Copy: %s to %s\n", localname, namebuf); return ret; } /* Compare mode: test and record on failure. GIF compression * is lossless for images with up to 8 bpp (but not for RGB * because it must generate a 256 color palette). Although * the read/write cycle for GIF is idempotent in the image * pixels for bpp <= 8, it is not idempotent in the actual * file bytes. Tests comparing file bytes before and after * a GIF read/write cycle will fail. So for GIF we uncompress * the two images and compare the actual pixels. From my tests, * PNG, in addition to being lossless, is idempotent in file * bytes on read/write, so comparing the pixels is not necessary. * (It also increases the regression test time by an an average * of about 8%.) JPEG is lossy and not idempotent in the image * pixels, so no tests are constructed that would require it. */ findFileFormat(localname, &format); if (format == IFF_GIF) { same = 0; pix1 = pixRead(localname); pix2 = pixRead(namebuf); pixEqual(pix1, pix2, &same); pixDestroy(&pix1); pixDestroy(&pix2); } else { filesAreIdentical(localname, namebuf, &same); } if (!same) { fprintf(rp->fp, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, localname, namebuf); fprintf(stderr, "Failure in %s_reg, index %d: comparing %s with %s\n", rp->testname, rp->index, localname, namebuf); rp->success = FALSE; } return 0; }