Skip to content

Commit

Permalink
unit tests - mandatory and bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoriagalli committed Sep 21, 2020
1 parent 56a20f2 commit 3427093
Show file tree
Hide file tree
Showing 33 changed files with 769 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.o
*.out
*.swp
*.swo
*.a
67 changes: 67 additions & 0 deletions runtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#! /bin/bash

declare -x ORIG=0
declare -x IMPLEM=1

rm -rf unit_tests/diffs
mkdir unit_tests/diffs
echo -e "---------------------------------"
echo -e "| \033[0;1m LIBASM UNIT TEST\033[0m |"
echo -e "---------------------------------"

function run_bonus()
{
make bonus
cd unit_tests
if [ "$2" == "" ]
then
bash run_atoi_base.sh
bash run_linked_list.sh
else
for var in $@
do
if [ "$var" == "atoi_base" ]
then
bash run_atoi_base.sh
elif [ "$var" == "list_push_front" ] ||[ "$var" == "list_size" ] ||
[ "$var" == "list_sort" ] || [ "$var" == "list_remove_if" ]
then
bash run_linked_list.sh "$var"
fi
done
fi
}

function run_mandatory()
{
make re
cd unit_tests
if [ "$1" == "" ]
then
bash run_stdfunct.sh strlen
bash run_stdfunct.sh strcpy
bash run_stdfunct.sh strcmp
bash run_stdfunct.sh strdup
bash run_syscall.sh read
bash run_syscall.sh write
else
for var in $@
do
if [ "$var" == "strlen" ] ||[ "$var" == "strcpy" ] ||
[ "$var" == "strcmp" ] || [ "$var" == "strdup" ]
then
bash run_stdfunct.sh "$var"
elif [ "$var" == "read" ] || [ "$var" == "write" ]
then
bash run_syscall.sh "$var"
fi
done
fi
}

if [ "$1" == "bonus" ]
then
run_bonus $@
else
run_mandatory $@
fi
49 changes: 49 additions & 0 deletions unit_tests/run_atoi_base.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#! /bin/bash

test_input()
{
./atoi_base "$1" > "$1".txt
diff "$1".txt "$1".exp.txt > diffs/atoi_base."$1".txt
DIFF=$(diff "$1".txt "$1".exp.txt)
if [ "$DIFF" != "" ]
then
echo -e "\033[0;31m[KO]\033[0m"
else
echo -e "\033[0;32m[OK]\033[0m"
rm -rf diffs/atoi_base."$1".txt
fi
rm -rf "$1".txt "$1".exp.txt
}

echo "56" > 1.exp.txt
echo "-255" > 2.exp.txt
echo "-42" > 3.exp.txt
echo "29" > 4.exp.txt
echo "-501" > 5.exp.txt
echo "105" > 6.exp.txt
echo "-3" > 7.exp.txt
echo "0" > 8.exp.txt
echo "0" > 9.exp.txt
echo "0" > 10.exp.txt
echo "0" > 11.exp.txt
echo "0" > 12.exp.txt
echo "0" > 13.exp.txt
echo "0" > 14.exp.txt
echo "0" > 15.exp.txt
echo "0" > 16.exp.txt
echo "0" > 17.exp.txt
echo "0" > 18.exp.txt
echo "0" > 19.exp.txt

echo -e ""
echo -e "\033[0;1m> ft_atoi_base\033[0m"

clang testers/test_atoi_base.c -L../ -lasm -o atoi_base

for i in {1..19}
do
test_input $i
done

rm -rf atoi_base

64 changes: 64 additions & 0 deletions unit_tests/run_linked_list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#! /bin/bash

test_input()
{
./linked_list "$2" "$3" > "$3".txt
diff "$3".txt txt/"$1"."$3".exp.txt > diffs/"$1"."$3".txt
DIFF=$(diff "$3".txt txt/"$1"."$3".exp.txt)
if [ "$DIFF" != "" ]
then
echo -e "\033[0;31m[KO]\033[0m"
else
echo -e "\033[0;32m[OK]\033[0m"
rm -rf diffs/"$1"."$3".txt
fi
rm -rf "$3".txt
}

run_all_functions()
{
echo -e ""
echo -e "\033[0;1m___LINKED LIST___\033[0m"
for i in {0..3}
do
echo -e ""
echo -e "\033[0;1m> ft_""${FUNCT[i]}""\033[0m"
for j in {0..3}
do
test_input ${FUNCT[i]} $i $j
done
done
}

run_function()
{
echo -e ""
echo -e "\033[0;1m> ft_""$1""\033[0m"
for j in {0..3}
do
test_input $1 $2 $j
done

}

clang testers/test_linked_list.c -L../ -lasm -o linked_list
FUNCT=(list_push_front list_size list_sort list_remove_if)

if [ "$1" == "list_push_front" ]; then
only_funct=0
elif [ "$1" == "list_size" ]; then
only_funct=1
elif [ "$1" == "list_sort" ]; then
only_funct=2
elif [ "$1" == "list_remove_if" ]; then
only_funct=3
fi

if [ "$1" == "" ]
then
run_all_functions
else
run_function "$1" $only_funct
fi

rm -rf linked_list
31 changes: 31 additions & 0 deletions unit_tests/run_stdfunct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /bin/bash

test_input()
{
./"$FUNCT" $ORIG "$2" > a
./"$FUNCT" $IMPLEM "$2" > b
diff a b > diffs/ft_"$FUNCT"."$1".txt
DIFF=$(diff a b)
if [ "$DIFF" != "" ]
then
echo -e "\033[0;31m[KO]\033[0m"
else
echo -e "\033[0;32m[OK]\033[0m"
rm -rf diffs/ft_"$FUNCT"."$1".txt
fi
rm -rf a b
}

