From: Michael Tremer Date: Tue, 25 Mar 2025 11:33:37 +0000 (+0000) Subject: tests: Add some simple tests for the buffer X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=295a4d12d502560edfd607d3128be5ca958438bf;p=pakfire.git tests: Add some simple tests for the buffer Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 7e949870..6847881f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ /tests/.root /tests/libpakfire/arch /tests/libpakfire/archive +/tests/libpakfire/buffer /tests/libpakfire/build /tests/libpakfire/cgroup /tests/libpakfire/config diff --git a/Makefile.am b/Makefile.am index 15c89c45..cedc6480 100644 --- a/Makefile.am +++ b/Makefile.am @@ -591,6 +591,7 @@ check_PROGRAMS += \ tests/libpakfire/main \ tests/libpakfire/arch \ tests/libpakfire/archive \ + tests/libpakfire/buffer \ tests/libpakfire/build \ tests/libpakfire/cgroup \ tests/libpakfire/config \ @@ -662,6 +663,21 @@ tests_libpakfire_archive_LDFLAGS = \ tests_libpakfire_archive_LDADD = \ $(TESTSUITE_LDADD) +tests_libpakfire_buffer_SOURCES = \ + tests/libpakfire/buffer.c + +tests_libpakfire_buffer_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_buffer_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_buffer_LDFLAGS = \ + $(TESTSUITE_LDFLAGS) + +tests_libpakfire_buffer_LDADD = \ + $(TESTSUITE_LDADD) + dist_tests_libpakfire_build_SOURCES = \ tests/libpakfire/build.c diff --git a/tests/libpakfire/buffer.c b/tests/libpakfire/buffer.c new file mode 100644 index 00000000..a472d70f --- /dev/null +++ b/tests/libpakfire/buffer.c @@ -0,0 +1,92 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include "../testsuite.h" + +static int test_allocate(const struct test* t) { + struct pakfire_buffer buffer = {}; + int r = EXIT_FAILURE; + + // The buffer should be empty, and not full + ASSERT_TRUE(pakfire_buffer_is_empty(&buffer)); + ASSERT_FALSE(pakfire_buffer_is_full(&buffer)); + + // Push something into the buffer + ASSERT_SUCCESS(pakfire_buffer_push(&buffer, "ABC", strlen("ABC"))); + + // Now the buffer should not be empty any more + ASSERT_FALSE(pakfire_buffer_is_empty(&buffer)); + ASSERT_FALSE(pakfire_buffer_is_full(&buffer)); + ASSERT(pakfire_buffer_length(&buffer) == 3); + + // Now pop a few bytes... + ASSERT_SUCCESS(pakfire_buffer_pop(&buffer, 3)); + + // The buffer should be empty again + ASSERT_TRUE(pakfire_buffer_is_empty(&buffer)); + ASSERT_FALSE(pakfire_buffer_is_full(&buffer)); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + pakfire_buffer_free(&buffer); + + return r; +} + +static int test_find_line(const struct test* t) { + struct pakfire_buffer buffer = {}; + int r = EXIT_FAILURE; + + const char* input = "ABC\nDEFG\nHIJKL\n"; + + // Push something into the buffer + ASSERT_SUCCESS(pakfire_buffer_push(&buffer, input, strlen(input))); + + // Try find the first line + ASSERT_EQUALS(pakfire_buffer_find_line(&buffer), 4); + + // Remove the first line + ASSERT_SUCCESS(pakfire_buffer_pop(&buffer, 4)); + + // Try find the second line + ASSERT_EQUALS(pakfire_buffer_find_line(&buffer), 5); + + // Remove the second line + ASSERT_SUCCESS(pakfire_buffer_pop(&buffer, 5)); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + pakfire_buffer_free(&buffer); + + return r; +} + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_allocate, 0); + testsuite_add_test(test_find_line, 0); + + return testsuite_run(argc, argv); +}