Adds json_pack / json_unpack variadic functions.
This commit is contained in:
committed by
Petri Lehtinen
parent
1acd1a7b56
commit
198d537be7
2
test/.gitignore
vendored
2
test/.gitignore
vendored
@@ -9,3 +9,5 @@ suites/api/test_number
|
||||
suites/api/test_object
|
||||
suites/api/test_simple
|
||||
suites/api/test_cpp
|
||||
suites/api/test_pack
|
||||
suites/api/test_unpack
|
||||
|
||||
@@ -8,7 +8,9 @@ check_PROGRAMS = \
|
||||
test_load \
|
||||
test_simple \
|
||||
test_number \
|
||||
test_object
|
||||
test_object \
|
||||
test_pack \
|
||||
test_unpack
|
||||
|
||||
test_array_SOURCES = test_array.c util.h
|
||||
test_copy_SOURCES = test_copy.c util.h
|
||||
@@ -17,6 +19,8 @@ test_load_SOURCES = test_load.c util.h
|
||||
test_simple_SOURCES = test_simple.c util.h
|
||||
test_number_SOURCES = test_number.c util.h
|
||||
test_object_SOURCES = test_object.c util.h
|
||||
test_pack_SOURCES = test_pack.c util.h
|
||||
test_unpack_SOURCES = test_unpack.c util.h
|
||||
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
AM_CFLAGS = -Wall -Werror
|
||||
|
||||
@@ -55,6 +55,8 @@ json_load_file
|
||||
json_equal
|
||||
json_copy
|
||||
json_deep_copy
|
||||
json_pack
|
||||
json_unpack
|
||||
EOF
|
||||
|
||||
# The list of functions are not exported in the library because they
|
||||
|
||||
155
test/suites/api/test_pack.c
Normal file
155
test/suites/api/test_pack.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
|
||||
* Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
||||
*
|
||||
* Jansson is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include "util.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
json_t *value;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Simple, valid json_pack cases
|
||||
*/
|
||||
|
||||
/* true */
|
||||
value = json_pack(NULL, "b", 1);
|
||||
if(!json_is_true(value))
|
||||
fail("json_pack boolean failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
fail("json_pack boolean refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* false */
|
||||
value = json_pack(NULL, "b", 0);
|
||||
if(!json_is_false(value))
|
||||
fail("json_pack boolean failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
fail("json_pack boolean refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* null */
|
||||
value = json_pack(NULL, "n");
|
||||
if(!json_is_null(value))
|
||||
fail("json_pack null failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
fail("json_pack null refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* integer */
|
||||
value = json_pack(NULL, "i", 1);
|
||||
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
||||
fail("json_pack integer failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack integer refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
|
||||
/* real */
|
||||
value = json_pack(NULL, "f", 1.0);
|
||||
if(!json_is_real(value) || json_real_value(value) != 1.0)
|
||||
fail("json_pack real failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack real refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* string */
|
||||
value = json_pack(NULL, "s", "test");
|
||||
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
|
||||
fail("json_pack string failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack string refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* empty object */
|
||||
value = json_pack(NULL, "{}", 1.0);
|
||||
if(!json_is_object(value) || json_object_size(value) != 0)
|
||||
fail("json_pack empty object failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack empty object refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* empty list */
|
||||
value = json_pack(NULL, "[]", 1.0);
|
||||
if(!json_is_array(value) || json_array_size(value) != 0)
|
||||
fail("json_pack empty list failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack empty list failed");
|
||||
json_decref(value);
|
||||
|
||||
/* non-incref'd object */
|
||||
value = json_pack(NULL, "o", json_integer(1));
|
||||
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
||||
fail("json_pack object failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
fail("json_pack integer refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* incref'd object */
|
||||
value = json_pack(NULL, "O", json_integer(1));
|
||||
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
||||
fail("json_pack object failed");
|
||||
if(value->refcount != (ssize_t)2)
|
||||
fail("json_pack integer refcount failed");
|
||||
json_decref(value);
|
||||
json_decref(value);
|
||||
|
||||
/* simple object */
|
||||
value = json_pack(NULL, "{s:[]}", "foo");
|
||||
if(!json_is_object(value) || json_object_size(value) != 1)
|
||||
fail("json_pack object failed");
|
||||
if(!json_is_array(json_object_get(value, "foo")))
|
||||
fail("json_pack object failed");
|
||||
if(json_object_get(value, "foo")->refcount != (ssize_t)1)
|
||||
fail("json_pack object refcount failed");
|
||||
json_decref(value);
|
||||
|
||||
/* simple array */
|
||||
value = json_pack(NULL, "[i,i,i]", 0, 1, 2);
|
||||
if(!json_is_array(value) || json_array_size(value) != 3)
|
||||
fail("json_pack object failed");
|
||||
for(i=0; i<3; i++)
|
||||
{
|
||||
if(!json_is_integer(json_array_get(value, i)) ||
|
||||
json_integer_value(json_array_get(value, i)) != i)
|
||||
|
||||
fail("json_pack integer array failed");
|
||||
}
|
||||
json_decref(value);
|
||||
|
||||
/*
|
||||
* Invalid cases
|
||||
*/
|
||||
|
||||
/* mismatched open/close array/object */
|
||||
if(json_pack(NULL, "[}"))
|
||||
fail("json_pack failed to catch mismatched '}'");
|
||||
|
||||
if(json_pack(NULL, "{]"))
|
||||
fail("json_pack failed to catch mismatched ']'");
|
||||
|
||||
/* missing close array */
|
||||
if(json_pack(NULL, "["))
|
||||
fail("json_pack failed to catch missing ']'");
|
||||
|
||||
/* missing close object */
|
||||
if(json_pack(NULL, "{"))
|
||||
fail("json_pack failed to catch missing '}'");
|
||||
|
||||
/* NULL string */
|
||||
if(json_pack(NULL, "s", NULL))
|
||||
fail("json_pack failed to catch null string");
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* vim: ts=4:expandtab:sw=4
|
||||
*/
|
||||
110
test/suites/api/test_unpack.c
Normal file
110
test/suites/api/test_unpack.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
|
||||
* Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
|
||||
*
|
||||
* Jansson is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include "util.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
json_t *j, *j2;
|
||||
int i1, i2, i3;
|
||||
int rv;
|
||||
//void* v;
|
||||
double f;
|
||||
char *s;
|
||||
|
||||
/*
|
||||
* Simple, valid json_pack cases
|
||||
*/
|
||||
|
||||
/* true */
|
||||
rv = json_unpack(json_true(), NULL, "b", &i1);
|
||||
if(rv || !i1)
|
||||
fail("json_unpack boolean failed");
|
||||
|
||||
/* false */
|
||||
rv = json_unpack(json_false(), NULL, "b", &i1);
|
||||
if(rv || i1)
|
||||
fail("json_unpack boolean failed");
|
||||
|
||||
/* null */
|
||||
rv = json_unpack(json_null(), NULL, "n");
|
||||
if(rv)
|
||||
fail("json_unpack null failed");
|
||||
|
||||
/* integer */
|
||||
j = json_integer(1);
|
||||
rv = json_unpack(j, NULL, "i", &i1);
|
||||
if(rv || i1 != 1)
|
||||
fail("json_unpack integer failed");
|
||||
json_decref(j);
|
||||
|
||||
/* real */
|
||||
j = json_real(1.0);
|
||||
rv = json_unpack(j, NULL, "f", &f);
|
||||
if(rv || f != 1.0)
|
||||
fail("json_unpack real failed");
|
||||
json_decref(j);
|
||||
|
||||
/* string */
|
||||
j = json_string("foo");
|
||||
rv = json_unpack(j, NULL, "s", &s);
|
||||
if(rv || strcmp(s, "foo"))
|
||||
fail("json_unpack string failed");
|
||||
json_decref(j);
|
||||
|
||||
/* empty object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, NULL, "{}");
|
||||
if(rv)
|
||||
fail("json_unpack empty object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* empty list */
|
||||
j = json_array();
|
||||
rv = json_unpack(j, NULL, "[]");
|
||||
if(rv)
|
||||
fail("json_unpack empty list failed");
|
||||
json_decref(j);
|
||||
|
||||
/* non-incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, NULL, "o", &j2);
|
||||
if(j2 != j || j->refcount != (ssize_t)1)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, NULL, "O", &j2);
|
||||
if(j2 != j || j->refcount != (ssize_t)2)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
json_decref(j);
|
||||
|
||||
/* simple object */
|
||||
j = json_pack(NULL, "{s:i}", "foo", 1);
|
||||
rv = json_unpack(j, NULL, "{s:i}", "foo", &i1);
|
||||
if(rv || i1!=1)
|
||||
fail("json_unpack simple object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* simple array */
|
||||
j = json_pack(NULL, "[iii]", 1, 2, 3);
|
||||
rv = json_unpack(j, NULL, "[i,i,i]", &i1, &i2, &i3);
|
||||
if(rv || i1 != 1 || i2 != 2 || i3 != 3)
|
||||
fail("json_unpack simple array failed");
|
||||
json_decref(j);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim: ts=4:expandtab:sw=4
|
||||
*/
|
||||
Reference in New Issue
Block a user