-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrtest.c
More file actions
258 lines (228 loc) · 9.06 KB
/
strtest.c
File metadata and controls
258 lines (228 loc) · 9.06 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "strlib.h"
#include <stdio.h>
int test_memchr(){
const char str[] = "http://www.tutorialspoint.com";
const char ch = '.';
char *ret = ics53_memchr(str, ch, ics53_strlen(str));
if(ret != str + 10){ fprintf(stderr, "Failed on memchr 1\n"); return 0;}
const char str2[] = "fffFfff";
const char ch2 = 'F';
ret = ics53_memchr(str2, ch2, ics53_strlen(str2));
if(ret != str2 + 3){ fprintf(stderr, "Failed on memchr 2\n"); return 0;}
const char str3[] = "its not here";
const char ch3 = 'd';
ret = ics53_memchr(str3, ch3, ics53_strlen(str3));
if(ret != NULL){ fprintf(stderr, "Failed on memchr 3\n"); return 0;}
return 1;
}
int test_memcmp(){
char str[] = "hello", str2[] = "help!";
if( ics53_memcmp(str, str2, strlen(str)) != 1 ){ fprintf(stderr, "Failed on memcmp 1\n"); return 0; }
char str3[] = "hello", str4[] = "Hello";
if( ics53_memcmp(str3, str4, strlen(str3)) != -1 ){ fprintf(stderr, "Failed on memcmp 2\n"); return 0; }
char str5[] = "hello", str6[] = "hello";
if( ics53_memcmp(str5, str6, strlen(str5)) != 0 ){ fprintf(stderr, "Failed on memcmp 3\n"); return 0; }
return 1;
}
int test_memcpy(){
char str[] = "hello", str2[] = "not hello";
char * ret = ics53_memcpy(str2, str, strlen(str));
if (ics53_memcmp(ret, "helloello", 9) != 0){ fprintf(stderr, "Failed on memcpy 1\n"); return 0;}
char str3[] = "hello again", str4[] = "still not hello";
ret = ics53_memcpy(str3, str4, 5);
if (ics53_memcmp(ret, "still again", 10) != 0){ fprintf(stderr, "Failed on memcpy 2\n"); return 0;}
char str5[] = "test hello", str6[] = "wont do hello";
ret = ics53_memcpy(str5, str6, 10);
if (ics53_memcmp(ret, "wont do he", 10) != 0){ fprintf(stderr, "Failed on memcpy2\n"); return 0;}
return 1;
}
int test_memmove(){
char str[] = "my grade is not good........";
memmove(str+16, str+12, 8);
if (strcmp(str, "my grade is not not good....") != 0) { fprintf(stderr, "Failed on memmove 1\n"); return 0;}
memmove(str+12, str+10, 5);
if (strcmp(str, "my grade is s notot good....") != 0) { fprintf(stderr, "Failed on memmove 2\n"); return 0;}
memmove(str+5, str, 8);
if (strcmp(str, "my grmy grade notot good....") != 0) { fprintf(stderr, "Failed on memmove 3\n"); return 0;}
return 1;
}
int test_memset(){
char str[50];
ics53_memset(str, '$', 7);
int i;
for (i = 0; i < 7; i++)
if (str[i] != '$'){
fprintf(stderr, "Failed on memset 1\n"); return 0;
}
ics53_memset(str, 'F', 2);
for(i = 0; i < 2; i++){
if (str[i] != 'F'){ fprintf(stderr, "Failed on memset 2\n"); return 0;}
}
ics53_memset(str, 'L', 4);
for(i = 0; i < 4; i++){
if (str[i] != 'L'){ fprintf(stderr, "Failed on memset 3\n"); return 0;}
}
return 1;
}
int test_strcat(){
char src[50], dest[50];
ics53_strcpy(src, "src");
ics53_strcpy(dest, "dest");
ics53_strcat(dest, src);
//printf("%s\n", dest);
char res[] = "destsrc";
int i;
for (i = 0; res[i]; i++)
if (res[i] != dest[i]){ fprintf(stderr, "Failed on strcat 1\n"); return 0;}
ics53_strcat(dest, src);
char res2[] = "destsrcsrc";
for (i = 0; res2[i]; i++)
if (res2[i] != dest[i]){ fprintf(stderr, "Failed on strcat 2\n"); return 0;}
ics53_strcat(dest, src);
char res3[] = "destsrcsrcsrc";
for (i = 0; res3[i]; i++)
if (res3[i] != dest[i]){ fprintf(stderr, "Failed on strcat 3\n"); return 0;}
return 1;
}
int test_strncat(){
return test_strcat();//its the same one as strcat
}
int test_strchr(){
char res[] = "aaxxxgfgkgggaaaagggggga";
if (ics53_strchr(res, 'x') != (res + 2)){ fprintf(stderr, "Failed on strchr 1\n"); return 0;}
if (ics53_strchr(res, 'a') != (res)){ fprintf(stderr, "Failed on strchr 2\n"); return 0;}
if (ics53_strchr(res, 'z') != NULL){ fprintf(stderr, "Failed on strchr 3\n"); return 0;}
return 1;
}
int test_strcmp(){
char str[] = "hello", str2[] = "help!";
if( ics53_strcmp(str, str2) != 1 ){ fprintf(stderr, "Failed on strcmp 1\n"); return 0; }
char str3[] = "hello", str4[] = "Hello";
if( ics53_strcmp(str3, str4) != -1 ){ fprintf(stderr, "Failed on strcmp 2\n"); return 0; }
char str5[] = "hello", str6[] = "hello";
if( ics53_strcmp(str5, str6) != 0 ){ fprintf(stderr, "Failed on strcmp 3\n"); return 0; }
return 1;
}
int test_strncmp(){
char str[] = "aaaab", str2[] = "aaaaa";
if( ics53_strncmp(str, str2, 4) != 0){ fprintf(stderr, "Failed on strncmp 1\n"); return 0; }
if( ics53_strncmp(str, str2, 5) != -1){ fprintf(stderr, "Failed on strncmp 2\n"); return 0; }
char str3[] = "ABCD", str4[] = "abcd";
if( ics53_strncmp(str3, str4, 3) != 1){ fprintf(stderr, "Failed on strncmp 3\n"); return 0; }
return 1;
}
int test_strcoll(){
return test_strncmp(); //both are the same in the code
}
int test_strncpy(){
char str[] = "aaaaaa", str2[] = "bbbbbbb", str3[] = "cccccc";
ics53_strncpy(str, str2, 4);
if( ics53_strcmp(str, "bbbbaa") != 0){ fprintf(stderr, "Failed on strncpy 1\n"); return 0; }
ics53_strncpy(str, str3, 2);
if( ics53_strcmp(str, "ccbbaa") != 0){ fprintf(stderr, "Failed on strncpy 2\n"); return 0; }
ics53_strncpy(str, str2, 1);
if( ics53_strcmp(str, "bcbbaa") != 0){ fprintf(stderr, "Failed on strncpy 3\n"); return 0; }
return 1;
}
int test_strcpy(){
return test_strncpy();//both are the same in the code
}
int test_strcspn(){
char str[] = "abcdeabcde", str2[] = "xyc", str3[] = "hge", str4[] = "eea";
//printf("%d\n", ics53_strcspn(str, str3));
if( ics53_strcspn(str, str2) != 3){ fprintf(stderr, "Failed on strcspn 1\n"); return 0; }
if( ics53_strcspn(str, str3) != 5){ fprintf(stderr, "Failed on strcspn 2\n"); return 0; }
if( ics53_strcspn(str, str4) != 1){ fprintf(stderr, "Failed on strcspn 3\n"); return 0; }
return 1;
}
int test_strlen(){
char str[] = "abc", str2[] = "", str3[] = "xyz123";
if( ics53_strlen(str) != 3){ fprintf(stderr, "Failed on strlen 1\n"); return 0; }
if( ics53_strlen(str2) != 0){ fprintf(stderr, "Failed on strlen 2\n"); return 0; }
if( ics53_strlen(str3) != 6){ fprintf(stderr, "Failed on strlen 3\n"); return 0; }
return 1;
}
int test_strpbrk(){
char str[] = "abcdefghello", str2[] = "gfe", str3[] = "zxy", str4[] = "lak";
//printf("%d %d\n", str, ics53_strpbrk(str, str2));
if( ics53_strpbrk(str, str2) != str + 4){ fprintf(stderr, "Failed on strpbrk 1\n"); return 0; }
if( ics53_strpbrk(str, str3) != NULL){ fprintf(stderr, "Failed on strpbrk 2\n"); return 0; }
if( ics53_strpbrk(str, str4) != str){ fprintf(stderr, "Failed on strpbrk 3\n"); return 0; }
return 1;
}
int test_strrchr(){
char str[] = "abcdedcba";
if( ics53_strrchr(str, 'e') != str + 4){ fprintf(stderr, "Failed on strrchr 1\n"); return 0; }
if( ics53_strrchr(str, 'a') != str + 8){ fprintf(stderr, "Failed on strrchr 2\n"); return 0; }
if( ics53_strrchr(str, 'c') != str + 6){ fprintf(stderr, "Failed on strrchr 3\n"); return 0; }
return 1;
}
int test_strspn(){
char str1[] = "ABCDEFGHIJKLMNOP", str2[] = "ABC", str3[] = "ABcde", str4[] = "EDCBA";
//printf("%d\n", ics53_strspn(str1, str2));
if( ics53_strspn(str1, str2) != 3){ fprintf(stderr, "Failed on strspn 1\n"); return 0; }
if( ics53_strspn(str1, str3) != 2){ fprintf(stderr, "Failed on strspn 2\n"); return 0; }
if( ics53_strspn(str1, str4) != 5){ fprintf(stderr, "Failed on strspn 3\n"); return 0; }
return 1;
}
int test_strstr(){
char haystack[] = "hello bob book", needle1[] = "hello", needle2[] = "bob", needle3[] = "book";
if( ics53_strstr(haystack, needle1) != haystack + 0){ fprintf(stderr, "Failed on strstr 1\n"); return 0; }
//printf("%d %d", haystack, ics53_strstr(haystack, needle3));
if( ics53_strstr(haystack, needle2) != haystack + 6){ fprintf(stderr, "Failed on strstr 2\n"); return 0; }
if( ics53_strstr(haystack, needle3) != haystack + 10){ fprintf(stderr, "Failed on strstr 3\n"); return 0; }
return 1;
}
int test_strxfrm(){
return test_strncpy();//same as previous code
}
int test(){
int score = 100;
if (!test_memchr())
score -= 5;
if (!test_memcmp())
score -= 5;
if (!test_memcpy())
score -= 5;
if (!test_memmove())
score -= 5;
if (!test_memset())
score -= 5;
if (!test_strcat())
score -= 5;
if (!test_strncat())
score -= 5;
if (!test_strchr())
score -= 5;
if (!test_strcmp())
score -= 5;
if (!test_strncmp())
score -= 5;
if (!test_strcoll())
score -= 5;
if (!test_strcpy())
score -= 5;
if (!test_strncpy())
score -= 5;
if (!test_strcspn())
score -= 5;
if (!test_strlen())
score -= 5;
if (!test_strpbrk())
score -= 5;
if (!test_strrchr())
score -= 5;
if (!test_strspn())
score -= 5;
if (!test_strstr())
score -= 5;
if (!test_strxfrm())
score -= 5;
// other test cases omitted
return score;
}
int main(void){
printf("score is %d\n", test());
exit(1);
return 0;
}