forked from toffaletti/libten
/
test_json.cc
252 lines (218 loc) · 8.63 KB
/
test_json.cc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#define BOOST_TEST_MODULE json test
#include <boost/test/unit_test.hpp>
#include <ten/json.hh>
#include <ten/jserial.hh>
#include <ten/jserial_maybe.hh>
#include <ten/jserial_seq.hh>
#include <ten/jserial_assoc.hh>
using namespace std;
using namespace ten;
const char json_text[] =
"{ \"store\": {"
" \"book\": ["
" { \"category\": \"reference\","
" \"author\": \"Nigel Rees\","
" \"title\": \"Sayings of the Century\","
" \"price\": 8.95"
" },"
" { \"category\": \"fiction\","
" \"author\": \"Evelyn Waugh\","
" \"title\": \"Sword of Honour\","
" \"price\": 12.99"
" },"
" { \"category\": \"fiction\","
" \"author\": \"Herman Melville\","
" \"title\": \"Moby Dick\","
" \"isbn\": \"0-553-21311-3\","
" \"price\": 8.99"
" },"
" { \"category\": \"fiction\","
" \"author\": \"J. R. R. Tolkien\","
" \"title\": \"The Lord of the Rings\","
" \"isbn\": \"0-395-19395-8\","
" \"price\": 22.99"
" }"
" ],"
" \"bicycle\": {"
" \"color\": \"red\","
" \"price\": 19.95"
" }"
" }"
"}";
BOOST_AUTO_TEST_CASE(json_test_path1) {
json o(json::load(json_text));
BOOST_REQUIRE(o.get());
static const char a1[] = "[\"Nigel Rees\", \"Evelyn Waugh\", \"Herman Melville\", \"J. R. R. Tolkien\"]";
json r1(o.path("/store/book/author"));
BOOST_CHECK_EQUAL(json::load(a1), r1);
json r2(o.path("//author"));
BOOST_CHECK_EQUAL(json::load(a1), r2);
// jansson hashtable uses size_t for hash
// we think this is causing the buckets to change on 32bit vs. 64bit
#if (__SIZEOF_SIZE_T__ == 4)
static const char a3[] = "[{\"category\": \"reference\", \"author\": \"Nigel Rees\", \"title\": \"Sayings of the Century\", \"price\": 8.95}, {\"category\": \"fiction\", \"author\": \"Evelyn Waugh\", \"title\": \"Sword of Honour\", \"price\": 12.99}, {\"category\": \"fiction\", \"author\": \"Herman Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 8.99}, {\"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99}, {\"color\": \"red\", \"price\": 19.95}]";
#elif (__SIZEOF_SIZE_T__ == 8)
static const char a3[] = "[{\"color\": \"red\", \"price\": 19.95}, {\"category\": \"reference\", \"author\": \"Nigel Rees\", \"title\": \"Sayings of the Century\", \"price\": 8.95}, {\"category\": \"fiction\", \"author\": \"Evelyn Waugh\", \"title\": \"Sword of Honour\", \"price\": 12.99}, {\"category\": \"fiction\", \"author\": \"Herman Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 8.99}, {\"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99}]";
#endif
json r3(o.path("/store/*"));
json t3(json::load(a3));
BOOST_CHECK_EQUAL(t3, r3);
#if (__SIZEOF_SIZE_T__ == 4)
static const char a4[] = "[8.95, 12.99, 8.99, 22.99, 19.95]";
#elif (__SIZEOF_SIZE_T__ == 8)
static const char a4[] = "[19.95, 8.95, 12.99, 8.99, 22.99]";
#endif
json r4(o.path("/store//price"));
BOOST_CHECK_EQUAL(json::load(a4), r4);
static const char a5[] = "{\"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99}";
json r5(o.path("//book[3]"));
BOOST_CHECK_EQUAL(json::load(a5), r5);
static const char a6[] = "\"J. R. R. Tolkien\"";
json r6(o.path("/store/book[3]/author"));
BOOST_CHECK_EQUAL(json::load(a6), r6);
BOOST_CHECK(json::load(a6) == r6);
static const char a7[] = "[{\"category\": \"fiction\", \"author\": \"Evelyn Waugh\", \"title\": \"Sword of Honour\", \"price\": 12.99}, {\"category\": \"fiction\", \"author\": \"Herman Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 8.99}, {\"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99}]";
json r7(o.path("/store/book[category=\"fiction\"]"));
BOOST_CHECK_EQUAL(json::load(a7), r7);
}
BOOST_AUTO_TEST_CASE(json_test_path2) {
json o(json::load("[{\"type\": 0}, {\"type\": 1}]"));
BOOST_CHECK_EQUAL(json::load("[{\"type\":1}]"), o.path("/[type=1]"));
}
BOOST_AUTO_TEST_CASE(json_test_filter_key_exists) {
json o(json::load(json_text));
BOOST_REQUIRE(o.get());
static const char a[] = "["
" { \"category\": \"fiction\","
" \"author\": \"Herman Melville\","
" \"title\": \"Moby Dick\","
" \"isbn\": \"0-553-21311-3\","
" \"price\": 8.99"
" },"
" { \"category\": \"fiction\","
" \"author\": \"J. R. R. Tolkien\","
" \"title\": \"The Lord of the Rings\","
" \"isbn\": \"0-395-19395-8\","
" \"price\": 22.99"
" }]";
json r(o.path("//book[isbn]"));
BOOST_CHECK_EQUAL(json::load(a), r);
json r1(o.path("//book[doesnotexist]"));
BOOST_REQUIRE(r1.is_array());
BOOST_CHECK_EQUAL(0, r1.asize());
}
BOOST_AUTO_TEST_CASE(json_test_truth) {
json o(json::object());
BOOST_CHECK(o.get("nothing").is_true() == false);
BOOST_CHECK(o.get("nothing").is_false() == false);
BOOST_CHECK(o.get("nothing").is_null() == false);
BOOST_CHECK(!o.get("nothing"));
}
BOOST_AUTO_TEST_CASE(json_test_path3) {
json o(json::load(json_text));
BOOST_REQUIRE(o.get());
BOOST_CHECK_EQUAL(o, o.path("/"));
BOOST_CHECK_EQUAL("Sayings of the Century",
o.path("/store/book[category=\"reference\"]/title"));
static const char text[] = "["
"{\"type\":\"a\", \"value\":0},"
"{\"type\":\"b\", \"value\":1},"
"{\"type\":\"c\", \"value\":2},"
"{\"type\":\"c\", \"value\":3}"
"]";
BOOST_CHECK_EQUAL(json(1), json::load(text).path("/[type=\"b\"]/value"));
}
BOOST_AUTO_TEST_CASE(json_init_list) {
json meta{
{ "foo", 17 },
{ "bar", 23 },
{ "baz", true },
{ "corge", json::array({ 1, 3.14159 }) },
{ "grault", json::array({ "hello", string("world") }) },
};
BOOST_REQUIRE(meta);
BOOST_REQUIRE(meta.is_object());
BOOST_CHECK_EQUAL(meta.osize(), 5);
BOOST_CHECK_EQUAL(meta["foo"].integer(), 17);
BOOST_CHECK_EQUAL(meta["corge"][0].integer(), 1);
BOOST_CHECK_EQUAL(meta["grault"][1].str(), "world");
}
template <class T>
inline void test_conv(T val, json j, json_type t) {
json j2 = to_json(val);
BOOST_CHECK_EQUAL(j2.type(), t);
BOOST_CHECK_EQUAL(j, j2);
T val2 = json_cast<T>(j2);
BOOST_CHECK_EQUAL(val, val2);
}
template <class T, json_type TYPE = JSON_INTEGER>
inline void test_conv_num() {
typedef numeric_limits<T> lim;
T range[5] = { lim::min(), T(-1), 0, T(1), lim::max() };
for (unsigned i = 0; i < 5; ++i)
test_conv<T>(range[i], json(range[i]), TYPE);
}
BOOST_AUTO_TEST_CASE(json_conversions) {
test_conv<string>(string("hello"), json::str("hello"), JSON_STRING);
BOOST_CHECK_EQUAL(to_json("world"), json::str("world"));
test_conv_num<short>();
test_conv_num<int>();
test_conv_num<long>();
test_conv_num<long long>();
test_conv_num<unsigned short>();
test_conv_num<unsigned>();
#if ULONG_MAX < LLONG_MAX
test_conv_num<unsigned long>();
#endif
test_conv_num<double, JSON_REAL>();
test_conv_num<float, JSON_REAL>();
test_conv<bool>(true, json::jtrue(), JSON_TRUE);
test_conv<bool>(false, json::jfalse(), JSON_FALSE);
}
struct corge {
int foo, bar;
corge() : foo(), bar() {}
corge(int foo_, int bar_) : foo(foo_), bar(bar_) {}
};
template <class AR>
inline AR & operator & (AR &ar, corge &c) {
return ar & kv("foo", c.foo) & kv("bar", c.bar);
}
inline bool operator == (const corge &a, const corge &b) {
return a.foo == b.foo && a.bar == b.bar;
}
BOOST_AUTO_TEST_CASE(json_serial) {
corge c1(42, 17);
auto j = jsave_all(c1);
corge c2;
JLoad(j) >> c2;
BOOST_CHECK(c1 == c2);
map<string, int> m;
JLoad(j) >> m;
BOOST_CHECK_EQUAL(m.size(), 2);
BOOST_CHECK(m.find("foo") != m.end());
BOOST_CHECK(m.find("bar") != m.end());
BOOST_CHECK_EQUAL(m["foo"], 42);
BOOST_CHECK_EQUAL(m["bar"], 17);
#if BOOST_VERSION >= 104800
flat_map<string, int> f;
JLoad(j) >> f;
BOOST_CHECK_EQUAL(f.size(), 2);
BOOST_CHECK(f.find("foo") != f.end());
BOOST_CHECK(f.find("bar") != f.end());
BOOST_CHECK_EQUAL(f["foo"], 42);
BOOST_CHECK_EQUAL(f["bar"], 17);
#endif
maybe<int> a;
j = jsave_all(a);
BOOST_CHECK(!j);
a = 42;
j = jsave_all(a);
BOOST_CHECK_EQUAL(j, 42);
a.reset();
BOOST_CHECK(!a.ok());
j = 17;
JLoad(j) >> a;
BOOST_CHECK(a.ok());
BOOST_CHECK_EQUAL(a.get(), 17);
}