Update array tests

This commit is contained in:
Gregory Lirent 2022-06-09 22:02:38 +03:00
parent f804d4277c
commit 26a33062e7
6 changed files with 327 additions and 70 deletions

View File

@ -1,29 +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_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(&x, -1);
array_remove(&y, -1);
array_print(&x, "source (last removed)");
array_print(&y, "cutted (last removed)");
array_free(&x);
array_free(&y);
}

View File

@ -3,36 +3,21 @@
#include "plug.h"
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();
}
int main(int argc, char** argv) {
test_init(argc, argv);
vtype_array x = { .mem = 0 };
arr_t x = { .mem = 0, .size = 0, .type = random_uint8()%(VTYPE_LDOUBLE + 1) };
arr_t y = { .mem = 0, .size = 0, .type = x.type };
do {
array_free(&x);
x = array_random(8);
} while (x.type >= VTYPE_FLOAT);
visual_push(&x, (random_uint8()%17) + 16);
array_info(&x);
array_find_minimal(&x);
visual_slice(&x, &y);
visual_push2(&x, (random_uint8()%5) + 12, &y, (random_uint8()%5) + 12);
array_reverse(&x);
array_print(&x, "reversed");
visual_sort2(&x, &y);
visual_remove2(&x, &y);
array_sort(&x);
array_print(&x, "sorted");
array_free(&x);
array_free(&y);
array_free(&y);
}

View File

@ -9,27 +9,22 @@
#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) {}
extern void array_print(arr_t* x, const char* prefix, unsigned int hpos);
extern void array_info(arr_t* x, unsigned int hpos);
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)); }
extern void visual_push(arr_t* x, size_t n);
extern void visual_sort(arr_t* x);
extern void visual_remove(arr_t* x);
extern void visual_push2(arr_t* x0, size_t n0, arr_t* x1, size_t n1);
extern void visual_remove2(arr_t* x0, arr_t* x1);
extern void visual_sort2(arr_t* x0, arr_t* x1);
extern void visual_slice(arr_t* x, arr_t* s);
extern void array_push_random (arr_t* x, _Bool silent, unsigned int hpos);
extern void array_remove_random(arr_t* x, _Bool silent, unsigned int hpos);
/*
static void array_push_random(vtype_array* x) {
switch (random_uint8()%13) {
default:
@ -95,3 +90,4 @@ static void array_info(const vtype_array* x) {
print_container_info("Array", 0, &x->type, array_size(x), array_nmemb(x));
put_separator();
}
*/

197
tests/src/array/src/io.c Normal file
View File

