Update list tests

This commit is contained in:
Gregory Lirent 2022-06-05 18:31:19 +03:00
parent a6a623bff6
commit 8774804289
2 changed files with 76 additions and 6 deletions

View File

@ -3,11 +3,22 @@
#include "plug.h" #include "plug.h"
vtype_list* list_duplicate(const vtype_list* x) { return 0; }
int list_compare(const vtype_list* s0, const vtype_list* s1) { return random_int8(); }
void list_free(vtype_list* x) {}
int main(int argc, char** argv) { int main(int argc, char** argv) {
test_init(argc, 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, 0);
}
list_sort(&x);
list_info(&x);
list_print(&x, "sorted");
list_reverse(&x);
list_print(&x, "reversed");
list_free(&x);
} }

View File

@ -1,12 +1,14 @@
/* This software is licensed by the MIT License, see LICENSE file */ /* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */ /* Copyright © 2022 Gregory Lirent */
#include "../../../include/list.h" #include "../../../include/extra/list.h"
#include "../../include/random.h" #include "../../include/random.h"
#include "../../include/test.h" #include "../../include/test.h"
#include "../../include/time.h" #include "../../include/time.h"
#include "../../../src/__internal/vnode.h"
vtype_string* string_duplicate(const vtype_string* x) { return 0; } vtype_string* string_duplicate(const vtype_string* x) { return 0; }
vtype_array* array_duplicate (const vtype_array* x) { return 0; } vtype_array* array_duplicate (const vtype_array* x) { return 0; }
vtype_map* map_duplicate (const vtype_map* x) { return 0; } vtype_map* map_duplicate (const vtype_map* x) { return 0; }
@ -21,3 +23,60 @@ int string_compare(const vtype_string* s0, const vtype_string* s1) { return rand
int array_compare (const vtype_array* s0, const vtype_array* 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 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(); } int vset_compare (const vtype_set* s0, const vtype_set* s1) { return random_int8(); }
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) {
printf("\e[32;1m%5ld: \e[m\e[31m%24s\e[m \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m\n", i,
libcdsb_vtype_stringify(v, t),
libcdsb_vtype_name(t));
return 0;
}
static void list_print(const vtype_list* x, const char* prefix) {
if (!prefix) puts("\e[36mList values:\e[m");
else printf("\e[36mList %s values:\e[m\n", prefix);
list_foreach(x, list_node_print);
put_separator();
}
static void list_info(const vtype_list* x) {
printf("List has \e[m\e[32m%lu\e[m\e[36m elements\e[m\n", list_size(x));
put_separator();
}