|
Atrinik Server 2.5
|
00001 /************************************************************************ 00002 * Atrinik, a Multiplayer Online Role Playing Game * 00003 * * 00004 * Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team * 00005 * * 00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game) * 00007 * and Crossfire (Multiplayer game for X-windows). * 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 * This program is distributed in the hope that it will be useful, * 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00017 * GNU General Public License for more details. * 00018 * * 00019 * You should have received a copy of the GNU General Public License * 00020 * along with this program; if not, write to the Free Software * 00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * 00022 * * 00023 * The author can be reached at admin@atrinik.org * 00024 ************************************************************************/ 00025 00026 #include <global.h> 00027 #include <check.h> 00028 #include <stdarg.h> 00029 00030 static void check_cleanup_string(const char *str, const char *expected) 00031 { 00032 char *cp; 00033 00034 cp = cleanup_string(strdup_local(str)); 00035 fail_if(strcmp(cp, expected), "cleanup_string() cleaned up string '%s' to '%s' but it was not the expected string '%s'.", str, cp, expected); 00036 } 00037 00038 START_TEST(test_cleanup_string) 00039 { 00040 check_cleanup_string(" ~", "~"); 00041 check_cleanup_string(" \b\aoa", "oa"); 00042 fail_if(cleanup_string(strdup_local(" ")) != NULL, "cleanup_string() on whitespace-only string did not return NULL."); 00043 } 00044 END_TEST 00045 00046 static void check_adjust_player_name(const char *str, const char *expected) 00047 { 00048 char *cp; 00049 00050 cp = strdup_local(str); 00051 adjust_player_name(cp); 00052 fail_if(strcmp(cp, expected), "adjust_player_name() adjusted string '%s' to '%s' but it was not the expected string '%s'.", str, cp, expected); 00053 free(cp); 00054 } 00055 00056 START_TEST(test_adjust_player_name) 00057 { 00058 check_adjust_player_name("_AaA", "_aaa"); 00059 check_adjust_player_name("bleh", "Bleh"); 00060 check_adjust_player_name("oOooooO", "Ooooooo"); 00061 check_adjust_player_name("19a4", "19a4"); 00062 check_adjust_player_name("--- ~ ; ", "--- ~ ;"); 00063 } 00064 END_TEST 00065 00066 static void check_split_string(const char *str, size_t array_size, ...) 00067 { 00068 char tmp[256], *array[64]; 00069 size_t result, i; 00070 va_list arg; 00071 00072 snprintf(tmp, sizeof(tmp), "%s", str); 00073 00074 for (i = 0; i < sizeof(array) / sizeof(*array); i++) 00075 { 00076 array[i] = NULL; 00077 } 00078 00079 result = split_string(tmp, array, array_size, ':'); 00080 fail_if(result > array_size, "result == %zu > %zu == array_size", result, array_size); 00081 va_start(arg, array_size); 00082 00083 for (i = 0; i < sizeof(array) / sizeof(*array); i++) 00084 { 00085 const char *expected_result = va_arg(arg, const char *); 00086 00087 if (expected_result == NULL) 00088 { 00089 break; 00090 } 00091 00092 if (i >= array_size) 00093 { 00094 fail("Internal error: too many arguments passed to check_split_string()."); 00095 } 00096 00097 if (i < result) 00098 { 00099 fail_if(strcmp(array[i], expected_result) != 0, "strcmp(array[%zu] == %s, %s) != 0", i, array[i], expected_result); 00100 } 00101 else 00102 { 00103 fail_if(array[i] != NULL, "array[%zu] == NULL", i); 00104 } 00105 } 00106 00107 va_end(arg); 00108 fail_if(result != i, "%zu != %zu", result, i); 00109 } 00110 00111 START_TEST(test_split_string) 00112 { 00113 check_split_string("", 0, NULL); 00114 check_split_string("", 5, "", NULL); 00115 check_split_string(":", 5, "", "", NULL); 00116 check_split_string("::", 5, "", "", "", NULL); 00117 check_split_string("abc:def:ghi", 0, NULL); 00118 check_split_string("abc:def:ghi", 1, "abc:def:ghi", NULL); 00119 check_split_string("abc:def:ghi", 2, "abc", "def:ghi", NULL); 00120 check_split_string("abc:def:ghi", 3, "abc", "def", "ghi", NULL); 00121 check_split_string("abc:def:ghi", 4, "abc", "def", "ghi", NULL); 00122 check_split_string("::abc::def::", 0, NULL); 00123 check_split_string("::abc::def::", 1, "::abc::def::", NULL); 00124 check_split_string("::abc::def::", 2, "", ":abc::def::", NULL); 00125 check_split_string("::abc::def::", 3, "", "", "abc::def::", NULL); 00126 check_split_string("::abc::def::", 4, "", "", "abc", ":def::", NULL); 00127 check_split_string("::abc::def::", 5, "", "", "abc", "", "def::", NULL); 00128 check_split_string("::abc::def::", 6, "", "", "abc", "", "def", ":", NULL); 00129 check_split_string("::abc::def::", 7, "", "", "abc", "", "def", "", "", NULL); 00130 check_split_string("::abc::def::", 8, "", "", "abc", "", "def", "", "", NULL); 00131 } 00132 END_TEST 00133 00134 START_TEST(test_buf_overflow) 00135 { 00136 int i; 00137 00138 i = buf_overflow("1", "22", 3); 00139 fail_if(i == 0, "'1' + '22' can't fit in a 3 char buffer but buf_overflow told us there won't be any overflow."); 00140 i = buf_overflow("1", NULL, 1); 00141 fail_if(i == 0, "'1' + NULL can't fit in a 1 char buffer but buf_overflow told us there won't be any overflow."); 00142 i = buf_overflow("1", NULL, 2); 00143 fail_if(i == 1, "'1' + NULL can fit in a 2 char buffer but buf_overflow told us it won't."); 00144 i = buf_overflow("", NULL, 1); 00145 fail_if(i == 1, "EMPTY + NULL can fit in a 1 char buffer but buf_overflow told us it won't."); 00146 i = buf_overflow("", NULL, 0); 00147 fail_if(i == 0, "EMPTY + NULL can't fit in a 0 char buffer but buf_overflow() told us there won't be any overflow."); 00148 } 00149 END_TEST 00150 00151 static void check_cleanup_chat_string(const char *str, const char *expected) 00152 { 00153 char *cp; 00154 00155 cp = cleanup_chat_string(strdup_local(str)); 00156 fail_if(strcmp(cp, expected), "cleanup_chat_string() adjusted string '%s' to '%s' but it was not the expected string '%s'.", str, cp, expected); 00157 } 00158 00159 START_TEST(test_cleanup_chat_string) 00160 { 00161 check_cleanup_chat_string(" --- ~ ~; ", "--- ~ ~; "); 00162 check_cleanup_chat_string(" ^test^ ping", "^test^ ping"); 00163 check_cleanup_chat_string(" ", ""); 00164 } 00165 END_TEST 00166 00167 static void check_format_number_comma(uint64 num, const char *expected) 00168 { 00169 char *cp; 00170 00171 cp = format_number_comma(num); 00172 fail_if(strcmp(cp, expected), "format_number_comma() adjusted number '%"FMT64"' to '%s' but it was not the expected string '%s'.", num, cp, expected); 00173 } 00174 00175 START_TEST(test_format_number_comma) 00176 { 00177 check_format_number_comma(0, "0"); 00178 check_format_number_comma(1, "1"); 00179 check_format_number_comma(10, "10"); 00180 check_format_number_comma(100, "100"); 00181 check_format_number_comma(1000, "1,000"); 00182 check_format_number_comma(10000, "10,000"); 00183 check_format_number_comma(100000, "100,000"); 00184 check_format_number_comma(1000000, "1,000,000"); 00185 check_format_number_comma(10000000, "10,000,000"); 00186 check_format_number_comma(100000000, "100,000,000"); 00187 check_format_number_comma(1000000000, "1,000,000,000"); 00188 check_format_number_comma(10000000000LLU, "10,000,000,000"); 00189 check_format_number_comma(100000000000LLU, "100,000,000,000"); 00190 check_format_number_comma(1000000000000LLU, "1,000,000,000,000"); 00191 check_format_number_comma(10000000000000LLU, "10,000,000,000,000"); 00192 check_format_number_comma(100000000000000LLU, "100,000,000,000,000"); 00193 } 00194 END_TEST 00195 00196 static Suite *shstr_suite() 00197 { 00198 Suite *s = suite_create("utils"); 00199 TCase *tc_core = tcase_create("Core"); 00200 00201 tcase_add_checked_fixture(tc_core, NULL, NULL); 00202 00203 suite_add_tcase(s, tc_core); 00204 tcase_add_test(tc_core, test_cleanup_string); 00205 tcase_add_test(tc_core, test_adjust_player_name); 00206 tcase_add_test(tc_core, test_split_string); 00207 tcase_add_test(tc_core, test_buf_overflow); 00208 tcase_add_test(tc_core, test_cleanup_chat_string); 00209 tcase_add_test(tc_core, test_format_number_comma); 00210 00211 return s; 00212 } 00213 00214 void check_server_utils() 00215 { 00216 Suite *s = shstr_suite(); 00217 SRunner *sr = srunner_create(s); 00218 00219 srunner_set_xml(sr, "unit/server/utils.xml"); 00220 srunner_set_log(sr, "unit/server/utils.out"); 00221 srunner_run_all(sr, CK_ENV); 00222 srunner_ntests_failed(sr); 00223 srunner_free(sr); 00224 }
1.7.4