test/harness/testcaseio.c
changeset 2 cbd63f7ba40f
child 5 f600b0d1fc5d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/harness/testcaseio.c	Wed Jan 25 19:33:43 2012 +0000
     1.3 @@ -0,0 +1,63 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stdio.h>
     1.6 +#include <string.h>
     1.7 +#include <gclib/gclib.h>
     1.8 +#include "testcaseparser.h"
     1.9 +#include "testcaseio.h"
    1.10 +
    1.11 +/*
    1.12 + * Read a testcase in from a file.
    1.13 + * On error, print a suitable message on stderr and return NULL.
    1.14 + * The returned testcase should be freed with testcase_free().
    1.15 + */
    1.16 +Testcase *testcase_parse_file(const char *filename)
    1.17 +{
    1.18 +    Testcase *testcase;
    1.19 +    TestcaseParser *parser;
    1.20 +    char *s;
    1.21 +    const char *tag,*text;
    1.22 +    boolean found_tag=FALSE;
    1.23 +    parser=testcase_parser_new_from_file(filename);
    1.24 +    if (!parser)
    1.25 +	return NULL;
    1.26 +    if (!*testcase_parser_get_flag(parser))
    1.27 +    {
    1.28 +	fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
    1.29 +	testcase_parser_free(parser);
    1.30 +	return NULL;
    1.31 +    }
    1.32 +    testcase=mem_new0(Testcase,1);
    1.33 +    testcase->basename=path_get_basename(filename);
    1.34 +    s=strrchr(testcase->basename,'.');
    1.35 +    if (s)
    1.36 +	*s='\0';
    1.37 +    while(testcase_parser_get_next_tag(parser,&tag,&text))
    1.38 +    {
    1.39 +	if (!testcase->input && !strcmp(tag,"INPUT"))
    1.40 +	    testcase->input=str_dup(text);
    1.41 +	else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
    1.42 +	    testcase->expected=str_dup(text);
    1.43 +	else
    1.44 +	{
    1.45 +	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
    1.46 +	    testcase_free(testcase);
    1.47 +	    testcase_parser_free(parser);
    1.48 +	    return NULL;
    1.49 +	}
    1.50 +	found_tag=TRUE;
    1.51 +    }
    1.52 +    if (!testcase_parser_at_eof(parser))
    1.53 +    {
    1.54 +	if (found_tag)
    1.55 +	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
    1.56 +	      filename);
    1.57 +	else
    1.58 +	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
    1.59 +	      filename);
    1.60 +	testcase_free(testcase);
    1.61 +	testcase_parser_free(parser);
    1.62 +	return NULL;
    1.63 +    }
    1.64 +    testcase_parser_free(parser);
    1.65 +    return testcase;
    1.66 +}