libcdsb/tests/src/map/src/random.c
2022-06-10 21:17:07 +03:00

54 lines
1.7 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int remove_callback(const void* k, vtype kt, void* v, vtype vt, void* _) {
struct { size_t n; map_t* x; unsigned int hp; } *d = _;
if (!d->n--) {
print_container_value(0, k, kt, 0, d->hp);
if (libcdsb_map_find(d->x, k, kt, 0, 0, 1) == 0) {
printf("\e[%dG\e[32;1mSUCCESS\e[m\n", d->hp+1);
} else printf("\e[%dG\e[31;1mFAILURE\e[m\n", d->hp+1);
return -2;
}
return 0;
}
void map_push_random(map_t* x, _Bool silent, unsigned int hpos) {
value_t k = random_value();
value_t v = random_value();
if (!silent) {
printf("\e[%dG\e[36mUpdate value in map (\e[m\e[32;1m%s\e[m\e[36m) with key:\e[m\n", hpos+1, vtype_name(x->type));
print_container_value(0, k.value, k.type, 1, hpos);
}
if (libcdsb_map_update(x, k.value, k.type, v.value, v.type)) {
if (!silent) printf("\e[%dG\e[33;1mCHANGE\e[m\n", hpos+1);
} else if (!silent) printf("\e[%dG\e[32;1mINSERT\e[m\n", hpos+1);
if (!silent) put_separator(0);
}
void map_remove_random(map_t* x, _Bool silent, unsigned int hpos) {
struct { size_t n; map_t* x; unsigned int hp; } d = { .n = map_size(x), .x = x, .hp = hpos };
if (!silent)
printf("\e[%dG\e[36mTry to remove value from map (\e[m\e[32;1m%s\e[m\e[36m) by key:\e[m\n", hpos+1, libcdsb_vtype_name(x->type));
if (d.n) {
d.n = random_uint32()%d.n;
map_foreach(x, &d, remove_callback);
} else if (!silent) {
printf("\e[%dG\e[32;1m\nFAILURE\e[m\n", hpos+1);
}
if (!silent) put_separator(hpos);
}