Crossfire Server, Trunk
check_friend.cpp
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_friend_c =
3  * "$Id$";
4  */
5 
6 /*
7  * CrossFire, A Multiplayer game for X-windows
8  *
9  * Copyright (C) 2022 the Crossfire Development Team
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * The authors can be reached via e-mail at crossfire-devel@real-time.com
26  */
27 
28 /*
29  * This is the unit tests file for common/friend.cpp
30  */
31 
32 #include <stdlib.h>
33 #include <check.h>
34 
35 #include "global.h"
36 #include "libproto.h"
37 #include "toolkit_common.h"
38 
39 void setup(void) {
40 }
41 
42 void teardown(void) {
44 }
45 
46 START_TEST(test_basic) {
47  object *ob = object_new();
50  FAIL_UNLESS(is_friendly(ob), "Object must be friendly");
52  FAIL_UNLESS(!is_friendly(ob), "Object must not be friendly");
53  FAIL_UNLESS(!QUERY_FLAG(ob, FLAG_FRIENDLY), "Object must not have FLAG_FRIENDLY");
54 }
55 END_TEST
56 
57 START_TEST(test_clean_friendly_list_count) {
58  object *ob = object_new();
60  ob->count = 0;
62  FAIL_UNLESS(!is_friendly(ob), "Object must not be friendly");
63 }
64 END_TEST
65 
66 START_TEST(test_clean_friendly_list_freed) {
67  object *ob = object_new();
71  FAIL_UNLESS(!is_friendly(ob), "Object must not be friendly");
72 }
73 END_TEST
74 
75 START_TEST(test_clean_friendly_list_not_friendly) {
76  object *ob = object_new();
80  FAIL_UNLESS(!is_friendly(ob), "Object must not be friendly");
81 }
82 END_TEST
83 
84 START_TEST(test_get_friends_of_null) {
85  object *ob = object_new(), *second = object_new();
87  add_friendly_object(second);
89  FAIL_UNLESS(list, "Must get a list");
90  // List is inverted because of its construction
91  FAIL_UNLESS(list->ob == second, "Second must be first in list");
92  FAIL_UNLESS(list->next != NULL, "List must have a second item");
93  FAIL_UNLESS(list->next->ob == ob, "Ob must be the second on the list");
94  FAIL_UNLESS(list->next->next == NULL, "List must not have a third item");
95 }
96 END_TEST
97 
98 START_TEST(test_get_friends_of_owner) {
99  object *ob = object_new(), *owner = object_new();
100  CLEAR_FLAG(owner, FLAG_REMOVED);
101  object_set_owner(ob, owner);
102  FAIL_UNLESS(ob->owner == owner, "Owner is wrong");
104  objectlink *list = get_friends_of(owner);
105  FAIL_UNLESS(list, "Must get a list of owner's friends");
106  FAIL_UNLESS(list->ob == ob, "Ob Must be first in list");
107 }
108 END_TEST
109 
110 START_TEST(test_get_friends_of_owner_only) {
111  object *ob = object_new(), *owner = object_new(), *second = object_new();
112  CLEAR_FLAG(owner, FLAG_REMOVED);
113  object_set_owner(ob, owner);
114  FAIL_UNLESS(ob->owner == owner, "Owner is wrong");
116  add_friendly_object(second);
117  objectlink *list = get_friends_of(owner);
118  FAIL_UNLESS(list, "Must get a list of owner's friends");
119  FAIL_UNLESS(list->ob == ob, "Ob Must be first in list");
120  FAIL_UNLESS(list->next == NULL, "There must be only one item on the list");
121 }
122 END_TEST
123 
124 START_TEST(test_get_next_friend_none) {
125  FAIL_UNLESS(get_next_friend(NULL) == NULL, "Must get NULL");
126 }
127 END_TEST
128 
129 START_TEST(test_get_next_friend_single) {
130  object *ob = object_new();
132  FAIL_UNLESS(get_next_friend(NULL) == ob, "Must get ob");
133  FAIL_UNLESS(get_next_friend(ob) == NULL, "Must get NULL, last friend");
134 }
135 END_TEST
136 
137 START_TEST(test_get_next_friend_two) {
138  object *ob = object_new(), *second = object_new();
140  add_friendly_object(second);
141  FAIL_UNLESS(get_next_friend(NULL) == ob, "Must get ob");
142  FAIL_UNLESS(get_next_friend(ob) == second, "Must get second");
143  FAIL_UNLESS(get_next_friend(second) == NULL, "Must get NULL");
144 }
145 END_TEST
146 
147 Suite *friend_suite(void) {
148  Suite *s = suite_create("friend");
149  TCase *tc_core = tcase_create("Core");
150 
151  /*setup and teardown will be called before each test in testcase 'tc_core' */
152  tcase_add_checked_fixture(tc_core, setup, teardown);
153 
154  suite_add_tcase(s, tc_core);
155  tcase_add_test(tc_core, test_basic);
156  tcase_add_test(tc_core, test_clean_friendly_list_count);
157  tcase_add_test(tc_core, test_clean_friendly_list_freed);
158  tcase_add_test(tc_core, test_clean_friendly_list_not_friendly);
159  tcase_add_test(tc_core, test_get_friends_of_null);
160  tcase_add_test(tc_core, test_get_friends_of_owner);
161  tcase_add_test(tc_core, test_get_friends_of_owner_only);
162  tcase_add_test(tc_core, test_get_next_friend_none);
163  tcase_add_test(tc_core, test_get_next_friend_single);
164  tcase_add_test(tc_core, test_get_next_friend_two);
165 
166  return s;
167 }
168 
169 int main(void) {
170  int nf;
171  Suite *s = friend_suite();
172  SRunner *sr = srunner_create(s);
173 
174  srunner_set_fork_status(sr, CK_NOFORK);
175  srunner_set_xml(sr, LOGDIR "/unit/common/friend.xml");
176  srunner_set_log(sr, LOGDIR "/unit/common/friend.out");
177  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
178  nf = srunner_ntests_failed(sr);
179  srunner_free(sr);
180  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
181 }
global.h
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.cpp:52
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
get_next_friend
object * get_next_friend(object *current)
Definition: friend.cpp:143
get_friends_of
objectlink * get_friends_of(const object *owner)
Definition: friend.cpp:117
guildoracle.list
list
Definition: guildoracle.py:87
object_set_owner
void object_set_owner(object *op, object *owner)
Definition: object.cpp:840
guildjoin.ob
ob
Definition: guildjoin.py:42
clear_friendly_list
void clear_friendly_list(void)
Definition: friend.cpp:134
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
is_friendly
int is_friendly(const object *op)
Definition: friend.cpp:108
clean_friendly_list
void clean_friendly_list(void)
Definition: friend.cpp:80
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
main
int main(void)
Definition: check_friend.cpp:169
setup
void setup(void)
Definition: check_friend.cpp:39
START_TEST
START_TEST(test_basic)
Definition: check_friend.cpp:46
friend_suite
END_TEST Suite * friend_suite(void)
Definition: check_friend.cpp:147
object_new
object * object_new(void)
Definition: object.cpp:1273
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Definition: define.h:246
add_friendly_object
void add_friendly_object(object *op)
Definition: friend.cpp:32
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
teardown
void teardown(void)
Definition: check_friend.cpp:42
libproto.h