From 1020cf8a825c252ed920ea9fc65a52944e624174 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Sun, 17 Sep 2023 18:32:28 -0700 Subject: [PATCH] util: Add a few basic tests for linear_alloc MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Not comprehensive, but those were the ones used to work on the previous linear_alloc changes. Also having a test already set up lower the barrier to add more tests for future in case of bugs. Reviewed-by: Marek Olšák Part-of: --- src/util/meson.build | 1 + src/util/tests/linear_test.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/util/tests/linear_test.cpp diff --git a/src/util/meson.build b/src/util/meson.build index ccb7c0f..c151388 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -361,6 +361,7 @@ if with_tests 'tests/gc_alloc_tests.cpp', 'tests/half_float_test.cpp', 'tests/int_min_max.cpp', + 'tests/linear_test.cpp', 'tests/mesa-sha1_test.cpp', 'tests/os_mman_test.cpp', 'tests/perf/u_trace_test.cpp', diff --git a/src/util/tests/linear_test.cpp b/src/util/tests/linear_test.cpp new file mode 100644 index 0000000..8fd95a3 --- /dev/null +++ b/src/util/tests/linear_test.cpp @@ -0,0 +1,55 @@ +/* + * Copyright © 2023 Intel Corporation. + * SPDX-License-Identifier: MIT + */ + +#include +#include "util/ralloc.h" + +TEST(LinearAlloc, Basic) +{ + void *ctx = ralloc_context(NULL); + linear_ctx *lin_ctx = linear_context(ctx); + + for (unsigned i = 0; i < 1024; i++) { + linear_alloc_child(lin_ctx, i * 4); + } + + ralloc_free(ctx); +} + +TEST(LinearAlloc, RallocParent) +{ + void *ctx = ralloc_context(NULL); + linear_ctx *lin_ctx = linear_context(ctx); + EXPECT_EQ(ralloc_parent_of_linear_context(lin_ctx), ctx); + ralloc_free(ctx); +} + +TEST(LinearAlloc, StrCat) +{ + void *ctx = ralloc_context(NULL); + linear_ctx *lin_ctx = linear_context(ctx); + + char *s = linear_strdup(lin_ctx, "hello,"); + bool ok = linear_strcat(lin_ctx, &s, " triangle"); + EXPECT_TRUE(ok); + EXPECT_STREQ(s, "hello, triangle"); + + ralloc_free(ctx); +} + +TEST(LinearAlloc, RewriteTail) +{ + void *ctx = ralloc_context(NULL); + linear_ctx *lin_ctx = linear_context(ctx); + + char *s = linear_strdup(lin_ctx, "hello, world"); + size_t start = 7; + bool ok = linear_asprintf_rewrite_tail(lin_ctx, &s, &start, "%s", "triangle"); + EXPECT_TRUE(ok); + EXPECT_STREQ(s, "hello, triangle"); + EXPECT_EQ(start, 7 + 8); + + ralloc_free(ctx); +} -- 2.7.4