Files
libcdsb/tests/src/test.c
T

85 lines
2.4 KiB
C
Raw Normal View History

2022-06-02 21:41:06 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#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();
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();
timer_free(&GLOBAL_TIMER);
}
}
void put_separator() { puts("\e[37;2m=== === === === === === === ===\e[m"); }
2022-06-06 22:07:35 +03:00
void print_container_values_prefix(const char* name, const char* prefix) {
if (prefix) {
printf("\e[36m%s %s values:\e[m\n", name, prefix);
} else printf("\e[36m%s values:\e[m\n", name);
}
void print_container_value(const ssize_t* index, const void* value, const vtype type, _Bool print_type) {
if (index) {
printf("\e[32;1m%5ld: \e[m", *index);
} else fputs(" ", stdout);
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) {
if (!el_name) el_name = "elements";
if (type) {
printf("\e[36m%s initialized with type `\e[m\e[32;1m%s\e[m\e[36m`\n", name, libcdsb_vtype_name(*type));
}
if (size >= 0 || nmemb >= 0) {
printf("\e[36m%s consists of \e[m", 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);
}
}
2022-06-02 21:41:06 +03:00
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();
2022-06-03 14:09:11 +03:00
printf("\e[H\e[J\e[36mTest \"\e[m\e[32;1m%s\e[m\e[36m\" is loaded\e[m\n", TEST_NAME);
2022-06-02 21:41:06 +03:00
put_separator();
random_init(argc, argv);
puts("");
}