[console] Add failing unit test for con_buffer

I was looking through the code for Con_BufferAddText trying to figure
out what it was doing (answer: ring buffer for both text and lines) and
got suspicious about its handling of the line objects. I decided an
automated test was in order. It turns out I was right: filling the
buffer with a single long line causes the tail line to trample the
current line, setting its pointer and length to 0 when the final
character is put in the buffer.
This commit is contained in:
Bill Currie 2022-09-17 12:59:36 +09:00
parent 437e447b6b
commit d13df6cd37
3 changed files with 128 additions and 0 deletions

View file

@ -1,3 +1,5 @@
include libs/console/test/Makemodule.am
lib_LTLIBRARIES += libs/console/libQFconsole.la
plugin_LTLIBRARIES += @console_plugins@
noinst_LTLIBRARIES += @client_static_plugins@ @server_static_plugins@

View file

@ -0,0 +1,15 @@
libs_console_tests = \
libs/console/test/test-buffer
TESTS += $(libs_console_tests)
check_PROGRAMS += $(libs_console_tests)
test_console_libs= \
libs/console/libQFconsole.la \
libs/util/libQFutil.la
libs_console_test_test_buffer_SOURCES= \
libs/console/test/test-buffer.c
libs_console_test_test_buffer_LDADD= $(test_console_libs)
libs_console_test_test_buffer_DEPENDENCIES=$(test_console_libs)

View file

@ -0,0 +1,111 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include "QF/console.h"
static int
test_1 (void)
{
int ret = 1;
__auto_type con = Con_CreateBuffer (1024, 25);
if (!con) {
printf ("con_buffer allocation failed\n");
goto fail;
}
if (!con->buffer) {
printf ("con_buffer buffer not set\n");
goto fail;
}
if (!con->lines) {
printf ("con_buffer lines not set\n");
goto fail;
}
if (con->buffer_size != 1024) {
printf ("con_buffer buffer_size incorrect: %zd\n", con->buffer_size);
goto fail;
}
if (con->max_lines != 25) {
printf ("con_buffer max_lines incorrect: %d\n", con->max_lines);
goto fail;
}
if (con->num_lines != 1) {
printf ("con_buffer num_lines incorrect: %d\n", con->num_lines);
goto fail;
}
if (con->cur_line != 0) {
printf ("con_buffer cur_line incorrect: %d\n", con->cur_line);
goto fail;
}
if (con->lines[con->cur_line].text != con->buffer) {
printf ("con_buffer cur_line.text incorrect: %p, %p\n",
con->lines[con->cur_line].text, con->buffer);
goto fail;
}
if (con->lines[con->cur_line].len != 0) {
printf ("con_buffer cur_line.line incorrect: %zd\n",
con->lines[con->cur_line].len);
goto fail;
}
ret = 0;
fail:
Con_DestroyBuffer (con);
return ret;
}
static int
test_2 (void)
{
int ret = 1;
char text[2049];
for (size_t i = 0; i < sizeof (text); i++) {
int x = i % 13;
text[i] = x + (x > 9 ? 'a' - 10 : '0');
}
text[sizeof(text) - 1] = 0;
__auto_type con = Con_CreateBuffer (1024, 25);
Con_BufferAddText (con, text);
if (con->num_lines != 1) {
printf ("con_buffer num_lines incorrect: %d\n", con->num_lines);
goto fail;
}
if (con->cur_line != 0) {
printf ("con_buffer cur_line incorrect: %d\n", con->cur_line);
goto fail;
}
if (con->lines[con->cur_line].text != con->buffer) {
printf ("con_buffer cur_line.text incorrect: %p, %p\n",
con->lines[con->cur_line].text, con->buffer);
goto fail;
}
if (con->lines[con->cur_line].len != 1024) {
printf ("con_buffer cur_line.line incorrect: %zd\n",
con->lines[con->cur_line].len);
goto fail;
}
ret = 0;
fail:
if (ret) {
printf ("%s failed\n", __FUNCTION__);
}
Con_DestroyBuffer (con);
return ret;
}
int
main (void)
{
int ret = 0;
ret |= test_1 ();
ret |= test_2 ();
return ret;
}