Skip to content

Commit 79b6c31

Browse files
committed
tests: Expand monochrome symbol matching tests
1 parent 419dc5c commit 79b6c31

File tree

1 file changed

+159
-31
lines changed

1 file changed

+159
-31
lines changed

tests/canvas-test.c

Lines changed: 159 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,201 @@
44
#include <stdio.h>
55

66
static void
7-
symbols_fgbg_test_canvas (ChafaCanvas *canvas)
7+
dump_char_buf (const gunichar *char_buf, gint width, gint height)
88
{
9-
const guint8 black_pixel [4] =
9+
gint x, y;
10+
gchar *row, *p;
11+
12+
row = g_malloc (width * 6 + 1);
13+
14+
for (y = 0; y < height; y++)
1015
{
11-
0x00, 0x00, 0x00, 0xff
12-
};
13-
const guint8 white_pixel [4] =
16+
p = row;
17+
18+
for (x = 0; x < width; x++)
19+
{
20+
gint len;
21+
22+
len = g_unichar_to_utf8 (char_buf [(y * width) + x], p);
23+
g_assert (len > 0);
24+
p += len;
25+
}
26+
27+
*(p++) = '\n';
28+
fwrite (row, sizeof (gchar), p - row, stdout);
29+
}
30+
31+
g_free (row);
32+
}
33+
34+
static gunichar *
35+
extract_char_buf (ChafaCanvas *canvas, gint width, gint height)
36+
{
37+
gunichar *char_buf;
38+
gint x, y;
39+
40+
char_buf = g_malloc (width * height * sizeof (gunichar));
41+
42+
for (y = 0; y < height; y++)
1443
{
15-
0xff, 0xff, 0xff, 0xff
16-
};
17-
gint i, j;
44+
for (x = 0; x < width; x++)
45+
{
46+
char_buf [(y * width) + x] = chafa_canvas_get_char_at (canvas, x, y);
47+
}
48+
}
1849

19-
chafa_canvas_draw_all_pixels (canvas,
20-
CHAFA_PIXEL_RGBA8_UNASSOCIATED,
21-
black_pixel,
22-
1, 1, 4);
23-
for (i = 0; i < 100; i++)
50+
return char_buf;
51+
}
52+
53+
static gboolean
54+
validate_char_buf (gunichar *char_buf, gint width, gint height,
55+
gunichar expected_char)
56+
{
57+
gint x, y;
58+
59+
for (y = 0; y < height; y++)
2460
{
25-
for (j = 0; j < 100; j++)
61+
for (x = 0; x < width; x++)
2662
{
27-
gunichar c = chafa_canvas_get_char_at (canvas, j, i);
28-
g_assert (c == ' ');
63+
if (char_buf [(y * width) + x] != expected_char)
64+
return FALSE;
2965
}
3066
}
3167

68+
return TRUE;
69+
}
70+
71+
static gboolean
72+
test_color_char (ChafaCanvas *canvas, ChafaPixelType pixel_type,
73+
const guint8 *pixel, gunichar expected_char)
74+
{
75+
gint width, height;
76+
gunichar *char_buf;
77+
gboolean result;
78+
79+
chafa_canvas_config_get_geometry (chafa_canvas_peek_config (canvas),
80+
&width, &height);
81+
g_assert (width > 0);
82+
g_assert (height > 0);
83+
3284
chafa_canvas_draw_all_pixels (canvas,
33-
CHAFA_PIXEL_RGBA8_UNASSOCIATED,
34-
white_pixel,
85+
pixel_type,
86+
pixel,
3587
1, 1, 4);
36-
for (i = 0; i < 100; i++)
88+
char_buf = extract_char_buf (canvas, width, height);
89+
90+
result = validate_char_buf (char_buf, width, height, expected_char);
91+
92+
if (!result)
3793
{
38-
for (j = 0; j < 100; j++)
39-
{
40-
gunichar c = chafa_canvas_get_char_at (canvas, j, i);
41-
g_assert (c == 'a');
42-
}
94+
g_print ("Unexpected canvas buffer (%dx%d, want '%c', ptype %d):\n",
95+
width, height,
96+
(gchar) expected_char,
97+
pixel_type);
98+
dump_char_buf (char_buf, width, height);
4399
}
100+
101+
g_free (char_buf);
102+
return result;
44103
}
45104

