daacbeabe256bc9f86cd1776c16f2f4ef75fccfa
[jansson.git] / test.cc
1 #include <iostream>
2 #include <iomanip>
3 #include <malloc.h>
4
5 #include "janssonxx.h"
6
7 using namespace std;
8
9 #define ASSERT_OP(lhs, rhs, op, m) \
10         do { \
11                 if(!((lhs) op (rhs))) { \
12                         std::cerr << std::boolalpha; \
13                         std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: " << (m) << std::endl; \
14                         std::cerr << "\ttest:   " << #lhs << ' ' << #op << ' ' << #rhs << std::endl; \
15                         std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' ' << (rhs) << std::endl; \
16                         return 1; \
17                 } \
18         } while(0)
19 #define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m)
20 #define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m)
21 #define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
22 #define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
23
24 int main() {
25         jansson::Value e1(jansson::Value::load_file("test.json"));
26         jansson::Value e2(e1);
27         jansson::Value e3;
28         jansson::Value e4(jansson::Value::load_string("{\"foo\": true, \"bar\": \"test\"}"));
29
30         ASSERT_TRUE(e1.is_object(), "e1 is not an object");
31         ASSERT_TRUE(e2.is_object(), "e2 is not an object");
32         ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value");
33         ASSERT_TRUE(e4.is_object(), "e4 is not an object");
34
35         ASSERT_EQ(e1.size(), 1, "e1 has too many properties");
36         ASSERT_EQ(e2.size(), 1, "e2 has too many properties");
37         ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements");
38
39         ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object");
40         ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
41         ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
42
43         ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value");
44
45         jansson::Iterator i(e1.get("web-app"));
46         ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key");
47         i.next();
48         ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key");
49         i.next();
50         ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key");
51         i.next();
52         ASSERT_FALSE(i.valid(), "iterator has more values than expected");
53
54         e3 = jansson::Value::from(12.34);
55         ASSERT_TRUE(e3.is_number(), "e3 is not a number after assignment");
56         ASSERT_EQ(e3.as_real(), 12.34, "e3 has incorrect value after assignment");
57
58         e3 = jansson::Value::from(true);
59         ASSERT_TRUE(e3.is_boolean(), "e3 is not a boolean after assignment");
60         ASSERT_EQ(e3.as_boolean(), true, "e3 has incorrect value after assignment");
61
62         e3 = jansson::Value::from("foobar");
63         ASSERT_TRUE(e3.is_string(), "e3 is not a string after assignment");
64         ASSERT_EQ(e3.as_string(), "foobar", "e3 has incorrect value after assignment");
65
66         e3 = jansson::Value::object();
67         ASSERT_TRUE(e3.is_object(), "e3 is not an object after assignment");
68
69         e3 = jansson::Value::null();
70         ASSERT_TRUE(e3.is_null(), "e3 is not null after assignment");
71
72         e3.set(0, jansson::Value::from("foobar"));
73         ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
74         ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of elements after assignment");
75         ASSERT_EQ(e3[0].as_string(), "foobar", "e3[0] has incorrect value after assignment");
76
77         e3.set(1, jansson::Value::from("foobar"));
78         ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
79         ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");
80         ASSERT_EQ(e3[1].as_string(), "foobar", "e3[0] has incorrect value after assignment");
81
82         e3.set(0, jansson::Value::from("barfoo"));
83         ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
84         ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");
85         ASSERT_EQ(e3[0].as_string(), "barfoo", "e3[0] has incorrect value after assignment");
86
87         e3.set(100, jansson::Value::null());
88         ASSERT_TRUE(e3.is_array(), "e3 is not an array after index assignment");
89         ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of elements after assignment");
90
91         e3.clear();
92         ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of elements after clear");
93
94         e3.set("foo", jansson::Value::from("test"));
95         ASSERT_TRUE(e3.is_object(), "e3 is not an object after property assignment");
96         ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of properties after assignment");
97         ASSERT_EQ(e3["foo"].as_string(), "test", "e3.foo has incorrect value after assignment");
98
99         e3.set("foo", jansson::Value::from("again"));
100         ASSERT_TRUE(e3.is_object(), "e3 is not an object after property assignment");
101         ASSERT_EQ(e3.size(), 1, "e3 has incorrect number of properties after assignment");
102         ASSERT_EQ(e3["foo"].as_string(), "again", "e3.foo has incorrect value after assignment");
103
104         e3.set("bar", jansson::Value::from("test"));
105         ASSERT_TRUE(e3.is_object(), "e3 is not an object after property assignment");
106         ASSERT_EQ(e3.size(), 2, "e3 has incorrect number of properties after assignment");
107         ASSERT_EQ(e3["bar"].as_string(), "test", "e3.foo has incorrect value after assignment");
108
109         e3.clear();
110         ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of properties after clear");
111
112         e3 = jansson::Value::object();
113         e3.set("foo", jansson::Value::from("test"));
114         e3.set("bar", jansson::Value::from(3));
115         char* out_cstr = e3.save_string(0);
116         string out(out_cstr);
117         free(out_cstr);
118         ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected");
119
120         return 0;
121 }