Atrinik Server  4.0
cache.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
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 General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <toolkit/string.h>
30 
31 START_TEST(test_cache)
32 {
33  char *str = estrdup("hello world");
34  cache_struct *res;
35  int i;
36  char buf[MAX_BUF];
37 
38  ck_assert(cache_add("cache_test", str, CACHE_FLAG_AUTOFREE));
39 
40  res = cache_find(find_string("cache_test"));
41  ck_assert_ptr_ne(res, NULL);
42  ck_assert_ptr_eq(res->ptr, str);
43  ck_assert_str_eq(res->ptr, "hello world");
44 
45  ck_assert(cache_remove(find_string("cache_test")));
46  ck_assert(!cache_find(find_string("cache_test")));
47 
48  for (i = 0; i <= 10; i++) {
49  snprintf(buf, sizeof(buf), "hello, hello! %d", i);
50  str = estrdup(buf);
51  snprintf(buf, sizeof(buf), "cache_test_%d", i);
52  ck_assert(cache_add(buf, str, CACHE_FLAG_AUTOFREE));
53  ck_assert_ptr_ne(cache_find(find_string(buf)), NULL);
54  }
55 
56  for (i = 0; i <= 10; i++) {
57  snprintf(buf, sizeof(buf), "cache_test_%d", i);
58  ck_assert_ptr_ne(cache_find(find_string(buf)), NULL);
59  }
60 
61  ck_assert(cache_remove(find_string("cache_test_0")));
62  ck_assert(cache_remove(find_string("cache_test_10")));
63  ck_assert(cache_remove(find_string("cache_test_4")));
64  ck_assert(cache_remove(find_string("cache_test_7")));
65  ck_assert(cache_remove(find_string("cache_test_2")));
66  ck_assert(cache_remove(find_string("cache_test_9")));
67 
68  ck_assert_ptr_ne(cache_find(find_string("cache_test_1")), NULL);
69  ck_assert_ptr_ne(cache_find(find_string("cache_test_3")), NULL);
70  ck_assert_ptr_ne(cache_find(find_string("cache_test_5")), NULL);
71  ck_assert_ptr_ne(cache_find(find_string("cache_test_6")), NULL);
72  ck_assert_ptr_ne(cache_find(find_string("cache_test_8")), NULL);
73 
75  ck_assert_ptr_eq(cache_find(find_string("cache_test_3")), NULL);
76  ck_assert_ptr_eq(cache_find(find_string("cache_test_5")), NULL);
77  ck_assert_ptr_eq(cache_find(find_string("cache_test_6")), NULL);
78  ck_assert_ptr_eq(cache_find(find_string("cache_test_8")), NULL);
79 
80  ck_assert(!cache_remove(find_string("cache_test_8")));
81  ck_assert(!cache_remove(find_string("cache_test_0")));
82 
83  str = estrdup("hello hello world!!!");
84  ck_assert(cache_add("cache_rem_test", str, CACHE_FLAG_AUTOFREE));
85  ck_assert(!cache_add("cache_rem_test", str, CACHE_FLAG_AUTOFREE));
86 
87  str = estrdup("leet");
88  ck_assert(cache_add("raas", str, CACHE_FLAG_PYOBJ));
89  ck_assert(cache_add("chair", str, CACHE_FLAG_PYOBJ));
91  ck_assert_ptr_eq(cache_find(find_string("raas")), NULL);
92  ck_assert_ptr_eq(cache_find(find_string("chair")), NULL);
93  efree(str);
94 
95  ck_assert(cache_remove(find_string("cache_rem_test")));
96 }
97 
98 END_TEST
99 
100 static Suite *suite(void)
101 {
102  Suite *s = suite_create("cache");
103  TCase *tc_core = tcase_create("Core");
104 
105  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
106  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
107 
108  suite_add_tcase(s, tc_core);
109  tcase_add_test(tc_core, test_cache);
110 
111  return s;
112 }
113 
114 void check_server_cache(void)
115 {
116  check_run_suite(suite(), __FILE__);
117 }
void cache_remove_all(void)
Definition: cache.c:205
int cache_remove(shstr *key)
Definition: cache.c:167
#define CACHE_FLAG_PYOBJ
Definition: global.h:499
cache_struct * cache_find(shstr *key)
Definition: cache.c:95
#define CACHE_FLAG_AUTOFREE
Definition: global.h:501
int cache_add(const char *key, void *ptr, uint32_t flags)
Definition: cache.c:121
void cache_remove_by_flags(uint32_t flags)
Definition: cache.c:222
void * ptr
Definition: global.h:512