219 lines
3.7 KiB
C
219 lines
3.7 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
|
|
{
|
|
char *test = "test";
|
|
printf("ft_strlen: %ld\n", ft_strlen(test));
|
|
}
|
|
// strlen
|
|
{
|
|
char *test = "test";
|
|
printf("strlen: %ld\n", strlen(test));
|
|
}
|
|
}
|
|
|
|
void
|
|
test_strcpy() {
|
|
|
|
// ft_strcpy
|
|
{
|
|
char src[10] = "src";
|
|
char dst[10] = "dst";
|
|
char *ret;
|
|
printf ("src: %s, dest: %s\n", src, dst);
|
|
ret = ft_strcpy(dst, src);
|
|
printf ("ft_strcpy ret: %s, src: %s, dest: %s\n", ret, src, dst);
|
|
printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst);
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// strcpy
|
|
{
|
|
char src[10] = "src";
|
|
char dst[10] = "dst";
|
|
char *ret;
|
|
ret = strcpy(dst, src);
|
|
printf ("strcpy ret: %s, src: %s, dest: %s\n", ret, src, dst);
|
|
printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_strcmp() {
|
|
|
|
// ft_strcmp
|
|
{
|
|
char *s1 = "test4";
|
|
char *s2 = "test5";
|
|
printf("ft_strcmp: %d, s1: %s, s2: %s\n", ft_strcmp(s1, s2), s1 , s2);
|
|
|
|
s1 = "te";
|
|
s2 = "test";
|
|
printf("ft_strcmp: %d, s1: %s, s2: %s\n", ft_strcmp(s1, s2), s1 , s2);
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// strcmp
|
|
{
|
|
char *s1 = "test4";
|
|
char *s2 = "test5";
|
|
printf("strcmp: %d, s1: %s, s2: %s\n", strcmp(s1, s2), s1 , s2);
|
|
|
|
s1 = "te";
|
|
s2 = "test";
|
|
printf("strcmp: %d, s1: %s, s2: %s\n", strcmp(s1, s2), s1 , s2);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_write() {
|
|
// ft_write
|
|
{
|
|
|
|
printf("ft_write ret: %ld\n", ft_write(1, "test ", 5));
|
|
size_t error = ft_write(-1, "test ", 5);
|
|
printf("ft_write error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// write
|
|
{
|
|
printf("write ret: %ld\n", write(1, "test ", 5));
|
|
size_t error = write(-1, "test ", 5);
|
|
printf("write error: %ld\n", error);
|
|
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
|
}
|
|
}
|
|
|
|
void
|
|
test_read() {
|
|
// ft_read
|
|
{
|
|
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
|
|
{
|
|
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
|
|
{
|
|
char *buf = malloc(10);
|
|
strcpy(buf, "test");
|
|
|
|
char *dup = ft_strdup(buf);
|
|
printf("ft_strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
|
|
|
free(buf);
|
|
free(dup);
|
|
}
|
|
|
|
|
|
// strdup
|
|
{
|
|
char *buf = malloc(10);
|
|
strcpy(buf, "test");
|
|
|
|
char *dup = strdup(buf);
|
|
printf("strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
|
|
|
free(buf);
|
|
free(dup);
|
|
}
|
|
}
|
|
|
|
void
|
|
test_calloc() {
|
|
// ft_calloc
|
|
{
|
|
printf("ft_calloc\n");
|
|
|
|
char *error = ft_calloc(2, 10); // return ""
|
|
printf("error: %p\n", error);
|
|
|
|
char *empty = ft_calloc(0, 0);
|
|
printf("empty: %p\n", empty);
|
|
|
|
char *buf = ft_calloc(10, 1);
|
|
strcpy(buf, "test");
|
|
printf("test: %s_%p\n", buf, buf);
|
|
|
|
free(error);
|
|
free(empty);
|
|
free(buf);
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// calloc
|
|
{
|
|
printf("calloc\n");
|
|
|
|
char *error = calloc(2, 10); // return ""
|
|
printf("error: %p\n", error);
|
|
|
|
char *empty = calloc(0, 0);
|
|
printf("empty: %p\n", empty);
|
|
|
|
char *buf = calloc(10, 1);
|
|
strcpy(buf, "test");
|
|
printf("test: %s_%p\n", buf, buf);
|
|
|
|
free(error);
|
|
free(empty);
|
|
free(buf);
|
|
}
|
|
}
|
|
|