46105
static void
47-
symbols_fgbg_test (void)
106+
symbols_fgbg_test_bw_canvas (ChafaCanvas *canvas, gchar black_char, gchar white_char)
107+
{
108+
const guint8 black_pixel_rgba8 [4] = { 0x00, 0x00, 0x00, 0xff };
109+
const guint8 black_pixel_argb8 [4] = { 0xff, 0x00, 0x00, 0x00 };
110+
const guint8 white_pixel [4] = { 0xff, 0xff, 0xff, 0xff };
111+
gboolean result = TRUE;
112+
113+
result &= test_color_char (canvas,
114+
CHAFA_PIXEL_RGBA8_UNASSOCIATED,
115+
black_pixel_rgba8,
116+
black_char);
117+
result &= test_color_char (canvas,
118+
CHAFA_PIXEL_ARGB8_UNASSOCIATED,
119+
black_pixel_argb8,
120+
black_char);
121+
result &= test_color_char (canvas,
122+
CHAFA_PIXEL_RGBA8_PREMULTIPLIED,
123+
black_pixel_rgba8,
124+
black_char);
125+
result &= test_color_char (canvas,
126+
CHAFA_PIXEL_ARGB8_PREMULTIPLIED,
127+
black_pixel_argb8,
128+
black_char);
129+
result &= test_color_char (canvas,
130+
CHAFA_PIXEL_RGB8,
131+
black_pixel_rgba8,
132+
black_char);
133+
result &= test_color_char (canvas,
134+
CHAFA_PIXEL_RGBA8_UNASSOCIATED,
135+
white_pixel,
136+
white_char);
137+
result &= test_color_char (canvas,
138+
CHAFA_PIXEL_RGBA8_PREMULTIPLIED,
139+
white_pixel,
140+
white_char);
141+
result &= test_color_char (canvas,
142+
CHAFA_PIXEL_BGR8,
143+
white_pixel,
144+
white_char);
145+
146+
g_assert (result == TRUE);
147+
}
148+
149+
static void
150+
symbols_fgbg_test_bw_params (ChafaCanvasMode canvas_mode, gboolean fg_only,
151+
gint width, gint height,
152+
const gchar *selectors, gchar black_char, gchar white_char,
153+
gfloat work_factor)
48154
{
49155
ChafaSymbolMap *symbol_map;
50156
ChafaCanvasConfig *config;
51157
ChafaCanvas *canvas, *canvas2;
52158

53159
symbol_map = chafa_symbol_map_new ();
54-
chafa_symbol_map_apply_selectors (symbol_map, "[ a]", NULL);
160+
chafa_symbol_map_apply_selectors (symbol_map, selectors, NULL);
55161

56162
config = chafa_canvas_config_new ();
57-
chafa_canvas_config_set_canvas_mode (config, CHAFA_CANVAS_MODE_FGBG_BGFG);
163+
chafa_canvas_config_set_canvas_mode (config, canvas_mode);
58164
chafa_canvas_config_set_symbol_map (config, symbol_map);
59-
chafa_canvas_config_set_geometry (config, 100, 100);
60-
chafa_canvas_config_set_fg_only_enabled (config, TRUE);
165+
chafa_canvas_config_set_geometry (config, width, height);
166+
chafa_canvas_config_set_fg_only_enabled (config, fg_only);
167+
chafa_canvas_config_set_work_factor (config, work_factor);
61168

62169
canvas = chafa_canvas_new (config);
63-
symbols_fgbg_test_canvas (canvas);
170+
symbols_fgbg_test_bw_canvas (canvas, black_char, white_char);
64171

65172
canvas2 = chafa_canvas_new_similar (canvas);
66173
chafa_canvas_unref (canvas);
67-
symbols_fgbg_test_canvas (canvas2);
174+
symbols_fgbg_test_bw_canvas (canvas2, black_char, white_char);
68175
chafa_canvas_unref (canvas2);
69176

70177
chafa_canvas_config_unref (config);
71178
chafa_symbol_map_unref (symbol_map);
72179
}
73180

181+
static void
182+
symbols_fgbg_test (void)
183+
{
184+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG_BGFG, TRUE,
185+
10, 10, "[ a]", ' ', 'a', 0.5f);
186+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG_BGFG, TRUE,
187+
17, 17, "[ .]", ' ', '.', 0.2f);
188+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG_BGFG, TRUE,
189+
1, 1, "[.Q]", '.', 'Q', 0.8f);
190+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG, FALSE,
191+
10, 10, "[ ']", ' ', '\'', 1.0f);
192+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG, TRUE,
193+
23, 23, "[ .]", ' ', '.', 0.05f);
194+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG, FALSE,
195+
3, 3, "[ /]", ' ', '/', 0.2f);
196+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG_BGFG, TRUE,
197+
41, 41, "[ .Q]", ' ', 'Q', 0.5f);
198+
symbols_fgbg_test_bw_params (CHAFA_CANVAS_MODE_FGBG_BGFG, TRUE,
199+
100, 100, "[ a]", ' ', 'a', 0.2f);
200+
}
201+
74202
int
75203
main (int argc, char *argv [])
76204
{

0 commit comments

Comments
 (0)