src/filemanager/Makefile.am: cleanup source file list.
[midnight-commander.git] / tests / lib / serialize.c
blob5bcf68ce7227ab206adc8e461b468e3aa166b72e
1 /*
2 lib/vfs - common serialize/deserialize functions
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib"
28 #include <config.h>
30 #include <check.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/serialize.h"
37 static void
38 setup (void)
40 str_init_strings (NULL);
43 static void
44 teardown (void)
46 str_uninit_strings ();
49 /* --------------------------------------------------------------------------------------------- */
50 #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
51 if (actual != NULL) \
52 { \
53 fail("actual value is '%s', but should be NULL", actual); \
54 g_free(actual); \
55 } \
56 else \
57 { \
58 fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
59 "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
60 error->code, etalon_code, error->message, etalon_str); \
61 g_clear_error(&error); \
62 } \
65 START_TEST (test_serialize_deserialize_str)
67 GError *error = NULL;
68 char *actual;
71 actual = mc_serialize_str('s', "some test string", &error);
73 if (actual == NULL)
75 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
76 g_clear_error(&error);
77 return;
79 fail_unless (strcmp(actual,"s16:some test string") == 0, "Actual value(%s) doesn't equal to etalon(s16:some test string)", actual);
80 g_free(actual);
82 actual = mc_deserialize_str('s', NULL, &error);
83 deserialize_check_incorrect( -1, "mc_serialize_str(): Input data is NULL or empty." );
85 actual = mc_deserialize_str('s', "incorrect string", &error);
86 deserialize_check_incorrect( -2, "mc_serialize_str(): String prefix doesn't equal to 's'" );
88 actual = mc_deserialize_str('s', "s12345string without delimiter", &error);
89 deserialize_check_incorrect( -3, "mc_serialize_str(): Length delimiter ':' doesn't exists" );
91 actual = mc_deserialize_str('s', "s1234567890123456789012345678901234567890123456789012345678901234567890:too big number", &error);
92 deserialize_check_incorrect( -3, "mc_serialize_str(): Too big string length" );
94 actual = mc_deserialize_str('s', "s500:actual string length less that specified length", &error);
95 deserialize_check_incorrect( -3, "mc_serialize_str(): Specified data length (500) is greater than actual data length (47)" );
97 actual = mc_deserialize_str('s', "s10:actual string length great that specified length", &error);
98 fail_unless (actual != NULL && strcmp(actual, "actual str") == 0, "actual (%s) doesn't equal to etalon(actual str)", actual);
99 g_free(actual);
101 actual = mc_deserialize_str('s', "s21:The right test string", &error);
102 fail_unless (actual != NULL && strcmp(actual, "The right test string") == 0, "actual (%s) doesn't equal to etalon(The right test string)", actual);
103 g_free(actual);
105 END_TEST
107 /* --------------------------------------------------------------------------------------------- */
109 #define etalon_str "g6:group1p6:param1v10:some valuep6:param2v11:some value " \
110 "g6:group2p6:param1v4:truep6:param2v6:123456" \
111 "g6:group3p6:param1v11:::bla-bla::p6:param2v31:bla-:p1:w:v2:12:g3:123:bla-bla\n" \
112 "g6:group4p6:param1v5:falsep6:param2v6:654321"
114 START_TEST (test_serialize_config)
116 mc_config_t *test_data;
117 GError *error = NULL;
118 char *actual;
120 test_data = mc_config_init (NULL, FALSE);
122 mc_config_set_string_raw (test_data, "group1", "param1", "some value");
123 mc_config_set_string (test_data, "group1", "param2", "some value ");
125 mc_config_set_bool (test_data, "group2", "param1", TRUE);
126 mc_config_set_int (test_data, "group2", "param2", 123456);
128 mc_config_set_string_raw (test_data, "group3", "param1", "::bla-bla::");
129 mc_config_set_string (test_data, "group3", "param2", "bla-:p1:w:v2:12:g3:123:bla-bla\n");
131 mc_config_set_bool (test_data, "group4", "param1", FALSE);
132 mc_config_set_int (test_data, "group4", "param2", 654321);
134 actual = mc_serialize_config (test_data, &error);
135 mc_config_deinit (test_data);
137 if (actual == NULL)
139 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
140 g_clear_error(&error);
141 return;
144 fail_unless(strcmp(actual, etalon_str) == 0, "Not equal:\nactual (%s)\netalon (%s)", actual, etalon_str);
145 g_free(actual);
147 END_TEST
149 /* --------------------------------------------------------------------------------------------- */
151 #undef deserialize_check_incorrect
152 #define deserialize_check_incorrect( etalon_code, etalon_str ) { \
153 if (actual != NULL) \
155 fail("actual value but should be NULL", actual); \
156 mc_config_deinit(actual); \
158 else \
160 fail_unless (error->code == etalon_code && strcmp(error->message, etalon_str) == 0, \
161 "\nerror code is %d (should be %d);\nerror message is '%s' (should be '%s')", \
162 error->code, etalon_code, error->message, etalon_str); \
163 g_clear_error(&error); \
167 START_TEST (test_deserialize_config)
169 mc_config_t *actual;
170 GError *error = NULL;
171 char *actual_value;
173 actual = mc_deserialize_config ("g123error in group name", &error);
174 deserialize_check_incorrect( -3,
175 "mc_deserialize_config() at 1: mc_serialize_str(): Length delimiter ':' doesn't exists");
177 actual = mc_deserialize_config ("p6:param1v10:some valuep6:param2v11:some value ", &error);
178 deserialize_check_incorrect( -2,
179 "mc_deserialize_config() at 1: mc_serialize_str(): String prefix doesn't equal to 'g'");
181 actual = mc_deserialize_config ("g6:group1v10:some valuep6:param2v11:some value ", &error);
182 deserialize_check_incorrect( -2,
183 "mc_deserialize_config() at 10: mc_serialize_str(): String prefix doesn't equal to 'p'");
185 actual = mc_deserialize_config ("g6:group1p6000:param2v11:some value ", &error);
186 deserialize_check_incorrect( -3,
187 "mc_deserialize_config() at 10: mc_serialize_str(): Specified data length (6000) is greater than actual data length (21)");
189 actual = mc_deserialize_config (etalon_str, &error);
191 if (actual == NULL)
193 fail("actual value is NULL!\nError code is '%d'; error message is '%s'", error->code, error->message);
194 g_clear_error(&error);
195 return;
198 actual_value = mc_config_get_string_raw(actual, "group1", "param1", "");
199 fail_unless( strcmp(actual_value, "some value") == 0,
200 "group1->param1(%s) should be equal to 'some value'", actual_value);
201 g_free(actual_value);
203 actual_value = mc_config_get_string(actual, "group1", "param2", "");
204 fail_unless( strcmp(actual_value, "some value ") == 0,
205 "group1->param2(%s) should be equal to 'some value '", actual_value);
206 g_free(actual_value);
208 fail_unless( mc_config_get_bool(actual, "group2", "param1", FALSE) == TRUE,
209 "group2->param1(FALSE) should be equal to TRUE");
211 fail_unless( mc_config_get_int(actual, "group2", "param2", 0) == 123456,
212 "group2->param2(%d) should be equal to 123456", mc_config_get_int(actual, "group2", "param2", 0));
214 actual_value = mc_config_get_string_raw(actual, "group3", "param1", "");
215 fail_unless( strcmp(actual_value, "::bla-bla::") == 0,
216 "group3->param1(%s) should be equal to '::bla-bla::'", actual_value);
217 g_free(actual_value);
219 actual_value = mc_config_get_string(actual, "group3", "param2", "");
220 fail_unless( strcmp(actual_value, "bla-:p1:w:v2:12:g3:123:bla-bla\n") == 0,
221 "group3->param2(%s) should be equal to 'bla-:p1:w:v2:12:g3:123:bla-bla\n'", actual_value);
222 g_free(actual_value);
224 fail_unless( mc_config_get_bool(actual, "group4", "param1", TRUE) == FALSE,
225 "group4->param1(TRUE) should be equal to FALSE");
227 fail_unless( mc_config_get_int(actual, "group4", "param2", 0) == 654321,
228 "group4->param2(%d) should be equal to 654321", mc_config_get_int(actual, "group4", "param2", 0));
230 mc_config_deinit (actual);
232 END_TEST
234 /* --------------------------------------------------------------------------------------------- */
237 main (void)
239 int number_failed;
241 Suite *s = suite_create (TEST_SUITE_NAME);
242 TCase *tc_core = tcase_create ("Core");
243 SRunner *sr;
245 tcase_add_checked_fixture (tc_core, setup, teardown);
247 /* Add new tests here: *************** */
248 tcase_add_test (tc_core, test_serialize_deserialize_str);
249 tcase_add_test (tc_core, test_serialize_config);
250 tcase_add_test (tc_core, test_deserialize_config);
251 /* *********************************** */
253 suite_add_tcase (s, tc_core);
254 sr = srunner_create (s);
255 srunner_set_log (sr, "serialize.log");
256 srunner_run_all (sr, CK_NORMAL);
257 number_failed = srunner_ntests_failed (sr);
258 srunner_free (sr);
259 return (number_failed == 0) ? 0 : 1;
262 /* --------------------------------------------------------------------------------------------- */