Skip to content

Instantly share code, notes, and snippets.

@nloomans
Last active December 5, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nloomans/e1bde69289876d9dca7e40c6876efe07 to your computer and use it in GitHub Desktop.
Save nloomans/e1bde69289876d9dca7e40c6876efe07 to your computer and use it in GitHub Desktop.
get_next_line tester
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: nloomans <nloomans@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/04/10 11:18:54 by nloomans #+# #+# */
/* Updated: 2019/12/05 11:44:20 by nloomans ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "get_next_line.h"
#define FD_AMOUNT 100
/* <libft> */
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
void ft_memdel(void **ap)
{
free(*ap);
*ap = NULL;
}
t_list *ft_lstnew(const void *content, size_t content_size)
{
t_list *new;
new = (t_list *)calloc(1, sizeof(t_list));
if (new == NULL)
return (NULL);
if (content == NULL || content_size == 0)
return (new);
new->content_size = content_size;
new->content = calloc(1, content_size);
if (new->content == NULL)
{
ft_memdel((void **)&new);
return (NULL);
}
memcpy(new->content, content, content_size);
return (new);
}
void ft_lstpushback(t_list **alst, t_list *new)
{
if (*alst != NULL)
return (ft_lstpushback(&(*alst)->next, new));
*alst = new;
}
t_list *ft_lstpop(t_list **alst)
{
t_list *node;
node = *alst;
*alst = (*alst)->next;
return (node);
}
typedef struct s_fd_testing
{
int fds[2];
t_list *lines;
} t_fd_testing;
/* </libft> */
int main(void)
{
t_fd_testing pipes[FD_AMOUNT];
for (int i = 0; i < FD_AMOUNT; i++)
{
if (pipe(pipes[i].fds) == -1)
{
fprintf(stderr, "pipe creation failed: %s\n", strerror(errno));
return (1);
}
pipes[i].lines = NULL;
}
// Seed the random number generator
srand(time(NULL));
for (int fd_index = 0; fd_index < FD_AMOUNT; fd_index++)
{
int amount_of_lines = rand() % 10;
if (amount_of_lines == 0)
{
printf("%d: (null)\n", fd_index);
}
for (int line_index = 0; line_index < amount_of_lines; line_index++)
{
int line_kind = rand() % 9;
char *line_to_add;
switch (line_kind)
{
case 0:
line_to_add = "";
break;
case 1:
line_to_add = "123456789012345678901234567890";
break;
case 2:
line_to_add = "1234567890123456789012345678901";
break;
case 3:
line_to_add = "12345678901234567890123456789012";
break;
case 4:
line_to_add = "123456789012345678901234567890123";
break;
case 5:
line_to_add = "1234567890123456789012345678901234";
break;
case 6:
line_to_add = "12345678901234567890123456789012345";
break;
case 7:
line_to_add = "1";
break;
case 8:
line_to_add = "1234567890123456789012345678901234567890123456789012345678901234567890";
break;
}
if (line_index == amount_of_lines - 1 && line_kind != 0 && rand() % 2)
{
dprintf(pipes[fd_index].fds[1], "%s", line_to_add);
printf("%d: %s\n", fd_index, line_to_add);
}
else
{
dprintf(pipes[fd_index].fds[1], "%s\n", line_to_add);
printf("%d: %s\\n\n", fd_index, line_to_add);
}
ft_lstpushback(&pipes[fd_index].lines, ft_lstnew(line_to_add, strlen(line_to_add) + 1));
}
close(pipes[fd_index].fds[1]);
}
int null_count = 0;
while (null_count < FD_AMOUNT * 100)
{
int fd_index = rand() % FD_AMOUNT;
char *output;
int ret = get_next_line(pipes[fd_index].fds[0], &output);
t_list *goal = (pipes[fd_index].lines == NULL)
? NULL
: ft_lstpop(&pipes[fd_index].lines);
if (goal == NULL)
{
assert(ret == 0);
null_count++;
}
else
{
null_count = 0;
assert(ret == 1);
printf("%d: expected: \"%s\", got: \"%s\"\n", fd_index, (char *)goal->content, output);
assert(strcmp(output, (char *)goal->content) == 0);
}
ft_memdel((void **)&output);
if (goal != NULL)
{
ft_memdel(&goal->content);
ft_memdel((void **)&goal);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment