From abf2af64aca1d4b3ac8e63f8bc59ff4df00adf81 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Tue, 19 Oct 2021 09:51:59 -0700 Subject: [PATCH] util: Convert sparse array multithread test to use gtest Reviewed-by: Dylan Baker Acked-by: Matt Turner Part-of: --- src/util/meson.build | 2 +- src/util/tests/sparse_array/meson.build | 31 -------- ...multi_threaded.c => sparse_array_test.cpp} | 70 +++++++++---------- 3 files changed, 33 insertions(+), 70 deletions(-) delete mode 100644 src/util/tests/sparse_array/meson.build rename src/util/tests/{sparse_array/multi_threaded.c => sparse_array_test.cpp} (56%) diff --git a/src/util/meson.build b/src/util/meson.build index beb868f4308..a39c6bfdcf2 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -305,6 +305,7 @@ if with_tests 'tests/fast_urem_by_const_test.cpp', 'tests/int_min_max.cpp', 'tests/set_test.cpp', + 'tests/sparse_array_test.cpp', 'tests/vector_test.cpp', 'u_atomic_test.cpp', 'u_debug_stack_test.cpp', @@ -373,6 +374,5 @@ if with_tests subdir('tests/cache') subdir('tests/hash_table') subdir('tests/vma') - subdir('tests/sparse_array') subdir('tests/format') endif diff --git a/src/util/tests/sparse_array/meson.build b/src/util/tests/sparse_array/meson.build deleted file mode 100644 index 9afb49301fb..00000000000 --- a/src/util/tests/sparse_array/meson.build +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright © 2019 Intel Corporation - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -test( - 'sparse_array_multi_threaded', - executable( - 'multi_threaded', - 'multi_threaded.c', - dependencies : [idep_mesautil], - include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], - ), - suite : ['util'], - timeout: 60, -) diff --git a/src/util/tests/sparse_array/multi_threaded.c b/src/util/tests/sparse_array_test.cpp similarity index 56% rename from src/util/tests/sparse_array/multi_threaded.c rename to src/util/tests/sparse_array_test.cpp index b084ac994e4..eeca8d35f9e 100644 --- a/src/util/tests/sparse_array/multi_threaded.c +++ b/src/util/tests/sparse_array_test.cpp @@ -21,64 +21,58 @@ * IN THE SOFTWARE. */ -#undef NDEBUG - #include "util/sparse_array.h" -#include -#include #include "c11/threads.h" -#define NUM_THREADS 16 -#define NUM_RUNS 16 +#include + #define NUM_SETS_PER_THREAD (1 << 10) #define MAX_ARR_SIZE (1 << 20) static int test_thread(void *_state) { - struct util_sparse_array *arr = _state; + struct util_sparse_array *arr = (struct util_sparse_array *)_state; for (unsigned i = 0; i < NUM_SETS_PER_THREAD; i++) { uint32_t idx = rand() % MAX_ARR_SIZE; - uint32_t *elem = util_sparse_array_get(arr, idx); + uint32_t *elem = (uint32_t *)util_sparse_array_get(arr, idx); *elem = idx; } return 0; } -static void -run_test(unsigned run_idx) +TEST(SparseArrayTest, Multithread) { - size_t node_size = 4 << (run_idx / 2); + const int num_runs = 16; - struct util_sparse_array arr; - util_sparse_array_init(&arr, sizeof(uint32_t), node_size); + for (unsigned run_idx = 0; run_idx < num_runs; run_idx++) { + size_t node_size = 4 << (run_idx / 2); - thrd_t threads[NUM_THREADS]; - for (unsigned i = 0; i < NUM_THREADS; i++) { - int ret = thrd_create(&threads[i], test_thread, &arr); - assert(ret == thrd_success); + struct util_sparse_array arr; + util_sparse_array_init(&arr, sizeof(uint32_t), node_size); + + thrd_t threads[16]; + for (unsigned i = 0; i < ARRAY_SIZE(threads); i++) { + int ret = thrd_create(&threads[i], test_thread, &arr); + ASSERT_EQ(ret, thrd_success); + } + + for (unsigned i = 0; i < ARRAY_SIZE(threads); i++) { + int ret = thrd_join(threads[i], NULL); + ASSERT_EQ(ret, thrd_success); + } + + util_sparse_array_validate(&arr); + + for (unsigned i = 0; i < MAX_ARR_SIZE; i++) { + uint32_t *elem = (uint32_t *)util_sparse_array_get(&arr, i); + ASSERT_TRUE(*elem == 0 || *elem == i) + << "Element at index " << i << " is " << *elem + << " but expected zero or " << i; + } + + util_sparse_array_finish(&arr); } - - for (unsigned i = 0; i < NUM_THREADS; i++) { - int ret = thrd_join(threads[i], NULL); - assert(ret == thrd_success); - } - - util_sparse_array_validate(&arr); - - for (unsigned i = 0; i < MAX_ARR_SIZE; i++) { - uint32_t *elem = util_sparse_array_get(&arr, i); - assert(*elem == 0 || *elem == i); - } - - util_sparse_array_finish(&arr); -} - -int -main(int argc, char **argv) -{ - for (unsigned i = 0; i < NUM_RUNS; i++) - run_test(i); }