diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-04 23:36:19 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-04 23:36:19 +0200 |
| commit | 65cf641e9533b190db870d0cc46f2f852239ebf6 (patch) | |
| tree | df2002170b96d189493e54cb484390fb2af2a2d1 | |
| parent | 42316d8393d32bd88fb8e0cba6825185f78dacd0 (diff) | |
| download | libft-65cf641e9533b190db870d0cc46f2f852239ebf6.tar.gz libft-65cf641e9533b190db870d0cc46f2f852239ebf6.tar.bz2 libft-65cf641e9533b190db870d0cc46f2f852239ebf6.zip | |
Added test for ft_strsjoin, ft_strsjoinf, ft_vecpush_safe, Added doc for algo functions, tested functions
| -rw-r--r-- | include/libft_algo.h | 25 | ||||
| -rw-r--r-- | include/libft_def.h | 26 | ||||
| -rw-r--r-- | include/libft_dstr.h | 5 | ||||
| -rw-r--r-- | include/libft_ht.h | 3 | ||||
| -rw-r--r-- | src/algo/ft_compar_int.c | 9 | ||||
| -rw-r--r-- | src/algo/ft_compar_str.c | 9 | ||||
| -rw-r--r-- | src/algo/ft_is_set.c | 10 | ||||
| -rw-r--r-- | src/algo/ft_mergesort.c | 24 | ||||
| -rw-r--r-- | src/algo/ft_qsort.c | 35 | ||||
| -rw-r--r-- | src/algo/ft_reverse.c | 9 | ||||
| -rw-r--r-- | src/ht/ft_htnew.c | 3 | ||||
| -rw-r--r-- | src/str/ft_atoi.c | 7 | ||||
| -rw-r--r-- | src/str/ft_strsjoin.c | 12 | ||||
| -rw-r--r-- | src/str/ft_strsjoinf.c | 9 | ||||
| -rw-r--r-- | test/src/main.c | 4 | ||||
| -rw-r--r-- | test/src/runner/test_runner_str.c | 10 | ||||
| -rw-r--r-- | test/src/runner/test_runner_vec.c | 7 | ||||
| -rw-r--r-- | test/src/str/test_ft_strsjoin.c | 57 | ||||
| -rw-r--r-- | test/src/str/test_ft_strsjoinf.c | 87 | ||||
| -rw-r--r-- | test/src/vec/test_ft_vecpush_safe.c | 45 |
20 files changed, 375 insertions, 21 deletions
diff --git a/include/libft_algo.h b/include/libft_algo.h index e726d1f..e859de5 100644 --- a/include/libft_algo.h +++ b/include/libft_algo.h @@ -6,10 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:22:57 by cacharle #+# #+# */ -/* Updated: 2020/04/04 15:46:10 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:33:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ +/* +** \file libft_algo.h +** \brief Algorithms +*/ + #ifndef LIBFT_ALGO_H # define LIBFT_ALGO_H @@ -18,12 +23,26 @@ # include "libft_def.h" # include "libft_str.h" +/* +** \brief Range struct +** \param lo Lower bound +** \param hi Upper bound +*/ + typedef struct { int lo; int hi; } t_ftrange; +/* +** \brief Merge sort consts struct +** \param base Array to sort +** \param left Left subarray +** \param right Right subarray +** \note Only used internaly by ft_mergesort +*/ + struct s_merge_sorted_arrays { void *base; @@ -31,6 +50,10 @@ struct s_merge_sorted_arrays void *right; }; +/* +** remove this horror +*/ + typedef struct s_ft_search_const { const void *key; diff --git a/include/libft_def.h b/include/libft_def.h index 69a7655..7406959 100644 --- a/include/libft_def.h +++ b/include/libft_def.h @@ -6,10 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:36:56 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:04:57 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:40:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ +/* +** \file libft_def.h +** \brief Type and constant definition +*/ + #ifndef LIBFT_DEF_H # define LIBFT_DEF_H @@ -30,7 +35,22 @@ typedef unsigned int t_ftuint; typedef long int t_ftlong; typedef unsigned long int t_ftulong; -typedef void (*t_ftdel_func)(void*); -typedef int (*t_ftcompar_func)(const void*, const void*); +/* +** \brief Standard delete function +** \param x Resource to delete +*/ + +typedef void (*t_ftdel_func)(void *x); + +/* +** \brief Standard comparison function +** \param x1 Resource 1 +** \param x2 Resource 2 +** \return negative number if x1 < x2, +** 0 if x1 == x2, +** positive number if x1 > x2 +*/ + +typedef int (*t_ftcompar_func)(const void *x1, const void *x2); #endif diff --git a/include/libft_dstr.h b/include/libft_dstr.h index 11bf2f4..fbe69db 100644 --- a/include/libft_dstr.h +++ b/include/libft_dstr.h @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 10:39:51 by charles #+# #+# */ -/* Updated: 2020/04/04 21:18:48 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:33:27 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ void ft_dstrdestroy(t_ftdstr *dstr); t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least); char *ft_dstrunwrap(t_ftdstr *dstr); t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i); -// t_ftdstr *ft_dstrsubstitute(t_ftdstr *dstr, char *sub, size_t start, size_t end); +t_ftdstr *ft_dstrsubstitute(t_ftdstr *dstr, char *sub, + size_t start, size_t end); #endif diff --git a/include/libft_ht.h b/include/libft_ht.h index b3c1d2d..c53ee4d 100644 --- a/include/libft_ht.h +++ b/include/libft_ht.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:36:09 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:12:12 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:34:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ # include "libft.h" # include "libft_def.h" # include "libft_lst.h" +# include "libft_mem.h" /* ** \brief Hash table entry, key/value pair diff --git a/src/algo/ft_compar_int.c b/src/algo/ft_compar_int.c index 848dc71..ab057bf 100644 --- a/src/algo/ft_compar_int.c +++ b/src/algo/ft_compar_int.c @@ -6,12 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 08:24:43 by cacharle #+# #+# */ -/* Updated: 2020/01/19 08:27:38 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:30:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/* +** \brief Comparison function for 2 ints +** \param a Pointer to first int +** \param b Pointer to first int +** \return The difference between a and b +*/ + int ft_compar_int(const void *a, const void *b) { return (*(int*)a - *(int*)b); diff --git a/src/algo/ft_compar_str.c b/src/algo/ft_compar_str.c index 0d3e6ff..6749ab0 100644 --- a/src/algo/ft_compar_str.c +++ b/src/algo/ft_compar_str.c @@ -6,12 +6,19 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 15:44:24 by charles #+# #+# */ -/* Updated: 2020/04/04 15:46:28 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:31:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief String comparison function +** \param s1_p Pointer to first string +** \param s2_p Pointer to second string +** \return `strcmp` of both strings +*/ + int ft_compar_str(const void *s1_p, const void *s2_p) { return (ft_strcmp(*(const char**)s1_p, *(const char**)s2_p)); diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c index 79a5e7c..fd26a88 100644 --- a/src/algo/ft_is_set.c +++ b/src/algo/ft_is_set.c @@ -6,12 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:33:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Test whether array only contains unique elements +** \param base Array to test +** \param nel Number of element in the array +** \param width Size of an element +** \param compar Comparison function to test if 2 elements are equal +*/ + bool ft_is_set(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_mergesort.c b/src/algo/ft_mergesort.c index 25b4255..8556145 100644 --- a/src/algo/ft_mergesort.c +++ b/src/algo/ft_mergesort.c @@ -6,12 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 02:26:41 by cacharle #+# #+# */ -/* Updated: 2020/02/13 23:14:21 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:40:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper function to return in case of error +** \param left Left subarray +** \param right Right subarray +** \return -1 to indicate error +*/ + static int st_mergesort_error(void *left, void *right) { free(left); @@ -19,6 +26,13 @@ static int st_mergesort_error(void *left, void *right) return (-1); } +/* +** \brief Merge 2 sorted arrays +** \param arrays Struct containing the arrays (base, left, right) +** \param nel Number of element in base +** \param compar Comparison function +*/ + static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, size_t width, int (*compar)(const void *, const void *)) { @@ -49,6 +63,14 @@ static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, arrays->right + ri++ * width, width); } +/* +** \brief Sort an array using the merge sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \return 0 on success, -1 on error +*/ + int ft_mergesort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) { diff --git a/src/algo/ft_qsort.c b/src/algo/ft_qsort.c index 9bcfcdf..0c1c0f7 100644 --- a/src/algo/ft_qsort.c +++ b/src/algo/ft_qsort.c @@ -6,12 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:55:14 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:10:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper to create a new range +** \param lo Lower bound +** \param hi Higher bound +** \return Range struct (not allocated) +*/ + static t_ftrange ft_range_new(int lo, int hi) { t_ftrange range; @@ -21,6 +28,16 @@ static t_ftrange ft_range_new(int lo, int hi) return (range); } +/* +** \brief Array partitionning, +** takes a pivot and place every element lower than it before +** and every element greater after +** \param base Partitioned array +** \param range Lower and upper bound +** \param width Single element size +** \param compar Comparison function +*/ + static int ft_qsort_partition(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -43,6 +60,14 @@ static int ft_qsort_partition(void *base, t_ftrange range, return (p); } +/* +** \brief Qsort recursion function +** \param base Array to sort +** \param range Lower and upper bound which define the area to sort +** \param width Single element size +** \param compar Comparision function +*/ + static void ft_qsort_rec(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -55,6 +80,14 @@ static void ft_qsort_rec(void *base, t_ftrange range, ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar); } +/* +** \brief Sort an array using the Quick sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \param compar Comparision function +*/ + void ft_qsort(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_reverse.c b/src/algo/ft_reverse.c index 0bc447f..c617338 100644 --- a/src/algo/ft_reverse.c +++ b/src/algo/ft_reverse.c @@ -6,12 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 05:07:13 by cacharle #+# #+# */ -/* Updated: 2020/02/10 05:19:22 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:36:23 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Reverse an array +** \param base Array to reverse +** \param nel Number of element in the array +** \param width Size of each elements +*/ + void ft_reverse(void *base, size_t nel, size_t width) { size_t i; diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index b6d1cc3..585c211 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,12 +6,11 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_ht.h" -#include "libft_mem.h" /* ** \brief Create a new hash table. diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index d6fa5bb..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,14 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ -/* Updated: 2020/01/15 10:56:06 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:34:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /* -** Convert a string to an int +** \brief Extract first int in a string +** (takes as much digits has possible) +** \param str String to convert +** \return Extracted int */ int ft_atoi(const char *str) diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c index 507903b..0923bde 100644 --- a/src/str/ft_strsjoin.c +++ b/src/str/ft_strsjoin.c @@ -6,12 +6,20 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:30:08 by charles #+# #+# */ -/* Updated: 2020/04/04 14:45:58 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings +** \param strs Array of strings +** \param delim String iterspersed between strings +** \return Joined string or NULL on error +** \note Empty strings are ignored +*/ + char *ft_strsjoin(char **strs, char *delim) { int i; @@ -35,7 +43,7 @@ char *ft_strsjoin(char **strs, char *delim) while (strs[++i] != NULL) { ft_strcat(join, strs[i]); - if (strs[i + 1] != NULL) + if (*strs[i] != '\0' && strs[i + 1] != NULL) ft_strcat(join, delim); } return (join); diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c index f5077c1..36a2892 100644 --- a/src/str/ft_strsjoinf.c +++ b/src/str/ft_strsjoinf.c @@ -6,12 +6,19 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:27:33 by charles #+# #+# */ -/* Updated: 2020/04/04 14:29:49 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings and free the array +** \param strs Array of strings +** \param delim String which will be put between each string +** \return Joined string or NULL on error +*/ + char *ft_strsjoinf(char **strs, char *delim) { char *ret; diff --git a/test/src/main.c b/test/src/main.c index 7bac44b..b8f5cf3 100644 --- a/test/src/main.c +++ b/test/src/main.c @@ -30,6 +30,8 @@ static void run_all_test(void) // str RUN_TEST_GROUP(ft_strlen); RUN_TEST_GROUP(ft_fnmatch); + RUN_TEST_GROUP(ft_strsjoin); + RUN_TEST_GROUP(ft_strsjoinf); // ht RUN_TEST_GROUP(ft_htentry_new); @@ -79,7 +81,9 @@ static void run_all_test(void) RUN_TEST_GROUP(ft_vecpush); RUN_TEST_GROUP(ft_vecremove); RUN_TEST_GROUP(ft_vecinsert); + RUN_TEST_GROUP(ft_vecpush_safe); + // dstr RUN_TEST_GROUP(ft_dstrdestroy); RUN_TEST_GROUP(ft_dstrgrow); RUN_TEST_GROUP(ft_dstrinsert); diff --git a/test/src/runner/test_runner_str.c b/test/src/runner/test_runner_str.c index a299786..a6c0da8 100644 --- a/test/src/runner/test_runner_str.c +++ b/test/src/runner/test_runner_str.c @@ -9,3 +9,13 @@ TEST_GROUP_RUNNER(ft_fnmatch) { RUN_TEST_CASE(ft_fnmatch, basic); } + +TEST_GROUP_RUNNER(ft_strsjoin) +{ + RUN_TEST_CASE(ft_strsjoin, basic); +} + +TEST_GROUP_RUNNER(ft_strsjoinf) +{ + RUN_TEST_CASE(ft_strsjoinf, basic); +} diff --git a/test/src/runner/test_runner_vec.c b/test/src/runner/test_runner_vec.c index c393bfa..5a57def 100644 --- a/test/src/runner/test_runner_vec.c +++ b/test/src/runner/test_runner_vec.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:55:53 by charles #+# #+# */ -/* Updated: 2020/04/04 19:36:41 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:26:54 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,3 +57,8 @@ TEST_GROUP_RUNNER(ft_vecsort) { RUN_TEST_CASE(ft_vecsort, basic); } + +TEST_GROUP_RUNNER(ft_vecpush_safe) +{ + RUN_TEST_CASE(ft_vecpush_safe, basic); +} diff --git a/test/src/str/test_ft_strsjoin.c b/test/src/str/test_ft_strsjoin.c new file mode 100644 index 0000000..0fac07b --- /dev/null +++ b/test/src/str/test_ft_strsjoin.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_strsjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:41:59 by charles #+# #+# */ +/* Updated: 2020/04/04 23:32:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_strsjoin); + +TEST_SETUP(ft_strsjoin) +{} + +TEST_TEAR_DOWN(ft_strsjoin) +{} + +TEST(ft_strsjoin, basic) +{ + char *join; + char *arr[] = {"bonjour", "je", "suis", NULL}; + char *arr2[] = {"", "bonjour", "", "", "", "je", "suis", NULL}; + char *null_arr[] = {NULL}; + + join = ft_strsjoin(arr, " "); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour je suis", join); + + join = ft_strsjoin(arr, "<|>"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour<|>je<|>suis", join); + + join = ft_strsjoin(arr, ""); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjourjesuis", join); + + join = ft_strsjoin(arr2, " "); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour je suis", join); + + join = ft_strsjoin(arr2, "<|>"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour<|>je<|>suis", join); + + join = ft_strsjoin(arr2, ""); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjourjesuis", join); + + join = ft_strsjoin(null_arr, "abc"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("", join); +} diff --git a/test/src/str/test_ft_strsjoinf.c b/test/src/str/test_ft_strsjoinf.c new file mode 100644 index 0000000..d5184a9 --- /dev/null +++ b/test/src/str/test_ft_strsjoinf.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_strsjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:41:59 by charles #+# #+# */ +/* Updated: 2020/04/04 23:25:39 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_strsjoinf); + +TEST_SETUP(ft_strsjoinf) +{} + +TEST_TEAR_DOWN(ft_strsjoinf) +{} + +TEST(ft_strsjoinf, basic) +{ + char *join; + char *arr_orig[] = {"bonjour", "je", "suis", NULL}; + char *arr2_orig[] = {"", "bonjour", "", "", "", "je", "suis", NULL}; + char **null_arr; + char **arr; + char **arr2; + + size_t i; + + arr = malloc(sizeof(arr_orig)); + for (i = 0; i < sizeof(arr_orig) / sizeof(char*) - 1; i++) + arr[i] = strdup(arr_orig[i]); + arr[i] = NULL; + join = ft_strsjoinf(arr, " "); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour je suis", join); + + arr = malloc(sizeof(arr_orig)); + for (i = 0; i < sizeof(arr_orig) / sizeof(char*) - 1; i++) + arr[i] = strdup(arr_orig[i]); + arr[i] = NULL; + join = ft_strsjoinf(arr, "<|>"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour<|>je<|>suis", join); + + arr = malloc(sizeof(arr_orig)); + for (i = 0; i < sizeof(arr_orig) / sizeof(char*) - 1; i++) + arr[i] = strdup(arr_orig[i]); + arr[i] = NULL; + join = ft_strsjoinf(arr, ""); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjourjesuis", join); + + arr2 = malloc(sizeof(arr2_orig)); + for (i = 0; i < sizeof(arr2_orig) / sizeof(char*) - 1; i++) + arr2[i] = strdup(arr2_orig[i]); + arr2[i] = NULL; + join = ft_strsjoinf(arr2, " "); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour je suis", join); + + arr2 = malloc(sizeof(arr2_orig)); + for (i = 0; i < sizeof(arr2_orig) / sizeof(char*) - 1; i++) + arr2[i] = strdup(arr2_orig[i]); + arr2[i] = NULL; + join = ft_strsjoinf(arr2, "<|>"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjour<|>je<|>suis", join); + + arr2 = malloc(sizeof(arr2_orig)); + for (i = 0; i < sizeof(arr2_orig) / sizeof(char*) - 1; i++) + arr2[i] = strdup(arr2_orig[i]); + arr2[i] = NULL; + join = ft_strsjoinf(arr2, ""); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("bonjourjesuis", join); + + null_arr = malloc(sizeof(char*)); + null_arr[0] = NULL; + join = ft_strsjoinf(null_arr, "abc"); + TEST_ASSERT_NOT_NULL(join); + TEST_ASSERT_EQUAL_STRING("", join); +} diff --git a/test/src/vec/test_ft_vecpush_safe.c b/test/src/vec/test_ft_vecpush_safe.c new file mode 100644 index 0000000..8546829 --- /dev/null +++ b/test/src/vec/test_ft_vecpush_safe.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_vecpush_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:41:59 by charles #+# #+# */ +/* Updated: 2020/04/04 23:31:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_test.h" + +TEST_GROUP(ft_vecpush_safe); + +TEST_SETUP(ft_vecpush_safe) +{} + +TEST_TEAR_DOWN(ft_vecpush_safe) +{} + +TEST(ft_vecpush_safe, basic) +{ + t_ftvec *vec; + + vec = ft_vecnew(1); + TEST_ASSERT_NULL(ft_vecpush_safe(vec, NULL)); + + TEST_ASSERT_NOT_NULL(ft_vecpush_safe(vec, (void*)0x1UL)); + TEST_ASSERT_EQUAL_HEX(0x1, (unsigned long)vec->data[0]); + TEST_ASSERT_GREATER_OR_EQUAL(1, vec->capacity); + TEST_ASSERT_EQUAL(1, vec->size); + + TEST_ASSERT_NULL(ft_vecpush_safe(vec, NULL)); + TEST_ASSERT_NULL(ft_vecpush_safe(vec, NULL)); + + TEST_ASSERT_NOT_NULL(ft_vecpush_safe(vec, (void*)0x2UL)); + TEST_ASSERT_EQUAL_HEX(0x2, (unsigned long)vec->data[1]); + TEST_ASSERT_GREATER_OR_EQUAL(2, vec->capacity); + TEST_ASSERT_EQUAL(2, vec->size); + + TEST_ASSERT_NULL(ft_vecpush_safe(vec, NULL)); + TEST_ASSERT_NULL(ft_vecpush_safe(vec, NULL)); +} |
