Added ability to serialize/deserialize strings and config objects(mc_config_t)
[midnight-commander.git] / lib / tests / serialize.c
blobc826a3593541606096c5109f7c080386923219aa
1 /* lib/vfs - common serialize/deserialize functions
3 Copyright (C) 2011 Free Software Foundation, Inc.
5 Written by:
6 Slava Zanko <slavazanko@gmail.com>, 2011
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define TEST_SUITE_NAME "/lib"
25 #include <check.h>
28 #include "lib/global.h"
29 #include "lib/strutil.h"
30 #include "lib/serialize.h"
33 static void
34 setup (void)
36 str_init_strings (NULL);
39 static void
40 teardown (void)
42 str_uninit_strings ();
45 /* --------------------------------------------------------------------------------------------- */
46 #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
47 if (actual != NULL) \
48 { \
49 fail("actual value is '%s', but should be NULL", actual); \
50 g_free(actual); \
51 } \
52 else \
53 { \
54 fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
55 "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
56 error->code, etalon_code, error->message, etalon_str); \
57 g_clear_error(&error); \
58 } \
61 START_TEST (test_serialize_deserialize_str)
63 GError *error = NULL;
64 char *actual;
67 actual = mc_serialize_str('s', "some test string", &error);
69 if (actual == NULL)
71 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
72 g_clear_error(&error);
73 return;
75 fail_unless (strcmp(actual,"s16:some test string") == 0, "Actual value(%s) doesn't equal to etalon(s16:some test string)", actual);
76 g_free(actual);
78 actual = mc_deserialize_str('s', NULL, &error);
79 deserialize_check_incorrect( -1, "mc_serialize_str(): Input data is NULL or empty." );
81 actual = mc_deserialize_str('s', "incorrect string", &error);
82 deserialize_check_incorrect( -2, "mc_serialize_str(): String prefix doesn't equal to 's'" );
84 actual = mc_deserialize_str('s', "s12345string without delimiter", &error);
85 deserialize_check_incorrect( -3, "mc_serialize_str(): Length delimiter ':' doesn't exists" );
87 actual = mc_deserialize_str('s', "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number", &error);
88 deserialize_check_incorrect( -3, "mc_serialize_str(): Too big string length" );
90 actual = mc_deserialize_str('s', "s500:actual string length less that specified length", &error);
91 deserialize_check_incorrect( -3, "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)" );
93 actual = mc_deserialize_str('s', "s10:actual string length great that specified length", &error);
94 fail_unless (actual != NULL && strcmp(actual, "actual str") == 0, "actual (%s) doesn't equal to etalon(actual str)", actual);
95 g_free(actual);
97 actual = mc_deserialize_str('s', "s21:The right test string", &error);
98 fail_unless (actual != NULL && strcmp(actual, "The right test string") == 0, "actual (%s) doesn't equal to etalon(The right test string)", actual);
99 g_free(actual);
101 END_TEST
103 /* --------------------------------------------------------------------------------------------- */
105 #define etalon_str "g6:group1p6:param1v10:some valuep6:param2v11:some value " \
106 "g6:group2p6:param1v4:truep6:param2v6:123456" \
107 "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n" \
108 "g6:group4p6:param1v5:falsep6:param2v6:654321"
110 START_TEST (test_serialize_config)
112 mc_config_t *test_data;
113 GError *error = NULL;
114 char *actual;
116 test_data = mc_config_init (NULL);
118 mc_config_set_string_raw (test_data, "group1", "param1", "some value");
119 mc_config_set_string (test_data, "group1", "param2", "some value ");
121 mc_config_set_bool (test_data, "group2", "param1", TRUE);
122 mc_config_set_int (test_data, "group2", "param2", 123456);
124 mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
125 mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
127 mc_config_set_bool (test_data, "group4", "param1", FALSE);
128 mc_config_set_int (test_data, "group4", "param2", 654321);
130 actual = mc_serialize_config (test_data, &error);
131 mc_config_deinit (test_data);
133 if (actual == NULL)
135 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
136 g_clear_error(&error);
137 return;
140 fail_unless(strcmp(actual, etalon_str) == 0, "Not equal:\nactual (%s)\netalon (%s)", actual, etalon_str);
141 g_free(actual);
143 END_TEST
145 /* --------------------------------------------------------------------------------------------- */
147 #undef deserialize_check_incorrect
148 #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
149 if (actual != NULL) \
151 fail("actual value but should be NULL", actual); \
152 mc_config_deinit(actual); \
154 else \
156 fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
157 "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
158 error->code, etalon_code, error->message, etalon_str); \
159 g_clear_error(&error); \
163 START_TEST (test_deserialize_config)
165 mc_config_t *actual;
166 GError *error = NULL;
167 char *actual_value;
169 actual = mc_deserialize_config ("g123error in group name", &error);
170 deserialize_check_incorrect( -3,
171 "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists");
173 actual = mc_deserialize_config ("p6:param1v10:some valuep6:param2v11:some value ", &error);
174 deserialize_check_incorrect( -2,
175 "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'");
177 actual = mc_deserialize_config ("g6:group1v10:some valuep6:param2v11:some value ", &error);
178 deserialize_check_incorrect( -2,
179 "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'");
181 actual = mc_deserialize_config ("g6:group1p6000:param2v11:some value ", &error);
182 deserialize_check_incorrect( -3,
183 "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)");
185 actual = mc_deserialize_config (etalon_str, &error);
187 if (actual == NULL)
189 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
190 g_clear_error(&error);
191 return;
194 actual_value = mc_config_get_string_raw(actual, "group1", "param1", "");
195 fail_unless( strcmp(actual_value, "some value") == 0,
196 "group1->param1(%s) should be equal to 'some value'", actual_value);
197 g_free(actual_value);
199 actual_value = mc_config_get_string(actual, "group1", "param2", "");
200 fail_unless( strcmp(actual_value, "some value ") == 0,
201 "group1->param2(%s) should be equal to 'some value '", actual_value);
202 g_free(actual_value);
204 fail_unless( mc_config_get_bool(actual, "group2", "param1", FALSE) == TRUE,
205 "group2->param1(FALSE) should be equal to TRUE");
207 fail_unless( mc_config_get_int(actual, "group2", "param2", 0) == 123456,
208 "group2->param2(%d) should be equal to 123456", mc_config_get_int(actual, "group2", "param2", 0));
210 actual_value = mc_config_get_string_raw(actual, "group3", "param1", "");
211 fail_unless( strcmp(actual_value, "::bla-bla::") == 0,
212 "group3->param1(%s) should be equal to '::bla-bla::'", actual_value);
213 g_free(actual_value);
215 actual_value = mc_config_get_string(actual, "group3", "param2", "");
216 fail_unless( strcmp(actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n") == 0,
217 "group3->param2(%s) should be equal to 'bla-:p1:w:v2:12:g3:123:bla-bla\n'", actual_value);
218 g_free(actual_value);
220 fail_unless( mc_config_get_bool(actual, "group4", "param1", TRUE) == FALSE,
221 "group4->param1(TRUE) should be equal to FALSE");
223 fail_unless( mc_config_get_int(actual, "group4", "param2", 0) == 654321,
224 "group4->param2(%d) should be equal to 654321", mc_config_get_int(actual, "group4", "param2", 0));
226 mc_config_deinit (actual);
228 END_TEST
230 /* --------------------------------------------------------------------------------------------- */
233 main (void)
235 int number_failed;
237 Suite *s = suite_create (TEST_SUITE_NAME);
238 TCase *tc_core = tcase_create ("Core");
239 SRunner *sr;
241 tcase_add_checked_fixture (tc_core, setup, teardown);
243 /* Add new tests here: *************** */
244 tcase_add_test (tc_core, test_serialize_deserialize_str);
245 tcase_add_test (tc_core, test_serialize_config);
246 tcase_add_test (tc_core, test_deserialize_config);
247 /* *********************************** */
249 suite_add_tcase (s, tc_core);
250 sr = srunner_create (s);
251 srunner_set_log (sr, "serialize.log");
252 srunner_run_all (sr, CK_NORMAL);
253 number_failed = srunner_ntests_failed (sr);
254 srunner_free (sr);
255 return (number_failed == 0) ? 0 : 1;
258 /* --------------------------------------------------------------------------------------------- */