1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
//------------------------------------------------------------------------------
// sokol-debugtext-test.c
// For best results, run with ASAN and UBSAN.
//------------------------------------------------------------------------------
#include "sokol_gfx.h"
#define SOKOL_DEBUGTEXT_IMPL
#include "sokol_debugtext.h"
#include "utest.h"
#define T(b) EXPECT_TRUE(b)
#define TFLT(f0,f1) {T(fabs((f0)-(f1))<=(0.000001));}
static void init(void) {
sg_setup(&(sg_desc){0});
sdtx_setup(&(sdtx_desc_t){0});
}
static void init_with(const sdtx_desc_t* desc) {
sg_setup(&(sg_desc){0});
sdtx_setup(desc);
}
static void shutdown(void) {
sdtx_shutdown();
sg_shutdown();
}
UTEST(sokol_debugtext, default_init_shutdown) {
init();
T(_sdtx.init_cookie == _SDTX_INIT_COOKIE);
T(_sdtx.desc.context_pool_size == _SDTX_DEFAULT_CONTEXT_POOL_SIZE);
T(_sdtx.desc.printf_buf_size == _SDTX_DEFAULT_PRINTF_BUF_SIZE);
T(_sdtx.desc.context.char_buf_size == _SDTX_DEFAULT_CHAR_BUF_SIZE);
T(_sdtx.desc.context.canvas_width == _SDTX_DEFAULT_CANVAS_WIDTH);
T(_sdtx.desc.context.canvas_height == _SDTX_DEFAULT_CANVAS_HEIGHT);
T(_sdtx.desc.context.tab_width == _SDTX_DEFAULT_TAB_WIDTH);
T(_sdtx.desc.context.color_format == 0);
T(_sdtx.desc.context.depth_format == 0);
T(_sdtx.desc.context.sample_count == 0);
for (int i = 0; i < SDTX_MAX_FONTS; i++) {
T(_sdtx.desc.fonts[i].data.ptr == 0);
T(_sdtx.desc.fonts[i].data.size == 0);
T(_sdtx.desc.fonts[i].first_char == 0);
T(_sdtx.desc.fonts[i].last_char == 0);
}
T(_sdtx.font_img.id != SG_INVALID_ID);
T(_sdtx.shader.id != SG_INVALID_ID);
T(_sdtx.fmt_buf_size == (_SDTX_DEFAULT_CHAR_BUF_SIZE + 1));
T(_sdtx.fmt_buf);
T(_sdtx.def_ctx_id.id != 0);
T(_sdtx.def_ctx_id.id == _sdtx.cur_ctx_id.id);
T(_sdtx.cur_ctx == _sdtx_lookup_context(_sdtx.cur_ctx_id.id));
T(_sdtx.cur_ctx->desc.char_buf_size == _sdtx.desc.context.char_buf_size);
T(_sdtx.cur_ctx->desc.canvas_width == _sdtx.desc.context.canvas_width);
T(_sdtx.cur_ctx->desc.canvas_height == _sdtx.desc.context.canvas_height);
T(_sdtx.cur_ctx->desc.tab_width == _sdtx.desc.context.tab_width);
T(_sdtx.cur_ctx->desc.color_format == 0);
T(_sdtx.cur_ctx->desc.depth_format == 0);
T(_sdtx.cur_ctx->desc.sample_count == 0);
T(_sdtx.cur_ctx->cur_vertex_ptr);
T(_sdtx.cur_ctx->max_vertex_ptr);
T(_sdtx.cur_ctx->vertices);
T(_sdtx.cur_ctx->vertices == _sdtx.cur_ctx->cur_vertex_ptr);
T(_sdtx.cur_ctx->max_vertex_ptr == (_sdtx.cur_ctx->vertices + _SDTX_DEFAULT_CHAR_BUF_SIZE * 6));
T(_sdtx.cur_ctx->vbuf.id != 0);
T(_sdtx.cur_ctx->pip.id != 0);
TFLT(_sdtx.cur_ctx->canvas_size.x, 640.0f);
TFLT(_sdtx.cur_ctx->canvas_size.y, 480.0f);
TFLT(_sdtx.cur_ctx->glyph_size.x, 8.0f / 640.0f);
TFLT(_sdtx.cur_ctx->glyph_size.y, 8.0f / 480.0f);
TFLT(_sdtx.cur_ctx->origin.x, 0.0f);
TFLT(_sdtx.cur_ctx->origin.y, 0.0f);
TFLT(_sdtx.cur_ctx->pos.x, 0.0f);
TFLT(_sdtx.cur_ctx->pos.y, 0.0f);
TFLT(_sdtx.cur_ctx->tab_width, 4.0f);
T(_sdtx.cur_ctx->color == _SDTX_DEFAULT_COLOR);
T(_sdtx.context_pool.contexts);
T(_sdtx.context_pool.pool.size == (_SDTX_DEFAULT_CONTEXT_POOL_SIZE + 1));
shutdown();
T(_sdtx.init_cookie == 0);
}
UTEST(sokol_debugtext, init_with_params) {
init_with(&(sdtx_desc_t){
.context_pool_size = 2,
.printf_buf_size = 128,
.context = {
.char_buf_size = 256,
.canvas_width = 320,
.canvas_height = 200,
.tab_width = 8,
.color_format = SG_PIXELFORMAT_RGBA8,
.depth_format = SG_PIXELFORMAT_DEPTH_STENCIL,
.sample_count = 4,
}
});
T(_sdtx.init_cookie == _SDTX_INIT_COOKIE);
T(_sdtx.desc.context_pool_size == 2);
T(_sdtx.desc.printf_buf_size == 128);
T(_sdtx.desc.context.char_buf_size == 256);
T(_sdtx.desc.context.canvas_width == 320);
T(_sdtx.desc.context.canvas_height == 200);
T(_sdtx.desc.context.tab_width == 8);
T(_sdtx.desc.context.color_format == SG_PIXELFORMAT_RGBA8);
T(_sdtx.desc.context.depth_format == SG_PIXELFORMAT_DEPTH_STENCIL);
T(_sdtx.desc.context.sample_count == 4);
T(_sdtx.fmt_buf_size == 129);
T(_sdtx.cur_ctx->desc.char_buf_size == _sdtx.desc.context.char_buf_size);
T(_sdtx.cur_ctx->desc.canvas_width == _sdtx.desc.context.canvas_width);
T(_sdtx.cur_ctx->desc.canvas_height == _sdtx.desc.context.canvas_height);
T(_sdtx.cur_ctx->desc.tab_width == _sdtx.desc.context.tab_width);
T(_sdtx.cur_ctx->desc.color_format == SG_PIXELFORMAT_RGBA8);
T(_sdtx.cur_ctx->desc.depth_format == SG_PIXELFORMAT_DEPTH_STENCIL);
T(_sdtx.cur_ctx->desc.sample_count == 4);
T(_sdtx.cur_ctx->max_vertex_ptr == (_sdtx.cur_ctx->vertices + 256 * 6));
TFLT(_sdtx.cur_ctx->canvas_size.x, 320.0f);
TFLT(_sdtx.cur_ctx->canvas_size.y, 200.0f);
TFLT(_sdtx.cur_ctx->glyph_size.x, 8.0f / 320.0f);
TFLT(_sdtx.cur_ctx->glyph_size.y, 8.0f / 200.0f);
TFLT(_sdtx.cur_ctx->tab_width, 8.0f);
T(_sdtx.context_pool.pool.size == 3);
shutdown();
}
UTEST(sokol_debugtext, make_destroy_context) {
init();
sdtx_context ctx_id = sdtx_make_context(&(sdtx_context_desc_t){
.char_buf_size = 64,
.canvas_width = 1024,
.canvas_height = 768,
.tab_width = 3,
.color_format = SG_PIXELFORMAT_RGBA32F,
.sample_count = 2
});
T(ctx_id.id != 0);
T(ctx_id.id != _sdtx.cur_ctx_id.id);
_sdtx_context_t* ctx = _sdtx_lookup_context(ctx_id.id);
T(ctx);
T(ctx != _sdtx.cur_ctx);
T(ctx->desc.char_buf_size == 64);
T(ctx->desc.canvas_width == 1024);
T(ctx->desc.canvas_height == 768);
T(ctx->desc.tab_width == 3);
T(ctx->desc.color_format == SG_PIXELFORMAT_RGBA32F);
T(ctx->desc.depth_format == 0);
T(ctx->desc.sample_count == 2);
T(ctx->vertices);
T(ctx->cur_vertex_ptr == ctx->vertices);
T(ctx->max_vertex_ptr == ctx->vertices + 64 * 6);
TFLT(ctx->canvas_size.x, 1024.0f);
TFLT(ctx->canvas_size.y, 768.0f);
TFLT(ctx->glyph_size.x, 8.0f / 1024.0f);
TFLT(ctx->glyph_size.y, 8.0f / 768.0f);
TFLT(ctx->tab_width, 3.0f);
sdtx_destroy_context(ctx_id);
T(0 == _sdtx_lookup_context(ctx_id.id));
T(ctx->desc.char_buf_size == 0);
T(ctx->vertices == 0);
shutdown();
}
UTEST(sokol_debugtext, get_default_context) {
// getting the default context must always return SDTX_DEFAULT_CONTEXT
init();
T(sdtx_get_context().id == SDTX_DEFAULT_CONTEXT.id);
shutdown();
}
UTEST(sokol_debugtext, set_get_context) {
init();
sdtx_context ctx_id = sdtx_make_context(&(sdtx_context_desc_t){ 0 });
T(ctx_id.id != 0);
T(ctx_id.id != _sdtx.cur_ctx_id.id);
sdtx_set_context(ctx_id);
T(sdtx_get_context().id == ctx_id.id);
T(ctx_id.id == _sdtx.cur_ctx_id.id);
const _sdtx_context_t* ctx = _sdtx_lookup_context(ctx_id.id);
T(ctx == _sdtx.cur_ctx);
sdtx_set_context(SDTX_DEFAULT_CONTEXT);
T(sdtx_get_context().id == SDTX_DEFAULT_CONTEXT.id);
T(_sdtx.cur_ctx);
T(ctx != _sdtx.cur_ctx);
T(_sdtx.cur_ctx == _sdtx_lookup_context(_sdtx.def_ctx_id.id));
shutdown();
}
UTEST(sokol_debugtext, destroy_default_context) {
// destroying the default context is not allowed
init();
sdtx_context def_ctx_id = _sdtx.def_ctx_id;
T(def_ctx_id.id == _sdtx.cur_ctx_id.id);
sdtx_destroy_context(def_ctx_id);
T(def_ctx_id.id == _sdtx.def_ctx_id.id);
T(def_ctx_id.id == _sdtx.cur_ctx_id.id);
T(_sdtx.cur_ctx);
shutdown();
}
UTEST(sokol_debugtext, destroy_current_context) {
// destroying the current context has the same effect
// as setting a current context with an invalid context handle
init();
sdtx_context ctx_id = sdtx_make_context(&(sdtx_context_desc_t){ 0 });
sdtx_set_context(ctx_id);
T(sdtx_get_context().id == ctx_id.id);
T(ctx_id.id == _sdtx.cur_ctx_id.id);
T(_sdtx_lookup_context(ctx_id.id) == _sdtx.cur_ctx);
sdtx_destroy_context(ctx_id);
T(_sdtx.cur_ctx_id.id == ctx_id.id);
T(_sdtx.cur_ctx == 0);
T(sdtx_get_context().id == ctx_id.id);
shutdown();
}
UTEST(sokol_debugtext, ignore_invalid_context_handle) {
// trying to render with an invalid context handle must not crash,
// instead ignore all operations
init();
sdtx_context ctx_id = sdtx_make_context(&(sdtx_context_desc_t){ 0 });
sdtx_set_context(ctx_id);
sdtx_destroy_context(ctx_id);
T(0 == _sdtx.cur_ctx);
T(sdtx_get_context().id == ctx_id.id);
sdtx_font(0);
sdtx_canvas(100.0f, 200.0f);
sdtx_origin(10.0f, 10.0f);
sdtx_home();
sdtx_pos(1.0f, 2.0f);
sdtx_pos_x(1.0f);
sdtx_pos_y(2.0f);
sdtx_move(2.0f, 3.0f);
sdtx_move_x(2.0f);
sdtx_move_y(3.0f);
sdtx_crlf();
sdtx_color3b(255, 255, 255);
sdtx_color3f(1.0f, 1.0f, 1.0f);
sdtx_color4b(255, 255, 255, 255);
sdtx_color4f(1.0f, 1.0f, 1.0f, 1.0f);
sdtx_color1i(0xFFFFFFFF);
sdtx_putc('A');
sdtx_puts("Hello World!");
sdtx_putr("Hello World!", 5);
sdtx_printf("Hello World %d %d %d", 1, 2, 3);
shutdown();
}
UTEST(sokol_debugtext, set_font) {
init();
T(0 == _sdtx.cur_ctx->cur_font);
sdtx_font(1);
T(1 == _sdtx.cur_ctx->cur_font);
sdtx_font(2);
T(2 == _sdtx.cur_ctx->cur_font);
shutdown();
}
UTEST(sokol_debugtext, set_canvas) {
init();
sdtx_origin(10.0f, 11.0f);
sdtx_pos(1.0f, 2.0f);
sdtx_canvas(320.0f, 200.0f);
TFLT(_sdtx.cur_ctx->canvas_size.x, 320.0f);
TFLT(_sdtx.cur_ctx->canvas_size.y, 200.0f);
TFLT(_sdtx.cur_ctx->glyph_size.x, 8.0f / 320.0f);
TFLT(_sdtx.cur_ctx->glyph_size.y, 8.0f / 200.0f);
// origin and pos must be reset to 0 when canvas is set
TFLT(_sdtx.cur_ctx->origin.x, 0.0f);
TFLT(_sdtx.cur_ctx->origin.y, 0.0f);
TFLT(_sdtx.cur_ctx->pos.x, 0.0f);
TFLT(_sdtx.cur_ctx->pos.y, 0.0f);
shutdown();
}
UTEST(sokol_debugtext, set_origin) {
init();
sdtx_origin(10.0f, 20.0f);
TFLT(_sdtx.cur_ctx->origin.x, 10.0f);
TFLT(_sdtx.cur_ctx->origin.y, 20.0f);
shutdown();
}
UTEST(sokol_debugtext, cursor_movement) {
init();
sdtx_pos(1.0f, 2.0f);
TFLT(_sdtx.cur_ctx->pos.x, 1.0f);
TFLT(_sdtx.cur_ctx->pos.y, 2.0f);
sdtx_pos_x(5.0f);
TFLT(_sdtx.cur_ctx->pos.x, 5.0f);
TFLT(_sdtx.cur_ctx->pos.y, 2.0f);
sdtx_pos_y(6.0f);
TFLT(_sdtx.cur_ctx->pos.x, 5.0f);
TFLT(_sdtx.cur_ctx->pos.y, 6.0f);
sdtx_move(-1.0f, -3.0f);
TFLT(_sdtx.cur_ctx->pos.x, 4.0f);
TFLT(_sdtx.cur_ctx->pos.y, 3.0f);
sdtx_move_x(+1.0f);
TFLT(_sdtx.cur_ctx->pos.x, 5.0f);
TFLT(_sdtx.cur_ctx->pos.y, 3.0f);
sdtx_move_y(+3.0f);
TFLT(_sdtx.cur_ctx->pos.x, 5.0f);
TFLT(_sdtx.cur_ctx->pos.y, 6.0f);
sdtx_crlf();
TFLT(_sdtx.cur_ctx->pos.x, 0.0f);
TFLT(_sdtx.cur_ctx->pos.y, 7.0f);
sdtx_pos(20.0f, 30.0f);
sdtx_home();
TFLT(_sdtx.cur_ctx->pos.x, 0.0f);
TFLT(_sdtx.cur_ctx->pos.y, 0.0f);
shutdown();
}
UTEST(sokol_debugtext, set_color) {
init();
T(_sdtx.cur_ctx->color == _SDTX_DEFAULT_COLOR);
sdtx_color3b(255, 127, 0);
T(_sdtx.cur_ctx->color == 0xFF007FFF);
sdtx_color4b(0, 127, 255, 255);
T(_sdtx.cur_ctx->color == 0xFFFF7F00);
sdtx_color3f(1.0f, 0.5f, 0.0f);
T(_sdtx.cur_ctx->color == 0xFF007FFF);
sdtx_color4f(0.0f, 0.5f, 1.0f, 1.0f);
T(_sdtx.cur_ctx->color == 0xFFFF7F00);
sdtx_color1i(0xFF000000);
T(_sdtx.cur_ctx->color == 0xFF000000);
shutdown();
}
UTEST(sokol_debugtext, vertex_overflow) {
// overflowing the vertex buffer must not crash
init_with(&(sdtx_desc_t){
.context.char_buf_size = 8,
});
for (int i = 0; i < 32; i++) {
sdtx_putc('A');
}
sdtx_puts("1234567890");
sdtx_putr("1234567890", 5);
sdtx_printf("Hello World %d!\n", 12);
T(_sdtx.cur_ctx->cur_vertex_ptr == _sdtx.cur_ctx->max_vertex_ptr);
shutdown();
}
UTEST(sokol_debugtext, context_overflow) {
// creating too many contexts should not crash
init_with(&(sdtx_desc_t){
.context_pool_size = 4,
});
T(_sdtx.context_pool.pool.size == 5);
// one slot is taken by the default context
sdtx_context ctx[4];
for (int i = 0; i < 4; i++) {
ctx[i] = sdtx_make_context(&(sdtx_context_desc_t){ 0 });
if (i < 3) {
T(ctx[i].id != 0);
}
else {
T(ctx[i].id == 0);
}
}
// destroying an invalid context should not crash
for (int i = 0; i < 4; i++) {
sdtx_destroy_context(ctx[i]);
}
shutdown();
}
UTEST(sokol_debugtext, printf_overflow) {
// overflowing the printf formatting buffer should not crash
init_with(&(sdtx_desc_t){
.printf_buf_size = 8
});
T(9 == _sdtx.fmt_buf_size);
T(16 == sdtx_printf("Hello %d\n", 123456789));
T('H' == _sdtx.fmt_buf[0])
T('e' == _sdtx.fmt_buf[1])
T('l' == _sdtx.fmt_buf[2])
T('l' == _sdtx.fmt_buf[3])
T('o' == _sdtx.fmt_buf[4])
T(' ' == _sdtx.fmt_buf[5])
T('1' == _sdtx.fmt_buf[6])
T('2' == _sdtx.fmt_buf[7])
T(0 == _sdtx.fmt_buf[8])
shutdown();
}
UTEST(sokol_debugtext, rewind_after_draw) {
// calling sdtx_draw() must rewind the cursor position, font and
// vertex pointer, to keep canvas size and origin as is
init();
sdtx_canvas(256, 128);
TFLT(_sdtx.cur_ctx->canvas_size.x, 256);
TFLT(_sdtx.cur_ctx->canvas_size.y, 128);
sdtx_origin(5, 5);
TFLT(_sdtx.cur_ctx->origin.x, 5);
TFLT(_sdtx.cur_ctx->origin.y, 5);
sdtx_pos(10, 20);
TFLT(_sdtx.cur_ctx->pos.x, 10);
TFLT(_sdtx.cur_ctx->pos.y, 20);
sdtx_font(3);
T(_sdtx.cur_ctx->cur_font == 3);
sdtx_printf("Hello World!\n");
T(_sdtx.cur_ctx->cur_vertex_ptr != _sdtx.cur_ctx->vertices);
sg_begin_default_pass(&(sg_pass_action){ 0 }, 256, 256);
sdtx_draw();
sg_end_pass();
sg_commit();
TFLT(_sdtx.cur_ctx->canvas_size.x, 256);
TFLT(_sdtx.cur_ctx->canvas_size.y, 128);
TFLT(_sdtx.cur_ctx->origin.x, 5);
TFLT(_sdtx.cur_ctx->origin.y, 5);
TFLT(_sdtx.cur_ctx->pos.x, 0);
TFLT(_sdtx.cur_ctx->pos.x, 0);
T(_sdtx.cur_ctx->cur_font == 0);
T(_sdtx.cur_ctx->cur_vertex_ptr == _sdtx.cur_ctx->vertices);
shutdown();
}
UTEST(sokol_debugtext, putr) {
// test if sdtx_putr() draws the right amount of characters
init();
_sdtx_vertex_t* start_ptr = _sdtx.cur_ctx->cur_vertex_ptr;
sdtx_putr("Hello World!", 5);
T((5 * 6) == (_sdtx.cur_ctx->cur_vertex_ptr - start_ptr));
start_ptr = _sdtx.cur_ctx->cur_vertex_ptr;
sdtx_putr("Hello!\n\n\n\n\n\n\n\n\n\n\n", 10);
// NOTE: the \n's don't result in rendered vertices
T((6 * 6) == (_sdtx.cur_ctx->cur_vertex_ptr - start_ptr));
shutdown();
}
UTEST(sokol_debugtext, default_context) {
init();
T(sdtx_default_context().id == SDTX_DEFAULT_CONTEXT.id);
shutdown();
}
|