FUNCT=$1
echo -e ""
echo -e "\033[0;1m> ft_"$FUNCT"\033[0m"

clang testers/test_"$FUNCT".c -L../ -lasm -o "$FUNCT"

test_input 1 "Hello World!"
test_input 2 "lorem\tipsum\tdolor\nsit\namet\n"
test_input 3 "\n\n\f\r\t"
test_input 4 ""
test_input 5 " "

rm -rf "$FUNCT"
52 changes: 52 additions & 0 deletions unit_tests/run_syscall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/bash

make_diff ()
{
diff "$2" "$3" > diffs/ft_"$FUNCT"."$1".txt
DIFF=$(diff a b)
if [ "$DIFF" != "" ]
then
echo -e "\033[0;31m[KO]\033[0m"
else
echo -e "\033[0;32m[OK]\033[0m"
rm -rf diffs/ft_"$FUNCT"."$1".txt
fi
}

test_std()
{
./"$FUNCT"_std $ORIG > a
./"$FUNCT"_std $IMPLEM > b
make_diff "$1" a b
rm -rf a b
}

test_fd()
{
if [ "$FUNCT" == "read" ]
then
echo "testing read function" > read.txt
echo "testing read function" > ft_read.txt
fi
./"$FUNCT"_fd "$2" $ORIG "$FUNCT".txt > a
./"$FUNCT"_fd "$2" $IMPLEM ft_"$FUNCT".txt > b
make_diff "$1" "$FUNCT".txt ft_"$FUNCT".txt
make_diff "$1" a b
rm -rf a b
rm -rf "$FUNCT".txt ft_"$FUNCT".txt
}

FD_OPEN=0
FD_WRONG=1
FUNCT=$1
echo -e ""
echo -e "\033[0;1m> ft_"$FUNCT"\033[0m"

clang testers/test_"$FUNCT"_std.c -L../ -lasm -o "$FUNCT"_std
clang testers/test_"$FUNCT"_fd.c -L../ -lasm -o "$FUNCT"_fd

test_std 1
test_fd 2 $FD_OPEN
test_fd 3 $FD_WRONG

rm -rf "$FUNCT"_std "$FUNCT"_fd
52 changes: 52 additions & 0 deletions unit_tests/testers/header_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* header_test.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vscabell <vscabell@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/21 22:04:20 by vscabell #+# #+# */
/* Updated: 2020/09/21 22:17:46 by vscabell ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef HEADER_TEST_H
# define HEADER_TEST_H

# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <unistd.h>
# include <errno.h>
# include <fcntl.h>

# define FD_OPEN '0'
# define FD_WRONG '1'
# define ORIG '0'
# define IMPLEM '1'
# define PUSH_FRONT 0
# define SIZE 1
# define SORT 2
# define REMOVE_IF 3

size_t ft_strlen(const char *s);
char *ft_strcpy(char *dest, const char *src);
int ft_strcmp(const char *s1, const char *s2);
ssize_t ft_write(int fd, const void *buf, size_t count);
ssize_t ft_read(int fd, void *buf, size_t count);
char *ft_strdup(const char *s);

typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;

int ft_atoi_base(char *str, char *base);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *begin_list);
void ft_list_sort(t_list **begin_list, int (*cmp)());
void ft_list_remove_if(t_list **begin_list, void *data_ref,
int (*cmp)(), void (*free_fct)(void *));

#endif
65 changes: 65 additions & 0 deletions unit_tests/testers/test_atoi_base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vscabell <vscabell@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/21 01:58:04 by vscabell #+# #+# */
/* Updated: 2020/09/21 22:07:49 by vscabell ### ########.fr */
/* */
/* ************************************************************************** */

#include "header_test.h"

void print_ft_atoi_base(char *str, char *base)
{
printf("%i\n", ft_atoi_base(str, base));
}

int main(int argc, char **argv)
{
int index;

index = atoi(argv[1]);

if (index == 1)
print_ft_atoi_base(" +--56", "0123456789");
else if (index == 2)
print_ft_atoi_base(" ---ff", "0123456789abcdef");
else if (index == 3)
print_ft_atoi_base("\v-42", "0123456789");
else if (index == 4)
print_ft_atoi_base("\f+-+-11101", "01");
else if (index == 5)
print_ft_atoi_base(" -+00501", "0123456789");
else if (index == 6)
print_ft_atoi_base(" +105", "0123456789");
else if (index == 7)
print_ft_atoi_base(" -+11", "01");
else if (index == 8)
print_ft_atoi_base("15256", "");
else if (index == 9)
print_ft_atoi_base("15", "5");
else if (index == 10)
print_ft_atoi_base("122", "-123456789");
else if (index == 11)
print_ft_atoi_base("122", "012+3456789");
else if (index == 12)
print_ft_atoi_base("111", "0 1");
else if (index == 13)
print_ft_atoi_base("568", "012345\t6789");
else if (index == 14)
print_ft_atoi_base("111", "01\r");
else if (index == 15)
print_ft_atoi_base("155", "01234506789");
else if (index == 16)
print_ft_atoi_base("05", "012345067890");
else if (index == 17)
print_ft_atoi_base("abc56", "0123456789");
else if (index == 18)
print_ft_atoi_base(NULL, "0123456789");
else if (index == 19)
print_ft_atoi_base("501", NULL);
return (0);
}
Loading

0 comments on commit 3427093

Please sign in to comment.