Update list tests

This commit is contained in:
Gregory Lirent 2022-06-09 11:39:37 +03:00
parent c6cc575759
commit 760c04fb49
6 changed files with 314 additions and 108 deletions

View File

@ -6,19 +6,18 @@
int main(int argc, char** argv) {
test_init(argc, argv);
vtype_list x;
list_init(&x);
for (int i = 0, n = 12 + random_boolean(); i < n; ++i) {
list_push_random(&x, random_boolean());
}
list_sort(&x);
list_info(&x);
list_print(&x, "sorted");
list_reverse(&x);
list_print(&x, "reversed");
list_t x = { .first = 0, .last = 0 };
list_t y = { .first = 0, .last = 0 };
list_free(&x);
visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12);
visual_extend(&x, &y);
visual_sort2(&x, &y);
visual_remove2(&x, &y);
list_free(&y);
list_free(&y);
}

View File

@ -9,69 +9,17 @@
#include "../../../src/__internal/vnode.h"
vtype_string* string_duplicate(const vtype_string* x) { return 0; }
vtype_array* array_duplicate (const vtype_array* x) { return 0; }
vtype_map* map_duplicate (const vtype_map* x) { return 0; }
vtype_set* vset_duplicate (const vtype_set* x) { return 0; }
extern void list_print(list_t* x, const char* prefix, unsigned int hpos);
extern void list_info (list_t* x, unsigned int hpos);
void string_free(vtype_string* x) {}
void array_free (vtype_array* x) {}
void map_free (vtype_map* x) {}
void vset_free (vtype_set* x) {}
extern void list_push_random(list_t* x, _Bool silent, unsigned int hpos);
extern void list_remove_random(list_t* x, _Bool silent, unsigned int hpos);
int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); }
int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); }
int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); }
int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); }
extern void visual_push(list_t* x, size_t n);
extern void visual_sort(list_t* x);
extern void visual_remove(list_t* x);
static void list_push_random(vtype_list* x, vtype_bool front) {
if (!front) switch (random_uint8()%13) {
default:
case 0: list_push_back(x, random_boolean()); break;
case 1: list_push_back(x, random_uint8()); break;
case 2: list_push_back(x, random_uint16()); break;
case 3: list_push_back(x, random_uint32()); break;
case 4: list_push_back(x, random_uint64()); break;
case 5: list_push_back(x, random_int8()); break;
case 6: list_push_back(x, random_int16()); break;
case 7: list_push_back(x, random_int32()); break;
case 8: list_push_back(x, random_int64()); break;
case 9: list_push_back(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break;
case 10: list_push_back(x, random_float()); break;
case 11: list_push_back(x, random_double()); break;
case 12: list_push_back(x, random_ldouble()); break;
} else switch (random_uint8()%13) {
default:
case 0: list_push_front(x, random_boolean()); break;
case 1: list_push_front(x, random_uint8()); break;
case 2: list_push_front(x, random_uint16()); break;
case 3: list_push_front(x, random_uint32()); break;
case 4: list_push_front(x, random_uint64()); break;
case 5: list_push_front(x, random_int8()); break;
case 6: list_push_front(x, random_int16()); break;
case 7: list_push_front(x, random_int32()); break;
case 8: list_push_front(x, random_int64()); break;
case 9: list_push_front(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break;
case 10: list_push_front(x, random_float()); break;
case 11: list_push_front(x, random_double()); break;
case 12: list_push_front(x, random_ldouble()); break;
}
}
static int list_node_print(void* v, ssize_t i, vtype t, void* _) {
print_container_value(&i, v, t, 1);
return 0;
}
static void list_print(const vtype_list* x, const char* prefix) {
print_container_values_prefix("List", prefix);
list_foreach(x, 0, list_node_print);
put_separator();
}
static void list_info(const vtype_list* x) {
print_container_info("List", "nodes", 0, list_size(x), -1);
put_separator();
}
extern void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1);
extern void visual_remove2(list_t* x0, list_t* x1);
extern void visual_sort2(list_t* x0, list_t* x1);
extern void visual_extend(list_t* x, list_t* s);

View File

@ -1,32 +0,0 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "plug.h"
int main(int argc, char** argv) {
test_init(argc, argv);
vtype_list x;
list_init(&x);
for (int i = 0, c = random_uint8()%12; i < 12; ++i) {
if (i == c) {
list_push_back(&x, 0);
} else list_push_random(&x, random_boolean());
}
list_info(&x);
list_print(&x, 0);
for (int i = 0, n = list_countof(&x, 0); i < n; ++i) {
int index = list_indexof(&x, 0);
list_remove(&x, 0);
printf("\e[36mRemove element with index \e[m\e[32;1m%d\e[m\e[36m from list\e[m\n", index);
}
put_separator();
list_print(&x, "cleaned");
list_free(&x);
}

188
tests/src/list/src/io.c Normal file
View File

@ -0,0 +1,188 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int list_node_print(void* v, ssize_t i, vtype t, void* _) {
print_container_value(&i, v, t, 1, *(unsigned int*)_);
return 0;
}
void list_print(list_t* x, const char* prefix, unsigned int hpos) {
print_container_values_prefix("List", prefix, hpos);
list_foreach(x, &hpos, list_node_print);
}
void list_info(list_t* x, unsigned int hpos) {
print_container_info("List", "nodes", 0, list_size(x), -1, hpos);
}
void visual_push(list_t* x, size_t n) {
for (int i = 0; i < n; ++i) {
fputs("\e[s", stdout);
list_push_random(x, 0, 0);
list_info(x, 0);
list_print(x, 0, 0);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_sort(list_t* x) {
puts("\e[s\e[36mTry to sort list values:\e[m\n\n");
put_separator(0);
list_sort(x);
list_info(x, 0);
list_print(x, "sorted", 0);
psleep(900000);
fputs("\e[u\e[J", stdout);
puts("\e[s\e[36mTry to reverse list values:\e[m\n\n");
put_separator(0);
list_reverse(x);
list_info(x, 0);
list_print(x, "reversed", 0);
psleep(900000);
fputs("\e[u\e[J", stdout);
}
void visual_remove(list_t* x) {
while (x->first) {
fputs("\e[s", stdout);
list_remove_random(x, 0, 0);
list_info(x, 0);
list_print(x, 0, 0);
psleep(100000);
if (!0) fputs("\e[u\e[J", stdout);
}
}
void visual_push2(list_t* x0, size_t n0, list_t* x1, size_t n1) {
for (;n0 || n1;) {
fputs("\e[s", stdout);
if (n0) {
list_push_random(x0, 0, 0);
--n0;
} else {
puts("\n\n");
put_separator(0);
}
fputs("\e[u\e[s", stdout);
if (n1) {
list_push_random(x1, 0, 60);
--n1;
} else {
puts("\n\n");
put_separator(60);
}
list_info(x0, 0);
list_print(x0, 0, 0);
fputs("\e[u\e[s\e[4E", stdout);
list_info(x1, 60);
list_print(x1, 0, 60);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_remove2(list_t* x0, list_t* x1) {
for (;x0->first || x1->first;) {
fputs("\e[s", stdout);
if (x0->first) {
list_remove_random(x0, 0, 0);
} else {
puts("\n\n");
put_separator(0);
}
fputs("\e[u\e[s", stdout);
if (x1->first) {
list_remove_random(x1, 0, 60);
} else {
puts("\n\n");
put_separator(60);
}
list_info(x0, 0);
list_print(x0, 0, 0);
fputs("\e[u\e[s\e[4E", stdout);
list_info(x1, 60);
list_print(x1, 0, 60);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_sort2(list_t* x0, list_t* x1) {
printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to sort list values:", "Try to sort list values:");
printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
list_sort(x0);
list_sort(x1);
list_info(x0, 0);
list_print(x0, "sorted", 0);
fputs("\e[u\e[s\e[4E", stdout);
list_info(x1, 60);
list_print(x1, "sorted", 60);
psleep(900000);
fputs("\e[u\e[J", stdout);
printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to reverse list values:", "Try to reverse list values:");
printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
list_reverse(x0);
list_reverse(x1);
list_info(x0, 0);
list_print(x0, "reversed", 0);
fputs("\e[u\e[s\e[4E", stdout);
list_info(x1, 60);
list_print(x1, "reversed", 60);
psleep(900000);
fputs("\e[u\e[J", stdout);
}
void visual_extend(list_t* x, list_t* s) {
puts("\e[s\e[36mTry to extend list:\e[m\n\n");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
list_info(x, 0);
list_print(x, "(dest)", 0);
fputs("\e[u\e[s\e[4E", stdout);
list_info(s, 60);
list_print(s, "(src)", 60);
psleep(900000);
fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout);
list_extend(x, s);
puts("");
put_separator(0);
list_info(x, 0);
list_print(x, "(dest)", 0);
psleep(900000);
fputs("\e[u\e[J", stdout);
}

20
tests/src/list/src/plug.c Normal file
View File

@ -0,0 +1,20 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../../../src/__internal/include.h"
#include "../../../include/random.h"
vtype_string* string_duplicate(const vtype_string* x) { return 0; }
vtype_array* array_duplicate (const vtype_array* x) { return 0; }
vtype_map* map_duplicate (const vtype_map* x) { return 0; }
vtype_set* vset_duplicate (const vtype_set* x) { return 0; }
void string_free(vtype_string* x) {}
void array_free (vtype_array* x) {}
void map_free (vtype_map* x) {}
void vset_free (vtype_set* x) {}
int string_compare(const vtype_string* s0, const vtype_string* s1) { return random_int8(); }
int array_compare (const vtype_array* s0, const vtype_array* s1) { return random_int8(); }
int map_compare (const vtype_map* s0, const vtype_map* s1) { return random_int8(); }
int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); }

View File

@ -0,0 +1,83 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int remove_callback(void* v, ssize_t i, vtype t, void* _) {
struct { list_t* x; _Bool s; unsigned int p; } *x = _;
if (!x->s) {
print_container_value(0, v, t, 1, x->p);
}
if (libcdsb_list_find(x->x, v, t, 0, 0, 1, 1)) {
return -2;
}
return 0;
}
void list_push_random(list_t* x, _Bool silent, unsigned int hpos) {
value_t v = random_value();
_Bool r;
if (random_boolean()) {
if (!silent) {
printf("\e[%dG\e[36mTry to push value to back of the list:\e[m\n", hpos+1);
}
r = libcdsb_list_update(x, -1, v.value, v.type, 1);
} else if (random_boolean()) {
if (!silent) {
printf("\e[%dG\e[36mTry to push value to front of the list:\e[m\n", hpos+1);
}
r = libcdsb_list_update(x, -1, v.value, v.type, 1);
} else {
ssize_t i = list_size(x);
i = random_uint64()% (i ? i : 1);
if (random_boolean()) i = ~i + 1;
if (!silent) {
printf("\e[%dG\e[36mTry to change value with index \e[32;1m%ld\e[36m into list:\e[m\n", hpos+1, i);
}
r = libcdsb_list_update(x, i, v.value, v.type, 0);
}
if (!silent) {
print_container_value(0, v.value, v.type, 1, hpos);
printf("\e[%dG%s\n", hpos+1, r ? "\e[32;1mSUCCESS\e[m" : "\e[31;1mFAILURE\e[m");
put_separator(hpos);
}
}
void list_remove_random(list_t* x, _Bool silent, unsigned int hpos) {
size_t n = list_size(x);
ssize_t i = random_uint64()%n;
if (random_boolean()) i = ~i + 1;
if (random_boolean()) {
if (!silent) {
printf("\e[%dG\e[36mTry to remove value from list by index:\e[m\n", hpos+1);
print_container_value(0, &i, (sizeof(ssize_t) == 8) ? VTYPE_INT64 : VTYPE_INT32, 0, hpos);
}
switch (list_remove_by_index(x, i)) {
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;
default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break;
}
} else {
struct { list_t* x; _Bool s; unsigned int p; } v = { .x = x, .s = silent, .p = hpos };
if (!silent) printf("\e[%dG\e[36mTry to remove value from list:\e[m\n", hpos+1);
switch (libcdsb_list_get(x, i, &v, remove_callback, 0)) {
case 0: if (!silent) printf("\e[%dG\e[32;1mSUCCESS\e[m\n", hpos+1); break;
case 2: if (!silent) printf("\n\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break;
default: if (!silent) printf("\e[%dG\e[32;1mFAILURE\e[m\n", hpos+1); break;
}
}
put_separator(hpos);
}