aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-06 00:28:25 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-06 00:28:25 +0100
commit88f29720d2a09eaef81ad3646169d6bc19be8bfb (patch)
treef7bcea07a8a7e4076c1817609c4a60d7e8213066
parent1ab2fedd108b26c9624454a897e2c518aaff7d32 (diff)
downloadlibasm_test-88f29720d2a09eaef81ad3646169d6bc19be8bfb.tar.gz
libasm_test-88f29720d2a09eaef81ad3646169d6bc19be8bfb.tar.bz2
libasm_test-88f29720d2a09eaef81ad3646169d6bc19be8bfb.zip
Added ft_list_size and ft_list_push_front test
-rw-r--r--Makefile15
-rw-r--r--functions_reference/ref_ft_atoi_base.c1
-rw-r--r--functions_reference/ref_ft_list_push_front.c11
-rw-r--r--functions_reference/ref_ft_list_size.c4
-rw-r--r--helper_list.c48
-rw-r--r--libasm_test.h18
-rw-r--r--main.c4
-rw-r--r--prettier.py37
-rw-r--r--test/ft_list_push_front_test.c49
-rw-r--r--test/ft_list_size_test.c44
-rw-r--r--test/ft_read_test.c27
-rw-r--r--test/ft_write_test.c28
12 files changed, 219 insertions, 67 deletions
diff --git a/Makefile b/Makefile
index 468998a..626d633 100644
--- a/Makefile
+++ b/Makefile
@@ -9,13 +9,14 @@ endif
LIBASM_PATH = ../libasm
-CC = gcc -fPIC -no-pie
+CC = gcc
CCFLAGS = -I. -Wall -Wextra
LDFLAGS = -L$(LIBASM_PATH) -lasm
NAME = runtest
SRC = main.c \
helper.c \
+ helper_list.c \
test/ft_strlen_test.c \
test/ft_strcpy_test.c \
test/ft_strcmp_test.c \
@@ -23,8 +24,11 @@ SRC = main.c \
test/ft_read_test.c \
test/ft_strdup_test.c \
test/ft_atoi_base_test.c \
- functions_reference/ref_ft_atoi_base.c
- #ft_list_push_front_test.c ft_list_size_test.c \
+ test/ft_list_size_test.c \
+ test/ft_list_push_front_test.c \
+ functions_reference/ref_ft_atoi_base.c \
+ functions_reference/ref_ft_list_size.c \
+ functions_reference/ref_ft_list_push_front.c
# ft_list_sort_test.c ft_list_remove_if_test.c \
OBJ = $(SRC:.c=.o)
@@ -32,9 +36,12 @@ OBJ = $(SRC:.c=.o)
run: pretty
pretty: all
- ./$(NAME) | $(PYTHON) prettier.py
+ ./$(NAME) 2> /dev/null | $(PYTHON) prettier.py
run_raw: all
+ ./$(NAME) 2> /dev/null
+
+run_debug: all
./$(NAME)
all: $(NAME)
diff --git a/functions_reference/ref_ft_atoi_base.c b/functions_reference/ref_ft_atoi_base.c
index b2a4231..d668254 100644
--- a/functions_reference/ref_ft_atoi_base.c
+++ b/functions_reference/ref_ft_atoi_base.c
@@ -1,3 +1,4 @@
+#include <ctype.h>
#include "libasm_test.h"
static bool
diff --git a/functions_reference/ref_ft_list_push_front.c b/functions_reference/ref_ft_list_push_front.c
index 821247d..22ca4a9 100644
--- a/functions_reference/ref_ft_list_push_front.c
+++ b/functions_reference/ref_ft_list_push_front.c
@@ -3,9 +3,12 @@
void
ref_ft_list_push_front(t_list **begin_list, void *data)
{
-
- if (begin_list == NULL || data == NULL)
+ if (begin_list == NULL)
+ return ;
+ t_list *new = malloc(sizeof(t_list));
+ if (new == NULL)
return ;
- data->next = *begin_list;
- *begin_list = data;
+ new->data = data;
+ new->next = *begin_list;
+ *begin_list = new;
}
diff --git a/functions_reference/ref_ft_list_size.c b/functions_reference/ref_ft_list_size.c
index 411f0f5..d3e7583 100644
--- a/functions_reference/ref_ft_list_size.c
+++ b/functions_reference/ref_ft_list_size.c
@@ -6,9 +6,9 @@ ref_ft_list_size(t_list *begin_list)
int counter;
counter = 0;
- while (begin_list)
+ while (begin_list != NULL)
{
- counter++
+ counter++;
begin_list = begin_list->next;
}
return counter;
diff --git a/helper_list.c b/helper_list.c
index 71e69fa..5ab94be 100644
--- a/helper_list.c
+++ b/helper_list.c
@@ -1,15 +1,23 @@
#include "libasm_test.h"
-static t_list*
-create_elem(int data)
+int*
+create_data_elem(int data)
{
int *data_ptr = malloc(sizeof(int));
if (data_ptr == NULL)
return (NULL);
+ *data_ptr = data;
+ return data_ptr;
+}
+
+t_list*
+create_elem(int data)
+{
t_list *new = malloc(sizeof(t_list));
if (new == NULL)
return NULL;
- new->data = data_ptr;
+ if ((new->data = create_data_elem(data)) == NULL)
+ return (NULL);
new->next = NULL;
return new;
}
@@ -39,17 +47,13 @@ list_from_format(char *fmt)
{
t_list *head = NULL;
- while (*fmt)
+ while (fmt != NULL && *fmt)
{
- int n = ft_atoi(*fmt);
+ int n = (int)strtol(fmt, &fmt, 10);
t_list *elem = create_elem(n);
push_front(&head, elem);
- while (isdigit(*fmt))
- fmt++;
- if (*fmt)
- fmt++;
}
- return head;
+ return reverse(head);
}
t_list*
@@ -66,7 +70,7 @@ list_dup(t_list *list)
}
int
-list_cmp(t_list *l1, t_list l2)
+list_cmp(t_list *l1, t_list *l2)
{
if (l1 == NULL && l2 == NULL)
return 0;
@@ -74,6 +78,10 @@ list_cmp(t_list *l1, t_list l2)
return -1;
if (l2 == NULL)
return 1;
+ if (l1->data == NULL)
+ return -1;
+ if (l2->data == NULL)
+ return 1;
if (*(int*)l1->data != *(int*)l2->data)
return *(int*)l1->data - *(int*)l2->data;
return list_cmp(l1->next, l2->next);
@@ -83,6 +91,20 @@ void
list_print(t_list *list)
{
while (list != NULL)
- printf("[%d] -> ", *(int*)list->data);
- printf("(null)\n");
+ {
+ printf("[%p] -> ", (int*)list->data);
+ list = list->next;
+ }
+ printf("(null)");
+}
+
+void
+list_destroy(t_list *list)
+{
+ if (list == NULL)
+ return ;
+ list_destroy(list->next);
+ if (list->data != NULL)
+ free(list->data);
+ free(list);
}
diff --git a/libasm_test.h b/libasm_test.h
index 8be0b68..28f516e 100644
--- a/libasm_test.h
+++ b/libasm_test.h
@@ -83,7 +83,6 @@ void
ft_list_remove_if_test(void);
-
/*
* helpers
*/
@@ -94,6 +93,21 @@ print_signaled_ko(void);
void
expect_int(int expected, int actual);
+int*
+create_data_elem(int data);
+t_list*
+create_elem(int data);
+t_list*
+list_from_format(char *fmt);
+t_list*
+list_dup(t_list *list);
+int
+list_cmp(t_list *l1, t_list *l2);
+void
+list_print(t_list *list);
+void
+list_destroy(t_list *list);
+
/*
* function of reference
*/
@@ -123,7 +137,7 @@ bool signaled;
exit(EXIT_SUCCESS); \
} else { \
wait(&pid); \
- signaled = !WIFEXITED(pid); \
+ signaled = !WIFEXITED(pid); \
} \
} while(0);
diff --git a/main.c b/main.c
index b81a1ed..8a170ff 100644
--- a/main.c
+++ b/main.c
@@ -13,8 +13,8 @@ main(void)
ft_strdup_test();
ft_atoi_base_test();
- /* ft_list_push_front_test(); */
- /* ft_list_size_test(); */
+ ft_list_push_front_test();
+ ft_list_size_test();
/* ft_list_sort_test(); */
/* ft_list_remove_if_test(); */
return 0;
diff --git a/prettier.py b/prettier.py
index b8f4aab..f73e0e4 100644
--- a/prettier.py
+++ b/prettier.py
@@ -1,3 +1,15 @@
+# **************************************************************************** #
+# #
+# ::: :::::::: #
+# prettier.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/02/05 23:04:13 by cacharle #+# #+# #
+# Updated: 2020/02/05 23:07:13 by cacharle ### ########.fr #
+# #
+# **************************************************************************** #
+
import os
import sys
import re
@@ -31,6 +43,7 @@ def parse():
print("\n", ''.join([" " for _ in range(1 + len(line[4:]))]), end="")
print(green("[OK] "), end="")
continue
+ m = None
if line.find("SEGFAULT") != -1:
name = line[4:]
else:
@@ -47,13 +60,21 @@ def parse():
print("\n\n", name, ": ", sep="", end="")
l = logs.get(name)
l["ko_counter"] += 1
- l["ko_info"].append({
- "type": "COMPARE",
- "expected": m.group(2),
- "actual": m.group(3),
- "with": None
- })
- if m.group(4) is not None:
+ if m is None:
+ l["ko_info"].append({
+ "type": "SEGFAULT",
+ "expected": None,
+ "actual": None,
+ "with": None
+ })
+ else:
+ l["ko_info"].append({
+ "type": "COMPARE",
+ "expected": m.group(2),
+ "actual": m.group(3),
+ "with": None
+ })
+ if m is not None and m.group(4) is not None:
l["ko_info"][-1]["with"] = m.group(4)[6:]
if (l["ok_counter"] + l["ko_counter"]) % 15 == 0:
print("\n", ''.join([" " for _ in range(1 + len(name))]), end="")
@@ -73,4 +94,4 @@ if __name__ == "__main__":
p = f"{k}"
if e["with"] is not None:
p += f"with {e['with']}"
- print(p, f":\n {green('expected: ', e['expected'])}\n {red('actual: ', e['actual'])}")
+ print(p, f":\n {green('expected: ') + e['expected']}\n {red('actual: ') + e['actual']}")
diff --git a/test/ft_list_push_front_test.c b/test/ft_list_push_front_test.c
index 411194e..2f9cf59 100644
--- a/test/ft_list_push_front_test.c
+++ b/test/ft_list_push_front_test.c
@@ -1,17 +1,60 @@
#include "libasm_test.h"
+static t_list *tmp;
+static t_list *expected;
+static t_list *actual;
+
+#define FT_LIST_PUSH_FRONT_EXPECT(fmt, push) do { \
+ expected = list_from_format(fmt); \
+ actual = list_from_format(fmt); \
+ ref_ft_list_push_front(&expected, create_data_elem(push)); \
+ ft_list_push_front(&actual, create_data_elem(push)); \
+ if (list_cmp(expected, actual) != 0) { \
+ printf("KO: [COMPARE]: %s: expected: ", test_name); \
+ list_print(expected); \
+ printf(" got: "); \
+ list_print(actual); \
+ putchar('\n'); \
+ } else \
+ print_ok(); \
+ list_destroy(expected); \
+ list_destroy(actual); \
+} while (0);
+
static void
ft_list_push_front_segfault(void)
{
-
+ TEST_ASM_FUNCTION(tmp = NULL; ft_list_push_front(&tmp, malloc(1));list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2 3"); ft_list_push_front(&tmp, create_data_elem(34)); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2 3 4 10"); ft_list_push_front(&tmp, create_data_elem(1)); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("111234 1234 1112"); ft_list_push_front(&tmp, create_data_elem(7)); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2"); ft_list_push_front(&tmp, create_data_elem(0)); list_destroy(tmp));
+ TEST_ASM_FUNCTION(
+ tmp = list_from_format("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20");
+ ft_list_push_front(tmp, create_data_elem(4));
+ ft_list_push_front(tmp, create_data_elem(4));
+ ft_list_push_front(tmp, create_data_elem(4));
+ ft_list_push_front(tmp, create_data_elem(4));
+ list_destroy(tmp);
+ );
}
+
static void
ft_list_push_front_compare(void)
{
-
+ FT_LIST_PUSH_FRONT_EXPECT("", 0);
+ FT_LIST_PUSH_FRONT_EXPECT("", 1);
+ FT_LIST_PUSH_FRONT_EXPECT("0", 1);
+ FT_LIST_PUSH_FRONT_EXPECT("1 2 3", 4);
+ FT_LIST_PUSH_FRONT_EXPECT("1 2 3", -1);
+ FT_LIST_PUSH_FRONT_EXPECT("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", 7);
}
+
void
ft_list_push_front_test(void)
{
-
+ test_name = "ft_list_push_front.s";
+ ft_list_push_front_segfault();
+ if (!signaled)
+ ft_list_push_front_compare();
}
diff --git a/test/ft_list_size_test.c b/test/ft_list_size_test.c
index 80394d7..5c36095 100644
--- a/test/ft_list_size_test.c
+++ b/test/ft_list_size_test.c
@@ -1,17 +1,55 @@
#include "libasm_test.h"
+static t_list *tmp;
+static int expected;
+static int actual;
+
+#define FT_LIST_SIZE_EXPECT(fmt) do { \
+ tmp = list_from_format(fmt); \
+ expected = ref_ft_list_size(tmp); \
+ actual = ft_list_size(tmp); \
+ if (expected != actual) { \
+ printf("KO: [COMPARE]: %s: expected: %d ", \
+ test_name, expected); \
+ list_print(tmp); \
+ printf(" got: %d\n", actual); \
+ } else \
+ print_ok(); \
+ list_destroy(tmp); \
+} while (0);
+
static void
ft_list_size_segfault(void)
{
-
+ TEST_ASM_FUNCTION(tmp = list_from_format(""); ft_list_size(tmp); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2 3"); ft_list_size(tmp); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2 3 4 10"); ft_list_size(tmp); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("111234 1234 1112"); ft_list_size(tmp); list_destroy(tmp));
+ TEST_ASM_FUNCTION(tmp = list_from_format("1 2"); ft_list_size(tmp); list_destroy(tmp));
+ TEST_ASM_FUNCTION(
+ tmp = list_from_format("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20");
+ ft_list_size(tmp);
+ list_destroy(tmp)
+ );
}
+
static void
ft_list_size_compare(void)
{
-
+ FT_LIST_SIZE_EXPECT("1 2 3");
+ FT_LIST_SIZE_EXPECT("");
+ FT_LIST_SIZE_EXPECT("1 2 3 4 10");
+ FT_LIST_SIZE_EXPECT("19879 123 12344");
+ FT_LIST_SIZE_EXPECT("1");
+ FT_LIST_SIZE_EXPECT("1 2");
+ FT_LIST_SIZE_EXPECT("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20");
}
+
void
ft_list_size_test(void)
{
-
+ test_name = "ft_list_size.s";
+ ft_list_size_segfault();
+ if (!signaled)
+ ft_list_size_compare();
}
diff --git a/test/ft_read_test.c b/test/ft_read_test.c
index 50a85bd..5b29803 100644
--- a/test/ft_read_test.c
+++ b/test/ft_read_test.c
@@ -6,19 +6,20 @@ static int ft_read_pipe[2];
static char buf[FT_READ_BUF_SIZE];
static int ret;
-#define FT_READ_EXPECT(str) do { \
- if (pipe(ft_read_pipe) < 0) \
- exit(EXIT_FAILURE); \
- fcntl(ft_read_pipe[0], F_SETFL, O_NONBLOCK); \
- write(ft_read_pipe[1], str, strlen(str)); \
- ret = ft_read(ft_read_pipe[0], buf, FT_READ_BUF_SIZE); \
- buf[ret] = '\0'; \
- if (strcmp(buf, str) != 0) \
- printf("KO: [COMPARE]: %s: expected: \"%s\" got: \"%s\"\n", test_name, str, buf); \
- else \
- print_ok(); \
- close(ft_read_pipe[1]); \
- close(ft_read_pipe[0]); \
+#define FT_READ_EXPECT(str) do { \
+ if (pipe(ft_read_pipe) < 0) \
+ exit(EXIT_FAILURE); \
+ fcntl(ft_read_pipe[0], F_SETFL, O_NONBLOCK); \
+ write(ft_read_pipe[1], str, strlen(str)); \
+ ret = ft_read(ft_read_pipe[0], buf, strlen(str)); \
+ buf[ret] = '\0'; \
+ if (strcmp(buf, str) != 0 || ret != strlen(str)) \
+ printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %d \"%s\"\n", \
+ test_name, strlen(str), str, ret, buf); \
+ else \
+ print_ok(); \
+ close(ft_read_pipe[1]); \
+ close(ft_read_pipe[0]); \
} while (0);
void
diff --git a/test/ft_write_test.c b/test/ft_write_test.c
index 1c14244..c44625e 100644
--- a/test/ft_write_test.c
+++ b/test/ft_write_test.c
@@ -4,21 +4,23 @@
static int ft_write_pipe[2];
static char buf[FT_WRITE_BUF_SIZE];
+static unsigned long write_ret;
static int ret;
-#define FT_WRITE_EXPECT(str) do { \
- if (pipe(ft_write_pipe) < 0) \
- exit(EXIT_FAILURE); \
- fcntl(ft_write_pipe[0], F_SETFL, O_NONBLOCK); \
- ft_write(ft_write_pipe[1], str, strlen(str)); \
- ret = read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \
- buf[ret] = '\0'; \
- if (strcmp(buf, str) != 0) \
- printf("KO: [COMPARE]: %s: expected: \"%s\" got: \"%s\"\n", test_name, str, buf); \
- else \
- print_ok(); \
- close(ft_write_pipe[1]); \
- close(ft_write_pipe[0]); \
+#define FT_WRITE_EXPECT(str) do { \
+ if (pipe(ft_write_pipe) < 0) \
+ exit(EXIT_FAILURE); \
+ fcntl(ft_write_pipe[0], F_SETFL, O_NONBLOCK); \
+ write_ret = ft_write(ft_write_pipe[1], str, strlen(str)); \
+ ret = read(ft_write_pipe[0], buf, FT_WRITE_BUF_SIZE); \
+ buf[ret] = '\0'; \
+ if (strcmp(buf, str) != 0 || write_ret != strlen(str)) \
+ printf("KO: [COMPARE]: %s: expected: %lu \"%s\" got: %lu \"%s\"\n", \
+ test_name, strlen(str), str, write_ret, buf); \
+ else \
+ print_ok(); \
+ close(ft_write_pipe[1]); \
+ close(ft_write_pipe[0]); \
} while (0);
void