yyang42 / moulitest

This repository contains tests for several projects done at 42.
127 stars 37 forks source link

gnl : some tests fail when taking all unit tests but pass when taking them one by one #49

Open Mouradif opened 8 years ago

Mouradif commented 8 years ago

Hi there,

First of all, thank you for your work and your commitment. Your Moulitest makes all our lives less painful here in school.

So here is my issue : I am trying to finish my get_next_line project so naturally before setting it as finished and subscribe to defense, I tested it with Moulitest.

When I launch the test with make gnl, I appear to fail 4 tests (I only copied the lines for the failed tests)

08_test_few_lines_of_08.spec. [FAIL] S [SEGV] simple_string -> strcmp(line, "abcdefgh") == 0
40_hard_test_medium_string.sp [FAIL] S [SEGV] test01 -> strcmp(line, str) == 0
10_test_two_lines_of_16.spec. [FAIL] S [SEGV] simple_string -> strcmp(line, "abcdefghijklmnop") == 0
02_test_eof_with_close.spec.c [FAIL] S [SEGV] simple_string -> strcmp(line, "aaa") == 0

>>>> Result: 17/21 test suites passed. 17/21 tests passed (dots).

But when I try these tests individually with respectively

make gnl PATTERN=^tests/08
make gnl PATTERN=^tests/40
make gnl PATTERN=^tests/10
make gnl PATTERN=^tests/02

I get the following results :

[ -------STARTING ALL UNIT TESTS------- ]
>>>> 08_test_few_lines_of_08.spec. [Ok !] .
[ ----------END OF UNIT TESTS---------- ]

>>>> Result: 1/1 test suites passed. 1/1 tests passed (dots).
[ -------STARTING ALL UNIT TESTS------- ]
>>>> 40_hard_test_medium_string.sp [Ok !] .
[ ----------END OF UNIT TESTS---------- ]

>>>> Result: 1/1 test suites passed. 1/1 tests passed (dots).
[ -------STARTING ALL UNIT TESTS------- ]
>>>> 10_test_two_lines_of_16.spec. [Ok !] .
[ ----------END OF UNIT TESTS---------- ]

>>>> Result: 1/1 test suites passed. 1/1 tests passed (dots).
[ -------STARTING ALL UNIT TESTS------- ]
>>>> 02_test_eof_with_close.spec.c [Ok !] .
[ ----------END OF UNIT TESTS---------- ]

>>>> Result: 1/1 test suites passed. 1/1 tests passed (dots).

I have been trying to understand this behaviour and to debug my code for a week now but can't seem to find what's wrong.

Edit

Here is my code

get_next_line.c

#include "get_next_line.h"

static void ft_stradd(char **s1, char *s2)
{
    char    *join;

    if (*s2 == '\0')
        return ;
    join = ft_strjoin(*s1, s2);
    if (join == NULL)
        return ;
    if (*s1 != NULL)
        free(*s1);
    *s1 = join;
}

static void ft_strcuthead(char **s, int start)
{
    char    *cut;

    cut = ft_strdup(*s + start);
    if (cut == NULL)
        return ;
    free(*s);
    *s = cut;
}

static int  extract_next_line(char **rest, char **line, int offset)
{
    int     len;
    char    c;

    c = (offset) ? '\n' : '\3';
    len = ft_strchr(*rest, c) - *rest;
    *line = ft_strnew(len);
    ft_memcpy(*line, *rest, len);
    ft_strcuthead(rest, len + offset);
    return (1);
}

int         get_next_line(const int fd, char **line)
{
    static char *rest = NULL;
    char        buf[BUFF_SIZE + 2];
    int         len;

    if (line == NULL || fd < 0)
        return (-1);
    ft_bzero(buf, sizeof(buf));
    if (rest && *rest == '\3')
    {
        ft_strdel(&rest);
        return (0);
    }
    if (rest && ft_strchr(rest, '\n'))
        return (extract_next_line(&rest, line, 1));
    if (rest && ft_strchr(rest, '\3'))
        return (extract_next_line(&rest, line, 0));
    len = read(fd, buf, BUFF_SIZE);
    if (len == -1)
        return (-1);
    buf[len] = (len == 0) ? '\3' : '\0';
    ft_stradd(&rest, buf);
    return (get_next_line(fd, line));
}

get_next_line.h

#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# define BUFF_SIZE 1

# include <sys/types.h>
# include <sys/uio.h>
# include <unistd.h>
# include <stdlib.h>
# include "libft/libft.h"

int                 get_next_line(const int fd, char **line);

#endif
emilwallner commented 7 years ago

I have the same problem.

Mouradif commented 7 years ago

The Problem in my case @emilwallner was that I didn't cleanup the line variable at the moment of returning 0. Right before you return 0, try to add a ft_strdel(line), that should solve the problem

emilwallner commented 7 years ago

@Mouradif Thanks! Tried it, but it still didn't work. Anyways, I submitted it and got the full score.