From b6b44127f23d70231156a8d20fa7d437cac1588f Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel Date: Wed, 20 Apr 2011 16:23:09 -0600 Subject: add tests cases for partial value config --- test/cases/np_partial_bad.json | 1 + test/cases/np_partial_bad.json.gold | 5 +++++ test/cases/partial_ok.json | 1 + test/cases/partial_ok.json.gold | 4 ++++ test/run_tests.sh | 8 ++++++-- test/yajl_test.c | 40 +++++++++++++++++++++---------------- 6 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 test/cases/np_partial_bad.json create mode 100644 test/cases/np_partial_bad.json.gold create mode 100644 test/cases/partial_ok.json create mode 100644 test/cases/partial_ok.json.gold (limited to 'test') diff --git a/test/cases/np_partial_bad.json b/test/cases/np_partial_bad.json new file mode 100644 index 0000000..2fbd027 --- /dev/null +++ b/test/cases/np_partial_bad.json @@ -0,0 +1 @@ +[ "foo", "bar" diff --git a/test/cases/np_partial_bad.json.gold b/test/cases/np_partial_bad.json.gold new file mode 100644 index 0000000..b981bda --- /dev/null +++ b/test/cases/np_partial_bad.json.gold @@ -0,0 +1,5 @@ +array open '[' +string: 'foo' +string: 'bar' +parse error: premature EOF +memory leaks: 0 diff --git a/test/cases/partial_ok.json b/test/cases/partial_ok.json new file mode 100644 index 0000000..2fbd027 --- /dev/null +++ b/test/cases/partial_ok.json @@ -0,0 +1 @@ +[ "foo", "bar" diff --git a/test/cases/partial_ok.json.gold b/test/cases/partial_ok.json.gold new file mode 100644 index 0000000..9f754c7 --- /dev/null +++ b/test/cases/partial_ok.json.gold @@ -0,0 +1,4 @@ +array open '[' +string: 'foo' +string: 'bar' +memory leaks: 0 diff --git a/test/run_tests.sh b/test/run_tests.sh index 08fff8e..9a7d0fc 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -35,6 +35,7 @@ for file in cases/*.json ; do allowComments="-c" forbidGarbage="" allowMultiple="" + noPartials="" # if the filename starts with dc_, we disallow comments for this test case $(basename $file) in @@ -46,16 +47,19 @@ for file in cases/*.json ; do ;; am_*) allowMultiple="-m"; + ;; + np_*) + noPartials="-p"; ;; esac ${ECHO} -n " test case: '$file': " iter=1 success="success" - ${ECHO} "$testBin $allowComments $forbidGarbage $allowMultiple -b $iter < $file > ${file}.test " + ${ECHO} "$testBin $noPartials $allowComments $forbidGarbage $allowMultiple -b $iter < $file > ${file}.test " # parse with a read buffer size ranging from 1-31 to stress stream parsing while [ $iter -lt 32 ] && [ $success = "success" ] ; do - $testBin $allowComments $forbidGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1 + $testBin $noPartials $allowComments $forbidGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1 diff ${DIFF_FLAGS} ${file}.gold ${file}.test if [ $? -eq 0 ] ; then if [ $iter -eq 31 ] ; then : $(( testsSucceeded += 1)) ; fi diff --git a/test/yajl_test.c b/test/yajl_test.c index ca4c737..1b07f8b 100644 --- a/test/yajl_test.c +++ b/test/yajl_test.c @@ -24,12 +24,12 @@ #include /* memory debugging routines */ -typedef struct +typedef struct { size_t numFrees; - size_t numMallocs; + size_t numMallocs; /* XXX: we really need a hash table here with per-allocation - * information */ + * information */ } yajlTestMemoryContext; /* cast void * into context */ @@ -53,9 +53,9 @@ static void * yajlTestRealloc(void * ctx, void * ptr, size_t sz) { if (ptr == NULL) { assert(sz != 0); - TEST_CTX(ctx)->numMallocs++; + TEST_CTX(ctx)->numMallocs++; } else if (sz == 0) { - TEST_CTX(ctx)->numFrees++; + TEST_CTX(ctx)->numFrees++; } return realloc(ptr, sz); @@ -94,7 +94,7 @@ static int test_yajl_string(void *ctx, const unsigned char * stringVal, { printf("string: '"); fwrite(stringVal, 1, stringLen, stdout); - printf("'\n"); + printf("'\n"); return 1; } @@ -151,14 +151,20 @@ static yajl_callbacks callbacks = { static void usage(const char * progname) { fprintf(stderr, - "usage: %s [options] \n" + "usage: %s [options]\n" + "Parse input from stdin as JSON and ouput parsing details " + "to stdout\n" + " -b set the read buffer size\n" " -c allow comments\n" - " -b set the read buffer size\n", + " -g forbid *g*arbage after valid JSON text\n" + " -m allows the parser to consume multiple JSON values\n" + " from a single string separated by whitespace\n" + " -p partial JSON documents should be considered invalid\n", progname); exit(1); } -int +int main(int argc, char ** argv) { yajl_handle hand; @@ -205,10 +211,10 @@ main(int argc, char ** argv) fprintf(stderr, "%zu is an invalid buffer size\n", bufSize); } - } else if (!strcmp("-m", argv[i])) { - multiple = 1; partial = 1; } else if (!strcmp("-g", argv[i])) { trailing = 1; + } else if (!strcmp("-m", argv[i])) { + multiple = 1; partial = 1; } else if (!strcmp("-p", argv[i])) { partial = 1; } else { @@ -237,28 +243,28 @@ main(int argc, char ** argv) for (;;) { rd = fread((void *) fileData, 1, bufSize, stdin); - + if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error reading from '%s'\n", fileName); } break; } - /* read file data, pass to parser */ + /* read file data, now pass to parser */ stat = yajl_parse(hand, fileData, rd); - + if (stat != yajl_status_ok) break; } stat = yajl_parse_complete(hand); - if (stat != yajl_status_ok) + if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 0, fileData, rd); fflush(stdout); fprintf(stderr, "%s", (char *) str); yajl_free_error(hand, str); } - + yajl_free(hand); free(fileData); @@ -272,7 +278,7 @@ main(int argc, char ** argv) */ fflush(stderr); fflush(stdout); - printf("memory leaks:\t%zu\n", memCtx.numMallocs - memCtx.numFrees); + printf("memory leaks:\t%zu\n", memCtx.numMallocs - memCtx.numFrees); return 0; } -- cgit v1.2.1