Debian Codegraph @

src/test_dinkc_bindings.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 "EditorMap.h"
44 #include "editor_screen.h"
45 #include "freedink.h"
46 #include "paths.h"
47
48 /* test headers */
49 void dc_fade_down(int script, int* yield, int* preturnint);
50 void dc_fade_up(int script, int* yield, int* preturnint);
51 void dc_sp_custom(int script, int* yield, int* preturnint, char* key, int sprite, int val);
52
53 class Test_dinkc_bindings : public CxxTest::TestSuite {
54 public:
55 /* fade_up() is prioritary over fade_down(),
56 post-fade callback script is still overwritten
57
58 - broken during 108 merge of truecolor fade
59
60 - half-fixed following:
61 http://lists.gnu.org/archive/html/bug-freedink/2009-08/msg00042.html
62 6b6bef615c4aae45206fa98bb8b4bfea96eb0f3b
63 "Give priority to fade_up() over fade_down() - fix SoB intro in truecolor mode"
64 -> in Pilgrim Quest, not Stone of Balance
65
66 - 2nd half-fixed following:
67 http://www.dinknetwork.com/forum.cgi?MID=189461#189461 "FreeDink-specific"
68 http://www.dinknetwork.com/forum.cgi?MID=107994
69 a54fb13973e6b733e594766f981b724436218061
70 */
71 void test_dinkc_concurrent_fades() {
72 int yield, returnint;
73 dinkc_init();
74 dinkc_bindings_init();
75
76 int script_id1 = ts_script_init("fade1", strdup(""));
77 int script_id2 = ts_script_init("fade2", strdup(""));
78
79 process_upcycle = process_downcycle = 0;
80 dc_fade_down(script_id1, &yield, &returnint);
81 dc_fade_up(script_id2, &yield, &returnint);
82 // callback set to last script that fade_xxx()'d
83 TS_ASSERT_EQUALS(cycle_script, script_id2);
84 // on fade_up/fade_down conflict, cancel fade effect
85 TS_ASSERT_EQUALS(process_downcycle, 0);
86 // TODO: in 108, flip_it would basically cancel the fade effect
87 // FreeDink just forces up_cycle=1 and let it finish
88 //TS_ASSERT_EQUALS(process_upcycle, 0);
89
90 process_upcycle = process_downcycle = 0;
91 dc_fade_up(script_id1, &yield, &returnint);
92 dc_fade_down(script_id2, &yield, &returnint);
93 // callback set to last script that fade_xxx()'d
94 TS_ASSERT_EQUALS(cycle_script, script_id2);
95 // on fade_up/fade_down conflict, cancel fade effect
96 TS_ASSERT_EQUALS(process_downcycle, 0);
97 // TODO: in 108, flip_it would basically cancel the fade effect
98 // FreeDink just forces up_cycle=1 and let it finish
99 //TS_ASSERT_EQUALS(process_upcycle, 0);
100
101 kill_script(script_id1);
102 kill_script(script_id2);
103 dinkc_bindings_quit();
104 dinkc_quit();
105 }
106
107 /* Run {2,2} / (char*,char*) functions without crashing */
108 void test_dinkc_make_global_function() {
109 dinkc_init();
110 dinkc_bindings_init();
111 int ret = dinkc_execute_one_liner("make_global_function(\"test\", \"my_function\")");
112 TS_ASSERT_EQUALS(ret, 0);
113 dinkc_bindings_quit();
114 dinkc_quit();
115 }
116
117 void test_dinkc_sp_custom() {
118 int yield, returnint;
119 int script_id = ts_script_init("test_dinkc_sp_custom", strdup(""));
120
121 dinkc_init();
122 dinkc_bindings_init();
123
124 spr[1].active = true; // player
125 spr[1].custom = new std::map<std::string, int>; // mode = 0
126 int sprite = add_sprite(0,0,0,0,0);
127 TS_ASSERT_EQUALS(sprite, 2);
128
129 /* Segfaults for sprite 1 in dink108 - and in FreeDink as well :P */
130 dc_sp_custom(script_id, &yield, &returnint, "foo", 1, 3);
131 dc_sp_custom(script_id, &yield, &returnint, "foo", 1, -1);
132 TS_ASSERT_EQUALS(returnint, 3);
133
134 dc_sp_custom(script_id, &yield, &returnint, "foo", sprite, 4);
135 dc_sp_custom(script_id, &yield, &returnint, "foo", sprite, -1);
136 TS_ASSERT_EQUALS(returnint, 4);
137
138 dc_sp_custom(script_id, &yield, &returnint, "foo", sprite, 4);
139 dc_sp_custom(script_id, &yield, &returnint, "bar", sprite, 34);
140
141 dc_sp_custom(script_id, &yield, &returnint, "foo", sprite, -1);
142 TS_ASSERT_EQUALS(returnint, 4);
143 dc_sp_custom(script_id, &yield, &returnint, "bar", sprite, -1);
144 TS_ASSERT_EQUALS(returnint, 34);
145
146 dc_sp_custom(script_id, &yield, &returnint, "idontexit", sprite, -1);
147 TS_ASSERT_EQUALS(returnint, 0);
148
149 delete(spr[sprite].custom);
150 }
151 };