From ac0559db82bfef3b7ca3d976b1d7700ed2c1c1fc Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 31 Jan 2020 10:44:30 +0100 Subject: hash table unit testing, norming --- test/Makefile | 27 +++++++++++++---- test/include/helper/helper_segfault.h | 19 ++++++++++++ test/include/libft_test.h | 6 ++++ test/libft_test | Bin 34012 -> 45308 bytes test/src/ht/test_ft_htcontent_new.c | 0 test/src/ht/test_ft_htdelone.c | 0 test/src/ht/test_ft_htdelone_key.c | 0 test/src/ht/test_ft_htdestroy.c | 0 test/src/ht/test_ft_htdestroy_all.c | 0 test/src/ht/test_ft_htdestroy_key.c | 0 test/src/ht/test_ft_htget.c | 35 ++++++++++++++++++++++ test/src/ht/test_ft_hthash.c | 0 test/src/ht/test_ft_htnew.c | 38 ++++++++++++++++++++++++ test/src/ht/test_ft_htset.c | 51 ++++++++++++++++++++++++++++++++ test/src/main.c | 14 +++++++++ test/src/runner/test_ft_strlen_runner.c | 6 ---- test/src/runner/test_runner_ht.c | 22 ++++++++++++++ test/src/runner/test_runner_str.c | 6 ++++ 18 files changed, 212 insertions(+), 12 deletions(-) create mode 100644 test/include/helper/helper_segfault.h create mode 100644 test/src/ht/test_ft_htcontent_new.c create mode 100644 test/src/ht/test_ft_htdelone.c create mode 100644 test/src/ht/test_ft_htdelone_key.c create mode 100644 test/src/ht/test_ft_htdestroy.c create mode 100644 test/src/ht/test_ft_htdestroy_all.c create mode 100644 test/src/ht/test_ft_htdestroy_key.c create mode 100644 test/src/ht/test_ft_htget.c create mode 100644 test/src/ht/test_ft_hthash.c create mode 100644 test/src/ht/test_ft_htnew.c create mode 100644 test/src/ht/test_ft_htset.c create mode 100644 test/src/main.c delete mode 100644 test/src/runner/test_ft_strlen_runner.c create mode 100644 test/src/runner/test_runner_ht.c create mode 100644 test/src/runner/test_runner_str.c (limited to 'test') diff --git a/test/Makefile b/test/Makefile index 4d84321..f429a29 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,11 +8,12 @@ SRC_DIR = src INCLUDE_DIR = include CC = gcc -CCFLAGS = -I$(INCLUDE_DIR) -I$(UNITY_DIR)/include -I$(LIBFT_DIR)/include -Wall -Wextra -Werror +CCFLAGS = -I$(INCLUDE_DIR) -I$(UNITY_DIR)/include -I$(LIBFT_DIR)/include -Wall -Wextra #-Werror LDFLAGS = -L$(UNITY_DIR) -lunity -L$(LIBFT_DIR) -lft NAME = libft_test +INCLUDE = $(shell find $(INCLUDE_DIR) -type f -name "*.h") SRC = $(shell find $(SRC_DIR) -type f -name "*.c") OBJ = $(SRC:.c=.o) @@ -23,26 +24,40 @@ run: all @echo "Test: Running" @./$(NAME) -$(NAME): $(OBJ) +run_v: all + @echo "Test: Running" + @./$(NAME) -v + +$(NAME): libft_all $(OBJ) @echo "Test: Linking: $@" - @$(CC) -o $@ $^ $(LDFLAGS) + @$(CC) -o $@ $(OBJ) $(LDFLAGS) -%.o: %.c +%.o: %.c $(INCLUDE) $(LIBFT_SRC) @echo "Test: Compiling: $@" @$(CC) $(CCFLAGS) -c -o $@ $< -clean: unity_fclean +clean: @echo "Test: Removing object" @$(RM) $(OBJ) -fclean: +clean_dep: unity_fclean libft_fclean + +fclean: clean @echo "Test: Removing executable" @$(RM) $(NAME) re: fclean all +re_dep: clean_dep fclean all + unity_all: @$(MAKE) $(MAKE_ARGS) -s -C $(UNITY_DIR) all unity_fclean: @$(MAKE) $(MAKE_ARGS) -s -C $(UNITY_DIR) fclean + +libft_all: + @$(MAKE) $(MAKE_ARGS) -s -C $(LIBFT_DIR) all + +libft_fclean: + @$(MAKE) $(MAKE_ARGS) -s -C $(LIBFT_DIR) fclean diff --git a/test/include/helper/helper_segfault.h b/test/include/helper/helper_segfault.h new file mode 100644 index 0000000..99351af --- /dev/null +++ b/test/include/helper/helper_segfault.h @@ -0,0 +1,19 @@ +#ifndef HELPER_SEGFAULT_H +# define HELPER_SEGFAULT_H + +extern int helper_segfault_pid; + +# define TEST_ASSERT_SEGFAULT(code) do { \ + if ((helper_segfault_pid = fork()) < 0) \ + exit(EXIT_FAILURE); \ + if (helper_segfault_pid == 0) \ + { \ + do { code; } while (0); \ + exit(EXIT_FAILURE); \ + } \ + wait(&helper_segfault_pid); \ + if (WIFSIGNALED(helper_segfault_pid)) \ + TEST_FAIL_MESSAGE("Segfault"); \ +} while (0) + +#endif diff --git a/test/include/libft_test.h b/test/include/libft_test.h index 1c5ebb5..c5eb5e2 100644 --- a/test/include/libft_test.h +++ b/test/include/libft_test.h @@ -1,8 +1,14 @@ #ifndef LIBFT_TEST_H # define LIBFT_TEST_H +# include +# include + # include "unity.h" # include "unity_fixture.h" # include "libft.h" +# include "libft_ht.h" + +# include "helper/helper_segfault.h" #endif diff --git a/test/libft_test b/test/libft_test index 13cbab2..050602b 100755 Binary files a/test/libft_test and b/test/libft_test differ diff --git a/test/src/ht/test_ft_htcontent_new.c b/test/src/ht/test_ft_htcontent_new.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htdelone.c b/test/src/ht/test_ft_htdelone.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htdelone_key.c b/test/src/ht/test_ft_htdelone_key.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htdestroy.c b/test/src/ht/test_ft_htdestroy.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htdestroy_all.c b/test/src/ht/test_ft_htdestroy_all.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htdestroy_key.c b/test/src/ht/test_ft_htdestroy_key.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htget.c b/test/src/ht/test_ft_htget.c new file mode 100644 index 0000000..5c7db28 --- /dev/null +++ b/test/src/ht/test_ft_htget.c @@ -0,0 +1,35 @@ +#include "libft_test.h" + +TEST_GROUP(ft_htget); + +static t_ftht *ht; + +TEST_SETUP(ft_htget) +{ + ht = ft_htnew(10); +} + +TEST_TEAR_DOWN(ft_htget) +{ + /* ft_htdestroy_key(ht); */ +} + +int helper_segfault_pid; + +TEST(ft_htget, segfault) +{ + TEST_ASSERT_SEGFAULT(ft_htget((t_ftht*)NULL, "")); + TEST_ASSERT_SEGFAULT(ft_htget(ft_htnew(1), (char*)NULL)); + TEST_ASSERT_SEGFAULT(ft_htget(ft_htnew(1), "")); + TEST_ASSERT_SEGFAULT(ft_htget(ft_htnew(1), "asdkfhjaklsdfhahjsdfk")); +} + +TEST(ft_htget, error_null) +{ + TEST_ASSERT_NULL(ft_htget(NULL, NULL)); + TEST_ASSERT_NULL(ft_htget(ht, NULL)); + TEST_ASSERT_NULL(ft_htget(NULL, "")); + TEST_ASSERT_NULL(ft_htget(ht, "")); + TEST_ASSERT_NULL(ft_htget(ht, "hi")); + TEST_ASSERT_NULL(ft_htget(ht, "asdfkasdflk")); +} diff --git a/test/src/ht/test_ft_hthash.c b/test/src/ht/test_ft_hthash.c new file mode 100644 index 0000000..e69de29 diff --git a/test/src/ht/test_ft_htnew.c b/test/src/ht/test_ft_htnew.c new file mode 100644 index 0000000..2696e7f --- /dev/null +++ b/test/src/ht/test_ft_htnew.c @@ -0,0 +1,38 @@ +#include "libft_test.h" + + +TEST_GROUP(ft_htnew); + +TEST_SETUP(ft_htnew) +{} + +TEST_TEAR_DOWN(ft_htnew) +{} + +int helper_segfault_pid; + +TEST(ft_htnew, segfault) +{ + TEST_ASSERT_SEGFAULT(ft_htnew(10)); + TEST_ASSERT_SEGFAULT(ft_htnew(0)); + TEST_ASSERT_SEGFAULT(ft_htnew((1 << 14) + 1)); +} + +TEST(ft_htnew, error_null) +{ + TEST_ASSERT_NOT_NULL(ft_htnew(10)); // leak + TEST_ASSERT_NULL(ft_htnew(0)); + TEST_ASSERT_NULL(ft_htnew((1 << 14) + 1)); +} + +TEST(ft_htnew, happy_path) +{ + t_ftht *ht; + + ht = ft_htnew(10); + TEST_ASSERT_NOT_NULL(ht); + TEST_ASSERT_EQUAL(ht->size, 10); + TEST_ASSERT_NOT_NULL(ht->entries); + for (t_ftsize i = 0; i < ht->size; i++) + TEST_ASSERT_NULL(ht->entries[i]); +} diff --git a/test/src/ht/test_ft_htset.c b/test/src/ht/test_ft_htset.c new file mode 100644 index 0000000..aa6bfc5 --- /dev/null +++ b/test/src/ht/test_ft_htset.c @@ -0,0 +1,51 @@ +#include "libft_test.h" + +TEST_GROUP(ft_htset); + +static t_ftht *ht; + +TEST_SETUP(ft_htset) +{ + ht = ft_htnew(10); +} + +TEST_TEAR_DOWN(ft_htset) +{ + ft_htdestroy_key(ht); +} + +TEST(ft_htset, segfault) +{ + TEST_ASSERT_SEGFAULT(ft_htset(NULL, "", strdup(""))); + TEST_ASSERT_SEGFAULT(ft_htset(ft_htnew(1), NULL, strdup(""))); + TEST_ASSERT_SEGFAULT(ft_htset(ft_htnew(1), "", NULL)); + TEST_ASSERT_SEGFAULT(ft_htset(ft_htnew(1), "", strdup(""))); + TEST_ASSERT_SEGFAULT(ft_htset(ft_htnew(1), "hi", strdup(""))); + TEST_ASSERT_SEGFAULT(ft_htset(ft_htnew(1), "asdfasdfasdc", strdup(""))); +} + +TEST(ft_htset, error_null) +{ + TEST_ASSERT_NULL(ft_htset(NULL, "", strdup("1"))); + TEST_ASSERT_NULL(ft_htset(ht, NULL, strdup("2"))); + TEST_ASSERT_NOT_NULL(ft_htset(ht, "", strdup("3"))); + TEST_ASSERT_NOT_NULL(ft_htset(ht, "a", strdup("4"))); + TEST_ASSERT_NOT_NULL(ft_htset(ht, "b", strdup("5"))); + TEST_ASSERT_NOT_NULL(ft_htset(ht, "c", strdup("6"))); + TEST_ASSERT_NOT_NULL(ft_htset(ht, "d", strdup("7"))); +} + +TEST(ft_htset, happy_path) +{ + t_ftht_content *content = ft_htset(ht, "bonjour", strdup("content")); + TEST_ASSERT_NOT_NULL(content); + TEST_ASSERT_NOT_NULL(content->key); + TEST_ASSERT_NOT_NULL(content->value); + TEST_ASSERT_EQUAL_STRING(content->key, "bonjour"); + TEST_ASSERT_EQUAL_STRING(content->value, "content"); +} + +TEST(ft_htset, collision) +{ + +} diff --git a/test/src/main.c b/test/src/main.c new file mode 100644 index 0000000..a81bc8f --- /dev/null +++ b/test/src/main.c @@ -0,0 +1,14 @@ +#include "libft_test.h" + +static void run_all_test(void) +{ + RUN_TEST_GROUP(ft_strlen); + RUN_TEST_GROUP(ft_htnew); + RUN_TEST_GROUP(ft_htget); + RUN_TEST_GROUP(ft_htset); +} + +int main(int argc, const char **argv) +{ + return UnityMain(argc, argv, run_all_test); +} diff --git a/test/src/runner/test_ft_strlen_runner.c b/test/src/runner/test_ft_strlen_runner.c deleted file mode 100644 index 368c033..0000000 --- a/test/src/runner/test_ft_strlen_runner.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "libft_test.h" - -TEST_GROUP_RUNNER(ft_strlen) -{ - RUN_TEST_CASE(ft_strlen, yo); -} diff --git a/test/src/runner/test_runner_ht.c b/test/src/runner/test_runner_ht.c new file mode 100644 index 0000000..d8718af --- /dev/null +++ b/test/src/runner/test_runner_ht.c @@ -0,0 +1,22 @@ +#include "libft_test.h" + +TEST_GROUP_RUNNER(ft_htnew) +{ + RUN_TEST_CASE(ft_htnew, segfault); + RUN_TEST_CASE(ft_htnew, error_null); + RUN_TEST_CASE(ft_htnew, happy_path); +} + +TEST_GROUP_RUNNER(ft_htget) +{ + RUN_TEST_CASE(ft_htget, segfault); + RUN_TEST_CASE(ft_htget, error_null); +} + +TEST_GROUP_RUNNER(ft_htset) +{ + RUN_TEST_CASE(ft_htset, segfault); + RUN_TEST_CASE(ft_htset, error_null); + RUN_TEST_CASE(ft_htset, happy_path); + RUN_TEST_CASE(ft_htset, collision); +} diff --git a/test/src/runner/test_runner_str.c b/test/src/runner/test_runner_str.c new file mode 100644 index 0000000..368c033 --- /dev/null +++ b/test/src/runner/test_runner_str.c @@ -0,0 +1,6 @@ +#include "libft_test.h" + +TEST_GROUP_RUNNER(ft_strlen) +{ + RUN_TEST_CASE(ft_strlen, yo); +} -- cgit