add more clean test

This commit is contained in:
Xamora 2026-09-20 12:58:34 +02:00
parent bbdc2f00fa
commit a133be704d

186
main.c
View file

@ -1,4 +1,4 @@
#include "main.h" # include "main.h"
void test_strlen(); void test_strlen();
void test_strcpy(); void test_strcpy();
@ -32,40 +32,107 @@ void
test_strlen() { test_strlen() {
// ft_strlen // ft_strlen
{ {
printf("ft_strlen:\n");
char *empty = "";
printf("empty: %ld\n", ft_strlen(empty));
char *test = "test"; char *test = "test";
printf("ft_strlen: %ld\n", ft_strlen(test)); printf("test: %ld\n", ft_strlen(test));
char *very_long = calloc(USHRT_MAX, 1);
very_long = memset(very_long, 'C', USHRT_MAX - 1);
very_long[USHRT_MAX] = '\0';
printf("very_long: %ld\n", ft_strlen(very_long));
// printf("%s\n", very_long);
free(very_long);
} }
printf("\n");
// strlen // strlen
{ {
printf("strlen:\n");
char *empty = "";
printf("empty: %ld\n", strlen(empty));
char *test = "test"; char *test = "test";
printf("strlen: %ld\n", strlen(test)); printf("test: %ld\n", strlen(test));
char *very_long = calloc(USHRT_MAX, 1);
very_long = memset(very_long, 'C', USHRT_MAX - 1);
very_long[USHRT_MAX] = '\0';
printf("very_long: %ld\n", strlen(very_long));
// printf("%s\n", very_long);
free(very_long);
} }
} }
void void
test_strcpy() { test_strcpy() {
// ft_strcpy // ft_strcpy
{ {
printf("ft_strcpy:\n");
char src_empty[] = "";
char dst_empty[] = "";
char *ret = ft_strcpy(dst_empty, src_empty);
printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n",
ret, ret, src_empty, src_empty, dst_empty, dst_empty);
char src[10] = "src"; char src[10] = "src";
char dst[10] = "dst"; char dst[10] = "dst";
char *ret;
printf ("src: %s, dest: %s\n", src, dst); printf ("src: %s, dest: %s\n", src, dst);
ret = ft_strcpy(dst, src); ret = ft_strcpy(dst, src);
printf ("ft_strcpy ret: %s, src: %s, dest: %s\n", ret, src, dst); printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n", ret, ret, src, src, dst, dst);
printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst); // printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst);
//
char *very_long_src = calloc(USHRT_MAX, 1);
char *very_long_dst = calloc(USHRT_MAX, 1);
very_long_src = memset(very_long_src, 'C', USHRT_MAX - 1);
very_long_src[USHRT_MAX] = '\0';
ret = ft_strcpy(very_long_dst, very_long_src);
printf ("ret: %p, src: %p, dest: %p\n",
ret, very_long_src, very_long_dst);
// printf("%s\n", very_long);
free(very_long_src);
free(very_long_dst);
} }
printf("\n"); printf("\n");
// strcpy // strcpy
{ {
printf("strcpy:\n");
char src_empty[] = "";
char dst_empty[] = "";
char *ret = strcpy(dst_empty, src_empty);
printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n",
ret, ret, src_empty, src_empty, dst_empty, dst_empty);
char src[10] = "src"; char src[10] = "src";
char dst[10] = "dst"; char dst[10] = "dst";
char *ret; printf ("src: %s, dest: %s\n", src, dst);
ret = strcpy(dst, src); ret = strcpy(dst, src);
printf ("strcpy ret: %s, src: %s, dest: %s\n", ret, src, dst); printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n", ret, ret, src, src, dst, dst);
printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst); // printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst);
//
char *very_long_src = calloc(USHRT_MAX, 1);
char *very_long_dst = calloc(USHRT_MAX, 1);
very_long_src = memset(very_long_src, 'C', USHRT_MAX - 1);
very_long_src[USHRT_MAX] = '\0';
ret = strcpy(very_long_dst, very_long_src);
printf ("ret: %p, src: %p, dest: %p\n",
ret, very_long_src, very_long_dst);
// printf("%s\n", very_long);
free(very_long_src);
free(very_long_dst);
} }
} }
@ -74,26 +141,38 @@ test_strcmp() {
// ft_strcmp // ft_strcmp
{ {
printf("ft_strcmp:\n");
printf("ret: %d, s1: '', s2: ''\n", ft_strcmp("", ""));
char *s1 = "test4"; char *s1 = "test4";
char *s2 = "test5"; char *s2 = "test5";
printf("ft_strcmp: %d, s1: %s, s2: %s\n", ft_strcmp(s1, s2), s1 , s2); printf("ret: %d, s1: '%s', s2: '%s'\n", ft_strcmp(s1, ""), s1 , "");
printf("ret: %d, s1: '%s', s2: '%s'\n", ft_strcmp("", s2), "" , s2);
printf("ret: %d, s1: '%s', s2: '%s'\n", ft_strcmp(s1, s2), s1 , s2);
s1 = "te"; s1 = "equal";
s2 = "test"; s2 = "equal";
printf("ft_strcmp: %d, s1: %s, s2: %s\n", ft_strcmp(s1, s2), s1 , s2); printf("ret: %d, s1: '%s', s2: '%s'\n", ft_strcmp(s1, s2), s1 , s2);
} }
printf("\n"); printf("\n");
// strcmp // strcmp
{ {
printf("strcmp:\n");
printf("ret: %d, s1: '', s2: ''\n", strcmp("", ""));
char *s1 = "test4"; char *s1 = "test4";
char *s2 = "test5"; char *s2 = "test5";
printf("strcmp: %d, s1: %s, s2: %s\n", strcmp(s1, s2), s1 , s2); printf("ret: %d, s1: '%s', s2: '%s'\n", strcmp(s1, ""), s1 , "");
printf("ret: %d, s1: '%s', s2: '%s'\n", strcmp("", s2), "" , s2);
printf("ret: %d, s1: '%s', s2: '%s'\n", strcmp(s1, s2), s1 , s2);
s1 = "te"; s1 = "equal";
s2 = "test"; s2 = "equal";
printf("strcmp: %d, s1: %s, s2: %s\n", strcmp(s1, s2), s1 , s2); printf("ret: %d, s1: '%s', s2: '%s'\n", strcmp(s1, s2), s1 , s2);
} }
} }
@ -101,10 +180,13 @@ void
test_write() { test_write() {
// ft_write // ft_write
{ {
printf("ft_write:\n");
ft_write(1, "ft_write(1, \"test \", 5): ", 25);
printf("-> %ld\n", ft_write(1, "test ", 5));
printf("ft_write ret: %ld\n", ft_write(1, "test ", 5));
size_t error = ft_write(-1, "test ", 5); size_t error = ft_write(-1, "test ", 5);
printf("ft_write error: %ld\n", error); printf("error: %ld\n", error);
printf("ERRNO_%d: %s\n", errno, strerror(errno)); printf("ERRNO_%d: %s\n", errno, strerror(errno));
} }
@ -113,9 +195,13 @@ test_write() {
// write // write
{ {
printf("write ret: %ld\n", write(1, "test ", 5)); printf("write:\n");
write(1, "write(1, \"test \", 5): ", 22);
printf("-> %ld\n", write(1, "test ", 5));
size_t error = write(-1, "test ", 5); size_t error = write(-1, "test ", 5);
printf("write error: %ld\n", error); printf("error: %ld\n", error);
printf("ERRNO_%d: %s\n", errno, strerror(errno)); printf("ERRNO_%d: %s\n", errno, strerror(errno));
} }
} }
@ -124,6 +210,12 @@ void
test_read() { test_read() {
// ft_read // ft_read
{ {
printf("ft_read:\n");
// char in[10];
// ft_read(0, in, 10);
// printf("%s\n", in);
char buf[10]; char buf[10];
printf("ft_read ret: %ld\n", ft_read(open("main.c", O_RDONLY), buf, 10)); printf("ft_read ret: %ld\n", ft_read(open("main.c", O_RDONLY), buf, 10));
printf ("buf: %s\n", buf); printf ("buf: %s\n", buf);
@ -136,6 +228,12 @@ test_read() {
// read // read
{ {
printf("read:\n");
// char in[10];
// read(0, in, 10);
// printf("%s\n", in);
char buf[10]; char buf[10];
printf("read ret: %ld\n", read(open("main.c", O_RDONLY), buf, 10)); printf("read ret: %ld\n", read(open("main.c", O_RDONLY), buf, 10));
printf ("buf: %s\n", buf); printf ("buf: %s\n", buf);
@ -149,27 +247,59 @@ void
test_strdup() { test_strdup() {
// ft_strdup // ft_strdup
{ {
char *buf = malloc(10); printf("ft_strdup:\n");
char *empty = calloc(1, 1);
char *copy_empty = ft_strdup(empty);
printf("empty: %s_%p, copy: %s_%p\n", empty, empty, copy_empty, copy_empty);
free(empty);
free(copy_empty);
char *buf = calloc(10, 1);
strcpy(buf, "test"); strcpy(buf, "test");
char *dup = ft_strdup(buf); char *dup = ft_strdup(buf);
printf("ft_strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup); printf("buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
free(buf); free(buf);
free(dup); free(dup);
char *very_long = calloc(USHRT_MAX, 1);
char *copy_long = ft_strdup(very_long);
printf ("long: %p, copy: %p\n", very_long, copy_long);
free(very_long);
free(copy_long);
} }
// strdup // strdup
{ {
char *buf = malloc(10); printf("strdup:\n");
char *empty = calloc(1, 1);
char *copy_empty = strdup(empty);
printf("empty: %s_%p, copy: %s_%p\n", empty, empty, copy_empty, copy_empty);
free(empty);
free(copy_empty);
char *buf = calloc(10, 1);
strcpy(buf, "test"); strcpy(buf, "test");
char *dup = strdup(buf); char *dup = strdup(buf);
printf("strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup); printf("buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
free(buf); free(buf);
free(dup); free(dup);
char *very_long = calloc(USHRT_MAX, 1);
char *copy_long = strdup(very_long);
printf ("long: %p, copy: %p\n", very_long, copy_long);
free(very_long);
free(copy_long);
} }
} }
@ -188,7 +318,7 @@ test_calloc() {
char *empty = ft_calloc(0, 0); char *empty = ft_calloc(0, 0);
printf("empty: %p\n", empty); printf("empty: %p\n", empty);
char *overflow = ft_calloc(LONG_MAX, LONG_MAX); char *overflow = ft_calloc(UINT_MAX, UINT_MAX);
printf("overflow: %s_%p\n", overflow, overflow); printf("overflow: %s_%p\n", overflow, overflow);
char *buf = ft_calloc(10, 1); char *buf = ft_calloc(10, 1);
@ -215,7 +345,7 @@ test_calloc() {
char *empty = calloc(0, 0); char *empty = calloc(0, 0);
printf("empty: %p\n", empty); printf("empty: %p\n", empty);
char *overflow = calloc(LONG_MAX, LONG_MAX); char *overflow = calloc(UINT_MAX, UINT_MAX);
printf("overflow: %s_%p\n", overflow, overflow); printf("overflow: %s_%p\n", overflow, overflow);
char *buf = calloc(10, 1); char *buf = calloc(10, 1);