added bgoulard lib + made lib sources in their own directory in build/ for readability

This commit is contained in:
B.Goulard 2024-11-01 00:00:14 +01:00
parent 83cc8419a0
commit 0a390934d6
457 changed files with 21807 additions and 61 deletions

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:13:02 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:13:07 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_add(void)
{
t_vector *vec;
vec = ft_vec_new();
ft_vec_add(&vec, (void *)42);
if (vec->count != 1)
return (1);
if (vec->datas[0] != (void *)42)
return (1);
ft_vec_add(&vec, (void *)43);
ft_vec_add(&vec, (void *)44);
ft_vec_add(&vec, (void *)45);
ft_vec_add(&vec, (void *)46);
ft_vec_add(&vec, (void *)47);
if (vec->count != 6)
return (1);
if (vec->datas[0] != (void *)42 || vec->datas[1] != (void *)43 || \
vec->datas[2] != (void *)44 || vec->datas[3] != (void *)45 || \
vec->datas[4] != (void *)46 || vec->datas[5] != (void *)47)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_apply.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:13:11 by bgoulard #+# #+# */
/* Updated: 2024/05/24 17:13:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include "tests/tests_lambda_functions.h"
int test_vec_apply(void)
{
t_vector *vec;
int i;
i = 0;
vec = ft_vec_new();
ft_vec_add(&vec, &i);
ft_vec_apply(vec, add42);
if (*(int *)vec->datas[0] != 42)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_at.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:22:41 by bgoulard #+# #+# */
/* Updated: 2024/05/24 20:44:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_at(void)
{
t_vector *vec;
int a;
int b;
int c;
a = 0;
b = 1;
c = 2;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
if (*(int *)ft_vec_at(vec, 0) != 0)
return (1);
else if (*(int *)ft_vec_at(vec, 1) != 1)
return (1);
else if (*(int *)ft_vec_at(vec, 2) != 2)
return (1);
else if (ft_vec_at(vec, 3))
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_cat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:15:21 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:11:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include <stdbool.h>
/*
ft_vec_add(&vec_a, &a); // 42
ft_vec_add(&vec_a, &b); // 42 43
ft_vec_add(&vec_a, &c); // 42 43 44
ft_vec_add(&vec_b, &d); // 45
ft_vec_add(&vec_b, &e); // 45 46
ft_vec_add(&vec_b, &f); // 45 46 47
ret = ft_vec_cat(&vec_a, vec_b); // 42 43 44 + 45 46 47
ft_vec_add(&vec_a, &a); // 42
ft_vec_add(&vec_a, &b); // 42 43
ft_vec_add(&vec_a, &c); // 42 43 44
ret = ft_vec_cat(&vec_a, vec_b); // 42 43 44 + 45 46 47
*/
void init_vec_cat(t_vector **vec_a, t_vector **vec_b)
{
*vec_a = ft_vec_from_size(6);
ft_vec_add(vec_a, (void *)42);
ft_vec_add(vec_a, (void *)43);
ft_vec_add(vec_a, (void *)44);
*vec_b = ft_vec_new();
ft_vec_add(vec_b, (void *)45);
ft_vec_add(vec_b, (void *)46);
ft_vec_add(vec_b, (void *)47);
}
int test_vec_cat(void)
{
bool ret;
t_vector *vec_a;
t_vector *vec_b;
init_vec_cat(&vec_a, &vec_b);
ret = ft_vec_cat(&vec_a, vec_b);
if (ret != true || vec_a->count != 6)
return (1);
else if (ft_vec_at(vec_a, 0) != (void *)42 || ft_vec_at(vec_a, 1) != \
(void *)43 || ft_vec_at(vec_a, 2) != (void *)44 || ft_vec_at(vec_a, 3) != \
(void *)45 || ft_vec_at(vec_a, 4) != (void *)46 || ft_vec_at(vec_a, 5) != \
(void *)47)
return (2);
ft_vec_destroy(&vec_a);
vec_a = ft_vec_new();
ft_vec_add(&vec_a, (void *)42);
ft_vec_add(&vec_a, (void *)43);
ft_vec_add(&vec_a, (void *)44);
ret = ft_vec_cat(&vec_a, vec_b);
if (ret != false || vec_a->count != 3)
return (3);
else if (ft_vec_at(vec_a, 0) != (void *)42 || ft_vec_at(vec_a, 1) != \
(void *)43 || ft_vec_at(vec_a, 2) != (void *)44)
return (4);
return (ft_vec_destroy(&vec_a), ft_vec_destroy(&vec_b), 0);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:21:55 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:05:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_clear(void)
{
t_vector *vec;
int a;
int b;
int c;
a = 0;
b = 1;
c = 2;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
ft_vec_clear(vec);
if (vec->count != 0)
return (1);
else if (vec->cappacity == 0)
return (1);
else if (vec->datas[0])
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_convert_alloc_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:32:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include <stdlib.h>
// ft_vec_destroy(&vec);
// free(data); -> segfault : double free or corruption.
// ft_vec_convert_alloccarray takes ownership of the data. refert to the
// doc.
int test_vec_convert_alloc_array(void)
{
void **data;
t_vector *vec;
int arr[3];
arr[0] = 42;
arr[1] = 43;
arr[2] = 44;
data = malloc(sizeof(void *) * 3);
data[0] = (void *)&arr[0];
data[1] = (void *)&arr[1];
data[2] = (void *)&arr[2];
vec = ft_vec_convert_alloccarray(data, 3);
if (vec->count != 3 || vec->cappacity != 3 || vec->datas != data)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 42 || *(int *)ft_vec_at(vec, 1) != \
43 || *(int *)ft_vec_at(vec, 2) != 44)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:20:58 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:20:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_destroy(void)
{
t_vector *vec;
int a;
int b;
int c;
a = 0;
b = 1;
c = 2;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
ft_vec_destroy(&vec);
if (vec)
return (1);
return (0);
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:20:40 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:29:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include "tests/tests_lambda_functions.h"
#include <stdlib.h>
int test_vec_filter(void)
{
const int arr[3] = {21, 42, 63};
t_vector *vec;
int *pp[3];
vec = ft_vec_new();
ft_vec_add(&vec, (void *)&arr[0]);
ft_vec_add(&vec, (void *)&arr[1]);
ft_vec_add(&vec, (void *)&arr[2]);
ft_vec_filter(vec, is42, NULL);
if (vec->count != 1 || *(int *)ft_vec_at(vec, 0) != 42)
return (1);
ft_vec_clear(vec);
pp[0] = malloc(sizeof(int));
*pp[0] = 21;
pp[1] = malloc(sizeof(int));
*pp[1] = 42;
pp[2] = malloc(sizeof(int));
*pp[2] = 63;
ft_vec_add(&vec, pp[0]);
ft_vec_add(&vec, pp[1]);
ft_vec_add(&vec, pp[2]);
ft_vec_filter(vec, is42, free);
if (vec->count != 1 || ft_vec_at(vec, 0) != pp[1])
return (1);
return (ft_vec_apply(vec, free), ft_vec_destroy(&vec), 0);
}

View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_from_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:19:37 by bgoulard #+# #+# */
/* Updated: 2024/05/28 08:04:26 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int checks_01(t_vector *vec, void **data)
{
if (vec->count != 3 || vec->cappacity != FT_VECTOR_BASE_LEN
|| !vec->datas)
return (1);
else if (ft_vec_at(vec, 0) != data[0] || ft_vec_at(vec, 1) != data[1]
|| ft_vec_at(vec, 2) != data[2])
return (2);
return (0);
}
int checks_02(t_vector *vec, void **data)
{
if (vec->count != 6 || vec->cappacity != 6 || !vec->datas)
return (3);
else if (ft_vec_at(vec, 0) != data[0] || ft_vec_at(vec, 1) != data[1]
|| ft_vec_at(vec, 2) != data[2] || ft_vec_at(vec, 3) != data[3]
|| ft_vec_at(vec, 4) != data[4] || ft_vec_at(vec, 5) != data[5])
return (4);
return (0);
}
int test_vec_from_array(void)
{
void *data[3];
void *data2[6];
t_vector *vec;
data[0] = (void *)12;
data[1] = (void *)13;
data[2] = (void *)14;
data2[0] = (void *)20;
data2[1] = (void *)21;
data2[2] = (void *)22;
data2[3] = (void *)23;
data2[4] = (void *)24;
data2[5] = (void *)25;
vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0]));
if (checks_01(vec, data))
return (checks_01(vec, data));
ft_vec_destroy(&vec);
vec = ft_vec_from_array(data2, sizeof(data2) / sizeof(*data2));
if (checks_02(vec, data2))
return (checks_02(vec, data2));
return (ft_vec_destroy(&vec), 0);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_from_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:20:00 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:20:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_from_size(void)
{
t_vector *vec;
vec = ft_vec_from_size(42);
if (vec->count != 0)
return (1);
else if (vec->cappacity != 42)
return (1);
else if (!vec->datas)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 11:25:01 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:41:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_vector.h"
static int cmp_fun(const void *v_data, const void *key)
{
return (ft_strcmp(v_data, key));
}
int test_vec_get(void)
{
t_vector *vector;
void *data_ret;
vector = ft_vec_new();
ft_vec_add(&vector, "Hello");
ft_vec_add(&vector, "world");
ft_vec_add(&vector, "this");
ft_vec_add(&vector, "is");
ft_vec_add(&vector, "Zod");
data_ret = ft_vec_get(vector, "world", cmp_fun);
if (ft_strcmp(data_ret, "world") != 0)
return (1);
data_ret = ft_vec_get(vector, "Zod", cmp_fun);
if (ft_strcmp(data_ret, "Zod") != 0)
return (1);
data_ret = ft_vec_get(vector, "not here", cmp_fun);
if (data_ret)
return (1);
ft_vec_destroy(&vector);
return (0);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:21:45 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:12:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include "tests/tests_lambda_functions.h"
#include <stdlib.h>
int test_vec_map(void)
{
t_vector *vec;
t_vector *ret;
int **arr;
arr = (int **)creat_tb();
vec = ft_vec_from_array((void **)arr, 3);
ret = ft_vec_map(vec, add42_ret);
if (ret->count != 3)
return (1);
else if (*(int *)ft_vec_at(ret, 0) != 84 || *(int *)ft_vec_at(ret, 1) != 85
|| *(int *)ft_vec_at(ret, 2) != 86)
return (2);
ft_vec_apply(ret, free);
ft_vec_destroy(&vec);
ft_vec_destroy(&ret);
return (0);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:21:20 by bgoulard #+# #+# */
/* Updated: 2024/05/24 20:44:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_new(void)
{
t_vector *vec;
vec = ft_vec_new();
if (vec->count != 0)
return (1);
else if (vec->cappacity != FT_VECTOR_BASE_LEN)
return (1);
else if (!vec->datas)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_pop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 13:47:14 by bgoulard #+# #+# */
/* Updated: 2024/06/01 13:56:27 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_vector.h"
#include <stdio.h>
int test_vec_pop(void)
{
t_vector *vec;
const char *str;
vec = ft_vec_new();
ft_vec_add(&vec, "value1");
ft_vec_add(&vec, "value2");
ft_vec_add(&vec, "value3");
str = ft_vec_pop(vec);
if (!str || ft_strcmp(str, "value3") != 0 || vec->count != 2)
return (1);
str = ft_vec_pop(vec);
if (!str || ft_strcmp(str, "value2") != 0 || vec->count != 1)
return (2);
str = ft_vec_pop(vec);
if (!str || ft_strcmp(str, "value1") != 0 || vec->count != 0)
return (3);
str = ft_vec_pop(vec);
if (str)
return (4);
return (ft_vec_destroy(&vec), 0);
}

View file

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_remove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 10:13:12 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:55:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include <stdlib.h>
// tests vector has removed the arr[2]orrect element
// and
// tests vector_remove is bound checked with 42's call
static int checks_01(t_vector *vec)
{
if (vec->count != 2)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 42 || \
*(int *)ft_vec_at(vec, 1) != 44)
return (2);
ft_vec_remove(vec, 42, NULL);
if (vec->count != 2)
return (3);
return (0);
}
// tests vector_remove calls free on the correct element
static int checks_02(t_vector *vec, const int *arr)
{
if (vec->count != 2)
return (1);
if (*(int *)ft_vec_at(vec, 0) != arr[0] || *(int *)ft_vec_at(vec, 1) != \
arr[2])
return (1);
return (0);
}
int test_vec_remove(void)
{
t_vector *vec;
const int arr[3] = {42, 43, 44};
int *ptr;
size_t i;
i = 0;
vec = ft_vec_new();
ft_vec_add(&vec, (void *)&arr[0]);
ft_vec_add(&vec, (void *)&arr[1]);
ft_vec_add(&vec, (void *)&arr[2]);
ft_vec_remove(vec, 1, NULL);
if (checks_01(vec))
return (checks_01(vec));
ft_vec_destroy(&vec);
vec = ft_vec_new();
while (i < 3)
{
ptr = (int *)malloc(sizeof(int));
*ptr = arr[i++];
ft_vec_add(&vec, (void *)ptr);
}
ft_vec_remove(vec, 1, free);
if (checks_02(vec, arr))
return (checks_02(vec, arr));
return (ft_vec_apply(vec, free), ft_vec_destroy(&vec), 0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_remove_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:51:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include "tests/tests_lambda_functions.h"
int test_vec_remove_if(void)
{
t_vector *vec;
int a;
int b;
int c;
a = 42;
b = 43;
c = 44;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
ft_vec_remove_if(vec, is42, NULL);
if (vec->count != 2)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 43)
return (1);
else if (*(int *)ft_vec_at(vec, 1) != 44)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_reserve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:28:57 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:30:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_reserve(void)
{
t_vector *vec;
bool ret[3];
vec = ft_vec_from_size(FT_VECTOR_BASE_LEN - 1);
ret[0] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN - 2);
ret[1] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN);
ret[2] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN + 2);
if (ret[0] != true)
return (1);
else if (ret[1] != true)
return (1);
else if (ret[2] == false)
return (1);
else if (vec->cappacity != FT_VECTOR_BASE_LEN + 2)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_reverse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:30:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_reverse(void)
{
t_vector *vec;
int a;
int b;
int c;
a = 42;
b = 43;
c = 44;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
ft_vec_shrink(vec);
ft_vec_reverse(vec);
if (vec->count != 3)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 44)
return (1);
else if (*(int *)ft_vec_at(vec, 1) != 43)
return (1);
else if (*(int *)ft_vec_at(vec, 2) != 42)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_shift.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:28:19 by bgoulard #+# #+# */
/* Updated: 2024/05/24 11:28:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_shift(void)
{
t_vector *vec;
int a;
int b;
int c;
int d;
a = 42;
b = 43;
c = 44;
d = 45;
vec = ft_vec_new();
ft_vec_add(&vec, &a);
ft_vec_add(&vec, &b);
ft_vec_add(&vec, &c);
ft_vec_add(&vec, &d);
ft_vec_shift(vec, 1, 2);
if (vec->count != 2)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 42)
return (1);
else if (*(int *)ft_vec_at(vec, 1) != 45)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_shrink.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:28:19 by bgoulard #+# #+# */
/* Updated: 2024/05/24 20:43:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
/*
// try to shrink an already shrunk vector
ft_vec_shrink(vec);
*/
int test_vec_shrink(void)
{
t_vector *vec;
void *data[3];
int numbers[3];
numbers[0] = 42;
numbers[1] = 43;
numbers[2] = 44;
data[0] = (void *)&numbers[0];
data[1] = (void *)&numbers[1];
data[2] = (void *)&numbers[2];
vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0]));
ft_vec_shrink(vec);
if (vec->count != 3 || vec->cappacity != 3)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 42 || *(int *)ft_vec_at(vec, 1) != \
43 || *(int *)ft_vec_at(vec, 2) != 44)
return (1);
ft_vec_shrink(vec);
if (!vec || !vec->datas || !vec->cappacity || !vec->count || \
vec->count != vec->cappacity || vec->count != 3)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:27:57 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:04:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
#include "tests/tests_lambda_functions.h"
int test_vec_sort(void)
{
t_vector *vec;
int nbrs[3];
nbrs[0] = 44;
vec = ft_vec_new();
ft_vec_add(&vec, (void *)&nbrs[0]);
nbrs[1] = 43;
ft_vec_add(&vec, (void *)&nbrs[1]);
nbrs[2] = 42;
ft_vec_add(&vec, (void *)&nbrs[2]);
ft_vec_sort(vec, cmp_int);
if (vec->count != 3)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 42)
return (2);
else if (*(int *)ft_vec_at(vec, 1) != 43)
return (3);
else if (*(int *)ft_vec_at(vec, 2) != 44)
return (4);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 11:27:24 by bgoulard #+# #+# */
/* Updated: 2024/05/24 21:18:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_vector.h"
#include "ft_vector_types.h"
int test_vec_swap(void)
{
t_vector *vec;
void *data[3];
int arr[3];
arr[0] = 42;
arr[1] = 43;
arr[2] = 44;
data[0] = (void *)&arr[0];
data[1] = (void *)&arr[1];
data[2] = (void *)&arr[2];
vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0]));
ft_vec_swap(vec, 0, 2);
if (vec->count != 3)
return (1);
else if (*(int *)ft_vec_at(vec, 0) != 44)
return (1);
else if (*(int *)ft_vec_at(vec, 1) != 43)
return (1);
else if (*(int *)ft_vec_at(vec, 2) != 42)
return (1);
ft_vec_destroy(&vec);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_vec_to_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 11:35:25 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:41:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_vector.h"
int test_vec_to_array(void)
{
t_vector *vector;
const char **array;
vector = ft_vec_new();
ft_vec_add(&vector, "Hello");
ft_vec_add(&vector, "world");
ft_vec_add(&vector, "this");
ft_vec_add(&vector, "is");
ft_vec_add(&vector, "Zod");
array = (const char **)ft_vec_to_array(&vector);
if (ft_strcmp(array[0], "Hello") != 0)
return (1);
if (ft_strcmp(array[1], "world") != 0)
return (1);
if (ft_strcmp(array[2], "this") != 0)
return (1);
if (ft_strcmp(array[3], "is") != 0)
return (1);
if (ft_strcmp(array[4], "Zod") != 0)
return (1);
free(array);
return (0);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector_tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/13 08:30:25 by bgoulard #+# #+# */
/* Updated: 2024/06/02 11:33:20 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "tests/tests.h"
#include "tests/vector_tests.h"
int tests_vector(void)
{
int collect;
const t_test tests[] = {
{"add", test_vec_add}, {"apply", test_vec_apply}, {"at", test_vec_at}, \
{"cat", test_vec_cat}, {"clear", test_vec_clear}, \
{"destroy", test_vec_destroy}, {"filter", test_vec_filter}, \
{"map", test_vec_map}, {"new", test_vec_new}, {"pop", test_vec_pop}, \
{"from_size", test_vec_from_size}, {"from_array", test_vec_from_array}, \
{"convert_alloc_array", test_vec_convert_alloc_array}, \
{"remove", test_vec_remove}, {"remove_if", test_vec_remove_if}, \
{"reserve", test_vec_reserve}, {"reverse", test_vec_reverse}, \
{"shift", test_vec_shift}, {"sort", test_vec_sort}, \
{"shrink", test_vec_shrink}, {"swap", test_vec_swap}, \
{"get", test_vec_get}, {"to_array", test_vec_to_array}, \
{NULL, NULL}
};
collect = 0;
run_test(tests, &collect);
return (collect);
}