1/* 2 * Copyright © 2019 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#undef NDEBUG 25 26#include "util/sparse_array.h" 27 28#include <assert.h> 29#include <stdlib.h> 30#include "c11/threads.h" 31 32#define NUM_THREADS 16 33#define NUM_RUNS 16 34#define NUM_SETS_PER_THREAD (1 << 10) 35#define MAX_ARR_SIZE (1 << 20) 36 37static int 38test_thread(void *_state) 39{ 40 struct util_sparse_array *arr = _state; 41 for (unsigned i = 0; i < NUM_SETS_PER_THREAD; i++) { 42 uint32_t idx = rand() % MAX_ARR_SIZE; 43 uint32_t *elem = util_sparse_array_get(arr, idx); 44 *elem = idx; 45 } 46 47 return 0; 48} 49 50static void 51run_test(unsigned run_idx) 52{ 53 size_t node_size = 4 << (run_idx / 2); 54 55 struct util_sparse_array arr; 56 util_sparse_array_init(&arr, sizeof(uint32_t), node_size); 57 58 thrd_t threads[NUM_THREADS]; 59 for (unsigned i = 0; i < NUM_THREADS; i++) { 60 int ret = thrd_create(&threads[i], test_thread, &arr); 61 assert(ret == thrd_success); 62 } 63 64 for (unsigned i = 0; i < NUM_THREADS; i++) { 65 int ret = thrd_join(threads[i], NULL); 66 assert(ret == thrd_success); 67 } 68 69 util_sparse_array_validate(&arr); 70 71 for (unsigned i = 0; i < MAX_ARR_SIZE; i++) { 72 uint32_t *elem = util_sparse_array_get(&arr, i); 73 assert(*elem == 0 || *elem == i); 74 } 75 76 util_sparse_array_finish(&arr); 77} 78 79int 80main(int argc, char **argv) 81{ 82 for (unsigned i = 0; i < NUM_RUNS; i++) 83 run_test(i); 84} 85