]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Add some simple tests for the buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 11:33:37 +0000 (11:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 11:33:37 +0000 (11:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/libpakfire/buffer.c [new file with mode: 0644]

index 7e94987060079fac3a830d935c40a4cfaaf5f3a0..6847881fd7f37ae59f03abf259a1cd48038d9024 100644 (file)
@@ -14,6 +14,7 @@
 /tests/.root
 /tests/libpakfire/arch
 /tests/libpakfire/archive
+/tests/libpakfire/buffer
 /tests/libpakfire/build
 /tests/libpakfire/cgroup
 /tests/libpakfire/config
index 15c89c45ac63c536d3bd10c6eabe47c5645a1400..cedc64802980d6a8ed74b7ddeb45e3f06929dc3e 100644 (file)
@@ -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 (file)
index 0000000..a472d70
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/buffer.h>
+
+#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);
+}