make it compile with modern C++
[prop.git] / lib-src / strings / qa.cc
blobff13c4344456962c183688100f039f671f05f78c
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #include <stdio.h>
26 #include <assert.h>
27 #include <AD/strings/bm.h> // Boyer Moore
28 #include <AD/strings/kmp.h> // Knuth Morris Pratt
29 #include <AD/strings/kr.h> // Karp Rabin
30 #include <AD/strings/quark.h> // Atomic strings
31 #include <AD/strings/regexp.h> // Regular expression search
32 #include <AD/strings/string.h> // Dynamic strings
33 #include <AD/strings/twoway.h> // Two way string matcher
35 int main(int argc, char * argv[])
37 if (RegExp("[a-z]+").match("The quick brown fox jumps over the lazy dog")
38 == 1)
39 printf("matched\n");
40 else
41 printf("unmatched\n");
43 const char * a = "The quick brown fox";
44 const char * b = "The lazy dog";
45 const char * c = "The quick brown fox";
46 KMP kmp = "The";
47 assert(kmp.match(a) == 0);
48 kmp = "fox";
49 assert(kmp.match(a) == 16);
50 kmp = "dog";
51 assert(kmp.match(b) == 9);
52 assert(kmp.match(c) == -1);
53 kmp = "XYXYZ";
54 assert(kmp.match("abcdeXYXYXYYXYXYZXXY") == 12);
55 printf("located\n");
57 BoyerMoore bm;
58 bm = "XYXYZ";
59 assert(bm.match("abcdeXYXYXYYXYXYZXXY") == 12);
60 bm = "WXYZ";
61 assert(bm.match("abcdWXYWYXYWXYXYWXYZ") == 16);
62 bm = "ZXYXY";
63 assert(bm.match("abcdWXYWYZYWXZXYWXYZXYXYZ") == 19);
64 bm = "variable length";
65 assert(bm.match(
66 "Description: TeX macros for drawing variable length wiggily lines.")
67 == 36);
69 KR kr;
70 kr = "XYXYZ";
71 assert(kr.match("abcdeXYXYXYYXYXYZXXY") == 12);
72 kr = "WXYZ";
73 assert(kr.match("abcdWXYWYXYWXYXYWXYZ") == 16);
74 kr = "ZXYXY";
75 assert(kr.match("abcdWXYWYZYWXZXYWXYZXYXYZ") == 19);
77 Quark q = "The quick brown fox";
78 Quark s = "The lazy dog";
79 Quark r = "The quick brown fox";
80 Quark t;
82 t = s;
83 assert(q != s && q == r && t == s);
85 RegExp re = "fox";
86 assert(re.ok());
87 assert(re.match("The quick brown fox") == 16);
88 assert(re.match("The quick brown fofox") == 18);
89 RegExp re2 = re;
90 assert(re2.match("The quick brown fofox") == 18);
91 assert(re2.match("The lazy dog f") == -1);
93 re = "fox$";
94 assert(re.match("The quick brown fox") == 16);
96 RegExp dirname = "^((\\w*/)*)([^/]+)$";
97 assert(dirname.ok());
98 printf("%d\n",dirname.match("/usr/etc/net/spool/worm.hack\n"));
99 assert(dirname.match("/usr/etc/net/spool/worm.hack\n") >= 0);
100 assert(dirname.match("/usr/etc/net/spool/worm.hack") >= 0);
102 printf("dir = %*.*s, base = %*.*s\n",
103 dirname.len(0), dirname.len(0), dirname[0],
104 dirname.len(2), dirname.len(2), dirname[2]);
106 printf("start(0) = %d\n", dirname.start(0));
107 printf("len(0) = %d\n", dirname.len(0));
108 printf("start(2) = %d\n", dirname.start(2));
109 printf("len(2) = %d\n", dirname.len(2));
111 assert(dirname.start(0) == 0);
112 assert(dirname.len(0) == 19);
113 assert(dirname.start(2) == 19);
114 assert(dirname.len(2) == 9);
116 printf("OK\n");
118 return 0;