Properly link flecs library

This commit is contained in:
2023-11-09 11:38:29 +01:00
parent dc585396c3
commit 8edcf9305c
1392 changed files with 390081 additions and 164 deletions

View File

@@ -0,0 +1,499 @@
#include <collections.h>
#include <stdlib.h>
static
uint64_t* generate_keys(
int count)
{
uint64_t *keys = ecs_os_malloc_n(uint64_t, count);
for (int i = 0; i < count; i ++) {
keys[i] = i + 1;
}
return keys;
}
static
uint64_t* generate_random_keys(
int count)
{
uint64_t *keys = ecs_os_malloc_n(uint64_t, count);
for (int i = 0; i < count; i ++) {
uint64_t key;
int j = 0;
do {
key = rand() % RAND_MAX;
if (rand() % 2) {
key = ecs_pair(key, rand() % RAND_MAX);
}
for (j = 0; j < i; j ++) {
if (keys[j] == key) {
break;
}
}
} while (j != i); /* no duplicate keys */
keys[i] = key;
}
return keys;
}
static
void insert_map(
ecs_map_t *map,
uint64_t *keys,
int count)
{
for (int i = 0; i < count; i ++) {
for (int j = 0; j < i; j ++) {
uint64_t *v = ecs_map_get(map, keys[j]);
test_assert(v != NULL);
test_assert(v[0] == keys[j]);
}
test_assert(ecs_map_count(map) == i);
test_assert(ecs_map_get(map, keys[i]) == NULL);
ecs_map_insert(map, keys[i], keys[i]);
test_assert(ecs_map_get(map, keys[i])[0] == keys[i]);
test_assert(ecs_map_count(map) == (i + 1));
}
}
static
ecs_map_t populate_map(
uint64_t *keys,
int count)
{
ecs_map_t map;
ecs_map_init(&map, NULL);
insert_map(&map, keys, count);
return map;
}
static
void free_map(
ecs_map_t *map,
uint64_t *keys,
int count)
{
for (int i = 0; i < count; i ++) {
for (int j = i; j < count; j ++) {
uint64_t *v = ecs_map_get(map, keys[j]);
test_assert(v != NULL);
test_assert(v[0] == keys[j]);
}
test_assert(ecs_map_count(map) == (count - i));
test_assert(ecs_map_get(map, keys[i]) != NULL);
ecs_map_remove(map, keys[i]);
test_assert(ecs_map_count(map) == ((count - i) - 1));
test_assert(ecs_map_get(map, keys[i]) == NULL);
}
}
static int32_t malloc_count;
static
void *test_malloc(ecs_size_t size) {
malloc_count ++;
return malloc(size);
}
static
void *test_calloc(ecs_size_t size) {
malloc_count ++;
return calloc(1, size);
}
static
void *test_realloc(void *old_ptr, ecs_size_t size) {
malloc_count ++;
return realloc(old_ptr, size);
}
void Map_setup(void) {
ecs_os_set_api_defaults();
ecs_os_api_t os_api = ecs_os_api;
os_api.malloc_ = test_malloc;
os_api.calloc_ = test_calloc;
os_api.realloc_ = test_realloc;
ecs_os_set_api(&os_api);
}
void Map_count(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_int(ecs_map_count(&map), 4);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_count_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
void Map_set_overwrite(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
uint64_t *val = ecs_map_get(&map, 1);
test_int(*val, 1);
uint64_t *el = ecs_map_ensure(&map, 1);
test_assert(val == el);
test_int(ecs_map_count(&map), 4);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_set_rehash(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_int(map.bucket_count, 4);
int i;
for (i = 5; i < 16; i ++) {
uint64_t *ptr = ecs_map_ensure(&map, i);
*ptr = i;
}
test_int(map.bucket_count, 32);
for (i = 1; i < 16; i ++) {
test_int(ecs_map_get(&map, i)[0], i);
}
for (i = 1; i < 8; i ++) {
uint64_t *ptr = ecs_map_ensure(&map, i + 1000000);
*ptr = i + 1000000;
}
test_int(map.bucket_count, 32);
for (i = 1; i < 16; i ++) {
test_int(ecs_map_get(&map, i)[0], i);
}
for (i = 1; i < 8; i ++) {
test_int(ecs_map_get(&map, i + 1000000)[0], i + 1000000);
}
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_get(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
uint64_t *value = ecs_map_get(&map, 1);
test_assert(value != NULL);
test_int(value[0], 1);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_get_all(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
uint64_t *value = ecs_map_get(&map, 1);
test_assert(value != NULL);
test_int(*value, 1);
value = ecs_map_get(&map, 2);
test_assert(value != NULL);
test_int(*value, 2);
value = ecs_map_get(&map, 3);
test_assert(value != NULL);
test_int(*value, 3);
value = ecs_map_get(&map, 4);
test_assert(value != NULL);
test_int(*value, 4);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_get_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
uint64_t *value = ecs_map_get(&map, 1);
test_assert(value == NULL);
ecs_map_fini(&map);
}
void Map_get_unknown(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
uint64_t *value = ecs_map_get(&map, 5);
test_assert(value == NULL);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_get_0_from_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
uint64_t *value = ecs_map_get(&map, 0);
test_assert(value == NULL);
ecs_map_fini(&map);
}
void Map_get_0_from_populated(void) {
uint64_t *keys = generate_keys(32);
ecs_map_t map = populate_map(keys, 32);
uint64_t *value = ecs_map_get(&map, 0);
test_assert(value == NULL);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_get_0_after_insert(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_insert(&map, 0, 10);
uint64_t *value = ecs_map_get(&map, 0);
test_assert(value != NULL);
test_int(value[0], 10);
ecs_map_fini(&map);
}
void Map_get_0_after_ensure(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_ensure(&map, 0)[0] = 10;
uint64_t *value = ecs_map_get(&map, 0);
test_assert(value != NULL);
test_int(value[0], 10);
ecs_map_fini(&map);
}
void Map_iter(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
int i = 0;
ecs_map_iter_t it = ecs_map_iter(&map);
while (ecs_map_next(&it)) {
ecs_map_key_t key = ecs_map_key(&it);
uint64_t val = ecs_map_value(&it);
test_assert(key == val);
i ++;
}
test_int(i, 4);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_iter_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_iter_t it = ecs_map_iter(&map);
test_assert(!ecs_map_next(&it));
ecs_map_fini(&map);
}
void Map_iter_null(void) {
ecs_map_iter_t it = ecs_map_iter(NULL);
test_assert(!ecs_map_next(&it));
}
void Map_remove(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_assert(ecs_map_get(&map, 3) != NULL);
test_int(ecs_map_count(&map), 4);
ecs_map_remove(&map, 3);
test_assert(ecs_map_get(&map, 3) == NULL);
test_int(ecs_map_count(&map), 3);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_remove_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_remove(&map, 3);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
void Map_remove_unknown(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_assert(ecs_map_get(&map, 5) == NULL);
test_int(ecs_map_count(&map), 4);
ecs_map_remove(&map, 5);
test_int(ecs_map_get(&map, 1)[0], 1);
test_int(ecs_map_get(&map, 2)[0], 2);
test_int(ecs_map_get(&map, 3)[0], 3);
test_int(ecs_map_get(&map, 4)[0], 4);
test_int(ecs_map_count(&map), 4);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_remove_twice(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_assert(ecs_map_get(&map, 3) != NULL);
test_int(ecs_map_count(&map), 4);
ecs_map_remove(&map, 3);
test_assert(ecs_map_get(&map, 3) == NULL);
test_int(ecs_map_count(&map), 3);
ecs_map_remove(&map, 3);
test_assert(ecs_map_get(&map, 3) == NULL);
test_int(ecs_map_count(&map), 3);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_clear_empty(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
void Map_clear_populated(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_int(ecs_map_count(&map), 4);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_clear_empty_twice(void) {
ecs_map_t map;
ecs_map_init(&map, NULL);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
void Map_clear_populated_twice(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_int(ecs_map_count(&map), 4);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_populate_after_clear(void) {
uint64_t *keys = generate_keys(4);
ecs_map_t map = populate_map(keys, 4);
test_int(ecs_map_count(&map), 4);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
insert_map(&map, keys, 4);
test_int(ecs_map_count(&map), 4);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
ecs_os_free(keys);
ecs_map_fini(&map);
}
void Map_randomized_insert(void) {
uint64_t *keys = generate_random_keys(100);
for (int count = 0; count < 100; count ++) {
ecs_map_t map = populate_map(keys, count);
test_int(ecs_map_count(&map), count);
ecs_map_fini(&map);
}
ecs_os_free(keys);
}
void Map_randomized_remove(void) {
uint64_t *keys = generate_random_keys(100);
for (int count = 0; count < 100; count ++) {
ecs_map_t map = populate_map(keys, count);
test_int(ecs_map_count(&map), count);
free_map(&map, keys, count);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
ecs_os_free(keys);
}
void Map_randomized_insert_large(void) {
uint64_t *keys = generate_random_keys(1000);
for (int count = 0; count < 1000; count += 27) {
ecs_map_t map = populate_map(keys, count);
test_int(ecs_map_count(&map), count);
ecs_map_fini(&map);
}
ecs_os_free(keys);
}
void Map_randomized_remove_large(void) {
uint64_t *keys = generate_random_keys(1000);
for (int count = 0; count < 1000; count += 50) {
ecs_map_t map = populate_map(keys, count);
test_int(ecs_map_count(&map), count);
free_map(&map, keys, count);
test_int(ecs_map_count(&map), 0);
ecs_map_fini(&map);
}
ecs_os_free(keys);
}
void Map_randomized_after_clear(void) {
uint64_t *keys = generate_random_keys(1000);
for (int count = 0; count < 1000; count += 50) {
ecs_map_t map = populate_map(keys, count);
test_int(ecs_map_count(&map), count);
ecs_map_clear(&map);
test_int(ecs_map_count(&map), 0);
insert_map(&map, keys, count);
test_int(ecs_map_count(&map), count);
ecs_map_fini(&map);
}
ecs_os_free(keys);
}

View File

@@ -0,0 +1,394 @@
#include <collections.h>
#include <flecs/private/sparse.h>
void Sparse_setup(void) {
ecs_os_set_api_defaults();
}
ecs_sparse_t* _flecs_sparse_new(
struct ecs_allocator_t *allocator,
struct ecs_block_allocator_t *page_allocator,
ecs_size_t elem_size)
{
ecs_sparse_t *sp = ecs_os_calloc_t(ecs_sparse_t);
flecs_sparse_init(sp, allocator, page_allocator, elem_size);
return sp;
}
#define flecs_sparse_new(a, p, T) _flecs_sparse_new(a, p, ECS_SIZEOF(T))
void flecs_sparse_free(
ecs_sparse_t *sp)
{
flecs_sparse_fini(sp);
ecs_os_free(sp);
}
static
void populate(ecs_sparse_t *sp, int count) {
int prev_count = flecs_sparse_count(sp);
int i;
for (i = 0; i < count; i ++) {
int *elem = flecs_sparse_add_t(sp, int);
test_assert(elem != NULL);
*elem = i;
}
test_int(flecs_sparse_count(sp), count + prev_count);
}
void Sparse_add_1(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
int* elem = flecs_sparse_add_t(sp, int);
test_assert(elem != NULL);
test_int(flecs_sparse_count(sp), 1);
int *ptr = flecs_sparse_get_dense_t(sp, int, 0);
test_assert(ptr != NULL);
test_assert(elem == ptr);
flecs_sparse_free(sp);
}
void Sparse_add_1_to_empty(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
int* elem = flecs_sparse_add_t(sp, int);
test_assert(elem != NULL);
test_int(flecs_sparse_count(sp), 1);
int *ptr = flecs_sparse_get_dense_t(sp, int, 0);
test_assert(ptr != NULL);
test_assert(elem == ptr);
flecs_sparse_free(sp);
}
void Sparse_add_1_chunk_size_1(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
int* elem = flecs_sparse_add_t(sp, int);
test_assert(elem != NULL);
test_int(flecs_sparse_count(sp), 1);
int *ptr = flecs_sparse_get_dense_t(sp, int, 0);
test_assert(ptr != NULL);
test_assert(elem == ptr);
flecs_sparse_free(sp);
}
void Sparse_add_n(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
int* elem1 = flecs_sparse_add_t(sp, int);
test_assert(elem1 != NULL);
test_int(flecs_sparse_count(sp), 1);
int* elem2 = flecs_sparse_add_t(sp, int);
test_assert(elem2 != NULL);
test_int(flecs_sparse_count(sp), 2);
int *ptr = flecs_sparse_get_dense_t(sp, int, 0);
test_assert(ptr != NULL);
test_assert(elem1 == ptr);
ptr = flecs_sparse_get_dense_t(sp, int, 1);
test_assert(ptr != NULL);
test_assert(elem2 == ptr);
flecs_sparse_free(sp);
}
void Sparse_add_n_chunk_size_1(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
int* elem1 = flecs_sparse_add_t(sp, int);
test_assert(elem1 != NULL);
test_int(flecs_sparse_count(sp), 1);
int* elem2 = flecs_sparse_add_t(sp, int);
test_assert(elem2 != NULL);
test_int(flecs_sparse_count(sp), 2);
int *ptr = flecs_sparse_get_dense_t(sp, int, 0);
test_assert(ptr != NULL);
test_assert(elem1 == ptr);
ptr = flecs_sparse_get_dense_t(sp, int, 1);
test_assert(ptr != NULL);
test_assert(elem2 == ptr);
flecs_sparse_free(sp);
}
void Sparse_remove(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 3);
const uint64_t *indices = flecs_sparse_ids(sp);
uint64_t i0 = indices[0];
uint64_t i1 = indices[2];
flecs_sparse_remove_t(sp, int, 1);
test_int(flecs_sparse_count(sp), 2);
test_assert(flecs_sparse_get_dense_t(sp, int, 0) == flecs_sparse_get_t(sp, int, i0));
test_assert(flecs_sparse_get_dense_t(sp, int, 1) == flecs_sparse_get_t(sp, int, i1));
flecs_sparse_free(sp);
}
void Sparse_remove_first(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 3);
const uint64_t *indices = flecs_sparse_ids(sp);
uint64_t i0 = indices[1];
uint64_t i1 = indices[2];
flecs_sparse_remove_t(sp, int, 0);
test_int(flecs_sparse_count(sp), 2);
test_assert(flecs_sparse_get_dense_t(sp, int, 0) == flecs_sparse_get_t(sp, int, i1));
test_assert(flecs_sparse_get_dense_t(sp, int, 1) == flecs_sparse_get_t(sp, int, i0));
flecs_sparse_free(sp);
}
void Sparse_remove_last(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 3);
const uint64_t *indices = flecs_sparse_ids(sp);
uint64_t i0 = indices[0];
uint64_t i1 = indices[1];
flecs_sparse_remove_t(sp, int, 2);
test_int(flecs_sparse_count(sp), 2);
test_assert(flecs_sparse_get_dense_t(sp, int, 0) == flecs_sparse_get_t(sp, int, i0));
test_assert(flecs_sparse_get_dense_t(sp, int, 1) == flecs_sparse_get_t(sp, int, i1));
flecs_sparse_free(sp);
}
void Sparse_remove_all(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 3);
void *elem2 = flecs_sparse_get_dense_t(sp, int, 1);
void *elem3 = flecs_sparse_get_dense_t(sp, int, 2);
flecs_sparse_remove_t(sp, int, 0);
test_int(flecs_sparse_count(sp), 2);
test_assert(flecs_sparse_try_t(sp, int, 0) == NULL);
test_assert(flecs_sparse_try_t(sp, int, 1) == elem2);
test_assert(flecs_sparse_try_t(sp, int, 2) == elem3);
flecs_sparse_remove_t(sp, int, 1);
test_int(flecs_sparse_count(sp), 1);
test_assert(flecs_sparse_try_t(sp, int, 0) == NULL);
test_assert(flecs_sparse_try_t(sp, int, 1) == NULL);
test_assert(flecs_sparse_try_t(sp, int, 2) == elem3);
flecs_sparse_remove_t(sp, int, 2);
test_int(flecs_sparse_count(sp), 0);
test_assert(flecs_sparse_try_t(sp, int, 0) == NULL);
test_assert(flecs_sparse_try_t(sp, int, 1) == NULL);
test_assert(flecs_sparse_try_t(sp, int, 2) == NULL);
flecs_sparse_free(sp);
}
void Sparse_remove_all_n_chunks(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 128);
int i;
for (i = 0; i < 128; i ++) {
flecs_sparse_remove_t(sp, int, i);
test_int(flecs_sparse_count(sp), 127 - i);
}
flecs_sparse_free(sp);
}
void Sparse_clear_1(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 1);
flecs_sparse_clear(sp);
test_int(flecs_sparse_count(sp), 0);
flecs_sparse_free(sp);
}
void Sparse_clear_empty(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
flecs_sparse_clear(sp);
test_int(flecs_sparse_count(sp), 0);
flecs_sparse_free(sp);
}
void Sparse_clear_n(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 3);
flecs_sparse_clear(sp);
test_int(flecs_sparse_count(sp), 0);
flecs_sparse_free(sp);
}
void Sparse_clear_n_chunks(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 128);
flecs_sparse_clear(sp);
test_int(flecs_sparse_count(sp), 0);
flecs_sparse_free(sp);
}
void Sparse_add_after_clear(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
populate(sp, 1);
flecs_sparse_clear(sp);
test_int(flecs_sparse_count(sp), 0);
int* elem = flecs_sparse_add_t(sp, int);
test_assert(elem != NULL);
*elem = 10;
const uint64_t *indices = flecs_sparse_ids(sp);
test_assert(indices != NULL);
int *ptr = flecs_sparse_get_t(sp, int, indices[0]);
test_assert(ptr != NULL);
test_assert(ptr == elem);
test_int(*ptr, 10);
flecs_sparse_free(sp);
}
void Sparse_create_delete(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
uint64_t id = flecs_sparse_new_id(sp);
test_int(flecs_sparse_count(sp), 1);
flecs_sparse_remove_t(sp, int, id);
test_int(flecs_sparse_count(sp), 0);
test_assert(!flecs_sparse_is_alive(sp, id));
flecs_sparse_free(sp);
}
void Sparse_create_delete_2(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
test_assert(sp != NULL);
test_int(flecs_sparse_count(sp), 0);
uint64_t id_1 = flecs_sparse_new_id(sp);
test_int(flecs_sparse_count(sp), 1);
test_assert(flecs_sparse_is_alive(sp, id_1));
uint64_t id_2 = flecs_sparse_new_id(sp);
test_int(flecs_sparse_count(sp), 2);
test_assert(flecs_sparse_is_alive(sp, id_2));
flecs_sparse_remove_t(sp, int, id_1);
test_int(flecs_sparse_count(sp), 1);
test_assert(!flecs_sparse_is_alive(sp, id_1));
flecs_sparse_remove_t(sp, int, id_2);
test_int(flecs_sparse_count(sp), 0);
test_assert(!flecs_sparse_is_alive(sp, id_2));
flecs_sparse_free(sp);
}
void Sparse_count_of_null(void) {
test_int(flecs_sparse_count(NULL), 0);
}
void Sparse_try_low_after_ensure_high(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
int *ptr_1 = flecs_sparse_ensure_t(sp, int, 5000);
test_assert(ptr_1 != NULL);
int *ptr_2 = flecs_sparse_try_t(sp, int, 100);
test_assert(ptr_2 == NULL);
flecs_sparse_free(sp);
}
void Sparse_is_alive_low_after_ensure_high(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
int *ptr_1 = flecs_sparse_ensure_t(sp, int, 5000);
test_assert(ptr_1 != NULL);
bool v = flecs_sparse_is_alive(sp, 100);
test_bool(v, false);
flecs_sparse_free(sp);
}
void Sparse_remove_low_after_ensure_high(void) {
ecs_sparse_t *sp = flecs_sparse_new(NULL, NULL, int);
int *ptr_1 = flecs_sparse_ensure_t(sp, int, 5000);
test_assert(ptr_1 != NULL);
flecs_sparse_remove_t(sp, int, 100);
flecs_sparse_free(sp);
}

View File

@@ -0,0 +1,306 @@
#include <collections.h>
#include <math.h>
void Strbuf_setup(void) {
ecs_os_set_api_defaults();
}
void Strbuf_append(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
ecs_strbuf_append(&b, "Bar %d", 10);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooBar 10");
ecs_os_free(str);
}
void Strbuf_appendstr(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b, "Foo");
ecs_strbuf_appendstr(&b, "Bar");
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooBar");
ecs_os_free(str);
}
void Strbuf_appendstrn(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
ecs_strbuf_appendstrn(&b, "Bar", 1);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooB");
ecs_os_free(str);
}
void Strbuf_appendstr_null(void) {
install_test_abort();
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
test_expect_abort();
ecs_strbuf_appendstr(&b, NULL);
}
void Strbuf_append_list(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
ecs_strbuf_list_push(&b, "{", ",");
ecs_strbuf_list_append(&b, "Foo %d", 10);
ecs_strbuf_list_appendstr(&b, "Bar");
ecs_strbuf_list_pop(&b, "}");
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "Foo{Foo 10,Bar}");
ecs_os_free(str);
}
void Strbuf_append_nested_list(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
ecs_strbuf_list_push(&b, "{", ",");
ecs_strbuf_list_append(&b, "Foo %d", 10);
ecs_strbuf_list_appendstr(&b, "Bar");
ecs_strbuf_list_push(&b, "[", ",");
ecs_strbuf_list_appendstr(&b, "Hello");
ecs_strbuf_list_appendstr(&b, "World");
ecs_strbuf_list_pop(&b, "]");
ecs_strbuf_list_next(&b);
ecs_strbuf_list_push(&b, "[", ",");
ecs_strbuf_list_appendstr(&b, "Hello");
ecs_strbuf_list_appendstr(&b, "World");
ecs_strbuf_list_pop(&b, "]");
ecs_strbuf_list_pop(&b, "}");
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "Foo{Foo 10,Bar[Hello,World],[Hello,World]}");
ecs_os_free(str);
}
void Strbuf_large_str(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_append(&b, "Foo");
int i;
for (i = 0; i < 200; i ++) {
ecs_strbuf_appendstr(&b, "Bar");
}
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_assert(!strncmp(str, "Foo", 3));
for (i = 1; i < 201; i ++) {
test_assert(!strncmp(&str[i * 3], "Bar", 3));
}
ecs_os_free(str);
}
void Strbuf_empty_str(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
char *str = ecs_strbuf_get(&b);
test_assert(str == NULL);
}
void Strbuf_append_zerocopy(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b, "Foo");
ecs_strbuf_appendstr_zerocpy(&b, ecs_os_strdup("Bar"));
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooBar");
ecs_os_free(str);
}
void Strbuf_append_zerocopy_const(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b, "Foo");
ecs_strbuf_appendstr_zerocpy_const(&b, "Bar");
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooBar");
ecs_os_free(str);
}
void Strbuf_append_zerocopy_only(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendstr_zerocpy(&b, ecs_os_strdup("Bar"));
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "Bar");
ecs_os_free(str);
}
void Strbuf_reset(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b, "Foo");
ecs_strbuf_appendstr(&b, "Bar");
ecs_strbuf_reset(&b);
char *str = ecs_strbuf_get(&b);
test_assert(str == NULL);
}
void Strbuf_merge(void) {
ecs_strbuf_t b1 = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b1, "Foo");
ecs_strbuf_appendstr(&b1, "Bar");
ecs_strbuf_t b2 = ECS_STRBUF_INIT;
ecs_strbuf_appendstr(&b2, "Hello");
ecs_strbuf_appendstr(&b2, "World");
ecs_strbuf_mergebuff(&b1, &b2);
char *str = ecs_strbuf_get(&b1);
test_str(str, "FooBarHelloWorld");
ecs_os_free(str);
str = ecs_strbuf_get(&b2);
test_assert(str == NULL);
}
void Strbuf_app_buffer(void) {
char buf[256];
ecs_strbuf_t b = ECS_STRBUF_INIT;
b.buf = buf;
b.max = 256;
ecs_strbuf_appendstr(&b, "Foo");
ecs_strbuf_appendstr(&b, "Bar");
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "FooBar");
ecs_os_free(str);
}
void Strbuf_append_char(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendch(&b, 'a');
ecs_strbuf_appendch(&b, 'b');
ecs_strbuf_appendch(&b, 'c');
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "abc");
ecs_os_free(str);
}
void Strbuf_append_511_chars(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
for (int i = 0; i < 511; i ++) {
ecs_strbuf_appendch(&b, 'a' + (i % 26));
}
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
for (int i = 0; i < 511; i ++) {
test_assert(str[i] == ('a' + i % 26));
}
ecs_os_free(str);
}
void Strbuf_append_512_chars(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
for (int i = 0; i < 512; i ++) {
ecs_strbuf_appendch(&b, 'a' + (i % 26));
}
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
for (int i = 0; i < 512; i ++) {
test_assert(str[i] == ('a' + i % 26));
}
ecs_os_free(str);
}
void Strbuf_append_513_chars(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
for (int i = 0; i < 513; i ++) {
ecs_strbuf_appendch(&b, 'a' + (i % 26));
}
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
for (int i = 0; i < 513; i ++) {
test_assert(str[i] == ('a' + i % 26));
}
ecs_os_free(str);
}
void Strbuf_append_flt(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, 10.5, 0);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "10.5");
ecs_os_free(str);
}
void Strbuf_append_nan(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, NAN, 0);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "NaN");
ecs_os_free(str);
}
void Strbuf_append_inf(void) {
{
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, INFINITY, 0);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "Inf");
ecs_os_free(str);
}
{
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, -INFINITY, 0);
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "Inf");
ecs_os_free(str);
}
}
void Strbuf_append_nan_delim(void) {
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, NAN, '"');
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "\"NaN\"");
ecs_os_free(str);
}
void Strbuf_append_inf_delim(void) {
{
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, INFINITY, '"');
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "\"Inf\"");
ecs_os_free(str);
}
{
ecs_strbuf_t b = ECS_STRBUF_INIT;
ecs_strbuf_appendflt(&b, -INFINITY, '"');
char *str = ecs_strbuf_get(&b);
test_assert(str != NULL);
test_str(str, "\"Inf\"");
ecs_os_free(str);
}
}

