aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-24 01:04:21 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-24 01:04:21 +0100
commit5a47d63887a0878243b17799b19c4a0f76d3756d (patch)
tree72da1070587a0cd5c0a9599f6aacf8d4cb15b9c2
parent97e84015b635cf51d32b00000dc3fa25390251b4 (diff)
downloadlibasm_test-5a47d63887a0878243b17799b19c4a0f76d3756d.tar.gz
libasm_test-5a47d63887a0878243b17799b19c4a0f76d3756d.tar.bz2
libasm_test-5a47d63887a0878243b17799b19c4a0f76d3756d.zip
ft_write.s, ft_read.s, ft_strdup.s tests and prettier
-rw-r--r--Makefile10
-rw-r--r--README.md5
-rw-r--r--ft_read_test.c60
-rw-r--r--ft_strcmp_test.c21
-rw-r--r--ft_strcpy_test.c2
-rw-r--r--ft_strdup_test.c62
-rw-r--r--ft_write_test.c60
-rw-r--r--helper.c6
-rw-r--r--libasm_test.h6
-rw-r--r--prettier.py86
-rw-r--r--screenshot.pngbin0 -> 436568 bytes
11 files changed, 312 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index fc5fc11..dd317a2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
RM = rm -f
+PYTHON = python3
LIBASM_PATH = ../libasm
@@ -11,8 +12,13 @@ SRC = main.c ft_strlen_test.c ft_strcpy_test.c ft_strcmp_test.c \
ft_write_test.c ft_read_test.c ft_strdup_test.c helper.c
OBJ = $(SRC:.c=.o)
-run: all
- ./runtest
+run: pretty
+
+pretty: all
+ ./$(NAME) | $(PYTHON) prettier.py
+
+run_raw: all
+ ./$(NAME)
all: $(NAME)
diff --git a/README.md b/README.md
index dee1186..bb65945 100644
--- a/README.md
+++ b/README.md
@@ -2,9 +2,12 @@
Unit tests for the libasm project.
+![example\_screenshot](./screenshot.png)
+
## Usage
-`> make run`
+- `> make` show regular pretty output.
+- `> make run_raw` show parsable output.
## Configuration
diff --git a/ft_read_test.c b/ft_read_test.c
index 78246b6..08c3c70 100644
--- a/ft_read_test.c
+++ b/ft_read_test.c
@@ -1,7 +1,67 @@
#include "libasm_test.h"
+#define FT_READ_BUF_SIZE (1 << 12)
+
+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]); \
+} while (0);
+
+void
+ft_read_test_segfault(void)
+{
+ int tmp[2];
+ if (pipe(tmp) < 0)
+ exit(EXIT_FAILURE);
+ TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 5));
+ TEST_ASM_FUNCTION(ft_read(tmp[1], "t", 1));
+ TEST_ASM_FUNCTION(ft_read(tmp[1], "", 0));
+ TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 4));
+ TEST_ASM_FUNCTION(ft_read(tmp[1], "test", 2));
+ TEST_ASM_FUNCTION(ft_read(tmp[1], NULL, 2));
+ close(tmp[0]);
+ close(tmp[1]);
+ TEST_ASM_FUNCTION(ft_read(-1, "tt", 2));
+ TEST_ASM_FUNCTION(ft_read(OPEN_MAX + 1, "tt", 2));
+}
+
+void
+ft_read_test_compare(void)
+{
+ FT_READ_EXPECT("");
+ FT_READ_EXPECT("bon");
+ FT_READ_EXPECT("bonjour");
+ FT_READ_EXPECT("%c%s%p%x%X%e%f%g");
+ FT_READ_EXPECT("the\0hidden");
+ FT_READ_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\
+elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\
+bibendum dolor mi, id fringilla tellus pulvinar eu. Fusce vel fermentum sem. Cras\
+volutpat, eros eget rhoncus rhoncus, diam augue egestas dolor, vitae rutrum nisi\
+felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Phasellus\
+ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\
+tortor, sit amet consequat amet.");
+}
+
void
ft_read_test(void)
{
+ test_name = "ft_read.s";
+ ft_read_test_segfault();
+ if (!signaled)
+ ft_read_test_compare();
}
diff --git a/ft_strcmp_test.c b/ft_strcmp_test.c
index f0ec048..db106df 100644
--- a/ft_strcmp_test.c
+++ b/ft_strcmp_test.c
@@ -67,6 +67,27 @@ tortor, sit amet consequat amet.");
FT_STRCMP_EXPECT("%c%s%p%x%X%e%f%g", "%s%p%x%X%e%f%g");
FT_STRCMP_EXPECT("the\0hidden", "thehidden");
FT_STRCMP_EXPECT("Lorem ipsum dolor st amet, consectetur adipiscing", "Lodsfsdfasdf");
+
+ FT_STRCMP_EXPECT("\x01", "\x01");
+ FT_STRCMP_EXPECT("\x02", "\x01");
+ FT_STRCMP_EXPECT("\x01", "\x02");
+ FT_STRCMP_EXPECT("\xff", "\xff");
+ FT_STRCMP_EXPECT("\xfe", "\xff");
+ FT_STRCMP_EXPECT("\xff", "\xfe");
+
+ FT_STRCMP_EXPECT("\x01\x01", "\x01");
+ FT_STRCMP_EXPECT("\x01\x02", "\x01");
+ FT_STRCMP_EXPECT("\x02\x01", "\x02");
+ FT_STRCMP_EXPECT("\xff\xff", "\xff");
+ FT_STRCMP_EXPECT("\xff\xfe", "\xff");
+ FT_STRCMP_EXPECT("\xfe\xff", "\xfe");
+
+ FT_STRCMP_EXPECT("\x01", "\x01\x01");
+ FT_STRCMP_EXPECT("\x01", "\x01\x02");
+ FT_STRCMP_EXPECT("\x02", "\x02\x01");
+ FT_STRCMP_EXPECT("\xff", "\xff\xff");
+ FT_STRCMP_EXPECT("\xff", "\xff\xfe");
+ FT_STRCMP_EXPECT("\xfe", "\xfe\xff");
}
void
diff --git a/ft_strcpy_test.c b/ft_strcpy_test.c
index 8fb065e..1ca7a49 100644
--- a/ft_strcpy_test.c
+++ b/ft_strcpy_test.c
@@ -47,6 +47,8 @@ volutpat, eros eget rhoncus rhoncus, diam augue egestas dolor, vitae rutrum nisi
felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Phasellus\
ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\
tortor, sit amet consequat amet.");
+ FT_STRCPY_EXPECT("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
+ FT_STRCPY_EXPECT("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
}
void
diff --git a/ft_strdup_test.c b/ft_strdup_test.c
index 2d5f5d0..b0d48b7 100644
--- a/ft_strdup_test.c
+++ b/ft_strdup_test.c
@@ -1,7 +1,69 @@
#include "libasm_test.h"
+static char *tmp;
+
+#define FT_STRDUP_EXPECT(str) do { \
+ tmp = ft_strdup(str); \
+ if (tmp == NULL || strcmp(str, tmp) != 0) \
+ printf("KO: [COMPARE]: %s: expected: \"%s\" got: \"%s\"\n", test_name, str, tmp); \
+ else \
+ print_ok(); \
+ free(tmp); \
+} while (0);
+
+static void
+ft_strdup_test_segfault(void)
+{
+ char *tmp2 = NULL;
+ char a;
+
+ TEST_ASM_FUNCTION(ft_strdup(""));
+ TEST_ASM_FUNCTION(ft_strdup("abc"));
+ TEST_ASM_FUNCTION(ft_strdup("asl;fjl;asdjfjkasdl;fjadjsf"));
+ TEST_ASM_FUNCTION(ft_strdup("yope\0la"));
+ TEST_ASM_FUNCTION(ft_strdup("Lorem ipsum dolor sit amet, consectetur adipiscing\
+elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\
+bibendum dolor mi, id fringilla tellus pulvinar eu. Fusce vel fermentum sem. Cras\
+volutpat, eros eget rhoncus rhoncus, diam augue egestas dolor, vitae rutrum nisi\
+felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Phasellus\
+ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\
+tortor, sit amet consequat amet."));
+
+ tmp2 = ft_strdup("");
+ TEST_ASM_FUNCTION(a = *tmp2);
+ tmp2 = ft_strdup("abc");
+ TEST_ASM_FUNCTION(a = *tmp2);
+ tmp2 = ft_strdup("asl;fjl;asd");
+ TEST_ASM_FUNCTION(a = *tmp2);
+ tmp2 = ft_strdup("yope\0la");
+ TEST_ASM_FUNCTION(a = *tmp2);
+}
+
+static void
+ft_strdup_test_compare(void)
+{
+ FT_STRDUP_EXPECT("");
+ FT_STRDUP_EXPECT("abc");
+ FT_STRDUP_EXPECT("asl;fjl;asdjfjkasdl;fjadjsf");
+ FT_STRDUP_EXPECT("yope\0la");
+ FT_STRDUP_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\
+elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\
+bibendum dolor mi, id fringilla tellus pulvinar eu. Fusce vel fermentum sem. Cras\
+volutpat, eros eget rhoncus rhoncus, diam augue egestas dolor, vitae rutrum nisi\
+felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Phasellus\
+ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\
+tortor, sit amet consequat amet.");
+ FT_STRDUP_EXPECT("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
+ FT_STRDUP_EXPECT("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
+}
+
+
void
ft_strdup_test(void)
{
+ test_name = "ft_strdup.s";
+ ft_strdup_test_segfault();
+ if (!signaled)
+ ft_strdup_test_compare();
}
diff --git a/ft_write_test.c b/ft_write_test.c
index 8b7d126..c98c153 100644
--- a/ft_write_test.c
+++ b/ft_write_test.c
@@ -1,7 +1,67 @@
#include "libasm_test.h"
+#define FT_WRITE_BUF_SIZE (1 << 12)
+
+static int ft_write_pipe[2];
+static char buf[FT_WRITE_BUF_SIZE];
+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]); \
+} while (0);
+
+void
+ft_write_test_segfault(void)
+{
+ int tmp[2];
+ if (pipe(tmp) < 0)
+ exit(EXIT_FAILURE);
+ TEST_ASM_FUNCTION(ft_write(tmp[1], "test", 5));
+ TEST_ASM_FUNCTION(ft_write(tmp[1], "t", 1));
+ TEST_ASM_FUNCTION(ft_write(tmp[1], "", 0));
+ TEST_ASM_FUNCTION(ft_write(tmp[1], "test", 4));
+ TEST_ASM_FUNCTION(ft_write(tmp[1], "test", 2));
+ TEST_ASM_FUNCTION(ft_write(tmp[1], NULL, 2));
+ close(tmp[0]);
+ close(tmp[1]);
+ TEST_ASM_FUNCTION(ft_write(-1, "tt", 2));
+ TEST_ASM_FUNCTION(ft_write(OPEN_MAX + 1, "tt", 2));
+}
+
+void
+ft_write_test_compare(void)
+{
+ FT_WRITE_EXPECT("");
+ FT_WRITE_EXPECT("bon");
+ FT_WRITE_EXPECT("bonjour");
+ FT_WRITE_EXPECT("%c%s%p%x%X%e%f%g");
+ FT_WRITE_EXPECT("the\0hidden");
+ FT_WRITE_EXPECT("Lorem ipsum dolor sit amet, consectetur adipiscing\
+elit. Sed in malesuada purus. Etiam a scelerisque massa. Ut non euismod elit. Aliquam\
+bibendum dolor mi, id fringilla tellus pulvinar eu. Fusce vel fermentum sem. Cras\
+volutpat, eros eget rhoncus rhoncus, diam augue egestas dolor, vitae rutrum nisi\
+felis sed purus. Mauris magna ex, mollis non suscipit eu, lacinia ac turpis. Phasellus\
+ac tortor et lectus fermentum lobortis eu at mauris. Vestibulum sit amet posuere\
+tortor, sit amet consequat amet.");
+}
+
void
ft_write_test(void)
{
+ test_name = "ft_write.s";
+ ft_write_test_segfault();
+ if (!signaled)
+ ft_write_test_compare();
}
diff --git a/helper.c b/helper.c
index b2b2a18..1808628 100644
--- a/helper.c
+++ b/helper.c
@@ -5,25 +5,29 @@ void
print_ok(void)
{
printf("OK: %s\n", test_name);
+ fflush(stdout);
}
void
print_stack_alignment_error(void)
{
printf("KO: [STACK ALIGNMENT]: %s\n", test_name);
+ fflush(stdout);
}
void
print_signaled_ko(void)
{
printf("KO: [SEGFAULT]: %s\n", test_name);
+ fflush(stdout);
}
void
expect_int(int expected, int actual)
{
if (actual != expected)
- printf("KO: [COMPARE]: %s: expected: %d got: %d\n", test_name, expected, actual);
+ printf("KO: [COMPARE]: %s: expected: %d got: %d\n", test_name, expected, actual);
else
print_ok();
+ fflush(stdout);
}
diff --git a/libasm_test.h b/libasm_test.h
index 98a6ee8..6a71788 100644
--- a/libasm_test.h
+++ b/libasm_test.h
@@ -2,11 +2,13 @@
# define LIBASM_TEST_H
# include <unistd.h>
+# include <fcntl.h>
# include <stdbool.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <sys/wait.h>
+# include <limits.h>
# include <stddef.h>
/*
@@ -83,11 +85,11 @@ bool signaled;
if ((pid = fork()) < 0) \
exit(EXIT_FAILURE); \
if (pid == 0) { \
- do { x; } while(0); \
+ do { x; } while(0); \
exit(EXIT_SUCCESS); \
} else { \
wait(&pid); \
- signaled = WIFSIGNALED(pid); \
+ signaled = !WIFEXITED(pid); \
} \
} while(0);
diff --git a/prettier.py b/prettier.py
new file mode 100644
index 0000000..ce8a839
--- /dev/null
+++ b/prettier.py
@@ -0,0 +1,86 @@
+import os
+import sys
+import re
+import argparse
+
+
+def green(*strings):
+ return "".join([f"\033[32m{s}\033[0m" for s in strings])
+
+
+def red(*strings):
+ return "".join([f"\033[31m{s}\033[0m" for s in strings])
+
+
+def parse_args():
+ parser = argparse.ArgumentParser( prog="ft_printf test", description="A ~quicker tester for ft_printf")
+ parser.add_argument("-v", "--verbose",
+ help="increase verbosity", action="store_true")
+ parser.add_argument("-q", "--quiet",
+ help="decrease vebosity", action="store_true")
+ parser.add_argument("-l", "--no-log",
+ help="disable result log", action="store_true")
+ parser.add_argument("-c", "--no-clear", help="disable terminal clear before output")
+ parser.add_argument("-i", "--interactive", help="print fail as them come",
+ action="store_true")
+ parser.add_argument("-f", "--output-file", help="output file name")
+ return vars(parser.parse_args(sys.argv[1:]))
+
+
+def print_log_ko(ko, options):
+ print(f"- [{red(ko['type'])}] ft_printf({ko['args']})")
+ if options["verbose"]:
+ print(" expected: ", ko["expected"])
+ print(" actual: ", ko["actual"])
+ print()
+
+def create_logs_entry(logs, key):
+ logs[key] = {}
+ logs[key]["ok_counter"] = 0
+ logs[key]["ko_counter"] = 0
+ logs[key]["ko_info"] = []
+
+def parse():
+ logs = {}
+ for line in sys.stdin:
+ line = line.strip()
+ if line[:2] == "OK":
+ l = logs.get(line[4:])
+ if l is None:
+ print("\n\n", line[4:], ": ", sep="", end="")
+ create_logs_entry(logs, line[4:])
+ logs[line[4:]]["ok_counter"] += 1
+ if (logs[line[4:]]["ok_counter"] + logs[line[4:]]["ko_counter"]) % 10 == 0:
+ print("\n", ''.join([" " for _ in range(1 + len(line[4:]))]), end="")
+ print(green("[OK] "), end="")
+ continue
+ m = re.search("^KO: \[(SEGFAULT|COMPARE)\]: (.*): expected: (.*) got: (.*)$", line)
+ if m is None:
+ print(line)
+ print("PARSING ERROR")
+ continue
+ l = logs.get(m.group(2))
+ if l is None:
+ print("\n")
+ create_logs_entry(logs, m.group(2))
+ l["ko_counter"] += 1
+ l["ko_info"].append({
+ "type": m.group(1),
+ "expected": m.group(3),
+ "actual": m.group(4),
+ })
+ if (l["ok_counter"] + l["ko_counter"]) % 10 == 0:
+ print("\n", ''.join([" " for _ in range(1 + len(m.group(2)))]), end="")
+ print(red("[KO] "), end="")
+ return logs
+
+if __name__ == "__main__":
+ os.system("clear")
+ logs = parse()
+ print("\n")
+ for k, v in logs.items():
+ for e in v["ko_info"]:
+ if e['type'] == "SEGFAULT":
+ print(f"{k} : {red(SEGFAULT)}")
+ elif e['type'] == "COMPARE":
+ print(f"{k}:\n {green('expected: ', e['expected'])}\n {red('actual: ', e['actual'])}")
diff --git a/screenshot.png b/screenshot.png
new file mode 100644
index 0000000..a64181a
--- /dev/null
+++ b/screenshot.png
Binary files differ