aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-31 10:44:30 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-02 22:12:42 +0100
commitac0559db82bfef3b7ca3d976b1d7700ed2c1c1fc (patch)
tree214d344c714bdf5b53d651dbd11fec9e0db283cb /test
parent9db52bc5ef545a3fa9973002e9a28a2ece68d029 (diff)
downloadlibft-ac0559db82bfef3b7ca3d976b1d7700ed2c1c1fc.tar.gz
libft-ac0559db82bfef3b7ca3d976b1d7700ed2c1c1fc.tar.bz2
libft-ac0559db82bfef3b7ca3d976b1d7700ed2c1c1fc.zip
hash table unit testing, norming
Diffstat (limited to 'test')
-rw-r--r--test/Makefile27
-rw-r--r--test/include/helper/helper_segfault.h19
-rw-r--r--test/include/libft_test.h6
-rwxr-xr-xtest/libft_testbin34012 -> 45308 bytes
-rw-r--r--test/src/ht/test_ft_htcontent_new.c0
-rw-r--r--test/src/ht/test_ft_htdelone.c0
-rw-r--r--test/src/ht/test_ft_htdelone_key.c0
-rw-r--r--test/src/ht/test_ft_htdestroy.c0
-rw-r--r--test/src/ht/test_ft_htdestroy_all.c0
-rw-r--r--test/src/ht/test_ft_htdestroy_key.c0
-rw-r--r--test/src/ht/test_ft_htget.c35
-rw-r--r--test/src/ht/test_ft_hthash.c0
-rw-r--r--test/src/ht/test_ft_htnew.c38
-rw-r--r--test/src/ht/test_ft_htset.c51
-rw-r--r--test/src/main.c14
-rw-r--r--test/src/runner/test_runner_ht.c22
-rw-r--r--test/src/runner/test_runner_str.c (renamed from test/src/runner/test_ft_strlen_runner.c)0
17 files changed, 206 insertions, 6 deletions
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 <string.h>
+# include <sys/wait.h>
+
# 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
--- a/test/libft_test
+++ b/test/libft_test
Binary files 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
--- /dev/null
+++ b/test/src/ht/test_ft_htcontent_new.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_htdelone.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_htdelone_key.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_htdestroy.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_htdestroy_all.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_htdestroy_key.c
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
--- /dev/null
+++ b/test/src/ht/test_ft_hthash.c
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_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_ft_strlen_runner.c b/test/src/runner/test_runner_str.c
index 368c033..368c033 100644
--- a/test/src/runner/test_ft_strlen_runner.c
+++ b/test/src/runner/test_runner_str.c