Crossfire Server, Trunk  R20513
check_weight_reduction.c
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2009 Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
28 #include <check.h>
29 #include <global.h>
30 #include <sproto.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 static void setup(void) {
35 }
36 
37 static void teardown(void) {
38 }
39 
40 START_TEST(test_weight_reduction) {
41  /* Weight reduction to apply, last value -1. */
42  const int reduction[] = { 0, 30, 50, 70, -1 };
43  /* Container archetypes, must be CONTAINER, last value NULL. */
44  const char *archs[] = { "sack", "luggage", "luggage", "luggage", NULL };
45  /* What to put into the containers, last value NULL. Must be mergeable items. */
46  const char *items[] = { "scroll", "crown", "gem", "tooth", NULL };
47  /* How many to put or remove from container. Sum should be 0, last value 0. */
48  const int add[] = { 2, 5, 7, 3, 3, 9, -5, -7, 11, -8, -9, -11, 0 };
49  int red, a, i, nrof;
50  uint32_t sum;
51  signed long carrying;
52  object *container, *item;
53 
54  for (red = 0; reduction[red] != -1; red++) {
55  for (a = 0; archs[a] != NULL; a++) {
56  container = create_archetype(archs[a]);
57  fail_unless(container != NULL, "couldn't find container arch %s!", archs[a]);
58  fail_unless(container->type == CONTAINER, "%s isn't a container, type %d instead of %d!", archs[a], container->type, CONTAINER);
59  fail_unless(container->carrying == 0, "%s has already some carrying?", archs[a]);
60 
61  container->stats.Str = reduction[red];
62 
63  for (i = 0; items[i] != NULL; i++) {
64  item = create_archetype(items[i]);
65  fail_unless(item != NULL, "couldn't find item %s to put", items[i]);
66  fail_unless(item->nrof == 1, "can't test item %s with nrof != 1", items[i]);
67  sum = 1;
68 
69  object_insert_in_ob(item, container);
70  carrying = (signed long)(item->weight * (100 - container->stats.Str) / 100);
71  fail_unless(container->carrying == carrying, "invalid weight %d instead of %d for %s in %s reduction %d!", container->carrying, carrying, items[i], archs[a], reduction[red]);
72 
73  for (nrof = 0; add[nrof] != 0; nrof++) {
74  sum += add[nrof];
75  if (add[nrof] > 0) {
76  item = create_archetype(items[i]);
77  item->nrof = add[nrof];
78  item = object_insert_in_ob(item, container);
79  } else {
80  object_decrease_nrof(item, - add[nrof]);
81  }
82  fail_unless(item->nrof == sum, "invalid item count %d instead of %d", item->nrof, sum);
83  carrying = (signed long)(item->nrof * item->weight * (100 - container->stats.Str) / 100);
84  fail_unless(container->carrying == carrying, "invalid weight %d instead of %d for %s in %s reduction %d after adding %d!", container->carrying, carrying, items[i], archs[a], reduction[red], add[nrof]);
85  }
86  object_remove(item);
87  object_free2(item, 0);
88  fail_unless(container->carrying == 0, "container %s is still carrying %d from %s instead of 0!", archs[a], container->carrying, items[i]);
89  }
90  object_free2(container, 0);
91  }
92  }
93 }
94 END_TEST
95 
96 static Suite *bug_suite(void) {
97  Suite *s = suite_create("bug");
98  TCase *tc_core = tcase_create("Core");
99 
100  tcase_add_checked_fixture(tc_core, setup, teardown);
101 
102  suite_add_tcase(s, tc_core);
103  tcase_add_test(tc_core, test_weight_reduction);
104  tcase_set_timeout(tc_core, 0);
105 
106  return s;
107 }
108 
109 int main(void) {
110  int nf;
111  Suite *s = bug_suite();
112  SRunner *sr = srunner_create(s);
113 
114  srunner_set_fork_status(sr, CK_NOFORK);
115  init(0, NULL);
116 
117  srunner_set_xml(sr, LOGDIR "/bugs/bugtrack/weight_reduction.xml");
118  srunner_set_log(sr, LOGDIR "/bugs/bugtrack/weight_reduction.out");
119  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
120  nf = srunner_ntests_failed(sr);
121  srunner_free(sr);
122  return nf == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
123 }
124 
int main(void)
START_TEST(test_weight_reduction)
static END_TEST Suite * bug_suite(void)
Global type definitions and header inclusions.
object * create_archetype(const char *name)
Finds which archetype matches the given name, and returns a new object containing a copy of the arche...
Definition: arch.c:620
object * object_insert_in_ob(object *op, object *where)
This function inserts the object op in the linked list inside the object environment.
Definition: object.c:2690
int32_t weight
Attributes of the object.
Definition: object.h:365
int32_t carrying
How much weight this object contains.
Definition: object.h:367
uint32_t nrof
How many of the objects.
Definition: object.h:333
void init(int argc, char **argv)
This is the main server initialization function.
Definition: init.c:978
static void teardown(void)
See Container.
Definition: object.h:231
int8_t Str
Definition: living.h:35
unsigned int uint32_t
Definition: win32.h:162
object * object_decrease_nrof(object *op, uint32_t i)
Decreases a specified number from the amount of an object.
Definition: object.c:2505
living stats
Str, Con, Dex, etc.
Definition: object.h:368
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
void object_free2(object *ob, int flags)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1391
static void setup(void)
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.c:1654