Initial Import
[profile/ivi/json-glib.git] / json-glib / tests / node.c
1 #include <glib.h>
2 #include <json-glib/json-glib.h>
3 #include <string.h>
4
5 static void
6 test_copy_null (void)
7 {
8   JsonNode *node = json_node_new (JSON_NODE_NULL);
9   JsonNode *copy = json_node_copy (node);
10
11   g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
12   g_assert_cmpint (json_node_get_value_type (node), ==, json_node_get_value_type (copy));
13   g_assert_cmpstr (json_node_type_name (node), ==, json_node_type_name (copy));
14
15   json_node_free (copy);
16   json_node_free (node);
17 }
18
19 static void
20 test_copy_value (void)
21 {
22   JsonNode *node = json_node_new (JSON_NODE_VALUE);
23   JsonNode *copy;
24
25   json_node_set_string (node, "hello");
26
27   copy = json_node_copy (node);
28   g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
29   g_assert_cmpstr (json_node_type_name (node), ==, json_node_type_name (copy));
30   g_assert_cmpstr (json_node_get_string (node), ==, json_node_get_string (copy));
31
32   json_node_free (copy);
33   json_node_free (node);
34 }
35
36 static void
37 test_copy_object (void)
38 {
39   JsonObject *obj = json_object_new ();
40   JsonNode *node = json_node_new (JSON_NODE_OBJECT);
41   JsonNode *value = json_node_new (JSON_NODE_VALUE);
42   JsonNode *copy;
43
44   json_node_set_int (value, 42);
45   json_object_set_member (obj, "answer", value);
46
47   json_node_take_object (node, obj);
48
49   copy = json_node_copy (node);
50
51   g_assert_cmpint (json_node_get_node_type (node), ==, json_node_get_node_type (copy));
52   g_assert (json_node_get_object (node) == json_node_get_object (copy));
53
54   json_node_free (copy);
55   json_node_free (node);
56 }
57
58 static void
59 test_null (void)
60 {
61   JsonNode *node = json_node_new (JSON_NODE_NULL);
62
63   g_assert (JSON_NODE_HOLDS_NULL (node));
64   g_assert_cmpint (json_node_get_value_type (node), ==, G_TYPE_INVALID);
65   g_assert_cmpstr (json_node_type_name (node), ==, "NULL");
66
67   json_node_free (node);
68 }
69
70 static void
71 test_value (void)
72 {
73   JsonNode *node = json_node_new (JSON_NODE_VALUE);
74   GValue value = { 0, };
75   GValue check = { 0, };
76
77   g_assert_cmpint (JSON_NODE_TYPE (node), ==, JSON_NODE_VALUE);
78
79   g_value_init (&value, G_TYPE_INT64);
80   g_value_set_int64 (&value, 42);
81
82   g_assert_cmpint (G_VALUE_TYPE (&value), ==, G_TYPE_INT64);
83   g_assert_cmpint (g_value_get_int64 (&value), ==, 42);
84
85   json_node_set_value (node, &value);
86   json_node_get_value (node, &check);
87
88   g_assert_cmpint (G_VALUE_TYPE (&value), ==, G_VALUE_TYPE (&check));
89   g_assert_cmpint (g_value_get_int64 (&value), ==, g_value_get_int64 (&check));
90   g_assert_cmpint (G_VALUE_TYPE (&check), ==, G_TYPE_INT64);
91   g_assert_cmpint (g_value_get_int64 (&check), ==, 42);
92
93   g_value_unset (&value);
94   g_value_unset (&check);
95   json_node_free (node);
96 }
97
98 int
99 main (int   argc,
100       char *argv[])
101 {
102   g_type_init ();
103   g_test_init (&argc, &argv, NULL);
104
105   g_test_add_func ("/nodes/null-node", test_null);
106   g_test_add_func ("/nodes/copy-null", test_copy_null);
107   g_test_add_func ("/nodes/copy-value", test_copy_value);
108   g_test_add_func ("/nodes/copy-object", test_copy_object);
109   g_test_add_func ("/nodes/value", test_value);
110
111   return g_test_run ();
112 }