-
Notifications
You must be signed in to change notification settings - Fork 0
/
intal_sampletest.c
280 lines (254 loc) · 12.1 KB
/
intal_sampletest.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "intal.h"
int main(int argc, char const *argv[]) {
int n = 12;
char **a = (char**) malloc(n * sizeof(char*));
for(int i = 0; i < n; i++) {
a[i] = (char*) malloc(1001 * sizeof(char));
}
strcpy(a[0], "1234512345123451234512345");
strcpy(a[1], "543215432154321543215432154321");
strcpy(a[2], "0");
strcpy(a[3], "1234512345123451234512345");
strcpy(a[4], "1234512345123451234512344");
strcpy(a[5], "12");
strcpy(a[6], "265252859812191058636308480000000");
strcpy(a[7], "265252859812191058636308480000000");
strcpy(a[8], "5432154321543215432154321");
strcpy(a[9], "3");
strcpy(a[10], "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
strcpy(a[11], "1230231922161117176931558813276752514640713895736833715766118029160058800614672948775360067838593459582429649254051804908512884180898236823585082482065348331234959350355845017413023320111360666922624728239756880416434478315693675013413090757208690376793296658810662941824493488451726505303712916005346747908623702673480919353936813105736620402352744776903840477883651100322409301983488363802930540482487909763484098253940728685132044408863734754271212592471778643949486688511721051561970432780747454823776808464180697103083861812184348565522740195796682622205511845512080552010310050255801589349645928001133745474220715013683413907542779063759833876101354235184245096670042160720629411581502371248008430447184842098610320580417992206662247328722122088513643683907670360209162653670641130936997002170500675501374723998766005827579300723253474890612250135171889174899079911291512399773872178519018229989376");
char *result1;
int index1;
result1 = intal_add(a[0], a[1]);
if(!result1) {
printf("Test intal_add FAILED.\n");
} else {
if(0 == strcmp(result1, "543216666666666666666666666666")) {
printf("Test intal_add PASSED\n");
} else {
printf("Test intal_add FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "543216666666666666666666666666");
}
free(result1);
}
index1 = intal_compare(a[0], a[1]);
if(-1 == index1) {
printf("Test intal_compare PASSED\n");
} else {
printf("Test intal_compare FAILED.\nYour answer: %d\nExpected answer: %d\n", index1, -1);
}
result1 = intal_diff(a[0], a[1]);
if(!result1) {
printf("Test intal_diff FAILED.\n");
} else {
if(0 == strcmp(result1, "543214197641976419764197641976")) {
printf("Test intal_diff PASSED\n");
} else {
printf("Test intal_diff FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "543214197641976419764197641976");
}
free(result1);
}
result1 = intal_multiply(a[8], a[5]);
if(!result1) {
printf("Test intal_multiply FAILED.\n");
} else {
if(0 == strcmp(result1, "65185851858518585185851852")) {
printf("Test intal_multiply PASSED\n");
} else {
printf("Test intal_multiply FAILED.\n.Your answer: %s\nExpected answer: %s\n", result1, "65185851858518585185851852");
}
free(result1);
}
result1 = intal_mod(a[3], a[4]);
if(!result1) {
printf("Test intal_mod FAILED.\n");
} else {
if(0 == strcmp(result1, "1")) {
printf("Test intal_mod PASSED\n");
} else {
printf("Test intal_mod FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "1");
}
free(result1);
}
result1 = intal_mod("978", "5");
if(!result1) {
printf("Test intal_mod FAILED.\n");
} else {
if(0 == strcmp(result1, "3")) {
printf("Test intal_mod PASSED\n");
} else {
printf("Test intal_mod FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "3");
}
free(result1);
}
result1 = intal_mod(a[0], a[5]);
if(!result1) {
printf("Test intal_mod FAILED.\n");
} else {
if(0 == strcmp(result1, "9")) {
printf("Test intal_mod PASSED\n");
} else {
printf("Test intal_mod FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "9");
}
free(result1);
}
result1 = intal_pow(a[5], 3);
if(!result1) {
printf("Test intal_pow FAILED.\n");
} else {
if(0 == strcmp(result1, "1728")) {
printf("Test intal_pow PASSED\n");
} else {
printf("Test intal_pow FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "1728");
}
free(result1);
}
result1 = intal_pow("10", 999);
if(!result1) {
printf("Test intal_pow FAILED.\n");
} else {
if(0 == strcmp(result1, a[10])) {
printf("Test intal_pow PASSED\n");
} else {
printf("Test intal_pow FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, a[10]);
}
free(result1);
}
result1 = intal_pow("2", 3000);
if(!result1) {
printf("Test intal_pow FAILED.\n");
} else {
if(0 == strcmp(result1, a[11])) {
printf("Test intal_pow PASSED\n");
} else {
printf("Test intal_pow FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, a[11]);
}
free(result1);
}
result1 = intal_gcd(a[0], a[5]);
if(!result1) {
printf("Test intal_gcd FAILED.\n");
} else {
if(0 == strcmp(result1, "3")) {
printf("Test intal_gcd PASSED\n");
} else {
printf("Test intal_gcd FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "3");
}
free(result1);
}
result1 = intal_fibonacci(3);
if(!result1) {
printf("Test intal_fibonacci FAILED.\n");
} else {
if(0 == strcmp(result1, "2")) {
printf("Test intal_fibonacci PASSED\n");
} else {
printf("Test intal_fibonacci FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "2");
}
free(result1);
}
result1 = intal_fibonacci(1000);
if(!result1) {
printf("Test intal_fibonacci FAILED.\n");
} else {
if(0 == strcmp(result1, "43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875")) {
printf("Test intal_fibonacci PASSED\n");
} else {
printf("Test intal_fibonacci FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875");
}
free(result1);
}
result1 = intal_factorial(30);
if(!result1) {
printf("Test intal_factorial FAILED.\n");
} else {
if(0 == strcmp(result1, a[6])) {
printf("Test intal_factorial PASSED\n");
} else {
printf("Test intal_factorial FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, a[6]);
}
free(result1);
}
result1 = intal_factorial(100);
if(!result1) {
printf("Test intal_factorial FAILED.\n");
} else {
if(0 == strcmp(result1, "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000")) {
printf("Test intal_factorial PASSED\n");
} else {
printf("Test intal_factorial FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000");
}
free(result1);
}
result1 = intal_bincoeff(10, 8);
if(!result1) {
printf("Test intal_bincoeff FAILED.\n");
} else {
if(0 == strcmp(result1, "45")) {
printf("Test intal_bincoeff PASSED\n");
} else {
printf("Test intal_bincoeff FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "45");
}
free(result1);
}
result1 = intal_bincoeff(1000, 900);
if(!result1) {
printf("Test intal_bincoeff FAILED.\n");
} else {
if(0 == strcmp(result1, "63850511926305130236698511142022274281262900693853331776286816221524376994750901948920974351797699894319420811933446197797592213357065053890")) {
printf("Test intal_bincoeff PASSED\n");
} else {
printf("Test intal_bincoeff FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "63850511926305130236698511142022274281262900693853331776286816221524376994750901948920974351797699894319420811933446197797592213357065053890");
}
free(result1);
}
index1 = intal_max(a, n);
if(10 == index1) {
printf("Test intal_max PASSED\n");
} else {
printf("Test intal_max FAILED.\nYour answer: %d\nExpected answer: %d\n", index1, 6);
}
index1 = intal_min(a, n);
if(2 == index1) {
printf("Test intal_min PASSED\n");
} else {
printf("Test intal_min FAILED.\nYour answer: %d\nExpected answer: %d\n", index1, 2);
}
index1 = intal_search(a, n, a[7]);
if(6 == index1) {
printf("Test intal_search PASSED\n");
} else {
printf("Test intal_search FAILED.\nYour answer: %d\nExpected answer: %d\n", index1, 6);
}
result1 = coin_row_problem(a, n);
if(!result1) {
printf("Test coin_row_problem FAILED.\n");
} else {
if(0 == strcmp("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000265796081911012046846190578820987", result1)) {
printf("Test coin_row_problem PASSED\n");
} else {
printf("Test coin_row_problem FAILED.\nYour answer: %s\nExpected answer: %s\n", result1, "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000265796081911012046846190578820987");
}
free(result1);
}
intal_sort(a, n);
printf("\nTest intal_sort PASSED only if the following sequence of %d intals are sorted in nondecreasing order.\n", n);
for (int i = 0; i < n; i++) {
printf("%s\n", a[i]);
}
printf("\n");
index1 = intal_binsearch(a, n, "3");
if(1 == index1) {
printf("Test intal_binsearch and probably intal_sort PASSED\n");
} else {
printf("Test intal_binsearch and probably intal_sort FAILED.\nYour answer: %d\nExpected answer: %d\n", index1, 1);
}
// Don't forget to free all the memory that is dynamically allocated.
for(int i = 0; i < n; i++) {
free(a[i]);
}
free(a);
return 0;
}