Update array tests

This commit is contained in:
Gregory Lirent 2022-06-03 14:09:23 +03:00
parent 315e35e7f6
commit ed737e3177
3 changed files with 125 additions and 80 deletions

29
tests/src/array/change.c Normal file
View File

@ -0,0 +1,29 @@
/* 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_array y;
vtype_array x = array_random(8);
array_info(&x);
array_sort(&x);
array_print(&x, 0);
array_slice(&y, &x, 2, 4, 1);
array_print(&x, "source");
array_print(&y, "cutted");
array_remove_by_index(&x, -1);
array_remove_by_index(&y, -1);
array_print(&x, "source (last removed)");
array_print(&y, "cutted (last removed)");
array_free(&x);
array_free(&y);
}

View File

@ -1,74 +1,20 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../../include/extra/vtype.h"
#include "plug.h"
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_find_minimal(const vtype_array* x) {
array_print(x, 0);
for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) {
ssize_t index = array_indexof(x, v);
if (index >= 0) {
printf("\e[36mValue \e[m\e[31m%lu\e[m\e[36m was found at the position \e[m\e[32;1m%ld\e[m\n", v, index);
break;
}
}
put_separator();
}
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 short n = random_int16();
vtype_array x;
array_init_random(&x);
for (int i = 0; i < 10; ++i) {
array_push_random(&x);
}
return x;
}
int main(int argc, char** argv) {
test_init(argc, argv);
@ -76,27 +22,17 @@ int main(int argc, char** argv) {
do {
array_free(&x);
x = array_random();
x = array_random(8);
} while (x.type >= VTYPE_FLOAT);
printf("Array initialized with type `%s`;\n", libcdsb_vtype_name(x.type));
printf("Array has %lu elements (%lu bytes);\n", array_size(&x), array_nmemb(&x));
puts("");
for (int i = 0; i < x.size; ++i) {
printf("%s\n", libcdsb_vtype_stringify(array_at(&x, i), x.type));
}
for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) {
ssize_t index = array_indexof(&x, v);
if (index >= 0) {
printf("Value %lu was found on the position %ld\n", v, index);
break;
}
}
array_info(&x);
array_find_minimal(&x);
array_reverse(&x);
array_print(&x, "reversed");
array_sort(&x);
array_print(&x, "sorted");
array_free(&x);
}

View File

@ -2,6 +2,7 @@
/* Copyright © 2022 Gregory Lirent */
#include <string.h>
#include "../../../include/extra/vtype.h"
#include "../../../include/array.h"
#include "../../include/random.h"
@ -27,3 +28,82 @@ extern void string_copy_init(vtype_string* x, const vtype_string* s) { memset(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();
}