Add test for stdin
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 27 Oct 2023 09:32:48 +0000 (11:32 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 27 Oct 2023 09:32:48 +0000 (11:32 +0200)
Add .stdin test, change main.c to be able to accept stdin, change test
to redirect when stdin is tested.
Improve Makefile, test.

Makefile
src/main.c
test
test_files/.stdin [new file with mode: 0644]

index 9431f046c638ac71692fd88f7ce3381575a683f9..2162dc38debd7f187a16e91472847e20bd8be6eb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,10 @@ SRCDIR := src/
 OBJDIR := obj/
 INCLUDE := include/
 
+RESDIR := results/
+TESTDIR := test_files/
+CORDIR := correct/
+
 SRCS := $(shell find $(SRCDIR) -name '*.c' -type f -printf "%f\n")
 GNLSRCS := get_next_line.c get_next_line_utils.c
 GNLBONUS := $(GNLSRCS:%.c=%_bonus.c)
@@ -13,9 +17,7 @@ OBJS := $(SRCS:%.c=$(OBJDIR)%.o)
 GNLOBJS := $(GNLSRCS:%.c=$(OBJDIR)%.o)
 GNLBONUSOBJS = $(GNLBONUS:%.c=$(OBJDIR)%.o)
 
-TESTDIR := test_files/
-CORDIR := correct/
-TESTFILES := $(shell find $(TESTDIR) -type f | sort)
+TESTFILES := $(shell find $(TESTDIR) -type f -not -path "*/.* | sort) $(TESTDIR).stdin
 CORFILES := $(TESTFILES:$(TESTDIR)%=$(CORDIR)%)
 
 NAME = tester
@@ -33,7 +35,7 @@ $(BONUS) : $(OBJS) $(GNLBONUSOBJS)
        $(CC) $(CFLAGS) $^ -o $@
 
 $(OBJS) : $(OBJDIR)%.o : $(SRCDIR)%.c | $(OBJDIR)
-       $(CC) $(CFLAGS) -c $< -o $@ -I$(INCLUDE)
+       $(CC) $(CFLAGS) -c $< -o $@ -I$(INCLUDE) -DTEST_DIR=\"$(TESTDIR)\" -DRESULT_DIR=\"$(RESDIR)\"
 
 $(GNLOBJS) $(GNLBONUSOBJS) : $(OBJDIR)%.o : $(GNLDIR)%.c | $(OBJDIR)
 ifndef BUFFER_SIZE
@@ -54,6 +56,7 @@ clean :
 
 fclean : clean
        $(RM) $(NAME) $(BONUS)
+       $(RM) $(CORDIR)* $(RESDIR)*
 
 re : fclean
        $(MAKE)
index b0ba366fa828501e54c1c173264089f9d2d1fe04..83efee3d96a6a96843c099563cc238b82230d10a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/12 14:52:24 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/10/26 15:53:28 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/10/27 11:30:50 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -40,6 +40,11 @@ int  *open_all(const int count, char *const *fname)
                return (fds);
        while (i < count)
        {
+               if (fname[i][0] == '-' && fname[i][1] == '\0')
+               {
+                       fds[i++] = 0;
+                       continue ;
+               }
                tmp = ft_strdup(TEST_DIR);
                path = ft_strjoin(tmp, fname[i]);
                fds[i++] = open(path, O_RDONLY);
@@ -52,7 +57,11 @@ int  *open_all(const int count, char *const *fname)
 void   close_all(int count, int *fds)
 {
        while (count > 0)
-               close(fds[--count]);
+       {
+               if (fds[--count] == 0)
+                       continue ;
+               close(fds[count]);
+       }
        free(fds);
        return ;
 }
diff --git a/test b/test
index d48adbc38eff6dbd19a1864269e7d4c88fa55355..9d7aa8824cdf89cfe02bc8213fedeccb3312e754 100755 (executable)
--- a/test
+++ b/test
@@ -5,14 +5,14 @@ m=0
 
 for arg in "$@"
 do
-       if [ $arg == 'a' ]
+       if [ "$arg" == 'a' ]
        then
                b=1
                m=1
-       elif [ $arg == 'b' ]
+       elif [ "$arg" == 'b' ]
        then
                b=1
-       elif [ $arg == 'm' ]
+       elif [ "$arg" == 'm' ]
        then
                m=1
        fi
@@ -48,25 +48,40 @@ then
        fi
        printf "\033[32mCompilation successful\n\033[0m"
 
-       make --silent all_tests SEP=$SEP
+       make --silent all_tests SEP="$SEP"
        wait
 
+       valgrind --quiet --leak-check=full --log-file='tmp' ./tester - <test_files/.stdin > /dev/null
+       wait
+       if $(cmp -s ./correct/- ./results/-)
+       then
+               printf "\033[32m"
+               printf "stdin: OK\n"
+               printf "$(<tmp)\n"
+       else
+               printf "\033[31m"
+               printf "stdin: KO\n"
+               printf "diff -y ./correct/"$file" ./results/"$file"\n"
+               printf "$(diff -y ./correct/"$file" ./results/"$file")\n"
+               printf "$(<tmp)\n"
+       fi
+       
        for file in $(ls test_files/)
        do
-               valgrind --quiet --leak-check=full --log-file='tmp' ./tester $file > /dev/null
+               valgrind --quiet --leak-check=full --log-file='tmp' ./tester "$file" > /dev/null
                wait
-                       if $(cmp -s ./correct/$file ./results/$file)
-                       then
-                               printf "\033[32m"
-                               printf "$file: OK\n"
-                               printf "$(<tmp)\n"
-                       else
-                               printf "\033[31m"
-                               printf "$file: KO\n"
-                               printf "diff -y ./correct/$file ./results/$file\n"
-                               printf "$(diff -y ./correct/$file ./results/$file)\n"
-                               printf "$(<tmp)\n"
-                       fi
+               if $(cmp -s ./correct/"$file" ./results/"$file")
+               then
+                       printf "\033[32m"
+                       printf "$file: OK\n"
+                       printf "$(<tmp)\n"
+               else
+                       printf "\033[31m"
+                       printf "$file: KO\n"
+                       printf "diff -y ./correct/$file ./results/$file\n"
+                       printf "$(diff -y ./correct/"$file" ./results/"$file")\n"
+                       printf "$(<tmp)\n"
+               fi
        done
 
        for BS in ${testing_sizes[@]}
@@ -76,9 +91,9 @@ then
                wait
                for file in $(ls test_files/)
                do
-                       valgrind --quiet --leak-check=full --log-file='tmp' ./tester $file > /dev/null
+                       valgrind --quiet --leak-check=full --log-file='tmp' ./tester "$file" > /dev/null
                        wait
-                       if $(cmp -s ./correct/$file ./results/$file)
+                       if $(cmp -s ./correct/"$file" ./results/"$file")
                        then
                                printf "\033[32m"
                                printf "$file: OK\n"
@@ -87,7 +102,7 @@ then
                                printf "\033[31m"
                                printf "$file: KO\n"
                                printf "diff -y ./correct/$file ./results/$file\n"
-                               printf "$(diff -y ./correct/$file ./results/$file)\n"
+                               printf "$(diff -y ./correct/"$file" ./results/"$file")\n"
                                printf "$(<tmp)\n"
                        fi
                done
@@ -122,7 +137,7 @@ then
                printf "\033[0m\nTrying BUFFER_SIZE = $BS\n"
                make --silent bonus BUFFER_SIZE=$BS
                wait
-               mix_res_path=$(valgrind --quiet --leak-check=full --log-file='tmp' ./tester_bonus $(find ./test_files/ -type f -printf "%f\n" | sort))
+               mix_res_path=$(valgrind --quiet --leak-check=full --log-file='tmp' ./tester_bonus - $(find ./test_files/ -type f -not -path "*/.*" -printf "%f\n" | sort) <test_files/.stdin)
                if $(cmp -s ./correct/mix $mix_res_path)
                then
                        printf "\033[32m"
diff --git a/test_files/.stdin b/test_files/.stdin
new file mode 100644 (file)
index 0000000..3aefd4d
--- /dev/null
@@ -0,0 +1,3 @@
+This file is used for standard input test.
+I am redirecting this file to the tester.
+Hopefully this goes well.