@ -0,0 +1,197 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../plug.h"
static int array_value_print(void* v, ssize_t i, vtype t, void* _) {
print_container_value(&i, v, t, 1, *(unsigned int*)_);
return 0;
}
void array_print(arr_t* x, const char* prefix, unsigned int hpos) {
print_container_values_prefix("Array", prefix, hpos);
array_foreach(x, &hpos, array_value_print);
}
void array_info(arr_t* x, unsigned int hpos) {
print_container_info("Array", "nodes", 0, array_size(x), -1, hpos);
}
void visual_push(arr_t* x, size_t n) {
for (int i = 0; i < n; ++i) {
fputs("\e[s", stdout);
array_push_random(x, 0, 0);
array_info(x, 0);
array_print(x, 0, 0);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_sort(arr_t* x) {
puts("\e[s\e[36mTry to sort array values:\e[m\n\n");
put_separator(0);
array_sort(x);
array_info(x, 0);
array_print(x, "sorted", 0);
psleep(900000);
fputs("\e[u\e[J", stdout);
puts("\e[s\e[36mTry to reverse array values:\e[m\n\n");
put_separator(0);
array_reverse(x);
array_info(x, 0);
array_print(x, "reversed", 0);
psleep(900000);
fputs("\e[u\e[J", stdout);
}
void visual_remove(arr_t* x) {
while (x->size) {
fputs("\e[s", stdout);
array_remove_random(x, 0, 0);
array_info(x, 0);
array_print(x, 0, 0);
psleep(100000);
if (!0) fputs("\e[u\e[J", stdout);
}
}
void visual_push2(arr_t* x0, size_t n0, arr_t* x1, size_t n1) {
for (;n0 || n1;) {
fputs("\e[s", stdout);
if (n0) {
array_push_random(x0, 0, 0);
--n0;
} else {
puts("\n\n");
put_separator(0);
}
fputs("\e[u\e[s", stdout);
if (n1) {
array_push_random(x1, 0, 60);
--n1;
} else {
puts("\n\n");
put_separator(60);
}
array_info(x0, 0);
array_print(x0, 0, 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(x1, 60);
array_print(x1, 0, 60);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_remove2(arr_t* x0, arr_t* x1) {
for (;x0->size || x1->size;) {
fputs("\e[s", stdout);
if (x0->size) {
array_remove_random(x0, 0, 0);
} else {
puts("\n\n");
put_separator(0);
}
fputs("\e[u\e[s", stdout);
if (x1->size) {
array_remove_random(x1, 0, 60);
} else {
puts("\n\n");
put_separator(60);
}
array_info(x0, 0);
array_print(x0, 0, 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(x1, 60);
array_print(x1, 0, 60);
psleep(100000);
fputs("\e[u\e[J", stdout);
}
}
void visual_sort2(arr_t* x0, arr_t* x1) {
printf("\e[s\e[36m%-60s%s\e[m\n\n","Try to sort array values:", "Try to sort array values:");
printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
array_sort(x0);
array_sort(x1);
array_info(x0, 0);
array_print(x0, "sorted", 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(x1, 60);
array_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 array values:", "Try to reverse array values:");
printf("\e[32;1m%-60s%s\e[m\n", "SUCCESS", "SUCCESS");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
array_reverse(x0);
array_reverse(x1);
array_info(x0, 0);
array_print(x0, "reversed", 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(x1, 60);
array_print(x1, "reversed", 60);
psleep(900000);
fputs("\e[u\e[J", stdout);
}
void visual_slice(arr_t* x, arr_t* s) {
size_t n = array_size(x);
ssize_t i = random_uint64()%(n - 1);
n = n - (i + 1);
puts("\e[s\e[36mTry to slice array:\e[m\n\n");
printf("\e[37;2m%-60s%s\e[m\n", "=== === === === === === === ===", "=== === === === === === === ===");
array_info(x, 0);
array_print(x, "(src)", 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(s, 60);
array_print(s, "(dest)", 60);
psleep(900000);
array_slice(s, x, i, n, 1);
fputs("\e[u\e[s\e[2E\e[32;1mSUCCESS\e[m\e[J", stdout);
puts("");
put_separator(0);
array_info(x, 0);
array_print(x, "(src)", 0);
fputs("\e[u\e[s\e[4E", stdout);
array_info(s, 60);
array_print(s, "(dest)", 60);
psleep(900000);
fputs("\e[u\e[J", stdout);
}

View File

@ -0,0 +1,25 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <string.h>
#include "../../../../include/extra/vtype.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) { random_int8(); }
int list_compare (const vtype_list* s0, const vtype_list* s1) { random_int8(); }
int map_compare (const vtype_map* s0, const vtype_map* s1) { random_int8(); }
int vset_compare (const vtype_set* s0, const vtype_set* s1) { 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)); }

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 { arr_t* x; _Bool s; unsigned int p; } *x = _;
if (!x->s) print_container_value(0, v, t, 1, x->p);
if (libcdsb_array_find(x->x, v, t, 0, 0, 1, 1))
return -2;
return 0;
}
static int change_callback(void* v, ssize_t i, vtype t, void* _) {
struct { value_t v; _Bool s; unsigned int p; } *x = _;
while (x->v.type != t) { x->v = random_value(); }
memcpy(v, &x->v, vtype_size(t));
return 0;
}
void array_push_random(arr_t* x, _Bool silent, unsigned int hpos) {
struct { value_t v; _Bool s; unsigned int p; } _ = { .v = random_value(), .s = silent, .p = hpos };
_Bool r;
if (random_boolean()) {
if (!silent) {
printf("\e[%dG\e[36mTry to push value to back of the array:\e[m\n", hpos+1);
}
r = libcdsb_array_push(x, _.v.value, _.v.type) >= 0;
} else {
ssize_t i = array_size(x);
i = random_uint64() % (i ? i : 1);
i = random_boolean() ? ~i + 1 : i ;
if (!silent) {
printf("\e[%dG\e[36mTry to change value in the array with index \e[32;1m%ld\e[36m:\e[m\n", hpos+1, i);
}
r = array_get_by_index(x, i, &_, change_callback) == 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 array_remove_random(arr_t* x, _Bool silent, unsigned int hpos) {
size_t n = array_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 (array_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 { arr_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_array_get(x, i, &v, remove_callback, 0)) {
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;
}
}
put_separator(hpos);
}