#include <stdio.h>
#include <string.h>
#include <assert.h>
unsigned int len(char* str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
unsigned int ft_strlcpy(char* dest, char* src, unsigned int size)
{
int dest_len;
int i;
i = 0;
dest_len = len(dest);
while (src[i] != '\0' && i < size - 1)
{
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
return len(src);
}
int main(void)
{
char dest[100];
// 테스트 케이스 1: 소스 문자열의 길이가 목적지 버퍼 크기보다 작은 경우
memset(dest, 0, sizeof(dest));
size_t result = ft_strlcpy(dest, "Hello, World!", sizeof(dest));
assert(strcmp(dest, "Hello, World!") == 0);
assert(result == strlen("Hello, World!"));
// 테스트 케이스 2: 소스 문자열의 길이가 목적지 버퍼 크기와 같은 경우
memset(dest, 0, sizeof(dest));
result = ft_strlcpy(dest, "1234567890", 11); // 버퍼 크기 11 (null 문자 포함)
assert(strcmp(dest, "1234567890") == 0);
assert(result == strlen("1234567890"));
// 테스트 케이스 3: 소스 문자열의 길이가 목적지 버퍼 크기보다 큰 경우
memset(dest, 0, sizeof(dest));
result = ft_strlcpy(dest, "This is a very long string that exceeds the destination buffer size.", 20);
assert(strcmp(dest, "This is a very long") == 0); // 19 문자 + null 문자
assert(result == strlen("This is a very long string that exceeds the destination buffer size."));
// 테스트 케이스 4: 빈 문자열을 복사하는 경우
memset(dest, 0, sizeof(dest));
result = ft_strlcpy(dest, "", sizeof(dest));
assert(strcmp(dest, "") == 0);
assert(result == 0);
// 테스트 케이스 5: 목적지 버퍼의 크기가 1인 경우 (null-종료 문자만 포함할 수 있음)
memset(dest, 0, sizeof(dest));
result = ft_strlcpy(dest, "Hello", 1);
assert(strcmp(dest, "") == 0);
assert(result == strlen("Hello"));
printf("test pass");
}
To embed this program on your website, copy the following code and paste it into your website's HTML: