Files
libcdsb/tests/src/dict/src/random.c
T
2022-08-17 22:21:56 +03:00

54 lines
1.6 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; dict_t* x; unsigned int hp; } *d = _;
if (!d->n--) {
print_container_value(0, k, kt, 0, d->hp);
if (libcdsb_dict_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 dict_push_random(dict_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 dict with key:\e[m\n", hpos+1);
print_container_value(0, k.value, k.type, 1, hpos);
}
if (libcdsb_dict_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 dict_remove_random(dict_t* x, _Bool silent, unsigned int hpos) {
struct { size_t n; dict_t* x; unsigned int hp; } d = { .n = dict_size(x), .x = x, .hp = hpos };
if (!silent)
printf("\e[%dG\e[36mTry to remove value from dict by key:\e[m\n", hpos+1);
if (d.n) {
d.n = random_uint32()%d.n;
dict_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);
}