Debian Codegraph @

src/test_integration.cxx

raw
1 /**
2 * FreeDink test suite
3
4 * Copyright (C) 2014, 2015 Sylvain Beucler
5
6 * This file is part of GNU FreeDink
7
8 * GNU FreeDink is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 3 of the
11 * License, or (at your option) any later version.
12
13 * GNU FreeDink is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * 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, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24 * Currently the testsuite is grossly in the same directory as the
25 * code. The reason is that there's no existing test suite, and that
26 * we need to write tests with minimal changes to the code to assess
27 * how the code _currently_ works. In a second step we'll modularize
28 * FreeDink more so that tests can target independent units of code -
29 * and check if we broke anything in the process with the test suite
30 * :)
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <cxxtest/TestSuite.h>
38
39 #include "dinkc.h"
40 #include "dinkc_bindings.h"
41 #include "game_engine.h"
42 #include "live_sprites_manager.h"
43 #include "DMod.h"
44 #include "editor_screen.h"
45 #include "freedink.h"
46 #include "paths.h"
47 #include "gfx.h"
48 #include "IOGfxSurfaceSW.h"
49
50 class TestIntegration : public CxxTest::TestSuite {
51 public:
52 // See also http://www.dinknetwork.com/forum.cgi?MID=186069#186263
53 void test_integration_player_position_is_updated_after_screen_is_loaded() {
54 //SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
55 ts_paths_init();
56 memset(&g_dmod.map.ts_loc_mem, 0, sizeof(g_dmod.map.ts_loc_mem));
57 SDL_Surface* GFX_background = SDL_CreateRGBSurface(0, 1, 1, 32, 0,0,0,0);
58 IOGFX_background = new IOGfxSurfaceSW(GFX_background);
59 SDL_Surface* GFX_tmp1 = SDL_CreateRGBSurface(0, 1, 1, 32, 0,0,0,0);
60 IOGFX_tmp1 = new IOGfxSurfaceSW(GFX_tmp1);
61
62 dinkc_init();
63 dinkc_bindings_init();
64 make_int("&tsx", 123451, DINKC_GLOBAL_SCOPE, 0);
65 make_int("&tsy", 123452, DINKC_GLOBAL_SCOPE, 0);
66 int tsx_id = lookup_var("&tsx", DINKC_GLOBAL_SCOPE);
67 int tsy_id = lookup_var("&tsy", DINKC_GLOBAL_SCOPE);
68 const char* screen_main_code =
69 "void main(void) {\n"
70 " &tsx = sp_x(1, -1);\n"
71 " &tsy = sp_y(1, -1);\n"
72 "}";
73 int screen_script_id = ts_script_init("ts_pos_check", strdup(screen_main_code));
74 rinfo[screen_script_id]->sprite = 1000; // don't kill me
75
76 int player_map = 33;
77 pplayer_map = &player_map;
78 int vision = 0;
79 pvision = &vision;
80
81 // Create 5 connected screens
82 struct editor_screen s;
83 s.ts_script_id = screen_script_id;
84 g_dmod.map.loc[33] = 1; g_dmod.map.ts_loc_mem[33] = &s;
85 g_dmod.map.loc[32] = 1; g_dmod.map.ts_loc_mem[32] = &s;
86 g_dmod.map.loc[34] = 1; g_dmod.map.ts_loc_mem[34] = &s;
87 g_dmod.map.loc[1] = 1; g_dmod.map.ts_loc_mem[1] = &s;
88 g_dmod.map.loc[65] = 1; g_dmod.map.ts_loc_mem[65] = &s;
89
90 screenlock = 0;
91 walk_off_screen = 0;
92 spr[1].active = true;
93
94 *pplayer_map = 33;
95 spr[1].x = -1;
96 did_player_cross_screen();
97 TS_ASSERT_EQUALS(play.var[tsx_id].var, -1);
98 TS_ASSERT_EQUALS(spr[1].x, 619);
99
100 *pplayer_map = 33;
101 spr[1].y = -1;
102 did_player_cross_screen();
103 TS_ASSERT_EQUALS(play.var[tsy_id].var, -1);
104 TS_ASSERT_EQUALS(spr[1].y, 399);
105
106 *pplayer_map = 33;
107 spr[1].x = 620;
108 did_player_cross_screen();
109 TS_ASSERT_EQUALS(play.var[tsx_id].var, 620);
110 TS_ASSERT_EQUALS(spr[1].x, 20);
111
112 *pplayer_map = 33;
113 spr[1].y = 401;
114 did_player_cross_screen();
115 TS_ASSERT_EQUALS(play.var[tsy_id].var, 401);
116 TS_ASSERT_EQUALS(spr[1].y, 0);
117
118
119 walk_off_screen = 1;
120
121 spr[1].x = -1;
122 did_player_cross_screen();
123 TS_ASSERT_EQUALS(spr[1].x, -1);
124
125 spr[1].y = -1;
126 did_player_cross_screen();
127 TS_ASSERT_EQUALS(spr[1].y, -1);
128
129 spr[1].x = 700;
130 did_player_cross_screen();
131 TS_ASSERT_EQUALS(spr[1].x, 700);
132
133 spr[1].y = 500;
134 did_player_cross_screen();
135 TS_ASSERT_EQUALS(spr[1].y, 500);
136
137 walk_off_screen = 0;
138
139
140 screenlock = 1;
141
142 spr[1].x = 19;
143 did_player_cross_screen();
144 TS_ASSERT_EQUALS(spr[1].x, 20);
145
146 spr[1].x = 20;
147 did_player_cross_screen();
148 TS_ASSERT_EQUALS(spr[1].x, 20);
149
150 spr[1].y = -1;
151 did_player_cross_screen();
152 TS_ASSERT_EQUALS(spr[1].y, 0);
153
154 spr[1].y = 0;
155 did_player_cross_screen();
156 TS_ASSERT_EQUALS(spr[1].y, 0);
157
158 spr[1].x = 620;
159 did_player_cross_screen();
160 TS_ASSERT_EQUALS(spr[1].x, 619);
161
162 spr[1].x = 619;
163 did_player_cross_screen();
164 TS_ASSERT_EQUALS(spr[1].x, 619);
165
166 spr[1].y = 400;
167 did_player_cross_screen();
168 TS_ASSERT_EQUALS(spr[1].y, 399);
169
170 spr[1].y = 399;
171 did_player_cross_screen();
172 TS_ASSERT_EQUALS(spr[1].y, 399);
173
174 screenlock = 0;
175
176 dinkc_bindings_quit();
177 dinkc_quit();
178 paths_quit();
179 }
180 };