From d13df6cd3777f53ba6c97005bfadd1aadba816f6 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 17 Sep 2022 12:59:36 +0900 Subject: [PATCH] [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. --- libs/console/Makemodule.am | 2 + libs/console/test/Makemodule.am | 15 +++++ libs/console/test/test-buffer.c | 111 ++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 libs/console/test/Makemodule.am create mode 100644 libs/console/test/test-buffer.c diff --git a/libs/console/Makemodule.am b/libs/console/Makemodule.am index fee9f194d..bf8a24f30 100644 --- a/libs/console/Makemodule.am +++ b/libs/console/Makemodule.am @@ -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@ diff --git a/libs/console/test/Makemodule.am b/libs/console/test/Makemodule.am new file mode 100644 index 000000000..2116b5919 --- /dev/null +++ b/libs/console/test/Makemodule.am @@ -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) diff --git a/libs/console/test/test-buffer.c b/libs/console/test/test-buffer.c new file mode 100644 index 000000000..692a9c780 --- /dev/null +++ b/libs/console/test/test-buffer.c @@ -0,0 +1,111 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include +#include +#include +#include + +#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; +}