Skip to content

Instantly share code, notes, and snippets.

@oyagci
Created February 13, 2018 16:32
Show Gist options
  • Save oyagci/bfba377debd0b03f2b7368a9660eac76 to your computer and use it in GitHub Desktop.
Save oyagci/bfba377debd0b03f2b7368a9660eac76 to your computer and use it in GitHub Desktop.
#include <criterion/criterion.h>
#include "ft_malloc.h"
#include <limits.h>
#include <stdio.h>
void *ft_malloc(size_t size);
Test(evaluation, test1)
{
int i;
char *addr;
i = 0;
while (i < 1024)
{
addr = (char *)ft_malloc(1024);
addr[0] = 42;
i += 1;
}
cr_assert(addr[0] == 42);
}
Test(tiny_basic, simple_integer)
{
int *p = 0;
p = ft_malloc(sizeof(int));
cr_assert_not(p == NULL);
*p = 42;
cr_assert_eq(*p, 42);
}
Test(tiny_basic, two_integer)
{
int *p = 0;
int *q = 0;
p = ft_malloc(sizeof(int));
q = ft_malloc(sizeof(int));
cr_assert_not(p == NULL);
cr_assert_not(q == NULL);
cr_assert_not(p == q);
*p = 42;
*q = 1337;
cr_assert_eq(*p, 42);
cr_assert_eq(*q, 1337);
}
Test(tiny_basic, int_max)
{
int *p = 0;
p = ft_malloc(sizeof(int));
cr_assert_not(p == NULL);
*p = INT_MAX;
cr_assert_eq(*p, INT_MAX);
}
Test(tiny_basic, many)
{
int *p[500] = { NULL };
int i;
i = 0;
while (i < 500)
{
p[i] = ft_malloc(sizeof(int));
*p[i] = 4242 + i;
i += 1;
}
i = 0;
while (i < 500)
{
cr_assert_eq(*p[i], 4242 + i);
i += 1;
}
}
Test(tiny_basic, many_more)
{
int *p[5000] = { NULL };
int i;
i = 0;
while (i < 5000)
{
p[i] = ft_malloc(sizeof(int));
*p[i] = 42 + i;
i += 1;
}
i = 0;
while (i < 5000)
{
cr_assert_eq(*p[i], 42 + i);
i += 1;
}
}
Test(small_basic, medium_struct)
{
struct s_med {
int a;
int b;
int c;
int d;
int e;
int f;
};
struct s_med *a = ft_malloc(sizeof(struct s_med));
cr_assert_not_null(a);
a->a = 42;
a->b = 1337;
a->c = 230598;
a->d = 42;
a->e = 1337;
a->f = 230598;
cr_assert_eq(a->a, 42);
cr_assert_eq(a->b, 1337);
cr_assert_eq(a->c, 230598);
cr_assert_eq(a->d, 42);
cr_assert_eq(a->e, 1337);
cr_assert_eq(a->f, 230598);
}
Test(small_basic, overwrite_int)
{
struct s_test {
int a;
int b;
int c;
int d;
int e;
};
struct s_test *a;
a = ft_malloc(sizeof(struct s_test));
a->a = 10;
a->b = 42;
a->c = 123134;
a->d = 1337;
a->e = 99999;
int *p = ft_malloc(sizeof(int));
*p = 628380674;
cr_assert_eq(a->a, 10);
cr_assert_eq(a->b, 42);
cr_assert_eq(a->c, 123134);
cr_assert_eq(a->d, 1337);
cr_assert_eq(a->e, 99999);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment