Debian Codegraph @

src/test_text.cxx

raw
1 /**
2 * Say my name 10x without failing
3
4 * Copyright (C) 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <cxxtest/TestSuite.h>
28
29 #include "text.h"
30 #include "live_sprites_manager.h"
31 #include "gfx.h"
32 #include "gfx_fonts.h"
33 #include "IOGfxDisplay.h"
34 IOGfxSurface *IOGFX_backbuffer = NULL;
35 IOGfxDisplay* g_display = NULL;
36
37 extern int add_text_sprite(char* text, int script, int sprite_owner, int mx, int my);
38
39 /* Mocks */
40 int last_text = 0;
41 int* plast_text = &last_text;
42 int dversion = 108;
43
44 static SDL_Color g_c;
45 void FONTS_SetTextColor(Uint8 r, Uint8 g, Uint8 b) {
46 g_c.r = r;
47 g_c.g = g;
48 g_c.b = b;
49 }
50 SDL_Color font_colors[16];
51
52 int print_text_wrap(char*, rect*, int, int, FONT_TYPE, std::vector<TextCommand>* imgs) { return 0; }
53 int print_text_wrap_getcmds(char *str, rect * box, /*bool*/int hcenter, int calc_only, FONT_TYPE font_type, std::vector<TextCommand>* cmds) { return 0; }
54 void print_text_cmds(std::vector<TextCommand>* cmds) {}
55 IOGfxSurface* print_text_flatten_cmds(std::vector<TextCommand>* cmds) { return NULL; }
56 void print_text_cache(IOGfxSurface*, SDL_Rect, int, int) {}
57 int gfx_fonts_init(void) { return 1; }
58 void gfx_fonts_init_colors() {}
59 void gfx_fonts_quit() {}
60
61 double truecolor_fade_brightness;
62 int truecolor = 1;
63 SDL_Surface* GFX_tmp2;
64 extern int PlayMidi(char *sFileName) { return 1; }
65 extern SDL_Surface* load_bmp_from_fp(FILE* in) { return NULL; }
66
67
68 class TestText : public CxxTest::TestSuite {
69 public:
70 void setUp() {
71 live_sprites_manager_init();
72 truecolor_fade_brightness = 256;
73 for (int i = 0; i < 16; i++) {
74 font_colors[i].r = i;
75 font_colors[i].g = i;
76 font_colors[i].b = i;
77 }
78 }
79 void tearDown() {
80 }
81
82 void test_text() {
83 TS_ASSERT_EQUALS(add_sprite(0, 0, 0, 0, 0), 1);
84 TS_ASSERT_EQUALS(add_sprite(0, 0, 0, 0, 0), 2);
85 spr[2].x = 500;
86
87 TS_ASSERT_EQUALS(add_text_sprite("Hi", 123, 2, 0, 0), 3);
88 TS_ASSERT_EQUALS(spr[3].text, "Hi");
89 TS_ASSERT_EQUALS(spr[3].text_owner, 2);
90 TS_ASSERT_EQUALS(spr[3].script, 123);
91 TS_ASSERT_EQUALS(spr[3].damage, -1);
92 TS_ASSERT_EQUALS(spr[3].kill_ttl, 2700);
93 text_draw(3, 256);
94
95 TS_ASSERT_EQUALS(say_text("Hello", 2, 0), 4);
96 text_draw(4, 256);
97
98 // Crash tests
99
100 TS_ASSERT_EQUALS(say_text("Hello1000", 1000, 0), 5);
101 text_draw(5, 256);
102
103 TS_ASSERT_EQUALS(say_text("Hello1200", 1200, 0), 6);
104 text_draw(6, 256);
105
106 TS_ASSERT_EQUALS(say_text("Hello100000", 100000, 0), 7);
107 text_draw(7, 256);
108 }
109
110 void test_fade() {
111 TS_ASSERT_EQUALS(add_sprite(0, 0, 0, 0, 0), 1);
112 TS_ASSERT_EQUALS(add_text_sprite("Fading", 0, 1, 0, 0), 2);
113 text_draw(2, 256);
114 TS_ASSERT_EQUALS(g_c.r, 14);
115 TS_ASSERT_EQUALS(add_text_sprite("`2Fading", 0, 1, 0, 0), 3);
116 text_draw(3, 256);
117 TS_ASSERT_EQUALS(g_c.r, 2);
118
119 text_draw(2, 0);
120 TS_ASSERT_EQUALS(g_c.r, 15);
121 text_draw(3, 0);
122 TS_ASSERT_EQUALS(g_c.r, 15);
123 }
124
125 void test_damage_experience() {
126 TS_ASSERT_EQUALS(add_sprite(0, 0, 0, 0, 0), 1);
127 TS_ASSERT_EQUALS(add_sprite(100,100,8,0,0), 2);
128 spr[2].damage = 10;
129
130 spr[2].brain_parm = 1;
131 text_draw(2, 256);
132 TS_ASSERT_EQUALS(g_c.r, 255);
133 TS_ASSERT_EQUALS(g_c.g, 255);
134 TS_ASSERT_EQUALS(g_c.b, 255);
135
136 spr[2].brain_parm = 5000;
137 spr[2].damage = 10;
138 text_draw(2, 256);
139 TS_ASSERT_EQUALS(g_c.r, 14);
140 }
141 };