forked from openwebos/cjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest5.c
96 lines (76 loc) · 2.71 KB
/
test5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*/
#include <string.h>
#include <assert.h>
#include "json.h"
#include <stdio.h>
int main()
{
const char *obj1 = "{ }";
const char *obj2 = "{ 'a' : 'b' }";
const char *obj3 = "{ 'c' : 'd', 'e' : 5 }";
const char *obj4 = "{ 'f' : 60.7, 'g' : null }";
struct json_object *root, *value;
struct json_object_iterator i, e;
/* validate obj1 */
root = json_tokener_parse(obj1);
e = json_object_iter_end(root);
i = json_object_iter_begin(root);
assert (json_object_iter_equal(&i, &e));
json_object_put(root);
/* validate obj2 */
root = json_tokener_parse(obj2);
e = json_object_iter_end(root);
i = json_object_iter_begin(root);
assert (!json_object_iter_equal(&i, &e));
value = json_object_iter_peek_value(&i);
assert (0 == memcmp("a", json_object_iter_peek_name(&i), 2));
assert (json_object_is_type(value, json_type_string));
assert (0 == memcmp("b", json_object_get_string(value), 2));
json_object_iter_next(&i);
assert (json_object_iter_equal(&i, &e));
json_object_put(root);
/* validate obj3 */
root = json_tokener_parse(obj3);
e = json_object_iter_end(root);
i = json_object_iter_begin(root);
assert (!json_object_iter_equal(&i, &e));
value = json_object_iter_peek_value(&i);
assert (0 == memcmp("c", json_object_iter_peek_name(&i), 2));
assert (json_object_is_type(value, json_type_string));
assert (0 == memcmp("d", json_object_get_string(value), 2));
json_object_iter_next(&i);
assert (!json_object_iter_equal(&i, &e));
value = json_object_iter_peek_value(&i);
assert (0 == memcmp("e", json_object_iter_peek_name(&i), 2));
assert (json_object_is_type(value, json_type_int));
assert (5 == json_object_get_int(value));
assert (0 == memcmp("5", json_object_get_string(value), 2));
json_object_iter_next(&i);
assert (json_object_iter_equal(&i, &e));
json_object_put(root);
/* validate obj4 */
root = json_tokener_parse(obj4);
e = json_object_iter_end(root);
i = json_object_iter_begin(root);
assert (!json_object_iter_equal(&i, &e));
value = json_object_iter_peek_value(&i);
assert (0 == memcmp("f", json_object_iter_peek_name(&i), 2));
assert (json_object_is_type(value, json_type_double));
assert (60.7 == json_object_get_double(value));
json_object_iter_next(&i);
assert (!json_object_iter_equal(&i, &e));
value = json_object_iter_peek_value(&i);
assert (0 == memcmp("g", json_object_iter_peek_name(&i), 2));
assert (value == NULL);
json_object_iter_next(&i);
assert (json_object_iter_equal(&i, &e));
json_object_put(root);
printf("No assertions -- test passed.\n");
return 0;
}