From: Petri Lehtinen Date: Thu, 28 Jan 2010 19:04:21 +0000 (+0200) Subject: test/suites/api/test_object.c: Enhance tests for iterators X-Git-Tag: v1.2.1~11 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=jansson.git;a=commitdiff_plain;h=f5662a82cd8fc4d5f43123ed81e5d4b7d5e34bb4 test/suites/api/test_object.c: Enhance tests for iterators --- diff --git a/test/suites/api/test_object.c b/test/suites/api/test_object.c index 7e9ada8..849dac0 100644 --- a/test/suites/api/test_object.c +++ b/test/suites/api/test_object.c @@ -205,10 +205,68 @@ static void test_set_nocheck() json_decref(object); } +static void test_iterators() +{ + json_t *object, *foo, *bar, *baz; + void *iter; + + if(json_object_iter(NULL)) + fail("able to iterate over NULL"); + + if(json_object_iter_next(NULL, NULL)) + fail("able to increment an iterator on a NULL object"); + + object = json_object(); + foo = json_string("foo"); + bar = json_string("bar"); + baz = json_string("baz"); + if(!object || !foo || !bar || !bar) + fail("unable to create values"); + + if(json_object_iter_next(object, NULL)) + fail("able to increment a NULL iterator"); + + if(json_object_set(object, "a", foo) || + json_object_set(object, "b", bar) || + json_object_set(object, "c", baz)) + fail("unable to populate object"); + + iter = json_object_iter(object); + if(!iter) + fail("unable to get iterator"); + if(strcmp(json_object_iter_key(iter), "a")) + fail("iterating failed: wrong key"); + if(json_object_iter_value(iter) != foo) + fail("iterating failed: wrong value"); + + iter = json_object_iter_next(object, iter); + if(!iter) + fail("unable to increment iterator"); + if(strcmp(json_object_iter_key(iter), "b")) + fail("iterating failed: wrong key"); + if(json_object_iter_value(iter) != bar) + fail("iterating failed: wrong value"); + + iter = json_object_iter_next(object, iter); + if(!iter) + fail("unable to increment iterator"); + if(strcmp(json_object_iter_key(iter), "c")) + fail("iterating failed: wrong key"); + if(json_object_iter_value(iter) != baz) + fail("iterating failed: wrong value"); + + if(json_object_iter_next(object, iter) != NULL) + fail("able to iterate over the end"); + + json_decref(object); + json_decref(foo); + json_decref(bar); + json_decref(baz); +} + static void test_misc() { json_t *object, *string, *other_string, *value; - void *iter; object = json_object(); string = json_string("test"); @@ -231,17 +289,6 @@ static void test_misc() if(!json_object_set(object, "a", NULL)) fail("able to set NULL value"); - iter = json_object_iter(object); - if(!iter) - fail("unable to get iterator"); - - if(strcmp(json_object_iter_key(iter), "a")) - fail("iterating failed: wrong key"); - if(json_object_iter_value(iter) != string) - fail("iterating failed: wrong value"); - if(json_object_iter_next(object, iter) != NULL) - fail("able to iterate over the end"); - /* invalid UTF-8 in key */ if(!json_object_set(object, "a\xefz", string)) fail("able to set invalid unicode key"); @@ -332,6 +379,7 @@ int main() test_update(); test_circular(); test_set_nocheck(); + test_iterators(); return 0; }