X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=test%2Fsuites%2Fapi%2Ftest_object.c;h=90370e5051420d3ac3110430d30bbd51c15c80ea;hb=f18ef5144a77ebdbe7285711884b217b86e2c3b6;hp=7e9ada8c13813edb18cf5db18e229b3ec667fd85;hpb=f9febb64c5dc15e03ecf3ca552e0654ee919b52e;p=jansson.git diff --git a/test/suites/api/test_object.c b/test/suites/api/test_object.c index 7e9ada8..90370e5 100644 --- a/test/suites/api/test_object.c +++ b/test/suites/api/test_object.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Petri Lehtinen + * Copyright (c) 2009, 2010 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -205,10 +205,98 @@ 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"); + + if(json_object_iter_at(object, "foo")) + fail("json_object_iter_at() succeeds for non-existent key"); + + iter = json_object_iter_at(object, "b"); + if(!iter) + fail("json_object_iter_at() fails for an existing key"); + + 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_set(object, iter, bar)) + fail("unable to set value at iterator"); + + if(strcmp(json_object_iter_key(iter), "c")) + fail("json_object_iter_key() fails after json_object_iter_set()"); + if(json_object_iter_value(iter) != bar) + fail("json_object_iter_value() fails after json_object_iter_set()"); + if(json_object_get(object, "c") != bar) + fail("json_object_get() fails after json_object_iter_set()"); + + 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 +319,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"); @@ -325,6 +402,41 @@ static void test_misc() json_decref(object); } +static void test_preserve_order() +{ + json_t *object; + char *result; + + const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}"; + + object = json_object(); + + json_object_set_new(object, "foobar", json_integer(1)); + json_object_set_new(object, "bazquux", json_integer(2)); + json_object_set_new(object, "lorem ipsum", json_integer(3)); + json_object_set_new(object, "dolor", json_integer(4)); + json_object_set_new(object, "sit amet", json_integer(5)); + + /* changing a value should preserve the order */ + json_object_set_new(object, "bazquux", json_integer(6)); + + /* deletion shouldn't change the order of others */ + json_object_del(object, "dolor"); + + /* add a new item just to make sure */ + json_object_set_new(object, "helicopter", json_integer(7)); + + result = json_dumps(object, JSON_PRESERVE_ORDER); + + if(strcmp(expected, result) != 0) { + fprintf(stderr, "%s != %s", expected, result); + fail("JSON_PRESERVE_ORDER doesn't work"); + } + + free(result); + json_decref(object); +} + int main() { test_misc(); @@ -332,6 +444,8 @@ int main() test_update(); test_circular(); test_set_nocheck(); + test_iterators(); + test_preserve_order(); return 0; }