42_libasm/test/test.c

214 lines
5.2 KiB
C
Raw Normal View History

2024-08-28 09:47:22 -04:00
#include <stdio.h>
#include "../include/libasm.h"
#include "string.h"
2024-09-05 07:15:14 -04:00
#include "../include/list.h"
#include <time.h>
#include <stdlib.h>
2024-08-28 09:47:22 -04:00
2024-08-29 11:24:48 -04:00
void test_size_t(size_t expected_value, size_t value)
2024-08-28 09:47:22 -04:00
{
if (expected_value != value)
printf("[FAILED] %zu != %zu", expected_value, value);
else
printf("[OK]");
}
2024-08-29 11:24:48 -04:00
void test_int(int expected_value, int value)
2024-08-28 09:47:22 -04:00
{
2024-08-29 11:24:48 -04:00
if (expected_value != value)
printf("[FAILED] %d != %d", expected_value, value);
2024-08-28 09:47:22 -04:00
else
printf("[OK]");
}
2024-08-29 11:24:48 -04:00
void test_str(const char *expected_value, const char *value)
2024-08-28 09:47:22 -04:00
{
2024-09-02 08:58:05 -04:00
if (strcmp(expected_value, value))
2024-08-29 11:24:48 -04:00
printf("[FAILED] %s != %s", expected_value, value);
2024-08-28 09:47:22 -04:00
else
printf("[OK]");
}
void multiple_test_int(size_t (*normal_func)(const char*), size_t (*own_func)(const char*), const char **values)
{
for (size_t i = 0; values[i] != NULL; i++)
{
printf("test: %s ", values[i]);
test_int(normal_func(values[i]), own_func(values[i]));
printf("\n");
}
}
2024-08-29 11:24:48 -04:00
void multiple_test_int_2(int (*normal_func)(const char *, const char *), int (*own_func)(const char *, const char *), const char *values[][2])
{
for (size_t i = 0; values[i][0] != NULL; i++)
{
printf("test: %s==%s", values[i][0], values[i][1]);
test_int(normal_func(values[i][0], values[i][1]), own_func(values[i][0], values[i][1]));
printf("\n");
}
}
2024-08-28 09:47:22 -04:00
2024-09-02 09:29:06 -04:00
void multiple_test_strcpy(char *(*own_func)(char *, const char *), const char * const *values)
2024-09-02 08:58:05 -04:00
{
char tmp[4096];
for (size_t i = 0; values[i] != NULL; i++)
{
printf("test: %s", values[i]);
2024-09-02 09:29:06 -04:00
test_size_t((size_t) strcpy(tmp, values[i]), (size_t) own_func(tmp, values[i]));
2024-09-02 08:58:05 -04:00
test_str(values[i], tmp);
printf("\n");
}
}
2024-09-05 07:15:14 -04:00
void multiple_test_atoi_base(char *(*own_func)(char *, const char *), const char * const *values[2])
{
char tmp[4096];
for (size_t i = 0; values[i] != NULL; i++)
{
printf("test: %s", values[i]);
printf("\n");
}
}
void destroy_list(t_list *root, void *(destroy_data)(void *))
{
t_list *current = root;
t_list *prev;
while (current != NULL)
{
prev = current;
if (current->data)
destroy_data(current->data);
current = current->next;
free(prev);
}
}
t_list *create_list(size_t len)
{
t_list *root;
t_list *current = root;
root = malloc(sizeof(t_list));
if (root == NULL)
return NULL;
current = root;
for (size_t i = 0; i < len; i++)
{
current->next = malloc(sizeof(t_list));
2024-09-05 12:14:20 -04:00
current->data = (void *) i;
2024-09-05 07:15:14 -04:00
if (current->next == NULL)
{
destroy_list(current, NULL);
return NULL;
}
current = current->next;
}
current->next = NULL;
return root;
}
#define nb_random_test 10
#define nb_specific_test 1
void multiple_test_list_size()
{
size_t len;
2024-09-05 12:14:20 -04:00
t_list *root;
2024-09-05 07:15:14 -04:00
size_t specific_size[nb_specific_test] = {0};
srand(time(NULL));
for (size_t i = 0; i < nb_random_test + nb_specific_test; i++)
{
len = i < nb_specific_test ? specific_size[i] : rand() % 100;
root = create_list(len);
if (root == NULL)
return;
test_int(len, ft_list_size(root));
printf("\n");
}
}
2024-09-05 12:14:20 -04:00
void multiple_test_push_front()
{
size_t len;
t_list *root;
srand(time(NULL));
for (size_t i = 0; i < nb_random_test; i++)
{
len = rand() % 100;
root = create_list(len);
if (root == NULL)
return;
ft_list_push_front(&root, (void *) 101);
printf("data: ");
test_size_t((size_t) root->data, 101);
printf(" data-next: ");
test_size_t((size_t) root->next->data, 0);
printf(" list-size: ");
test_int(len + 1, ft_list_size(root));
printf("\n");
}
}
2024-09-05 12:39:38 -04:00
void multiple_test_strdup(const char * const *values)
{
char *tmp;
for (size_t i = 0; values[i] != NULL; i++)
{
tmp = ft_strdup(values[i]);
test_str(tmp, values[i]);
free(tmp);
printf("\n");
}
}
2024-08-28 09:47:22 -04:00
int main()
{
2024-09-02 08:58:05 -04:00
printf("STRLEN\n");
2024-08-28 09:47:22 -04:00
const char *strlen_tests[] = {"yo", "", "bonjour", "co\0fgf", NULL};
2024-09-02 08:58:05 -04:00
multiple_test_int(strlen, ft_strlen, strlen_tests);
printf("\n");
2024-08-28 09:47:22 -04:00
2024-09-02 08:58:05 -04:00
printf("STRCMP\n");
2024-08-29 11:24:48 -04:00
const char *strcmp_tests[][2] = {{"w", "z"}, {"bonjour", "bonjour"}, {"", ""}, {"bonjour", "salut"}, {"co\0fgf", "co\0fgf"}, NULL};
2024-09-02 08:58:05 -04:00
multiple_test_int_2(strcmp, ft_strcmp, strcmp_tests);
printf("\n");
2024-08-28 09:47:22 -04:00
2024-09-02 08:58:05 -04:00
printf("STRCPY\n");
const char *strcpy_tests[] = {"yo", "", "bonjour", "co\0fgf", NULL};
multiple_test_strcpy(ft_strcpy, strcpy_tests);
printf("\n");
2024-09-02 11:22:08 -04:00
2024-09-05 12:39:38 -04:00
printf("STRDUP\n");
const char *strdup_tests[] = {"yo", "", "bonjour", "co\0fgf", NULL};
multiple_test_strdup(strdup_tests);
printf("\n");
2024-09-05 07:15:14 -04:00
/*
printf("ATOI_BASE\n");
const char *atoi_base_tests[][2] = {{"0123", ""}, {"0123", "0"}, {"0123", "0123456789"}, {"bonjour", "bonjour"}, {"", ""}, {"bonjour", "salut"}, {"co\0fgf", "co\0fgf"}, NULL};
multiple_test_int_2(strcmp, ft_strcmp, strcmp_tests);
printf("\n");*/
printf("FT_LIST_SIZE\n");
multiple_test_list_size();
printf("\n");
2024-09-05 12:14:20 -04:00
printf("FT_LIST_PUSH_FRONT\n");
multiple_test_push_front();
printf("\n");
2024-08-28 09:47:22 -04:00
}