diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2017-11-23 23:36:27 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2017-11-23 23:36:27 +0300 |
commit | a786baa193d28e2193a9f2953b61e659c7df92e0 (patch) | |
tree | 0808fba080bdcdd9d362cd0797ca7fe7d521f933 /tests/gtests/blenlib/BLI_task_test.cc | |
parent | c9477888305b647abb89702ab2316ba5f93033b1 (diff) | |
parent | 43ddf0e9a7f0f9986ed24e05df0ce7eac5f944b6 (diff) |
Merge branch 'master' into blender2.8
Diffstat (limited to 'tests/gtests/blenlib/BLI_task_test.cc')
-rw-r--r-- | tests/gtests/blenlib/BLI_task_test.cc | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/gtests/blenlib/BLI_task_test.cc b/tests/gtests/blenlib/BLI_task_test.cc new file mode 100644 index 00000000000..e6464164ecb --- /dev/null +++ b/tests/gtests/blenlib/BLI_task_test.cc @@ -0,0 +1,76 @@ +/* Apache License, Version 2.0 */ + +#include "testing/testing.h" +#include <string.h> + +#include "atomic_ops.h" + +extern "C" { +#include "BLI_mempool.h" +#include "BLI_task.h" +#include "BLI_utildefines.h" +}; + +#define NUM_ITEMS 10000 + +static void task_mempool_iter_func(void *userdata, MempoolIterData *item) { + int *data = (int *)item; + int *count = (int *)userdata; + + EXPECT_TRUE(data != NULL); + + *data += 1; + atomic_sub_and_fetch_uint32((uint32_t *)count, 1); +} + +TEST(task, MempoolIter) +{ + int *data[NUM_ITEMS]; + BLI_mempool *mempool = BLI_mempool_create(sizeof(*data[0]), NUM_ITEMS, 32, BLI_MEMPOOL_ALLOW_ITER); + + int i; + + /* 'Randomly' add and remove some items from mempool, to create a non-homogenous one. */ + int num_items = 0; + for (i = 0; i < NUM_ITEMS; i++) { + data[i] = (int *)BLI_mempool_alloc(mempool); + *data[i] = i - 1; + num_items++; + } + + for (i = 0; i < NUM_ITEMS; i += 3) { + BLI_mempool_free(mempool, data[i]); + data[i] = NULL; + num_items--; + } + + for (i = 0; i < NUM_ITEMS; i += 7) { + if (data[i] == NULL) { + data[i] = (int *)BLI_mempool_alloc(mempool); + *data[i] = i - 1; + num_items++; + } + } + + for (i = 0; i < NUM_ITEMS - 5; i += 23) { + for (int j = 0; j < 5; j++) { + if (data[i + j] != NULL) { + BLI_mempool_free(mempool, data[i + j]); + data[i + j] = NULL; + num_items--; + } + } + } + + BLI_task_parallel_mempool(mempool, &num_items, task_mempool_iter_func, true); + + /* Those checks should ensure us all items of the mempool were processed once, and only once - as expected. */ + EXPECT_EQ(num_items, 0); + for (i = 0; i < NUM_ITEMS; i++) { + if (data[i] != NULL) { + EXPECT_EQ(*data[i], i); + } + } + + BLI_mempool_destroy(mempool); +} |