-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.c
More file actions
executable file
·227 lines (186 loc) · 5.38 KB
/
tests.c
File metadata and controls
executable file
·227 lines (186 loc) · 5.38 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
/** tests.c
* This is the main testing c file. It will test various cases and a few
* special corner cases provided.
*
* Created on: February 12, 2019
* Author: Tom Graham
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "tests.h"
#include "production.h"
#include "mystring.h"
/**
* This is the main boolean driver of the file.
* It will extensively test a lot of cases in the file
* and special corner cases.
* @return true if pass, false otherwise
*/
bool tests(void)
{
bool ok = false;
bool ok1 = testMystrlen1();
if (ok1) {
puts("mystrlen1() passed.");
}
bool ok2 = testMystrlen2();
if (ok2) {
puts("mystrlen2() passed.");
}
bool ok3 = testMystrdup();
if (ok3) {
puts("mystrdup() passed.");
}
bool ok4 = testMystrncpy();
if (ok4) {
puts("mystrncpy() passed.");
}
bool ok5 = testMystrcpy();
if (ok5) {
puts("mystrcpy() passed.");
}
bool ok6 = testMystrncat();
if (ok6) {
puts("mystrncat() passed.");
}
ok = ok1 && ok2 && ok3 && ok4 && ok5 && ok6; // Did all tests pass?
return ok;
}
/** Tests the mystrlen1() function by comparing it to how the standard strlen() works..
* @return true if it passes all tests.
*/
bool testMystrlen1(void) {
bool ok = false; // True if all tests passed
bool ok1 = false; // Individual test results
bool ok2 = false;
char* s1 = "Some sample string."; // String we will take the length of.
size_t l1a = strlen(s1); // Get the lengths with the two functions.
size_t l1b = mystrlen1(s1);
if (l1a == l1b) {
ok1 = true;
}
char* s2 = ""; // Empty string.
size_t l2a = strlen(s2); // Get the lengths with the two functions.
size_t l2b = mystrlen1(s2);
if (l2a == l2b) {
ok2 = true;
}
ok = ok1 && ok2;
return ok;
}
/** Tests the mystrlen2() function by comparing it to how the standard strlen() works..
* @return true if it passes all tests.
*/
bool testMystrlen2(void) {
bool ok = false; // True if all tests passed
bool ok1 = false; // Individual test results
bool ok2 = false;
char* s1 = "Some sample string."; // String we will take the length of.
size_t l1a = strlen(s1); // Get the lengths with the two functions.
size_t l1b = mystrlen2(s1);
if (l1a == l1b) {
ok1 = true;
}
char* s2 = ""; // Empty string.
size_t l2a = strlen(s2); // Get the lengths with the two functions.
size_t l2b = mystrlen2(s2);
if (l2a == l2b) {
ok2 = true;
}
ok = ok1 && ok2;
return ok;
return ok;
}
/** Test mystrdup() function by comparing it to the standard strdup().
* @return true if it passes all tests.
*/
bool testMystrdup() {
bool ok1 = false;
char s1[] = "Some sample string."; // String we will duplicate.
char* s2 = strdup(s1); // Copy it using standard function
char* s3 = mystrdup(s1); // Copy it using my function
if (strcmp(s2, s3) == 0) { // Same result!
ok1 = true;
}
char* s4 = "Some other string."; // Another string for testing
printf("Address of a local variable ok1, on stack: %p\n", &ok1);
printf("Address of original string, on stack: %p\n", s1);
printf("Address of first duplicate string, on heap: %p\n", s2);
printf("Address of second duplicate string, on heap: %p\n", s3);
printf("Address of other string, on heap?: %p\n", s4);
bool ok = ok1;
return ok;
}
/**
* Test mystrcpy() function.
* @return true if pass, false if fail.
*/
bool testMystrcpy() {
bool ok1 = false;
bool ok2 = false;
char s1[] = "Some sample string."; // String we will copy.
char s2[30]; // Empty string we will copy into
char* s3 = mystrcpy(s2, s1); // Copy the string
if (strcmp(s1, s2) == 0) { // Does original equal copy?
ok1 = true; // Yes!
}
if (s3 == s2) { // Is the return value correct?
ok2 = true; // Yes!
}
bool ok = ok1 && ok2;
return ok;
}
/**
* Test mystrncpy() function. This is a boolean function used to test
* mystrncpy vs the regular strncpy.
* @return true if pass, false if fail.
*/
bool testMystrncpy() {
bool ok1 = false;
bool ok2 = false;
char s1a[] = "012345678901234567890123456789"; // A long string
char s2a[] = "ABCDEF"; // A short string we will copy into it.
char* s3a = strncpy(s1a, s2a, 6); // The result
char s1b[] = "012345678901234567890123456789"; // A long string
char s2b[] = "ABCDEF"; // A short string we will copy into it.
char* s3b = mystrncpy(s1b, s2b, 6); // The result
if (strcmp(s3a, s3b) == 0) {
ok1 = true;
}
if (!ok1){
printf("In strncpy(): s1a = /%s/\n", s1a);
printf("In testMystrncpy(): s2a = /%s/\n", s2a);
}
return ok1;
}
/**
* Test mystrncat() function. This is a boolean function used to test
* mystrcat vs the regular strcat.
* @return true if pass, false if fail.
*/
bool testMystrncat() {
bool ok1 = false;
bool ok2 = false;
char s1a[] = "012345678901234567890123456789"; // A long string
char s2a[] = "ABCDEF"; // A short string we will copy into it twice.
char* s3a = mystrcpy(s1a, s2a);
char* s4a = strncat(s1a, s2a, 6); // The result
char s1b[] = "012345678901234567890123456789"; // A long string
char s2b[] = "ABCDEF"; // A short string we will copy into it twice.
char* s3b = mystrcpy(s1b, s2b);
char* s4b = mystrncat(s1b, s2b, 6); // The result
if (strcmp(s3a, s3b) == 0) {
ok1 = true;
}
if (strcmp(s4a, s4b) == 0){
ok2 = true;
}
if (!ok1 || !ok2){
printf("In testMystrncat(): s3a = /%s/\n", s3a);
printf("In testMystrncat(): s3b = /%s/\n", s3b);
printf("In testMystrncat(): s4a = /%s/\n", s4a);
printf("In testMystrncat(): s4b = /%s/\n", s4b);
}
return ok1 && ok2;
}