Crossfire Server, Branch 1.12  R12190
check_shstr.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_check_shstr_c =
00003  *   "$Id: check_shstr.c 11578 2009-02-23 22:02:27Z lalo $";
00004  */
00005 
00006 /*
00007  * CrossFire, A Multiplayer game for X-windows
00008  *
00009  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
00010  * Copyright (C) 1992 Frank Tore Johansen
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  *
00026  * The authors can be reached via e-mail at crossfire-devel@real-time.com
00027  */
00028 
00029 /*
00030  * This is the unit tests file for common/shstr.c
00031  */
00032 
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <check.h>
00036 #include "global.h"
00037 
00038 /*
00039 extern const char *add_string(const char *str);
00040 extern const char *add_refcount(const char *str);
00041 extern int query_refcount(const char *str);
00042 extern const char *find_string(const char *str);
00043 extern void free_string(const char *str);
00044 extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
00045 */
00046 
00047 void setup(void) {
00048     init_hash_table();
00049 }
00050 
00051 void teardown(void) {
00052     /* nothing to do */
00053 }
00054 
00055 START_TEST(test_add_string) {
00056     const char *str1;
00057     const char *str2;
00058     const char *str3;
00059     char *temp;
00060 #ifndef MANY_CORES
00061     fail_unless(add_string(NULL) == NULL, "add_string should null when receiving a null as parameter.");
00062 #endif
00063     str1 = add_string("Hello world");
00064     fail_unless(str1 != NULL, "add_string should not return null when receiving content.");
00065     temp = malloc(strlen(str1)+1);
00066     strcpy(temp, str1);
00067     str2 = add_string(temp);
00068     fail_unless(str2 == str1, "add_string should return same pointer for 2 same strings but str1 (%p -> '%s') != str2 (%p -> '%s').", str1, str1, str2, str2);
00069     str3 = add_string("");
00070     fail_unless(str3 != NULL, "add_string should handle gracefully empty non null strings.");
00071     free(temp);
00072 }
00073 END_TEST
00074 
00075 START_TEST(test_add_refcount) {
00076     const char *str1;
00077     const char *str2;
00078 
00079     str1 = add_string("Crossfire Rulez");
00080     str2 = add_refcount(str1);
00081     fail_unless(str1 == str2, "result of add_refcount (%p) should be the same as original pointer (%p).", str2, str1);
00082     fail_unless(query_refcount(str1) == 2, "add_refcount (%p) should have made refcount to value 2 but was %d", str1, query_refcount(str1));
00083 }
00084 END_TEST
00085 
00086 START_TEST(test_query_refcount) {
00087     const char *str1;
00088 
00089     str1 = add_string("Hello World");
00090     fail_unless(query_refcount(str1) == 1, "After add_string, query_refcount should return 1 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
00091     add_string("Hello World");
00092     fail_unless(query_refcount(str1) == 2, "After twice add_string with same string, query_refcount should return 2 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
00093     add_refcount(str1);
00094     fail_unless(query_refcount(str1) == 3, "After call to add_refcount, query_refcount should now return 3 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
00095 }
00096 END_TEST
00097 
00098 START_TEST(test_find_string) {
00099     const char *str1;
00100     const char *str2;
00101     const char *result;
00102 
00103     str1 = add_string("Hello world");
00104     str2 = add_string("Bonjour le monde");
00105     result = find_string("Hello world");
00106     fail_unless(str1 == result, "find_string for %s should return %p but returned %p(%s).", str1, str1, result, result);
00107     result = find_string("Bonjour le monde");
00108     fail_unless(str2 == result, "find_string for %s should return %p but returned %p(%s).", str2, str2, result, result);
00109     result = find_string("Hola mundo");
00110     fail_unless(result == NULL, "Searching for an inexistant string should return NULL but returned %p(%s)", result, result);
00111     str1 = add_string("");
00112     result = find_string("");
00113     fail_unless(result == str1, "Search for empty string should return it(%p), but returned %p", str1, result);
00114     free_string(str2);
00115     result = find_string("Bonjour le monde");
00116     fail_unless(result == NULL, "add_string + free_string should mean i can't find the string anymore but find string returned %p(%s)", result, result);
00117 }
00118 END_TEST
00119 
00120 START_TEST(test_free_string) {
00121     const char *str1;
00122     const char *str2;
00123 
00124     str1 = add_string("Cr0ssf1r3 r|_|1z");
00125     free_string(str1);
00126     str2 = find_string("Cr0ssf1r3 r|_|1z");
00127     fail_unless(str2 == NULL, "find_String should return null after a free_string but it returned %p (%s)", str2, str2);
00128     str1 = add_string("bleh");
00129     add_string("bleh");
00130     free_string(str1);
00131     str2 = find_string("bleh");
00132     fail_unless(str2 == str1, "find_string should return the string(%p) after a add_string, add_string, free_string but returned %p", str1, str2);
00133     free_string(str1);
00134     str2 = find_string("bleh");
00135     fail_unless(str2 == NULL, "find_string should return null after add_string, add_string, free_string, free_string but returned %p", str2);
00136 }
00137 END_TEST
00138 
00139 START_TEST(test_buf_overflow) {
00140     int i;
00141 
00142     i = buf_overflow("1", "22", 3);
00143     fail_unless(i, "'1' +'22' can't fit in a 3 char buffer but buf_overflow told us there won't be any overflow");
00144     i = buf_overflow("1", NULL, 1);
00145     fail_unless(i, "'1' +NULL can't fit in a 1 char buffer but buf_overflow told us there won't be any overflow");
00146     i = buf_overflow("1", NULL, 2);
00147     fail_unless(!i, "'1' +NULL can fit in a 2 char buffer but buf_overflow told us it won't");
00148     i = buf_overflow("", NULL, 1);
00149     fail_unless(!i, "EMPTY +NULL can fit in a 1 char buffer but buf_overflow told us it won't");
00150     i = buf_overflow("", NULL, 0);
00151     fail_unless(i, "EMPTY +NULL can't fit in a 0 char buffer but buf_overflow told us there won't be any overflow");
00152 }
00153 END_TEST
00154 
00155 Suite *shstr_suite(void) {
00156     Suite *s = suite_create("shstr");
00157     TCase *tc_core = tcase_create("Core");
00158 
00159     /*setup and teardown will be called before each test in testcase 'tc_core' */
00160     tcase_add_checked_fixture(tc_core, setup, teardown);
00161 
00162     suite_add_tcase(s, tc_core);
00163     tcase_add_test(tc_core, test_add_string);
00164     tcase_add_test(tc_core, test_add_refcount);
00165     tcase_add_test(tc_core, test_query_refcount);
00166     tcase_add_test(tc_core, test_find_string);
00167     tcase_add_test(tc_core, test_free_string);
00168     tcase_add_test(tc_core, test_buf_overflow);
00169 
00170     return s;
00171 }
00172 
00173 int main(void) {
00174     int nf;
00175     Suite *s = shstr_suite();
00176     SRunner *sr = srunner_create(s);
00177 
00178     srunner_set_xml(sr, LOGDIR "/unit/common/shstr.xml");
00179     srunner_set_log(sr, LOGDIR "/unit/common/shstr.out");
00180     srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
00181     nf = srunner_ntests_failed(sr);
00182     srunner_free(sr);
00183     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00184 }