Update test program to process all files on the command-line, add a --help and

--verbose option, and return the results of the unit tests.
This commit is contained in:
Michael R Sweet 2021-08-23 14:40:15 -04:00
parent 543364dfa2
commit 3f2de9c46a
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244

View File

@ -29,7 +29,7 @@
// Local functions... // Local functions...
// //
static int do_test_file(const char *filename, int objnum); static int do_test_file(const char *filename, int objnum, bool verbose);
static int do_unit_tests(void); static int do_unit_tests(void);
static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label); static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label);
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
@ -56,22 +56,41 @@ int // O - Exit status
main(int argc, // I - Number of command-line arguments main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments char *argv[]) // I - Command-line arguments
{ {
int ret = 0; // Return value
if (argc > 1) if (argc > 1)
{ {
int i; // Looping var int i; // Looping var
bool verbose = false; // Be verbose?
for (i = 1; i < argc; i ++) for (i = 1; i < argc; i ++)
{ {
if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255)) if (!strcmp(argv[i], "--help"))
{
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]");
return (0);
}
else if (!strcmp(argv[i], "--verbose"))
{
verbose = true;
}
else if (argv[i][0] == '-')
{
printf("Unknown option '%s'.\n\n", argv[i]);
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]");
return (1);
}
else if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255))
{ {
// filename.pdf object-number // filename.pdf object-number
if (do_test_file(argv[i], atoi(argv[i + 1]))) if (do_test_file(argv[i], atoi(argv[i + 1]), verbose))
return (1); ret = 1;
i ++; i ++;
} }
else if (do_test_file(argv[i], 0)) else if (do_test_file(argv[i], 0, verbose))
return (1); ret = 1;
} }
} }
else else
@ -82,10 +101,10 @@ main(int argc, // I - Number of command-line arguments
_chdir("../.."); _chdir("../..");
#endif // _WIN32 #endif // _WIN32
do_unit_tests(); ret = do_unit_tests();
} }
return (0); return (ret);
} }
@ -95,7 +114,8 @@ main(int argc, // I - Number of command-line arguments
static int // O - Exit status static int // O - Exit status
do_test_file(const char *filename, // I - PDF filename do_test_file(const char *filename, // I - PDF filename
int objnum) // I - Object number to dump, if any int objnum, // I - Object number to dump, if any
bool verbose) // I - Be verbose?
{ {
bool error = false; // Have we shown an error yet? bool error = false; // Have we shown an error yet?
pdfio_file_t *pdf; // PDF file pdfio_file_t *pdf; // PDF file
@ -158,48 +178,51 @@ do_test_file(const char *filename, // I - PDF filename
printf(" PDF %s, %d pages, %d objects.\n", pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs); printf(" PDF %s, %d pages, %d objects.\n", pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs);
// Show a summary of each page... if (verbose)
for (n = 0; n < num_pages; n ++)
{ {
if ((obj = pdfioFileGetPage(pdf, n)) == NULL) // Show a summary of each page...
for (n = 0; n < num_pages; n ++)
{ {
printf("%s: Unable to get page #%d.\n", filename, (int)n + 1); if ((obj = pdfioFileGetPage(pdf, n)) == NULL)
}
else
{
pdfio_rect_t media_box; // MediaBox value
memset(&media_box, 0, sizeof(media_box));
dict = pdfioObjGetDict(obj);
if (!pdfioDictGetRect(dict, "MediaBox", &media_box))
{ {
if ((obj = pdfioDictGetObj(dict, "Parent")) != NULL) printf("%s: Unable to get page #%d.\n", filename, (int)n + 1);
{
dict = pdfioObjGetDict(obj);
pdfioDictGetRect(dict, "MediaBox", &media_box);
}
} }
else
{
pdfio_rect_t media_box; // MediaBox value
printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2); memset(&media_box, 0, sizeof(media_box));
dict = pdfioObjGetDict(obj);
if (!pdfioDictGetRect(dict, "MediaBox", &media_box))
{
if ((obj = pdfioDictGetObj(dict, "Parent")) != NULL)
{
dict = pdfioObjGetDict(obj);
pdfioDictGetRect(dict, "MediaBox", &media_box);
}
}
printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2);
}
} }
}
// Show the associated value with each object... // Show the associated value with each object...
for (n = 0; n < num_objs; n ++) for (n = 0; n < num_objs; n ++)
{
if ((obj = pdfioFileGetObj(pdf, n)) == NULL)
{ {
printf(" Unable to get object #%d.\n", (int)n); if ((obj = pdfioFileGetObj(pdf, n)) == NULL)
} {
else printf(" Unable to get object #%d.\n", (int)n);
{ }
dict = pdfioObjGetDict(obj); else
{
dict = pdfioObjGetDict(obj);
printf(" %u %u obj dict=%p(%lu pairs)\n", (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL); printf(" %u %u obj dict=%p(%lu pairs)\n", (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL);
fputs(" ", stdout); fputs(" ", stdout);
_pdfioValueDebug(&obj->value, stdout); _pdfioValueDebug(&obj->value, stdout);
putchar('\n'); putchar('\n');
}
} }
} }
} }