-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
326 lines (286 loc) · 13.4 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Command line interface for suffix tree functions
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <argp.h>
#include "suffix_tree.h"
void self_test();
unsigned char *input_text; //Input string 1 (for suffix tree)
unsigned char *input_text2; //Input string 2 (for generalized suffix tree)
int opt_print_tree = 0;
int opt_lcs = 0;
int opt_acs = 0;
// Process command line options and arguments
static int
parse_opt (int key, char *arg, struct argp_state *state)
{
int *arg_count = state->input;
switch (key)
{
case 't': //Run the self test suite
{
self_test();
exit(0);
}
case 'p': //Print the generated tree
{
opt_print_tree = 1;
break;
}
case 'l': //Find Longest Common Substring (LCS)
{
opt_lcs = 1;
break;
}
case 'a': //Find Longest Common Substring (LCS)
{
opt_acs = 1;
break;
}
case ARGP_KEY_ARG: //Process the command line arguments
{
(*arg_count)--;
if (*arg_count == 3){
input_text = (unsigned char*)arg;
}
else if (*arg_count == 2){
input_text2 = (unsigned char*)arg;
}
}
break;
case ARGP_KEY_END:
{
printf ("\n");
if (*arg_count >= 4){
argp_failure (state, 1, 0, "too few arguments");
}
else if (*arg_count < 0){
argp_failure (state, 1, 0, "too many arguments");
}
else {
if (opt_print_tree){
// Construct the tree and process based on supplied options
buildSuffixTree(input_text, input_text2, opt_print_tree);
freeSuffixTreeByPostOrder(root);
printf(tree_string, 's');
}
if (opt_lcs){
char *lcs;
if(!input_text2){
argp_failure (state, 1, 0, "missing comparison string");
}
printf("Longest Common Substring in %s and %s is: ",
input_text,
input_text2);
lcs = getLongestCommonSubstring(input_text,
input_text2,
opt_print_tree);
printf(lcs, 's');
}
if (opt_acs){
char *acs;
if(!input_text2){
argp_failure (state, 1, 0, "missing comparison string");
}
printf("All Common Substrings in %s and %s are: ",
input_text,
input_text2);
acs = getAllCommonSubstrings(input_text,
input_text2,
opt_print_tree);
printf(acs, 's');
}
}
}
break;
}
return 0;
}
/**
* run self tests
*/
void self_test(){
printf("Running Self Tests... \n");
printf("Build suffix tree test: \n");
char *tree_output = "$ [3]\n"
"abc$ [0]\n"
"bc$ [1]\n"
"c$ [2]\n"; //Expected output
printf("Building suffix tree for string: abc \n");
buildSuffixTree((unsigned char *)"abc", NULL, 1); //Build Suffix tree for this string
freeSuffixTreeByPostOrder(root);
printf(tree_string, 's');
int compare_result = strcmp(tree_string, tree_output);
assert(compare_result == 0);
memset(tree_string,0,strlen(tree_string)); //clear string from previous test
char *tree_output2 = "$ [10]\n"
"ab [-1]\n"
"c [-1]\n"
"abxabcd$ [0]\n"
"d$ [6]\n"
"xabcd$ [3]\n"
"b [-1]\n"
"c [-1]\n"
"abxabcd$ [1]\n"
"d$ [7]\n"
"xabcd$ [4]\n"
"c [-1]\n"
"abxabcd$ [2]\n"
"d$ [8]\n"
"d$ [9]\n"
"xabcd$ [5]\n"; //Expected output
printf("Building suffix tree for string: abcabxabcd \n");
buildSuffixTree((unsigned char *)"abcabxabcd", NULL, 1); //Build Suffix tree for this string
freeSuffixTreeByPostOrder(root);
printf(tree_string, 's');
int compare_result2 = strcmp(tree_string, tree_output2);
assert(compare_result2 == 0);
memset(tree_string,0,strlen(tree_string)); //clear string from previous test
printf("Suffix tree build test: Passed\n\n");
printf("Longest substrings test: \n");
char *lcs;
printf("Longest Common Substring in xabxac and abcabxabcd is: ");
lcs = getLongestCommonSubstring((unsigned char *)"xabxac",
(unsigned char *)"abcabxabcd",
0);
printf(lcs, 's');
int compare_result3 = strcmp(lcs, "abxa, of length: 4\n");
assert(compare_result3 == 0);
memset(lcs,0,strlen(lcs)); //clear string from previous test; //clear string from previous test
printf("Longest Common Substring in xabxaabxa and babxba is: ");
lcs = getLongestCommonSubstring((unsigned char *)"xabxaabxa",
(unsigned char *)"babxba",
0);
printf(lcs, 's');
int compare_result4 = strcmp(lcs, "abx, of length: 3\n");
assert(compare_result4 == 0);
memset(lcs,0,strlen(lcs)); //clear string from previous test;
printf("Longest Common Substring test: Passed\n\n");
printf("Substrings test: \n");
int is_substring = 0;
printf("\"test\" is a substring of \"this is a test\" \n");
is_substring = checkForSubString((unsigned char *)"test",
(unsigned char *)"this is a test");
assert(is_substring == 1);
printf("\"foo\" is a substring of \"this is a test\" \n");
is_substring = checkForSubString((unsigned char *)"foo",
(unsigned char *)"this is a test");
assert(is_substring == 0);
printf("Substrings test: Passed\n\n");
printf("All occurrences of Substring test: \n");
int *all_substrings;
size_t i = 0;
printf("Text: AABAACAADAABAAABAA, Pattern to search: AABA\n");
all_substrings = checkAllSubStringOccurences((unsigned char *)"AABA",
(unsigned char *)"AABAACAADAABAAABAA");
assert(all_substrings[0] == 3);
assert(all_substrings[1] == 13);
assert(all_substrings[2] == 9);
assert(all_substrings[3] == 0);
printf("Substring found count: %d\n", all_substrings[0]);
printf("Substrings found at positions: ");
for( i = 1; i <all_substrings[0] + 1; i++)
{
printf ("%d,",all_substrings[i]);
}
free(all_substrings);
printf("\n\nText: AABAACAADAABAAABAA, Pattern to search: AABAACAAD\n");
all_substrings = checkAllSubStringOccurences((unsigned char *)"AABAACAAD",
(unsigned char *)"AABAACAADAABAAABAA");
assert(all_substrings[0] == 1);
assert(all_substrings[1] == 0);
printf("Substring found count: %d\n", all_substrings[0]);
printf("Substrings found at position: %d", all_substrings[1]);
free(all_substrings);
printf("\n\nText: AABAACAADAABAAABAA, Pattern to search: AA\n");
all_substrings = checkAllSubStringOccurences((unsigned char *)"AA",
(unsigned char *)"AABAACAADAABAAABAA");
assert(all_substrings[0] == 7);
assert(all_substrings[1] == 16);
assert(all_substrings[2] == 12);
assert(all_substrings[3] == 13);
assert(all_substrings[4] == 9);
assert(all_substrings[5] == 0);
assert(all_substrings[6] == 3);
assert(all_substrings[7] == 6);
printf("Substring found count: %d\n", all_substrings[0]);
printf("Substrings found at positions: ");
for( i = 1; i <all_substrings[0] + 1; i++)
{
printf ("%d,",all_substrings[i]);
}
free(all_substrings);
printf("\n\nText: AABAACAADAABAAABAA, Pattern to search: ZZ\n");
all_substrings = checkAllSubStringOccurences((unsigned char *)"ZZ",
(unsigned char *)"AABAACAADAABAAABAA");
assert(all_substrings[0] == 0);
printf("No Substrings found\n");
printf("\nAll occurences of Substring test: Passed\n\n");
printf("Longest Repeated Substring test: \n");
char * lrs;
int lcs_compare;
printf("Longest Repeated Substring in AAAAAAAAAA is: ");
lrs = getLongestRepeatedSubstring((unsigned char *)"AAAAAAAAAA");
lcs_compare = strcmp(lrs, "AAAAAAAAA");
assert(lcs_compare == 0);
printf(lrs, 's');
memset(lrs,0,strlen(lrs)); //clear string from previous test;
printf("\nLongest Repeated Substring in ABABABA is: ");
lrs = getLongestRepeatedSubstring((unsigned char *)"ABABABA");
lcs_compare = strcmp(lrs, "ABABA");
assert(lcs_compare == 0);
printf(lrs, 's');
memset(lrs,0,strlen(lrs)); //clear string from previous test;
printf("\nLongest Repeated Substring in ABCDEFG is: ");
lrs = getLongestRepeatedSubstring((unsigned char *)"ABCDEFG");
lcs_compare = strcmp(lrs, "No repeated substring");
assert(lcs_compare == 0);
printf(lrs, 's');
memset(lrs,0,strlen(lrs)); //clear string from previous test;
printf("\nLongest Repeated Substring in pqrpqpqabab is: ");
lrs = getLongestRepeatedSubstring((unsigned char *)"pqrpqpqabab");
lcs_compare = strcmp(lrs, "ab");
assert(lcs_compare == 0);
printf(lrs, 's');
memset(lrs,0,strlen(lrs)); //clear string from previous test;
printf("\nLongest Repeated Substring in abcpqrabpqpq is: ");
lrs = getLongestRepeatedSubstring((unsigned char *)"abcpqrabpqpq");
lcs_compare = strcmp(lrs, "ab");
assert(lcs_compare == 0);
printf(lrs, 's');
memset(lrs,0,strlen(lrs)); //clear string from previous test;
printf("\nLongest Repeated Substring test: Passed\n\n");
// printf("All Common substrings test: \n");
//
// printf("All Common Substrings in orangeisatypeoffruit and fruitsomestugfruitgoeshereorange are: \n");
// getAllCommonSubstrings((unsigned char *)"orangeisatypeoffruit",
// (unsigned char *)"fruitsomestugfruitgoeshereorange",
// 0);
printf("done");
}
int main(int argc, char **argv)
{
struct argp_option options[] =
{
{ "print", 'p', 0, 0, "Print the tree for the supplied string"},
{ "test", 't', 0, 0, "Run self tests"},
{ "lcs", 'l', 0, 0, "Find longest common substring"},
{ "acs", 'a', 0, 0, "Find all common substring"},
{ 0 }
};
int arg_count = 4;
struct argp argp = { options, parse_opt, "STRING [STRING]" };
return argp_parse (&argp, argc, argv, 0, 0, &arg_count);
// strcpy(text, "abcabxabcd$"); buildSuffixTree(); freeSuffixTreeByPostOrder(root);
// size1 = 21;
// printf("All Common Substrings in orangeisatypeoffruit and fruitsomestugfruitgoeshereorange are: \n");
// strcpy((char*)text, "orangeisatypeoffruit#fruitsomestugfruitgoeshereorange$");
// buildSuffixTree();
// getLongestCommonSubstring();
// //Free the dynamically allocated memory
// freeSuffixTreeByPostOrder(root);
// return 0;
}