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 
38 void setup(void) {
39 }
40 
41 void teardown(void) {
43 }
44 
45 START_TEST(test_basic) {
46  object *ob = object_new();
49  fail_unless(is_friendly(ob), "Object must be friendly");
51  fail_unless(!is_friendly(ob), "Object must not be friendly");
52  fail_unless(!QUERY_FLAG(ob, FLAG_FRIENDLY), "Object must not have FLAG_FRIENDLY");
53 }
54 END_TEST
55 
56 START_TEST(test_clean_friendly_list_count) {
57  object *ob = object_new();
59  ob->count = 0;
61  fail_unless(!is_friendly(ob), "Object must not be friendly");
62 }
63 END_TEST
64 
65 START_TEST(test_clean_friendly_list_freed) {
66  object *ob = object_new();
70  fail_unless(!is_friendly(ob), "Object must not be friendly");
71 }
72 END_TEST
73 
74 START_TEST(test_clean_friendly_list_not_friendly) {
75  object *ob = object_new();
79  fail_unless(!is_friendly(ob), "Object must not be friendly");
80 }
81 END_TEST
82 
83 START_TEST(test_get_friends_of_null) {
84  object *ob = object_new(), *second = object_new();
86  add_friendly_object(second);
88  fail_unless(list, "Must get a list");
89  // List is inverted because of its construction
90  fail_unless(list->ob == second, "Second must be first in list");
91  fail_unless(list->next != NULL, "List must have a second item");
92  fail_unless(list->next->ob == ob, "Ob must be the second on the list");
93  fail_unless(list->next->next == NULL, "List must not have a third item");
94 }
95 END_TEST
96 
97 START_TEST(test_get_friends_of_owner) {
98  object *ob = object_new(), *owner = object_new();
99  CLEAR_FLAG(owner, FLAG_REMOVED);
100  object_set_owner(ob, owner);
101  fail_unless(ob->owner == owner);
103  objectlink *list = get_friends_of(owner);
104  fail_unless(list, "Must get a list of owner's friends");
105  fail_unless(list->ob == ob, "Ob Must be first in list");
106 }
107 END_TEST
108 
109 START_TEST(test_get_friends_of_owner_only) {
110  object *ob = object_new(), *owner = object_new(), *second = object_new();
111  CLEAR_FLAG(owner, FLAG_REMOVED);
112  object_set_owner(ob, owner);
113  fail_unless(ob->owner == owner);
115  add_friendly_object(second);
116  objectlink *list = get_friends_of(owner);
117  fail_unless(list, "Must get a list of owner's friends");
118  fail_unless(list->ob == ob, "Ob Must be first in list");
119  fail_unless(list->next == NULL, "There must be only one item on the list");
120 }
121 END_TEST
122 
123 START_TEST(test_get_next_friend_none) {
124  fail_unless(get_next_friend(NULL) == NULL, "Must get NULL");
125 }
126 END_TEST
127 
128 START_TEST(test_get_next_friend_single) {
129  object *ob = object_new();
131  fail_unless(get_next_friend(NULL) == ob, "Must get ob");
132  fail_unless(get_next_friend(ob) == NULL, "Must get NULL, last friend");
133 }
134 END_TEST
135 
136 START_TEST(test_get_next_friend_two) {
137  object *ob = object_new(), *second = object_new();
139  add_friendly_object(second);
140  fail_unless(get_next_friend(NULL) == ob, "Must get ob");
141  fail_unless(get_next_friend(ob) == second, "Must get second");
142  fail_unless(get_next_friend(second) == NULL, "Must get NULL");
143 }
144 END_TEST
145 
146 Suite *friend_suite(void) {
147  Suite *s = suite_create("friend");
148  TCase *tc_core = tcase_create("Core");
149 
150  /*setup and teardown will be called before each test in testcase 'tc_core' */
151  tcase_add_checked_fixture(tc_core, setup, teardown);
152 
153  suite_add_tcase(s, tc_core);
154  tcase_add_test(tc_core, test_basic);
155  tcase_add_test(tc_core, test_clean_friendly_list_count);
156  tcase_add_test(tc_core, test_clean_friendly_list_freed);
157  tcase_add_test(tc_core, test_clean_friendly_list_not_friendly);
158  tcase_add_test(tc_core, test_get_friends_of_null);
159  tcase_add_test(tc_core, test_get_friends_of_owner);
160  tcase_add_test(tc_core, test_get_friends_of_owner_only);
161  tcase_add_test(tc_core, test_get_next_friend_none);
162  tcase_add_test(tc_core, test_get_next_friend_single);
163  tcase_add_test(tc_core, test_get_next_friend_two);
164 
165  return s;
166 }
167 
168 int main(void) {
169  int nf;
170  Suite *s = friend_suite();
171  SRunner *sr = srunner_create(s);
172 
173  srunner_set_fork_status(sr, CK_NOFORK);
174  srunner_set_xml(sr, LOGDIR "/unit/common/friend.xml");
175  srunner_set_log(sr, LOGDIR "/unit/common/friend.out");
176  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
177  nf = srunner_ntests_failed(sr);
178  srunner_free(sr);
179  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
180 }
global.h
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.cpp:54
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:145
get_friends_of
objectlink * get_friends_of(const object *owner)
Definition: friend.cpp:119
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:136
is_friendly
int is_friendly(const object *op)
Definition: friend.cpp:110
clean_friendly_list
void clean_friendly_list(void)
Definition: friend.cpp:82
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
main
int main(void)
Definition: check_friend.cpp:168
setup
void setup(void)
Definition: check_friend.cpp:38
START_TEST
START_TEST(test_basic)
Definition: check_friend.cpp:45
friend_suite
END_TEST Suite * friend_suite(void)
Definition: check_friend.cpp:146
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:34
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
oblnk
Definition: object.h:449
teardown
void teardown(void)
Definition: check_friend.cpp:41
libproto.h