From 460ad70c505e172a52044414ceb9181d8ec195af Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Mon, 27 Nov 2023 14:08:24 +0100 Subject: [PATCH] Add a unit test for ptrs_list map Change-Id: I9ed5a01238f66a32f28dfd366fa696771e0bf51e --- Makefile.am | 5 ++++ src/shared/ptrs_list.c | 2 ++ src/tests/test_ptrs_list_map.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/tests/test_ptrs_list_map.c diff --git a/Makefile.am b/Makefile.am index 83563b1..6116afb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -491,6 +491,7 @@ test_config_redirect_SOURCES = \ check_PROGRAMS = \ src/tests/test_ptrs_list_foreach_pos \ + src/tests/test_ptrs_list_map \ src/tests/test_compression_common \ src/tests/test_fastlz_neg \ src/tests/fuzz_logprint \ @@ -598,6 +599,10 @@ src_tests_test_ptrs_list_neg_SOURCES = src/tests/test_ptrs_list_neg.c src/shared src_tests_test_ptrs_list_neg_CFLAGS = $(check_CFLAGS) src_tests_test_ptrs_list_neg_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=free,--wrap=calloc +src_tests_test_ptrs_list_map_SOURCES = src/tests/test_ptrs_list_map.c src/shared/ptrs_list.c +src_tests_test_ptrs_list_map_CFLAGS = $(check_CFLAGS) +src_tests_test_ptrs_list_map_LDFLAGS = $(AM_LDFLAGS) + src_tests_test_common_SOURCES = src/tests/test_common.c src/shared/logcommon.c src/shared/buffer_traits.c src/shared/logconfig.c src/shared/parsers.c src/shared/backend_androidlogger.c src_tests_test_common_CFLAGS = $(check_CFLAGS) src_tests_test_common_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sendmsg,--wrap=recvmsg,--wrap=writev,--wrap=read,--wrap=poll,--wrap=fcntl,--wrap=fcntl64,--wrap=malloc,--wrap=calloc,--wrap=connect,--wrap=socket,--wrap=open,--wrap=open64,--wrap=ioctl diff --git a/src/shared/ptrs_list.c b/src/shared/ptrs_list.c index 5e63b95..771693c 100644 --- a/src/shared/ptrs_list.c +++ b/src/shared/ptrs_list.c @@ -178,6 +178,8 @@ list_head list_map(list_head head, void *user_data, map_cb map, apply_cb clear) return NULL; } + /* We can't just `list_add(ret)`, because that prepends + * and would reverse the order if used naively. */ if (!list_add(target, new)) { if (clear) { clear(new, user_data); diff --git a/src/tests/test_ptrs_list_map.c b/src/tests/test_ptrs_list_map.c new file mode 100644 index 0000000..7143802 --- /dev/null +++ b/src/tests/test_ptrs_list_map.c @@ -0,0 +1,68 @@ +/* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + +#include + +#include +#include +#include + +static void sum_list(void *element, void *userdata) +{ + *((int *) userdata) += (intptr_t) element; +} + +static elem_value map_to_square(elem_value value, void *_) +{ + intptr_t v = (intptr_t) value; + intptr_t v2 = v * v; + return (elem_value) v2; +} + +int main() +{ + // Make a simple list: 1-2-3 + list_head L = NULL; + for (int i = 1; i <= 3; ++i) + list_add(&L, (void *) ((intptr_t) i)); + + // Run some basic checks as a reference point for later. + int sum = 0; + list_foreach(L, &sum, sum_list); + assert(sum == 1+2+3); + + // Map it to squares, i.e. 1-4-9 + list_head M = list_map(L, NULL, map_to_square, NULL); + sum = 0; + list_foreach(M, &sum, sum_list); + assert(sum == 1+4+9); + + // Iterate manually to make sure the list is linked 1-4-9 and not 9-4-1 + list_head Li = L; + list_head Mi = M; + while (Li && Mi) { + intptr_t x = (intptr_t) list_at(Li); + assert((intptr_t) list_at(Mi) == x * x); + + list_next(&Li); + list_next(&Mi); + } + assert(!Li && !Mi); + + list_clear(&L); + list_clear(&M); + + return 0; +} + -- 2.7.4