-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
executable file
·99 lines (89 loc) · 4.12 KB
/
tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* useless.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: paulohl <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/24 15:44:20 by paulohl #+# #+# */
/* Updated: 2020/05/24 11:19:29 by paulohl ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include "libasm.h"
void test_strcmp(char *str1, char *str2)
{
printf("Original: %i\n", strcmp(str1, str2));
printf("Mine : %i\n", ft_strcmp(str1, str2));
}
int main()
{
printf("\033[0;36mstrlen tests:\033[0m\n");
printf("Original: %ld %ld %ld\n", strlen("Hello World!"), strlen(""), strlen("Weird Characters \200 \t\n"));
printf("Mine : %ld %ld %ld\n", ft_strlen("Hello World!"), ft_strlen(""), ft_strlen("Weird Characters \200 \t\n"));
printf("\n\033[0;36mstrcmp tests:\033[0m\n");
test_strcmp("Hello World!", "Hello World!");
test_strcmp("Hello World!", "Hello");
test_strcmp("Hello", "Hello World!");
test_strcmp("\150", "\40");
test_strcmp("\xff\xff", "\xff");
// test_strcmp("Hello World!", NULL)); // Segfaults
printf("\n");
printf("\n\033[0;36mstrcpy tests:\033[0m\n");
char good[20];
printf("Original: %s", strcpy(good, "Hello World!"));
//char too_short[2];
//printf(" %s", strcpy(too_short, "Hello World!")); // Segfaults
//char empty[4];
//printf(" %s", strcpy(empty, NULL)); // Segfaults
printf("\n");
char ft_good[20];
printf("Mine : %s", ft_strcpy(ft_good, "Hello World!"));
//char ft_too_short[2];
//printf(" %s", ft_strcpy(ft_too_short, "Hello World!")); // Segfaults
//char ft_empty[4];
//printf(" %s", ft_strcpy(ft_empty, NULL)); // Segfaults
printf("\n");
printf("\n\033[0;36mstrdup:\033[0m\n");
printf("Original: %s\n", strdup("Hello World!"));
printf("Mine : %s\n", ft_strdup("Hello World!"));
printf("Original: %s\n", strdup(""));
printf("Mine : %s\n", ft_strdup(""));
printf("\n\033[0;36mwrite:\033[0m\n");
errno = 0;
printf(" => Original, stdout: %zd ; errno: %d\n", write(1, "bonjour", 7), errno);
errno = 0;
printf(" => Mine , stdout: %zd ; errno: %d\n", ft_write(1, "bonjour", 7), errno);
int fd = open("test.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
errno = 0;
printf("open: fd: %d, errno: %d\n", fd, errno);
errno = 0;
printf("Mine on file: %zd ; errno: %d\n", ft_write(fd, "bonjour", 7), errno);
errno = 0;
printf("Original, error: %zd ; errno: %d\n", write(-1, "bonjour", 7), errno);
errno = 0;
printf("Mine , error: %zd ; errno: %d\n", ft_write(-1, "bonjour", 7), errno);
close(fd);
puts("\n\033[0;36mread:\033[0m");
char content[8];
content[7] = 0;
fd = open("test.txt", O_RDONLY);
errno = 42;
puts("setting errno = 42");
printf("Mine: %zd ; errno: %d => content: ", read(fd, content, 7), errno);
printf("%s\n", content);
close(fd);
printf("\n\033[0;36mft_atoi_base:\033[0m\n");
printf("base hexa: \"ff\" => %d\n", ft_atoi_base("ff", "0123456789abcdef"));
printf("base hexa: \" \\t\\nff\" => %d\n", ft_atoi_base(" \t\nff", "0123456789abcdef"));
printf("base hexa: \"-ff\" => %d\n", ft_atoi_base("-ff", "0123456789abcdef"));
printf("base hexa: \"-+-ff\" => %d\n", ft_atoi_base("-+-ff", "0123456789abcdef"));
printf("base hexa: \"-+-ff w\" => %d\n", ft_atoi_base("-+-ff w", "0123456789abcdef"));
printf("Invalid characters: \"wx\" => %d\n", ft_atoi_base("wx", "0123456789abcdef"));
printf("base with doubles (825b94fbalf) => %d\n", ft_atoi_base("bb", "825b94fbalf"));
}