/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include #include "../../../include/extra/vtype.h" #include "../../../include/extra/array.h" #include "../../include/random.h" #include "../../include/test.h" #include "../../include/time.h" vtype_string* string_duplicate(const vtype_string* x) { return 0; } vtype_list* list_duplicate (const vtype_list* 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 list_free (vtype_list* 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 list_compare (const vtype_list* s0, const vtype_list* 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 string_copy_init(vtype_string* x, const vtype_string* s) { memset(x, 0, sizeof(*x)); } extern void list_copy_init (vtype_list* x, const vtype_list* s) { memset(x, 0, sizeof(*x)); } extern void map_copy_init (vtype_map* x, const vtype_map* s) { memset(x, 0, sizeof(*x)); } extern void vset_copy_init (vtype_set* x, const vtype_set* s) { memset(x, 0, sizeof(*x)); } static void array_push_random_value(vtype_array* x) { switch (random_uint8()%10) { default: case 0: array_push(x, random_boolean()); break; case 1: array_push(x, random_uint8()); break; case 2: array_push(x, random_uint16()); break; case 3: array_push(x, random_uint32()); break; case 4: array_push(x, random_uint64()); break; case 5: array_push(x, random_int8()); break; case 6: array_push(x, random_int16()); break; case 7: array_push(x, random_int32()); break; case 8: array_push(x, random_int64()); break; case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; } } static void array_push_random_float(vtype_array* x) { switch (random_uint8()%3) { default: case 0: array_push(x, random_float()); break; case 1: array_push(x, random_double()); break; case 2: array_push(x, random_ldouble()); break; } } static void array_push_random(vtype_array* x) { if (x->type < VTYPE_FLOAT) { array_push_random_value(x); } else array_push_random_float(x); } static void array_init_random(vtype_array* x) { switch (random_uint8()%13) { default: case 0: array_init(x, VTYPE_BOOLEAN); break; case 1: array_init(x, VTYPE_UINT8); break; case 2: array_init(x, VTYPE_UINT16); break; case 3: array_init(x, VTYPE_UINT32); break; case 4: array_init(x, VTYPE_UINT64); break; case 5: array_init(x, VTYPE_INT8); break; case 6: array_init(x, VTYPE_INT16); break; case 7: array_init(x, VTYPE_INT32); break; case 8: array_init(x, VTYPE_INT64); break; case 9: array_init(x, VTYPE_POINTER); break; case 10: array_init(x, VTYPE_FLOAT); break; case 11: array_init(x, VTYPE_DOUBLE); break; case 12: array_init(x, VTYPE_LDOUBLE); break; } } static vtype_array array_random(unsigned int n) { vtype_array x; array_init_random(&x); for (int i = 0; i < n; ++i) { array_push_random(&x); } return x; } static void array_print(const vtype_array* x, const char* prefix) { if (!prefix) puts("\e[36mArray values:\e[m"); else printf("\e[36mArray %s values:\e[m\n", prefix); for (int i = 0; i < x->size; ++i) { printf("\e[32;1m%5d: \e[m\e[31m%24s\e[m\n", i, libcdsb_vtype_stringify(array_at(x, i), x->type)); } put_separator(); } static void array_info(const vtype_array* x) { printf("\e[36mArray initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", libcdsb_vtype_name(x->type)); printf("Array has \e[m\e[32m%lu\e[m\e[36m elements (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", array_size(x), array_nmemb(x)); put_separator(); }