-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·225 lines (174 loc) · 4.99 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "new_string.h"
int test_strcpy(char* src, int log)
{
char *return1, *return2;
char dest1[20], dest2[20];
return1 = strcpy(dest1, src);
return2 = new_strcpy(dest2, src);
if(log)
{
printf("strcpy: src: %s dest: %s rtrn: %s\n", src, dest1, return1);
printf("new_strcpy: src: %s dest: %s rtrn: %s\n", src, dest2, return2);
}
return strcmp(dest1, dest2)==0 && strcmp(return2, return2)==0;
}
int test_strncpy(char* src, int size, int log)
{
char *return1, *return2;
char dest2[20], dest1[20];
return1 = strncpy(dest1, src, size);
return2 = new_strncpy(dest2, src, size);
if(log)
{
printf("strncpy: src: %s dest: %s size: %d rtrn: %s\n",
src, dest1, size, return1);
printf("new_strncpy: src: %s dest: %s size: %d rtrn: %s\n",
src, dest2, size, return2);
}
return strncmp(dest1, dest2, size)==0 && strncmp(return2, return2, size)==0;
}
int test_strcmp(char* str1, char* str2, int log)
{
char *src1, *src2, *dest1, *dest2;
int return1, return2;
src1 = src2 = str1;
dest1 = dest2 = str2;
return1 = strcmp(src1, dest1);
return2 = new_strcmp(src2, dest2);
if(log)
{
printf("strcmp: str1: %s str2: %s rtrn: %d\n", src1, dest1, return1);
printf("new_strcmp: str1: %s str2: %s rtrn: %d\n", src2, dest2, return2);
}
return strcmp(src1, src2)==0 && strcmp(dest1, dest2)==0 && (return1 == return2);
}
int test_strncmp(char* str1, char* str2, int size, int log)
{
int return1, return2;
return1 = strncmp(str1, str2, size);
return2 = new_strncmp(str1, str2, size);
if(log)
{
printf("strncmp: str1: %s str2: %s size: %d rtrn: %d\n",
str1, str2, size, return1);
printf("new_strncmp: str1: %s str2: %s size: %d rtrn: %d\n",
str1, str2, size, return2);
}
return (return1 == return2);
}
int test_strcat(char* src, char* dest, int log)
{
char *return1, *return2;
char dest1[20], dest2[20];
strcpy(dest1, dest);
strcpy(dest2, dest);
return1 = strcat(dest1, src);
return2 = new_strcat(dest2, src);
if(log)
{
printf("strcat: src: %s dest: %s return: %s\n",
src, dest1, return1);
printf("new_strcat: src: %s dest: %s return: %s\n",
src, dest2, return2);
}
return strcmp(dest1, dest2)==0 && strcmp(return1, return2)==0;
}
int test_strlen(char* str, int log)
{
int return1, return2;
return1 = strlen(str);
return2 = new_strlen(str);
if(log)
{
printf("strlen: str: %s return: %d\n",
str, return1);
printf("new_strlen: str: %s return: %d\n",
str, return2);
}
return return1==return2;
}
int test_strncat(char* src, char* dest, int size, int log)
{
char *return1, *return2;
char dest1[20], dest2[20];
strcpy(dest1, dest);
strcpy(dest2, dest);
return1 = strncat(dest1, src, size);
return2 = new_strncat(dest2, src, size);
if(log)
{
printf("strcat: src: %s dest: %s size: %d return: %s\n",
src, dest1, size, return1);
printf("new_strcat: src: %s dest: %s size: %d return: %s\n",
src, dest2, size, return2);
}
return strcmp(dest1, dest2)==0 && strcmp(return1, return2)==0;
}
int test_strchr(char* str, char c, int log)
{
char *return1, *return2;
return1 = strchr(str, c);
return2 = new_strchr(str, c);
if(log)
{
printf("strchr: str: %s c: %c return: %p\n",
str, c, return1);
printf("new_strchr: str: %s c: %c return: %p\n",
str, c, return2);
}
return return1==return2;
}
int test_strstr(char* str, char* needle, int log)
{
char *return1, *return2;
return1 = strstr(str, needle);
return2 = new_strstr(str, needle);
if(log)
{
printf("strstr: str: %s needle: %s return: %p\n",
str, needle, return1);
printf("new_strstr: str: %s needle: %s return: %p\n",
str, needle, return2);
}
return return1==return2;
}
int main()
{
//strcpy
printf("%d\n", test_strcpy("hello", 0));
//strncpy
printf("%d\n", test_strncpy("hellohellohello", 7, 0));
printf("%d\n", test_strncpy("hello", 7, 0));
printf("%d\n", test_strncpy("hellohello", 10, 0));
//strcmp
printf("%d\n", test_strcmp("aaa", "arc", 0));
printf("%d\n", test_strcmp("abc", "abc", 0));
printf("%d\n", test_strcmp("ccc", "abc", 0));
//strncmp
printf("%d\n", test_strncmp("aaaddd", "alcccc", 3, 0));
printf("%d\n", test_strncmp("abcddd", "abcccc", 3, 0));
printf("%d\n", test_strncmp("cccddd", "abcccc", 3, 0));
//strcat
printf("%d\n", test_strcat("world", "hello", 0));
printf("%d\n", test_strcat("fg", "", 0));
printf("%d\n", test_strcat("", "hello", 0));
//strncat
printf("%d\n", test_strncat("world", "hello", 3, 0));
printf("%d\n", test_strncat("world", "hello", 10, 0));
printf("%d\n", test_strncat("", "hello", 10, 0));
//strlen
printf("%d\n", test_strlen("", 0));
printf("%d\n", test_strlen("hello", 0));
//strchr
printf("%d\n", test_strchr("world", 'r', 0));
printf("%d\n", test_strchr("world", 'a', 0));
printf("%d\n", test_strchr("", 'a', 0));
//strstr
printf("%d\n", test_strstr("world", "rl", 0));
printf("%d\n", test_strstr("needle", "nee", 0));
printf("%d\n", test_strstr("world", "hello", 0));
return 0;
}