diff options
author | Lloyd Hilaiel <lloyd@hilaiel.com> | 2009-04-01 18:05:07 +0300 |
---|---|---|
committer | Lloyd Hilaiel <lloyd@hilaiel.com> | 2009-04-01 18:05:07 +0300 |
commit | b78dd790825e3347422ef8ac56d2ad557a473e8f (patch) | |
tree | e1caa8c32bad3281b704d334b1a4d94c561af2ce /test | |
parent | af305526e77828621de645c4dd4c93f2d76b821b (diff) | |
download | yajl-b78dd790825e3347422ef8ac56d2ad557a473e8f.tar.gz |
* BREAKING API CHANGE: allow client to specify memory allocation routines
* bump yajl version to 1.0.0
* update TODO list, removing completed tasks
* update yajl_test to override allocation routines and count allocations/frees, providing an automated standalone way to validate we're not leaky
* update all copyright dates
* update all .gold files with new test output
* update ChangeLog in preparation for 1.0.0 release
Diffstat (limited to 'test')
41 files changed, 110 insertions, 4 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c6d65e9..85d673d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2007, Lloyd Hilaiel. +# Copyright 2007-2009, Lloyd Hilaiel. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are diff --git a/test/cases/array.json.gold b/test/cases/array.json.gold index 95ec334..477559d 100644 --- a/test/cases/array.json.gold +++ b/test/cases/array.json.gold @@ -19,3 +19,4 @@ array close ']' string: '
\' array close ']' +memory leaks: 0 diff --git a/test/cases/array_close.json.gold b/test/cases/array_close.json.gold index 4eedbc9..972fd39 100644 --- a/test/cases/array_close.json.gold +++ b/test/cases/array_close.json.gold @@ -1 +1,2 @@ parse error: unallowed token at this point in JSON text +memory leaks: 0 diff --git a/test/cases/array_open.json.gold b/test/cases/array_open.json.gold index 86c13ec..478b6b9 100644 --- a/test/cases/array_open.json.gold +++ b/test/cases/array_open.json.gold @@ -1 +1,2 @@ array open '[' +memory leaks: 0 diff --git a/test/cases/bogus_char.json.gold b/test/cases/bogus_char.json.gold index ccbeed5..0c68a49 100644 --- a/test/cases/bogus_char.json.gold +++ b/test/cases/bogus_char.json.gold @@ -7,3 +7,4 @@ string: 'be' string: 'a happy bit of json' string: 'but someone, misspelled "true"' lexical error: invalid string in json text. +memory leaks: 0 diff --git a/test/cases/codepoints_from_unicode_org.json.gold b/test/cases/codepoints_from_unicode_org.json.gold index 12b358a..f359a1b 100644 --- a/test/cases/codepoints_from_unicode_org.json.gold +++ b/test/cases/codepoints_from_unicode_org.json.gold @@ -1 +1,2 @@ string: 'Mа二𐌂' +memory leaks: 0 diff --git a/test/cases/dc_simple_with_comments.json.gold b/test/cases/dc_simple_with_comments.json.gold index 92be7a5..d222e9b 100644 --- a/test/cases/dc_simple_with_comments.json.gold +++ b/test/cases/dc_simple_with_comments.json.gold @@ -2,3 +2,4 @@ map open '{' key: 'this' string: 'is' lexical error: probable comment found in input text, comments are not enabled. +memory leaks: 0 diff --git a/test/cases/deep_arrays.json.gold b/test/cases/deep_arrays.json.gold index e549637..4d5cd99 100644 --- a/test/cases/deep_arrays.json.gold +++ b/test/cases/deep_arrays.json.gold @@ -2046,3 +2046,4 @@ array close ']' array close ']' array close ']' array close ']' +memory leaks: 0 diff --git a/test/cases/difficult_json_c_test_case.json.gold b/test/cases/difficult_json_c_test_case.json.gold index eaaf41a..c3adc11 100644 --- a/test/cases/difficult_json_c_test_case.json.gold +++ b/test/cases/difficult_json_c_test_case.json.gold @@ -33,3 +33,4 @@ array close ']' map close '}' map close '}' map close '}' +memory leaks: 0 diff --git a/test/cases/difficult_json_c_test_case_with_comments.json.gold b/test/cases/difficult_json_c_test_case_with_comments.json.gold index eaaf41a..c3adc11 100644 --- a/test/cases/difficult_json_c_test_case_with_comments.json.gold +++ b/test/cases/difficult_json_c_test_case_with_comments.json.gold @@ -33,3 +33,4 @@ array close ']' map close '}' map close '}' map close '}' +memory leaks: 0 diff --git a/test/cases/doubles.json.gold b/test/cases/doubles.json.gold index 0426b37..c0ea726 100644 --- a/test/cases/doubles.json.gold +++ b/test/cases/doubles.json.gold @@ -4,3 +4,4 @@ double: 10 double: 3.14157 double: 1000 array close ']' +memory leaks: 0 diff --git a/test/cases/empty_array.json.gold b/test/cases/empty_array.json.gold index 45924af..f069c8f 100644 --- a/test/cases/empty_array.json.gold +++ b/test/cases/empty_array.json.gold @@ -1,2 +1,3 @@ array open '[' array close ']' +memory leaks: 0 diff --git a/test/cases/empty_string.json.gold b/test/cases/empty_string.json.gold index c3a3cd3..c197bba 100644 --- a/test/cases/empty_string.json.gold +++ b/test/cases/empty_string.json.gold @@ -1 +1,2 @@ string: '' +memory leaks: 0 diff --git a/test/cases/escaped_bulgarian.json.gold b/test/cases/escaped_bulgarian.json.gold index 9f2aa00..a0c9ae9 100644 --- a/test/cases/escaped_bulgarian.json.gold +++ b/test/cases/escaped_bulgarian.json.gold @@ -4,3 +4,4 @@ string: 'Му' string: 'Еба' string: 'Майката' array close ']' +memory leaks: 0 diff --git a/test/cases/escaped_foobar.json.gold b/test/cases/escaped_foobar.json.gold index 774e867..5753c64 100644 --- a/test/cases/escaped_foobar.json.gold +++ b/test/cases/escaped_foobar.json.gold @@ -1 +1,2 @@ string: 'foobar' +memory leaks: 0 diff --git a/test/cases/false.json.gold b/test/cases/false.json.gold index c91690a..e55fa1f 100644 --- a/test/cases/false.json.gold +++ b/test/cases/false.json.gold @@ -1 +1,2 @@ bool: false +memory leaks: 0 diff --git a/test/cases/false_then_garbage.json.gold b/test/cases/false_then_garbage.json.gold index c91690a..e55fa1f 100644 --- a/test/cases/false_then_garbage.json.gold +++ b/test/cases/false_then_garbage.json.gold @@ -1 +1,2 @@ bool: false +memory leaks: 0 diff --git a/test/cases/integers.json.gold b/test/cases/integers.json.gold index 7341a2d..bb55c39 100644 --- a/test/cases/integers.json.gold +++ b/test/cases/integers.json.gold @@ -11,3 +11,4 @@ integer: -123456789 integer: 2147483647 integer: -2147483647 parse error: integer overflow +memory leaks: 0 diff --git a/test/cases/invalid_utf8.json.gold b/test/cases/invalid_utf8.json.gold index 0cabb13..c0dec77 100644 --- a/test/cases/invalid_utf8.json.gold +++ b/test/cases/invalid_utf8.json.gold @@ -1,2 +1,3 @@ array open '[' lexical error: invalid bytes in UTF8 string. +memory leaks: 0 diff --git a/test/cases/isolated_surrogate_marker.json.gold b/test/cases/isolated_surrogate_marker.json.gold index 1ad9e8b..cca172e 100644 --- a/test/cases/isolated_surrogate_marker.json.gold +++ b/test/cases/isolated_surrogate_marker.json.gold @@ -1 +1,2 @@ string: '?' +memory leaks: 0 diff --git a/test/cases/leading_zero_in_number.json.gold b/test/cases/leading_zero_in_number.json.gold index 828aec8..1a1580d 100644 --- a/test/cases/leading_zero_in_number.json.gold +++ b/test/cases/leading_zero_in_number.json.gold @@ -2,3 +2,4 @@ map open '{' key: 'bad thing' integer: 0 parse error: after key and value, inside map, I expect ',' or '}' +memory leaks: 0 diff --git a/test/cases/lonely_minus_sign.json.gold b/test/cases/lonely_minus_sign.json.gold index 4b23c61..d15ede9 100644 --- a/test/cases/lonely_minus_sign.json.gold +++ b/test/cases/lonely_minus_sign.json.gold @@ -6,3 +6,4 @@ string: 'blue' string: 'baby where are you?' string: 'oh boo hoo!' lexical error: malformed number, a digit is required after the minus sign. +memory leaks: 0 diff --git a/test/cases/lonely_number.json.gold b/test/cases/lonely_number.json.gold index cdc08ba..181fdfa 100644 --- a/test/cases/lonely_number.json.gold +++ b/test/cases/lonely_number.json.gold @@ -1 +1,2 @@ integer: 123456789 +memory leaks: 0 diff --git a/test/cases/map_close.json.gold b/test/cases/map_close.json.gold index 4eedbc9..972fd39 100644 --- a/test/cases/map_close.json.gold +++ b/test/cases/map_close.json.gold @@ -1 +1,2 @@ parse error: unallowed token at this point in JSON text +memory leaks: 0 diff --git a/test/cases/map_open.json.gold b/test/cases/map_open.json.gold index a823ebe..ab1f33d 100644 --- a/test/cases/map_open.json.gold +++ b/test/cases/map_open.json.gold @@ -1 +1,2 @@ map open '{' +memory leaks: 0 diff --git a/test/cases/missing_integer_after_decimal_point.json.gold b/test/cases/missing_integer_after_decimal_point.json.gold index 1d85c91..c2e7e2e 100644 --- a/test/cases/missing_integer_after_decimal_point.json.gold +++ b/test/cases/missing_integer_after_decimal_point.json.gold @@ -1 +1,2 @@ lexical error: malformed number, a digit is required after the decimal point. +memory leaks: 0 diff --git a/test/cases/missing_integer_after_exponent.json.gold b/test/cases/missing_integer_after_exponent.json.gold index b9f184f..5089681 100644 --- a/test/cases/missing_integer_after_exponent.json.gold +++ b/test/cases/missing_integer_after_exponent.json.gold @@ -1 +1,2 @@ lexical error: malformed number, a digit is required after the exponent. +memory leaks: 0 diff --git a/test/cases/non_utf8_char_in_string.json.gold b/test/cases/non_utf8_char_in_string.json.gold index b3780ae..5e3c580 100644 --- a/test/cases/non_utf8_char_in_string.json.gold +++ b/test/cases/non_utf8_char_in_string.json.gold @@ -5,3 +5,4 @@ key: 'CoreletType' string: 'standalone' key: 'documentation' lexical error: invalid bytes in UTF8 string. +memory leaks: 0 diff --git a/test/cases/null.json.gold b/test/cases/null.json.gold index 19765bd..94ad0fa 100644 --- a/test/cases/null.json.gold +++ b/test/cases/null.json.gold @@ -1 +1,2 @@ null +memory leaks: 0 diff --git a/test/cases/null_then_garbage.json.gold b/test/cases/null_then_garbage.json.gold index 19765bd..94ad0fa 100644 --- a/test/cases/null_then_garbage.json.gold +++ b/test/cases/null_then_garbage.json.gold @@ -1 +1,2 @@ null +memory leaks: 0 diff --git a/test/cases/nulls_and_bools.json.gold b/test/cases/nulls_and_bools.json.gold index 8dc173c..51f990b 100644 --- a/test/cases/nulls_and_bools.json.gold +++ b/test/cases/nulls_and_bools.json.gold @@ -6,3 +6,4 @@ bool: false key: 'null' null map close '}' +memory leaks: 0 diff --git a/test/cases/simple.json.gold b/test/cases/simple.json.gold index 59b7d6a..80fcad2 100644 --- a/test/cases/simple.json.gold +++ b/test/cases/simple.json.gold @@ -6,3 +6,4 @@ string: 'simple' key: 'json' string: 'right?' map close '}' +memory leaks: 0 diff --git a/test/cases/simple_with_comments.json.gold b/test/cases/simple_with_comments.json.gold index 59b7d6a..80fcad2 100644 --- a/test/cases/simple_with_comments.json.gold +++ b/test/cases/simple_with_comments.json.gold @@ -6,3 +6,4 @@ string: 'simple' key: 'json' string: 'right?' map close '}' +memory leaks: 0 diff --git a/test/cases/string_invalid_escape.json.gold b/test/cases/string_invalid_escape.json.gold index bdc473e..439df43 100644 --- a/test/cases/string_invalid_escape.json.gold +++ b/test/cases/string_invalid_escape.json.gold @@ -1,2 +1,3 @@ array open '[' lexical error: inside a string, '\' occurs before a character which it may not. +memory leaks: 0 diff --git a/test/cases/string_invalid_hex_char.json.gold b/test/cases/string_invalid_hex_char.json.gold index d8b535e..8498792 100644 --- a/test/cases/string_invalid_hex_char.json.gold +++ b/test/cases/string_invalid_hex_char.json.gold @@ -1 +1,2 @@ lexical error: invalid (non-hex) character occurs after '\u' inside string. +memory leaks: 0 diff --git a/test/cases/string_with_escapes.json.gold b/test/cases/string_with_escapes.json.gold index ac878a2..086a9dc 100644 --- a/test/cases/string_with_escapes.json.gold +++ b/test/cases/string_with_escapes.json.gold @@ -4,3 +4,4 @@ string: ' string: '"and this string has an escape at the beginning' string: 'and this string has no escapes' array close ']' +memory leaks: 0 diff --git a/test/cases/string_with_invalid_newline.json.gold b/test/cases/string_with_invalid_newline.json.gold index 80c1b8a..c38bdef 100644 --- a/test/cases/string_with_invalid_newline.json.gold +++ b/test/cases/string_with_invalid_newline.json.gold @@ -1 +1,2 @@ lexical error: invalid character inside string. +memory leaks: 0 diff --git a/test/cases/true.json.gold b/test/cases/true.json.gold index 3283d03..0858bf7 100644 --- a/test/cases/true.json.gold +++ b/test/cases/true.json.gold @@ -1 +1,2 @@ bool: true +memory leaks: 0 diff --git a/test/cases/true_then_garbage.json.gold b/test/cases/true_then_garbage.json.gold index 3283d03..0858bf7 100644 --- a/test/cases/true_then_garbage.json.gold +++ b/test/cases/true_then_garbage.json.gold @@ -1 +1,2 @@ bool: true +memory leaks: 0 diff --git a/test/cases/unescaped_bulgarian.json.gold b/test/cases/unescaped_bulgarian.json.gold index ac34442..8db076e 100644 --- a/test/cases/unescaped_bulgarian.json.gold +++ b/test/cases/unescaped_bulgarian.json.gold @@ -1,3 +1,4 @@ array open '[' string: 'Да Му Еба Майката' array close ']' +memory leaks: 0 diff --git a/test/yajl_test.c b/test/yajl_test.c index 3c2fcf1..77281a5 100644 --- a/test/yajl_test.c +++ b/test/yajl_test.c @@ -1,5 +1,5 @@ /* - * Copyright 2007, Lloyd Hilaiel. + * Copyright 2007-2009, Lloyd Hilaiel. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -37,6 +37,48 @@ #include <stdlib.h> #include <string.h> +#include <assert.h> + +/* memory debugging routines */ +typedef struct +{ + unsigned int numFrees; + unsigned int numMallocs; + /* XXX: we really need a hash table here with per-allocation + * information */ +} yajlTestMemoryContext; + +/* cast void * into context */ +#define TEST_CTX(vptr) ((yajlTestMemoryContext *) (vptr)) + +static void yajlTestFree(void * ctx, void * ptr) +{ + assert(ptr != NULL); + TEST_CTX(ctx)->numFrees++; + free(ptr); +} + +static void * yajlTestMalloc(void * ctx, unsigned int sz) +{ + assert(sz != 0); + TEST_CTX(ctx)->numMallocs++; + return malloc(sz); +} + +static void * yajlTestRealloc(void * ctx, void * ptr, unsigned int sz) +{ + if (ptr == NULL) { + assert(sz != 0); + TEST_CTX(ctx)->numMallocs++; + } else if (sz == 0) { + TEST_CTX(ctx)->numFrees++; + } + + return realloc(ctx, sz); +} + + +/* begin parsing callback routines */ #define BUF_SIZE 2048 static int test_yajl_null(void *ctx) @@ -144,6 +186,21 @@ main(int argc, char ** argv) yajl_parser_config cfg = { 0, 1 }; int i, j, done; + /* memory allocation debugging: allocate a structure which collects + * statistics */ + yajlTestMemoryContext memCtx = { 0,0 }; + + /* memory allocation debugging: allocate a structure which holds + * allocation routines */ + yajl_alloc_funcs allocFuncs = { + yajlTestMalloc, + yajlTestRealloc, + yajlTestFree, + (void *) NULL + }; + + allocFuncs.ctx = (void *) &memCtx; + /* check arguments. We expect exactly one! */ for (i=1;i<argc;i++) { if (!strcmp("-c", argv[i])) { @@ -183,7 +240,7 @@ main(int argc, char ** argv) fileName = argv[argc-1]; /* ok. open file. let's read and parse */ - hand = yajl_alloc(&callbacks, &cfg, NULL); + hand = yajl_alloc(&callbacks, &cfg, &allocFuncs, NULL); done = 0; while (!done) { @@ -210,7 +267,7 @@ main(int argc, char ** argv) unsigned char * str = yajl_get_error(hand, 0, fileData, rd); fflush(stdout); fprintf(stderr, (char *) str); - yajl_free_error(str); + yajl_free_error(hand, str); break; } } @@ -218,5 +275,15 @@ main(int argc, char ** argv) yajl_free(hand); free(fileData); + /* finally, print out some memory statistics */ + +/* (lth) only print leaks here, as allocations and frees may vary depending + * on read buffer size, causing false failures. + * + * printf("allocations:\t%u\n", memCtx.numMallocs); + * printf("frees:\t\t%u\n", memCtx.numFrees); +*/ + printf("memory leaks:\t%u\n", memCtx.numMallocs - memCtx.numFrees); + return 0; } |