Files
libcdsb/tests/src/test.c
T
2022-06-09 11:39:19 +03:00

104 lines
2.8 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdarg.h>
#include <string.h>
#include "../include/test.h"
#include "../include/time.h"
#include "../include/random.h"
static TIMER GLOBAL_TIMER;
static const char* TEST_NAME = 0;
static void test_uload() __attribute__((destructor));
static void test_uload() {
if (TEST_NAME) {
timer_stop(&GLOBAL_TIMER);
puts("");
put_separator(0);
printf("\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is end.\nExecusion time: \e[m\e[32m%.6Lf sec\e[m\n", TEST_NAME, timer_value(&GLOBAL_TIMER));
put_separator(0);
timer_free(&GLOBAL_TIMER);
}
}
void test_init(int argc, char** argv) {
timer_init(&GLOBAL_TIMER);
timer_start(&GLOBAL_TIMER);
TEST_NAME = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
put_separator(0);
printf("\e[H\e[J\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME);
put_separator(0);
random_init(argc, argv);
puts("");
}
void hp_printf(unsigned int hpos, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\e[%dG", hpos+1);
vprintf(format, args);
va_end(args);
}
void put_separator(unsigned int hpos) {
printf("\e[%dG\e[37;2m=== === === === === === === ===\e[m\n", hpos+1);
}
void print_container_values_prefix(const char* name, const char* prefix, unsigned int hpos) {
if (prefix) {
printf("\e[%dG\e[36m%s %s values:\e[m\n", hpos+1, name, prefix);
} else {
printf("\e[%dG\e[36m%s values:\e[m\n", hpos+1, name);
}
}
void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type, unsigned int hpos) {
if (index) {
printf("\e[%dG\e[32;1m%5ld: \e[m", hpos+1, *index);
} else {
printf("\e[%dG ", hpos+1);
}
printf("\e[31m%24s\e[m", libcdsb_vtype_stringify(value, type));
if (print_type) {
printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(type));
}
puts("");
}
void print_container_info(const char* name, const char* el_name, const vtype* type, ssize_t size, ssize_t nmemb, unsigned int hpos) {
if (!el_name) el_name = "elements";
if (type) {
printf("\e[%dG\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", hpos+1, name, libcdsb_vtype_name(*type));
}
if (size >= 0 || nmemb >= 0) {
printf("\e[%dG\e[36m%s consists of \e[m", hpos+1, name);
}
if (size >= 0) {
printf("\e[32m%ld\e[m \e[36m%s", size, el_name);
if (nmemb >= 0) {
printf(" (\e[m\e[32m%ld bytes\e[m\e[36m)\e[m\n", nmemb);
} else puts("\e[m");
} else if (nmemb >= 0) {
printf("\e[32m%ld bytes\e[m\n", nmemb);
}
}