tizen 2.3.1 release
[framework/telephony/libtcore.git] / unit-test / test-util.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #include <glib.h>
7
8 #include <tcore.h>
9 #include <util.h>
10
11 static void test_marshal(void)
12 {
13         GHashTable *list;
14         int value_int;
15
16         list = tcore_util_marshal_create ();
17         g_assert(list);
18
19         value_int = 1;
20         tcore_util_marshal_add_data (list, "key1", &value_int, TCORE_UTIL_MARSHAL_DATA_INT_TYPE);
21         g_assert(tcore_util_marshal_get_int (list, "key1") == value_int);
22
23         value_int = 2;
24         tcore_util_marshal_add_data (list, "key2", &value_int, TCORE_UTIL_MARSHAL_DATA_INT_TYPE);
25         g_assert(tcore_util_marshal_get_int (list, "key2") == value_int);
26
27         tcore_util_marshal_destory (list);
28 }
29
30 static void test_marshal_serialize(void)
31 {
32         GHashTable *list;
33         GHashTable *item;
34         GHashTable *tmp;
35         gchar *serialized;
36         int i;
37         char buf[255];
38         int value_int;
39
40         list = tcore_util_marshal_create ();
41         g_assert(list);
42
43         value_int = 1;
44         tcore_util_marshal_add_data (list, "key1", &value_int, TCORE_UTIL_MARSHAL_DATA_INT_TYPE);
45         g_assert(tcore_util_marshal_get_int (list, "key1") == value_int);
46
47         value_int = 2;
48         tcore_util_marshal_add_data (list, "key2", &value_int, TCORE_UTIL_MARSHAL_DATA_INT_TYPE);
49         g_assert(tcore_util_marshal_get_int (list, "key2") == value_int);
50
51         item = tcore_util_marshal_create ();
52         g_assert (item);
53         tcore_util_marshal_add_data (list, "key_object", item, TCORE_UTIL_MARSHAL_DATA_OBJECT_TYPE);
54
55         for (i = 0; i < 3; i++) {
56                 item = tcore_util_marshal_create ();
57                 g_assert (item);
58
59                 value_int = i * 10;
60                 snprintf (buf, 255, "sub-%d", i);
61                 tcore_util_marshal_add_data (item, buf, &value_int, TCORE_UTIL_MARSHAL_DATA_INT_TYPE);
62                 g_assert(tcore_util_marshal_get_int (item, buf) == value_int);
63
64                 tcore_util_marshal_add_data (list, buf, item, TCORE_UTIL_MARSHAL_DATA_OBJECT_TYPE);
65         }
66
67         serialized = tcore_util_marshal_serialize (list);
68         g_assert (serialized);
69         tcore_util_marshal_destory (list);
70
71         tmp = tcore_util_marshal_deserialize_string (serialized);
72         g_assert (tmp);
73
74         free (serialized);
75
76         g_assert(tcore_util_marshal_get_int (tmp, "key1") == 1);
77         g_assert(tcore_util_marshal_get_int (tmp, "key2") == 2);
78
79         tcore_util_marshal_destory (tmp);
80 }
81
82 int main(int argc, char **argv)
83 {
84         g_test_init(&argc, &argv, NULL);
85
86         g_type_init();
87
88         g_test_add_func("/util/marshal", test_marshal);
89         g_test_add_func("/util/marshal_serialize", test_marshal_serialize);
90
91         return g_test_run();
92 }