361 lines
7.5 KiB
C
361 lines
7.5 KiB
C
# include "main.h"
|
|
|
|
void test_strlen();
|
|
void test_strcpy();
|
|
void test_strcmp();
|
|
void test_write();
|
|
void test_read();
|
|
void test_strdup();
|
|
void test_calloc();
|
|
|
|
int
|
|
main(void) {
|
|
|
|
test_strlen();
|
|
printf("\n");
|
|
test_strcpy();
|
|
printf("\n");
|
|
test_strcmp();
|
|
printf("\n");
|
|
test_write();
|
|
printf("\n");
|
|
test_read();
|
|
printf("\n");
|
|
test_strdup();
|
|
printf("\n");
|
|
test_calloc();
|
|
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
test_strlen() {
|
|
// ft_strlen
|
|
{
|
|
printf("ft_strlen:\n");
|
|
|
|
char *empty = "";
|
|
printf("empty: %ld\n", ft_strlen(empty));
|
|
|
|
char *test = "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
|
|
{
|
|
printf("strlen:\n");
|
|
|
|
char *empty = "";
|
|
printf("empty: %ld\n", strlen(empty));
|
|
|
|
char *test = "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
|
|
test_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 dst[10] = "dst";
|
|
printf ("src: %s, dest: %s\n", src, dst);
|
|
ret = ft_strcpy(dst, src);
|
|
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);
|
|
//
|
|
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");
|
|
|
|
// 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 dst[10] = "dst";
|
|
printf ("src: %s, dest: %s\n", src, dst);
|
|
ret = strcpy(dst, src);
|
|
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);
|
|
//
|
|
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);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_strcmp() {
|
|
|
|
// ft_strcmp
|
|
{
|
|
printf("ft_strcmp:\n");
|
|
|
|
printf("ret: %d, s1: '', s2: ''\n", ft_strcmp("", ""));
|
|
|
|
char *s1 = "test4";
|
|
char *s2 = "test5";
|
|
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 = "equal";
|
|
s2 = "equal";
|
|
printf("ret: %d, s1: '%s', s2: '%s'\n", ft_strcmp(s1, s2), s1 , s2);
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// strcmp
|
|
{
|
|
printf("strcmp:\n");
|
|
|
|
printf("ret: %d, s1: '', s2: ''\n", strcmp("", ""));
|
|
|
|
char *s1 = "test4";
|
|
char *s2 = "test5";
|
|
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 = "equal";
|
|
s2 = "equal";
|
|
printf("ret: %d, s1: '%s', s2: '%s'\n", strcmp(s1, s2), s1 , s2);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_write() {
|
|
// ft_write
|
|
{
|
|
printf("ft_write:\n");
|
|
|
|
ft_write(1, "ft_write(1, \"test \", 5): ", 25);
|
|
printf("-> %ld\n", ft_write(1, "test ", 5));
|
|
|
|
size_t error = ft_write(-1, "test ", 5);
|
|
printf("error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// write
|
|
{
|
|
printf("write:\n");
|
|
|
|
write(1, "write(1, \"test \", 5): ", 22);
|
|
printf("-> %ld\n", write(1, "test ", 5));
|
|
|
|
size_t error = write(-1, "test ", 5);
|
|
printf("error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
}
|
|
}
|
|
|
|
void
|
|
test_read() {
|
|
// ft_read
|
|
{
|
|
printf("ft_read:\n");
|
|
|
|
// char in[10];
|
|
// ft_read(0, in, 10);
|
|
// printf("%s\n", in);
|
|
|
|
char buf[10];
|
|
printf("ft_read ret: %ld\n", ft_read(open("main.c", O_RDONLY), buf, 10));
|
|
printf ("buf: %s\n", buf);
|
|
size_t error = ft_read(open("test", O_RDWR), buf, 10);
|
|
printf("ft_read error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// read
|
|
{
|
|
printf("read:\n");
|
|
|
|
// char in[10];
|
|
// read(0, in, 10);
|
|
// printf("%s\n", in);
|
|
|
|
char buf[10];
|
|
printf("read ret: %ld\n", read(open("main.c", O_RDONLY), buf, 10));
|
|
printf ("buf: %s\n", buf);
|
|
size_t error = read(open("test", O_RDWR), buf, 10);
|
|
printf("read error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
}
|
|
}
|
|
|
|
void
|
|
test_strdup() {
|
|
// ft_strdup
|
|
{
|
|
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");
|
|
|
|
char *dup = ft_strdup(buf);
|
|
printf("buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
|
|
|
free(buf);
|
|
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
|
|
{
|
|
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");
|
|
|
|
char *dup = strdup(buf);
|
|
printf("buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
|
|
|
free(buf);
|
|
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);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_calloc() {
|
|
// ft_calloc
|
|
{
|
|
printf("ft_calloc\n");
|
|
|
|
char *normal = ft_calloc(10, 1); // return ""
|
|
printf("normal: %s_%p\n", normal, normal);
|
|
for (int i = 0; i < 10; i++)
|
|
printf("'%b' ", normal[i]);
|
|
printf("\n");
|
|
|
|
char *empty = ft_calloc(0, 0);
|
|
printf("empty: %p\n", empty);
|
|
|
|
char *overflow = ft_calloc(UINT_MAX, UINT_MAX);
|
|
printf("overflow: %s_%p\n", overflow, overflow);
|
|
|
|
char *buf = ft_calloc(10, 1);
|
|
strcpy(buf, "test");
|
|
printf("test: %s_%p\n", buf, buf);
|
|
|
|
free(normal);
|
|
free(empty);
|
|
free(buf);
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// calloc
|
|
{
|
|
printf("calloc\n");
|
|
|
|
char *normal = ft_calloc(10, 1);
|
|
printf("normal: %s_%p\n", normal, normal);
|
|
for (int i = 0; i < 10; i++)
|
|
printf("'%b' ", normal[i]);
|
|
printf("\n");
|
|
|
|
char *empty = calloc(0, 0);
|
|
printf("empty: %p\n", empty);
|
|
|
|
char *overflow = calloc(UINT_MAX, UINT_MAX);
|
|
printf("overflow: %s_%p\n", overflow, overflow);
|
|
|
|
char *buf = calloc(10, 1);
|
|
strcpy(buf, "test");
|
|
printf("test: %s_%p\n", buf, buf);
|
|
|
|
free(normal);
|
|
free(empty);
|
|
free(buf);
|
|
}
|
|
}
|
|
|