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 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:27:46 by bgoulard #+# #+# */
/* Updated: 2024/06/07 14:59:37 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "tests/tests.h"
#include "tests/map_tests.h"
// see with --leak-check=full
int tests_map(void)
{
int collect;
const t_test test[] = {
{"create", test_map_create},
{"destroy", test_map_destroy}, // here
{"destroy_free", test_map_destroy_free},
{"clear", test_map_clear}, // here
{"set", test_map_set},
{"set_cmp", test_map_set_cmp},
{"set_hash", test_map_set_hash},
{"get", test_map_get},
{"size", test_map_size},
{"capacity", test_map_capacity},
{"remove", test_map_remove}, // here
{"hash", test_map_hash},
{NULL, NULL}
};
collect = 0;
run_test(test, &collect);
return (collect);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_cappacity.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:16:49 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:03:26 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
int test_map_capacity(void)
{
t_map *map;
size_t ret;
map = ft_map_create(10);
ret = ft_map_capacity(map);
if (ret != 10)
return (1);
ft_map_destroy(map);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:11:13 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:41:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
// clear the map and check if the value is still present
// try to double clear the map
int test_map_clear(void)
{
t_map *map;
map = ft_map_create(10);
ft_map_set(map, "key", "value", ft_strlen("key"));
ft_map_clear(map);
if (ft_map_get(map, "key", ft_strlen("key")))
return (1);
ft_map_clear(map);
ft_map_destroy(map);
return (0);
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:07:38 by bgoulard #+# #+# */
/* Updated: 2024/05/19 18:07:42 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
int test_map_create(void)
{
t_map *map;
map = ft_map_create(10);
if (!map)
return (1);
ft_map_destroy(map);
return (0);
}

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:08:04 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:07:40 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_string.h"
#include "ft_map_types.h"
#include <stdlib.h>
// check valgrind for no memory leak
int test_map_destroy(void)
{
t_map *map;
map = ft_map_create(10);
ft_map_destroy(map);
return (0);
}
// accessing to key[0] and value[0] after the call to check
// that no free occured on the data at first call
int test_map_destroy_free(void)
{
t_map *map;
char *key;
char *value;
key = ft_strdup("keys");
value = ft_strdup("values");
map = ft_map_create(10);
ft_map_set(map, key, value, ft_strlen(key));
ft_map_destroy_free(map, NULL);
key[0] = 'k';
value[0] = 'v';
map = ft_map_create(15);
ft_map_set(map, key, value, ft_strlen(key));
ft_map_destroy_free(map, free);
free(key);
return (0);
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:14:47 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:28:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
/*
// search for a non existing key
ret = ft_map_get(map, "key2", strlen("key2") + 1);
---
// search for a non existing key in a full map
map = ft_map_create(1);
*/
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
#include <stdlib.h>
int test_map_get_ultra_small(void)
{
char *str[2];
t_map *map;
void *ret;
str[0] = ft_strdup("value");
str[1] = ft_strdup("value2");
map = ft_map_create(1);
ft_map_set(map, "key", str[0], ft_strlen("key"));
ft_map_set(map, "key2", str[1], ft_strlen("key"));
ret = ft_map_get(map, "key", ft_strlen("key"));
if (!ret)
return (1);
else if (ft_strcmp((char *)ret, "value"))
return (2);
ret = ft_map_get(map, "key2", ft_strlen("key2"));
if (!ret)
return (3);
else if (ft_strcmp((char *)ret, "value2"))
return (4);
ft_map_destroy_free(map, free);
return (0);
}
int test_map_normal(void)
{
char *str;
t_map *map;
void *ret;
str = ft_strdup("value");
map = ft_map_create(10);
ft_map_set(map, "key", str, ft_strlen("key"));
ret = ft_map_get(map, "key", ft_strlen("key"));
if (!ret)
return (1);
else if (ft_strcmp((char *)ret, "value"))
return (2);
ret = ft_map_get(map, "key2", ft_strlen("key2"));
if (ret)
return (3);
ft_map_destroy(map);
free(str);
return (0);
}
int test_map_get(void)
{
int ret;
ret = test_map_get_ultra_small();
if (ret != 0)
return (ret + 10 * 0);
ret = test_map_normal();
if (ret != 0)
return (ret + 10 * 1);
return (0);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:21:33 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:24:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_map.h"
#include "ft_string.h"
// we dont really test the hash function, we just test that
// it doesn't overflow our map
int test_map_hash(void)
{
size_t ret;
size_t ret2;
ret = ft_hash_djb2("key", ft_strlen("key"));
ret2 = ft_hash_djb2("key2", ft_strlen("key2"));
if (ret == ret2)
return (1);
ret = ft_hash_sdbm("key", ft_strlen("key"));
ret2 = ft_hash_sdbm("key2", ft_strlen("key2"));
if (ret == ret2)
return (1);
ret = ft_hash_fnv1a("key", ft_strlen("key"));
ret2 = ft_hash_fnv1a("key2", ft_strlen("key2"));
if (ret == ret2)
return (1);
ret = ft_hash_dummy("key", ft_strlen("key"));
ret2 = ft_hash_dummy("key2", ft_strlen("key2"));
if (ret == ret2)
return (1);
return (0);
}

View file

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_remove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:18:53 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:31:50 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_string.h"
#include "ft_map_types.h"
#include "ft_string.h"
#include <stdlib.h>
static int test_map_remove_normal(void)
{
char *str;
void *ret;
t_map *map;
str = ft_strdup("value");
map = ft_map_create(10);
ft_map_set(map, "key", str, ft_strlen("key"));
ret = ft_map_remove(map, "key", ft_strlen("key"));
if (!ret)
return (1);
else if (ret != str)
return (2);
ret = ft_map_remove(map, "key2", ft_strlen("key2"));
if (ret)
return (3);
ft_map_destroy(map);
free(str);
return (0);
}
static int test_map_remove_colision(void)
{
const char *keys[] = {"key", "key2", "key3", "key4", NULL};
const char *str[] = {
ft_strdup("value"), ft_strdup("value2"), ft_strdup("value3"),
ft_strdup("value4"), NULL};
t_map *map;
int ret;
map = ft_map_create(1);
ret = 0;
while (str[ret])
{
ft_map_set(map, keys[ret], str[ret], ft_strlen(keys[ret]));
ret++;
}
ret = 0;
if (ft_map_remove(map, keys[2], ft_strlen(keys[2])) != str[2])
return (1);
if (ft_map_remove(map, keys[1], ft_strlen(keys[1])) != str[1])
return (2);
if (ft_map_remove(map, keys[0], ft_strlen(keys[0])) != str[0])
return (3);
if (ft_map_remove(map, keys[3], ft_strlen(keys[3])) != str[3])
return (4);
return (ft_map_destroy(map), ft_apply_2d((void **)str, free), 0);
}
int test_map_remove(void)
{
int ret;
ret = test_map_remove_normal();
if (ret != 0)
return (ret);
ret = test_map_remove_colision();
if (ret != 0)
return (ret + 10);
return (0);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:12:34 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:39:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
int test_map_set(void)
{
char *str;
char *str2;
t_map *map;
str = ft_strdup("value");
str2 = ft_strdup("value2");
map = ft_map_create(1);
if (ft_map_set(map, "key", str, ft_strlen("key")) != true)
return (1);
if (ft_map_set(map, "key", str2, ft_strlen("key")) != true)
return (1);
ft_map_destroy(map);
free(str);
free(str2);
return (0);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_set_cmphash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:13:15 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:14:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
int test_map_set_cmp(void)
{
t_map *map;
map = ft_map_create(10);
ft_map_set_cmp(map, NULL);
if (map->cmp)
return (1);
ft_map_destroy(map);
return (0);
}
int test_map_set_hash(void)
{
t_map *map;
map = ft_map_create(10);
ft_map_set_hash(map, NULL);
if (map->hash)
return (1);
ft_map_destroy(map);
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests_map_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 18:15:34 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:17:17 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
#include <stdlib.h>
int test_map_size(void)
{
char *str;
t_map *map;
size_t ret;
str = ft_strdup("value");
map = ft_map_create(10);
ft_map_set(map, "key", str, ft_strlen("key"));
ret = ft_map_size(map);
if (ret != 1)
return (1);
ft_map_destroy(map);
free(str);
return (0);
}