-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.c
88 lines (72 loc) · 2.14 KB
/
test.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "string_matching.h"
void print_array(int *arr, int size){
for (int i=0; i<size; i++){
printf("%d ",arr[i]);
}
printf("\n");
}
void short_test (char *text, int N, char * pattern, int M){
printf("text='%s', pattern='%s'\n", text, pattern);
int result1 = string_matching_naive(text, N, pattern, M);
int result2 = string_matching_kmp(text, N, pattern, M);
printf("Number of occurrences: result1=%d, result2=%d\n", result1, result2);
}
void stress_test(int N, int M){
int i;
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
srand(time(NULL)); // Initialization, should only be called once.
while (1) {
int n = rand() % (N-3) + 3; // Returns a pseudo-random integer between 3 and N.
int m = rand() % M + 1;
char *text = malloc(n+1);
for (i=0; i<n; i++){
int pos = rand() % (int)(sizeof(charset) -1);
text[i] = charset[pos];
}
text[i] = '\0';
char *pattern = malloc(m +1);
for (i=0; i<m; i++){
int pos = rand() % (int)(sizeof(charset) - 1);
pattern[i] = charset[pos];
}
pattern[i] = '\0';
printf("text='%s', pattern='%s'\n", text, pattern);
int result1 = string_matching_naive(text, n, pattern, m);
int result2 = string_matching_kmp(text, n, pattern, m);
if (result1==result2)
printf("OK\n");
else {
printf("Wrong answer: correct=%d, got instead=%d\n", result1, result2);
exit(0);
}
free(text);
free(pattern);
}
}
int main(int argc, char **argv ){
if (argc < 4){
printf("To run: test <1> <text> <N> <pattern> <M>\n or test <2> <N> <M>\n");
return 0;
}
int mode = atoi(argv[1]);
if (mode == 1){
if (argc < 6){
printf("To run: test <1> <text> <N> <pattern> <M>\n");
return 0;
}
char *text = argv[2];
int N = atoi(argv[3]);
char *pattern = argv[4];
int M = atoi(argv[5]);
printf("M=%d\n",M);
short_test(text, N, pattern, M);
return 0;
}
int N = atoi(argv[2]);
int M = atoi(argv[3]);
stress_test(N, M);
return 0;
}