View File

@@ -0,0 +1,420 @@
/* A friendly warning from bake.test
* ----------------------------------------------------------------------------
* This file is generated. To add/remove testcases modify the 'project.json' of
* the test project. ANY CHANGE TO THIS FILE IS LOST AFTER (RE)BUILDING!
* ----------------------------------------------------------------------------
*/
#include <collections.h>
// Testsuite 'Map'
void Map_setup(void);
void Map_count(void);
void Map_count_empty(void);
void Map_set_overwrite(void);
void Map_set_rehash(void);
void Map_get(void);
void Map_get_all(void);
void Map_get_empty(void);
void Map_get_unknown(void);
void Map_get_0_from_empty(void);
void Map_get_0_from_populated(void);
void Map_get_0_after_insert(void);
void Map_get_0_after_ensure(void);
void Map_iter(void);
void Map_iter_empty(void);
void Map_iter_null(void);
void Map_remove(void);
void Map_remove_empty(void);
void Map_remove_unknown(void);
void Map_remove_twice(void);
void Map_clear_empty(void);
void Map_clear_populated(void);
void Map_clear_empty_twice(void);
void Map_clear_populated_twice(void);
void Map_populate_after_clear(void);
void Map_randomized_insert(void);
void Map_randomized_remove(void);
void Map_randomized_insert_large(void);
void Map_randomized_remove_large(void);
void Map_randomized_after_clear(void);
// Testsuite 'Sparse'
void Sparse_setup(void);
void Sparse_add_1(void);
void Sparse_add_1_to_empty(void);
void Sparse_add_1_chunk_size_1(void);
void Sparse_add_n(void);
void Sparse_add_n_chunk_size_1(void);
void Sparse_remove(void);
void Sparse_remove_first(void);
void Sparse_remove_last(void);
void Sparse_remove_all(void);
void Sparse_remove_all_n_chunks(void);
void Sparse_clear_1(void);
void Sparse_clear_empty(void);
void Sparse_clear_n(void);
void Sparse_clear_n_chunks(void);
void Sparse_add_after_clear(void);
void Sparse_create_delete(void);
void Sparse_create_delete_2(void);
void Sparse_count_of_null(void);
void Sparse_try_low_after_ensure_high(void);
void Sparse_is_alive_low_after_ensure_high(void);
void Sparse_remove_low_after_ensure_high(void);
// Testsuite 'Strbuf'
void Strbuf_setup(void);
void Strbuf_append(void);
void Strbuf_appendstr(void);
void Strbuf_appendstrn(void);
void Strbuf_appendstr_null(void);
void Strbuf_append_list(void);
void Strbuf_append_nested_list(void);
void Strbuf_large_str(void);
void Strbuf_empty_str(void);
void Strbuf_append_zerocopy(void);
void Strbuf_append_zerocopy_only(void);
void Strbuf_append_zerocopy_const(void);
void Strbuf_reset(void);
void Strbuf_merge(void);
void Strbuf_app_buffer(void);
void Strbuf_append_char(void);
void Strbuf_append_511_chars(void);
void Strbuf_append_512_chars(void);
void Strbuf_append_513_chars(void);
void Strbuf_append_flt(void);
void Strbuf_append_nan(void);
void Strbuf_append_inf(void);
void Strbuf_append_nan_delim(void);
void Strbuf_append_inf_delim(void);
bake_test_case Map_testcases[] = {
{
"count",
Map_count
},
{
"count_empty",
Map_count_empty
},
{
"set_overwrite",
Map_set_overwrite
},
{
"set_rehash",
Map_set_rehash
},
{
"get",
Map_get
},
{
"get_all",
Map_get_all
},
{
"get_empty",
Map_get_empty
},
{
"get_unknown",
Map_get_unknown
},
{
"get_0_from_empty",
Map_get_0_from_empty
},
{
"get_0_from_populated",
Map_get_0_from_populated
},
{
"get_0_after_insert",
Map_get_0_after_insert
},
{
"get_0_after_ensure",
Map_get_0_after_ensure
},
{
"iter",
Map_iter
},
{
"iter_empty",
Map_iter_empty
},
{
"iter_null",
Map_iter_null
},
{
"remove",
Map_remove
},
{
"remove_empty",
Map_remove_empty
},
{
"remove_unknown",
Map_remove_unknown
},
{
"remove_twice",
Map_remove_twice
},
{
"clear_empty",
Map_clear_empty
},
{
"clear_populated",
Map_clear_populated
},
{
"clear_empty_twice",
Map_clear_empty_twice
},
{
"clear_populated_twice",
Map_clear_populated_twice
},
{
"populate_after_clear",
Map_populate_after_clear
},
{
"randomized_insert",
Map_randomized_insert
},
{
"randomized_remove",
Map_randomized_remove
},
{
"randomized_insert_large",
Map_randomized_insert_large
},
{
"randomized_remove_large",
Map_randomized_remove_large
},
{
"randomized_after_clear",
Map_randomized_after_clear
}
};
bake_test_case Sparse_testcases[] = {
{
"add_1",
Sparse_add_1
},
{
"add_1_to_empty",
Sparse_add_1_to_empty
},
{
"add_1_chunk_size_1",
Sparse_add_1_chunk_size_1
},
{
"add_n",
Sparse_add_n
},
{
"add_n_chunk_size_1",
Sparse_add_n_chunk_size_1
},
{
"remove",
Sparse_remove
},
{
"remove_first",
Sparse_remove_first
},
{
"remove_last",
Sparse_remove_last
},
{
"remove_all",
Sparse_remove_all
},
{
"remove_all_n_chunks",
Sparse_remove_all_n_chunks
},
{
"clear_1",
Sparse_clear_1
},
{
"clear_empty",
Sparse_clear_empty
},
{
"clear_n",
Sparse_clear_n
},
{
"clear_n_chunks",
Sparse_clear_n_chunks
},
{
"add_after_clear",
Sparse_add_after_clear
},
{
"create_delete",
Sparse_create_delete
},
{
"create_delete_2",
Sparse_create_delete_2
},
{
"count_of_null",
Sparse_count_of_null
},
{
"try_low_after_ensure_high",
Sparse_try_low_after_ensure_high
},
{
"is_alive_low_after_ensure_high",
Sparse_is_alive_low_after_ensure_high
},
{
"remove_low_after_ensure_high",
Sparse_remove_low_after_ensure_high
}
};
bake_test_case Strbuf_testcases[] = {
{
"append",
Strbuf_append
},
{
"appendstr",
Strbuf_appendstr
},
{
"appendstrn",
Strbuf_appendstrn
},
{
"appendstr_null",
Strbuf_appendstr_null
},
{
"append_list",
Strbuf_append_list
},
{
"append_nested_list",
Strbuf_append_nested_list
},
{
"large_str",
Strbuf_large_str
},
{
"empty_str",
Strbuf_empty_str
},
{
"append_zerocopy",
Strbuf_append_zerocopy
},
{
"append_zerocopy_only",
Strbuf_append_zerocopy_only
},
{
"append_zerocopy_const",
Strbuf_append_zerocopy_const
},
{
"reset",
Strbuf_reset
},
{
"merge",
Strbuf_merge
},
{
"app_buffer",
Strbuf_app_buffer
},
{
"append_char",
Strbuf_append_char
},
{
"append_511_chars",
Strbuf_append_511_chars
},
{
"append_512_chars",
Strbuf_append_512_chars
},
{
"append_513_chars",
Strbuf_append_513_chars
},
{
"append_flt",
Strbuf_append_flt
},
{
"append_nan",
Strbuf_append_nan
},
{
"append_inf",
Strbuf_append_inf
},
{
"append_nan_delim",
Strbuf_append_nan_delim
},
{
"append_inf_delim",
Strbuf_append_inf_delim
}
};
static bake_test_suite suites[] = {
{
"Map",
Map_setup,
NULL,
29,
Map_testcases
},
{
"Sparse",
Sparse_setup,
NULL,
21,
Sparse_testcases
},
{
"Strbuf",
Strbuf_setup,
NULL,
23,
Strbuf_testcases
}
};
int main(int argc, char *argv[]) {
return bake_test_run("collections", argc, argv, suites, 3);
}

View File

@@ -0,0 +1,9 @@
#include <collections.h>
void install_test_abort(void) {
ecs_os_set_api_defaults();
ecs_os_api_t os_api = ecs_os_api;
os_api.abort_ = test_abort;
ecs_os_set_api(&os_api);
ecs_log_set_level(-5);
}