Merge branch 'makefiles'
[prop.git] / tests / qa1.cc
blob07a1ac24cca1673c33304b42a1bdd4acdab1cb26
1 // Test strings
3 #include <stdio.h>
4 #include <assert.h>
5 #include <AD/strings/bm.h> // Boyer Moore
6 #include <AD/strings/kmp.h> // Knuth Morris Pratt
7 #include <AD/strings/kr.h> // Karp Rabin
8 #include <AD/strings/quark.h> // Atomic strings
9 #include <AD/strings/regexp.h> // Regular expression search
10 #include <AD/strings/string.h> // Dynamic strings
11 #include <AD/strings/twoway.h> // Two way string matcher
13 int main(int argc, char * argv[])
15 if (RegExp("[a-z]+").Match("The quick brown fox jumps over the lazy dog")
16 == 1)
17 printf("matched\n");
18 else
19 printf("unmatched\n");
21 const char * a = "The quick brown fox";
22 const char * b = "The lazy dog";
23 const char * c = "The quick brown fox";
24 KMP kmp ("The");
25 assert(kmp.Match(a) == 0);
26 kmp = "fox";
27 assert(kmp.Match(a) == 16);
28 kmp = "dog";
29 assert(kmp.Match(b) == 9);
30 assert(kmp.Match(c) == -1);
31 kmp = "XYXYZ";
32 assert(kmp.Match("abcdeXYXYXYYXYXYZXXY") == 12);
33 printf("located\n");
35 BoyerMoore bm;
36 bm = "XYXYZ";
37 assert(bm.Match("abcdeXYXYXYYXYXYZXXY") == 12);
38 bm = "WXYZ";
39 assert(bm.Match("abcdWXYWYXYWXYXYWXYZ") == 16);
40 bm = "ZXYXY";
41 assert(bm.Match("abcdWXYWYZYWXZXYWXYZXYXYZ") == 19);
42 bm = "variable length";
43 assert(bm.Match(
44 "Description: TeX macros for drawing variable length wiggily lines.")
45 == 36);
47 KR kr;
48 kr = "XYXYZ";
49 assert(kr.Match("abcdeXYXYXYYXYXYZXXY") == 12);
50 kr = "WXYZ";
51 assert(kr.Match("abcdWXYWYXYWXYXYWXYZ") == 16);
52 kr = "ZXYXY";
53 assert(kr.Match("abcdWXYWYZYWXZXYWXYZXYXYZ") == 19);
55 Quark q = "The quick brown fox";
56 Quark s = "The lazy dog";
57 Quark r = "The quick brown fox";
58 Quark t;
60 t = s;
61 assert(q != s && q == r && t == s);
63 RegExp re = "fox";
64 assert(re.ok());
65 assert(re.Match("The quick brown fox") == 16);
66 assert(re.Match("The quick brown fofox") == 18);
67 RegExp re2 = re;
68 assert(re2.Match("The quick brown fofox") == 18);
69 assert(re2.Match("The lazy dog f") == -1);
71 re = "fox$";
72 assert(re.Match("The quick brown fox") == 16);
74 RegExp dirname = "^((\\w*/)*)([^/]+)$";
75 assert(dirname.ok());
76 printf("%d\n",dirname.Match("/usr/etc/net/spool/worm.hack\n"));
77 assert(dirname.Match("/usr/etc/net/spool/worm.hack\n") >= 0);
78 assert(dirname.Match("/usr/etc/net/spool/worm.hack") >= 0);
80 printf("dir = %*.*s, base = %*.*s\n",
81 dirname.len(0), dirname.len(0), dirname[0],
82 dirname.len(2), dirname.len(2), dirname[2]);
84 printf("start(0) = %d\n", dirname.start(0));
85 printf("len(0) = %d\n", dirname.len(0));
86 printf("start(2) = %d\n", dirname.start(2));
87 printf("len(2) = %d\n", dirname.len(2));
89 assert(dirname.start(0) == 0);
90 assert(dirname.len(0) == 19);
91 assert(dirname.start(2) == 19);
92 assert(dirname.len(2) == 9);
94 printf("OK\n");
96 return 0;