-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.test.c
More file actions
187 lines (153 loc) · 5.94 KB
/
Copy pathjson.test.c
File metadata and controls
187 lines (153 loc) · 5.94 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <standardloop/testing.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./json.h"
static void testJSONSimple()
{
JSONValue *json_v = JSONValueInit(JSONSTRING_t, strdup("testing"));
TestCaseVerify(true, "Ensure JSONValueInit returns a non null value",
json_v != NULL);
TestCaseVerify(true, "Ensure JSONValue is has a json value type of string",
json_v->value_type == JSONSTRING_t);
TestCaseVerify(true, "Ensure JSONValue str is not NULL",
json_v->str != NULL);
TestCaseVerify(true, "Ensure JSONValue str is equal to the testing string",
strcmp(json_v->str, "testing") == 0);
Item *json_item = ItemInit(json_v, &ItemValueJSONValueOperations);
TestCaseVerify(true, "Ensure Item *json_item is not NULL",
json_item != NULL);
JSON *json = JSONInit();
TestCaseVerify(true, "Ensure JSONInit returns a non NULL pointer",
json != NULL);
json->root = json_item;
JSONFree(json);
// ---
JSON *json_2 = JSONInit();
TestCaseVerify(true, "Ensure JSONInit returns a non NULL pointer",
json_2 != NULL);
json_2->root = ItemInit(JSONValueInit(JSONOBJ_t, HashMapInitDefault()),
&ItemValueJSONValueOperations);
TestCaseVerify(true, "Ensure json_2->root is not NULL",
json_2->root != NULL);
JSONFree(json_2);
}
static void testJSONInit()
{
// printf("testJSONInit\n");
JSON *test = JSONInit();
TestCaseVerify(true, "Ensure JSONInit returns a non NULL pointer",
test != NULL);
TestCaseVerify(true,
"Ensure JSON * test->root is NULL because it shouldn't "
"have be initialized",
test->root == NULL);
JSONFree(test);
}
static void testStringToJSON()
{
// printf("testStringToJSON\n");
// test 1
char *test_string = "[]";
JSON *test_json = NULL;
test_json = StringToJSON("[]");
TestCaseVerify(true,
"ensure StringToJSON(\"[]\") returns a non NULL pointer",
test_json != NULL);
TestCaseVerify(
true,
"ensure the root value of the pointer has a value type of JSONLIST_t",
JSONGetRoot(test_json)->value_type == JSONLIST_t);
JSONFree(test_json);
// test 2
test_string = "{}";
test_json = NULL;
test_json = StringToJSON(test_string);
TestCaseVerify(true, "ensure StringToJSON(\"{}\") returns a non NULL ptr",
test_json != NULL);
TestCaseVerify(
true,
"ensure the root value of the pointer has a value type of JSONOBJ_t",
JSONGetRoot(test_json)->value_type == JSONOBJ_t);
JSONFree(test_json);
// -
test_string = "test";
test_json = NULL;
test_json = StringToJSON(test_string);
TestCaseVerify(true,
"ensure StringToJSON(\"test\") returns a non NULL ptr ",
test_json != NULL);
TestCaseVerify(
true,
"ensure the root value of the pointer has a value type of JSONSTRING_t",
test_json != NULL &&
JSONGetRoot(test_json)->value_type == JSONSTRING_t);
JSONFree(test_json);
// -
test_string = "{\"foo\": \"bar\", \"bar\": \"foo\"}";
test_json = NULL;
test_json = StringToJSON(test_string);
TestCaseVerify(true,
"ensure StringToJSON(\"{\"foo\": \"bar\", \"bar\": "
"\"foo\"}\") returns a non NULL ptr",
test_json != NULL);
TestCaseVerify(
true,
"ensure the root value of the pointer has a value type of JSONOBJ_t",
JSONGetRoot(test_json)->value_type == JSONOBJ_t);
JSONFree(test_json);
}
static void testJSONFromFile()
{
// printf("testJSONFromFile\n");
JSON *test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-array.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONLIST_t);
JSONFree(test);
test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-obj.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONOBJ_t);
JSONFree(test);
test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-bool.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONBOOL_t);
JSONFree(test);
test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-double.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "",
JSONGetRoot(test)->value_type == JSONNUMBER_DOUBLE_t);
JSONFree(test);
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-int.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONNUMBER_INT_t);
JSONFree(test);
test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-int.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONNUMBER_INT_t);
JSONFree(test);
test = NULL;
// ------------------------------------------------------------
test = JSONFromFile("./testfiles/simple/simple-int.json");
TestCaseVerify(true, "", test != NULL);
TestCaseVerify(true, "", JSONGetRoot(test)->value_type == JSONNUMBER_INT_t);
JSONFree(test);
test = NULL;
}
extern void TestJSON()
{
testJSONSimple();
testJSONInit();
testStringToJSON();
testJSONFromFile();
}