From ee19c93bb8e1d1423458aad8ea519f88dba91c14 Mon Sep 17 00:00:00 2001 From: "Dominick C. Pastore" Date: Wed, 27 May 2020 16:37:28 -0400 Subject: [PATCH] Update tests for new feature macros --- Makefile | 25 +++++++++++++++++-------- jsmn.h | 6 +++--- test/tests.c | 27 ++++++++++++++------------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 9ff174e3..35df9ca4 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,27 @@ # You can put your build options here -include config.mk -test: test_default test_strict test_links test_strict_links +test: test_default test_nonstrict test_lowmem test_nonstrict_lowmem test_permissiveprims test_permissivestrs test_primkeys test_default: test/tests.c jsmn.h $(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@ ./test/$@ -test_strict: test/tests.c jsmn.h - $(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ +test_nonstrict: test/tests.c jsmn.h + $(CC) -DJSMN_NON_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ ./test/$@ -test_links: test/tests.c jsmn.h - $(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ +test_lowmem: test/tests.c jsmn.h + $(CC) -DJSMN_LOW_MEMORY=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ ./test/$@ -test_strict_links: test/tests.c jsmn.h - $(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ +test_nonstrict_lowmem: test/tests.c jsmn.h + $(CC) -DJSMN_NON_STRICT=1 -DJSMN_LOW_MEMORY=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ + ./test/$@ +test_permissiveprims: test/tests.c jsmn.h + $(CC) -DJSMN_PERMISSIVE_PRIMITIVES=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ + ./test/$@ +test_permissivestrs: test/tests.c jsmn.h + $(CC) -DJSMN_PERMISSIVE_STRINGS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ + ./test/$@ +test_primkeys: test/tests.c jsmn.h + $(CC) -DJSMN_PRIMITIVE_KEYS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@ ./test/$@ simple_example: example/simple.c jsmn.h @@ -31,7 +40,7 @@ clean: rm -f *.o example/*.o rm -f simple_example rm -f jsondump - rm -f test/test_default test/test_strict test/test_links test/test_strict_links + rm -f test/test_default test/test_nonstrict test/test_lowmem test/test_nonstrict_lowmem test/test_permissiveprims test/test_permissivestrs test/test_primkeys .PHONY: clean test diff --git a/jsmn.h b/jsmn.h index dccfceea..b38201e9 100644 --- a/jsmn.h +++ b/jsmn.h @@ -44,13 +44,13 @@ extern "C" { #endif #ifdef JSMN_NON_STRICT -#ifndef +#ifndef JSMN_PERMISSIVE_PRIMITIVES #define JSMN_PERMISSIVE_PRIMITIVES #endif -#ifndef +#ifndef JSMN_PERMISSIVE_STRINGS #define JSMN_PERMISSIVE_STRINGS #endif -#ifndef +#ifndef JSMN_PRIMITIVE_KEYS #define JSMN_PRIMITIVE_KEYS #endif #endif diff --git a/test/tests.c b/test/tests.c index 4ce90af7..1eb92137 100644 --- a/test/tests.c +++ b/test/tests.c @@ -30,11 +30,10 @@ int test_object(void) { JSMN_STRING, "a", 1, JSMN_PRIMITIVE, "0", JSMN_STRING, "b", 1, JSMN_STRING, "c", 0)); -#ifdef JSMN_STRICT +#ifndef JSMN_NON_STRICT check(parse("{\"a\"\n0}", JSMN_ERROR_INVAL, 3)); check(parse("{\"a\", 0}", JSMN_ERROR_INVAL, 3)); check(parse("{\"a\": {2}}", JSMN_ERROR_INVAL, 4)); - check(parse("{\"a\": {2: 3}}", JSMN_ERROR_INVAL, 5)); check(parse("{\"a\": {\"a\": 2 3}}", JSMN_ERROR_INVAL, 6)); check(parse("{\"a\"}", JSMN_ERROR_INVAL, 2)); check(parse("{\"a\": 1, \"b\"}", JSMN_ERROR_INVAL, 4)); @@ -88,7 +87,7 @@ int test_primitive(void) { check(parse("{\"floatVar\" : 12e+6}", 3, 3, JSMN_OBJECT, -1, -1, 1, JSMN_STRING, "floatVar", 1, JSMN_PRIMITIVE, "12e+6")); -#ifdef JSMN_STRICT +#ifndef JSMN_PERMISSIVE_PRIMITIVES check(parse("{\"boolVar\" : tru }", JSMN_ERROR_INVAL, 3)); check(parse("{\"boolVar\" : falsee }", JSMN_ERROR_INVAL, 3)); check(parse("{\"nullVar\" : nulm }", JSMN_ERROR_INVAL, 3)); @@ -145,7 +144,7 @@ int test_string(void) { check(parse("{\"a\":\"str\xc2\xa9\"}", 3, 3, JSMN_OBJECT, -1, -1, 1, JSMN_STRING, "a", 1, JSMN_STRING, "str\xc2\xa9", 0)); -#ifdef JSMN_STRICT +#ifndef JSMN_PERMISSIVE_STRINGS check(parse("{\"a\":\"str\nstr\"}", JSMN_ERROR_INVAL, 3)); check(parse("{\"a\":\"str\\uFFGFstr\"}", JSMN_ERROR_INVAL, 3)); check(parse("{\"a\":\"str\\u@FfF\"}", JSMN_ERROR_INVAL, 3)); @@ -236,20 +235,22 @@ int test_array_nomem(void) { } int test_unquoted_keys(void) { -#ifndef JSMN_STRICT int r; jsmn_parser p; jsmntok_t tok[10]; const char *js; jsmn_init(&p); - js = "{key1: \"value\", key2 : 123}"; + js = "{123: \"value\", 456 : null}"; r = jsmn_parse(&p, js, strlen(js), tok, 10); +#ifdef JSMN_PRIMITIVE_KEYS check(r >= 0); - check(tokeq(js, tok, 5, JSMN_OBJECT, -1, -1, 2, JSMN_PRIMITIVE, "key1", - JSMN_STRING, "value", 0, JSMN_PRIMITIVE, "key2", - JSMN_PRIMITIVE, "123")); + check(tokeq(js, tok, 5, JSMN_OBJECT, -1, -1, 2, JSMN_PRIMITIVE, "123", + JSMN_STRING, "value", 0, JSMN_PRIMITIVE, "456", + JSMN_PRIMITIVE, "null")); +#else + check(r == JSMN_ERROR_INVAL); #endif return 0; } @@ -349,7 +350,7 @@ int test_unenclosed(void) { check(parse(js, 1, 1, JSMN_PRIMITIVE, "false")); js = "0garbage"; -#ifdef JSMN_STRICT +#ifndef JSMN_PERMISSIVE_PRIMITIVES check(parse(js, JSMN_ERROR_INVAL, 1)); #else check(parse(js, 1, 1, JSMN_PRIMITIVE, "0garbage")); @@ -376,7 +377,7 @@ int test_unenclosed(void) { js = " 1234 "; check(parse(js, 1, 1, JSMN_PRIMITIVE, "1234")); -#ifdef JSMN_STRICT +#ifndef JSMN_PERMISSIVE_PRIMITIVES js = "fal"; check(parse(js, JSMN_ERROR_PART, 1)); @@ -441,7 +442,7 @@ int test_object_key(void) { js = "{\"key\": 1}"; check(parse(js, 3, 3, JSMN_OBJECT, 0, 10, 1, JSMN_STRING, "key", 1, JSMN_PRIMITIVE, "1")); -#ifdef JSMN_STRICT +#ifndef JSMN_PRIMITIVE_KEYS js = "{true: 1}"; check(parse(js, JSMN_ERROR_INVAL, 3)); js = "{1: 1}"; @@ -493,7 +494,7 @@ int main(void) { test(test_input_length, "test strings that are not null-terminated"); test(test_issue_22, "test issue #22"); test(test_count, "test tokens count estimation"); - test(test_unenclosed, "test for non-strict mode"); + test(test_unenclosed, "test for non-strict mode"); test(test_unmatched_brackets, "test for unmatched brackets"); test(test_object_key, "test for key type"); test(test_multiple_objects, "test parsing multiple items at once");