File: faop_regex.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (256 lines) | stat: -rw-r--r-- 7,752 bytes parent folder | download | duplicates (9)
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
# -*- tcl -*-
# fa_operations.test:  tests for the FA operations.
#
# Copyright (c) 2004-2007 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
#
# RCS: @(#) $Id: faop_regex.test,v 1.7 2007/12/03 21:46:25 andreas_kupries Exp $

# -------------------------------------------------------------------------

test faop-regex-${setimpl}-1.0 {fromRegex, error} {
    catch {grammar::fa::op::fromRegex} res
    set res
} {wrong # args: should be "grammar::fa::op::fromRegex fa regex ?over?"}


test faop-regex-${setimpl}-1.1 {fromRegex, error} {
    catch {grammar::fa::op::fromRegex a b c d} res
    set res
} {wrong # args: should be "grammar::fa::op::fromRegex fa regex ?over?"}


test faop-regex-${setimpl}-1.2 {fromRegex, error} {
    catch {grammar::fa::op::fromRegex a b} res
    set res
} {Expected . ! ? * | &, or S, got "b"}


test faop-regex-${setimpl}-1.3 {fromRegex, error} {
    catch {grammar::fa::op::fromRegex a {S b}} res
    set res
} {invalid command name "a"}


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

foreach {n over re fres} {
    00 {} {}
    {grammar::fa {} {}}

    01 {} {S x}
    {grammar::fa x {0 {1 0 {x 1}} 1 {0 1 {}}}}

    02 {} {. {S x} {S y}}
    {grammar::fa {x y} {0 {1 0 {x 1}} 1 {0 0 {{} 2}} 2 {0 0 {y 3}} 3 {0 1 {}}}}

    03 {} {| {S x} {S y}}
    {grammar::fa {x y} {0 {1 0 {{} {2 4}}} 1 {0 1 {}} 2 {0 0 {x 3}} 3 {0 0 {{} 1}} 4 {0 0 {y 5}} 5 {0 0 {{} 1}}}}

    04 {} {? {S x}}
    {grammar::fa x {0 {1 0 {{} {2 1}}} 1 {0 1 {}} 2 {0 0 {x 3}} 3 {0 0 {{} 1}}}}

    05 {} {* {S x}}
    {grammar::fa x {0 {1 1 {{} 1}} 1 {0 0 {x 2}} 2 {0 0 {{} 0}}}}

    06 {} {+ {S x}}
    {grammar::fa x {0 {1 0 {{} 2}} 1 {0 1 {{} 0}} 2 {0 0 {x 3}} 3 {0 0 {{} 1}}}}

    07 {} {! {S x}}
    {grammar::fa x {0 {1 0 {{} 2}} 1 {0 1 {}} 2 {0 0 {x 3 {} 1}} 3 {0 0 {x 6}} 6 {0 0 {x 6 {} 1}}}}

    08 {/ * { } a} {. {S /} {S *} {+ {! {. {S *} {S /}}}} {S *} {S /}}
    {grammar::fa {{ } a * /} {0 {1 0 {/ 1}} 1 {0 0 {{} 2}} 2 {0 0 {* 3}} 3 {0 0 {{} 4}} 4 {0 0 {{} 6}} 5 {0 0 {{} {4 16}}} 6 {0 0 {{} 12}} 7 {0 0 {{} 5}} 12 {0 0 {{ } 15 {} 7 a 15 * 13 / 15}} 13 {0 0 {{ } 15 {} 7 a 15 * 15 / 14}} 14 {0 0 {{ } 15 a 15 * 15 / 15}} 15 {0 0 {{ } 15 {} 7 a 15 * 15 / 15}} 16 {0 0 {* 17}} 17 {0 0 {{} 18}} 18 {0 0 {/ 19}} 19 {0 1 {}}}}

    09 {} {. {S x}}
    {grammar::fa x {0 {1 0 {x 1}} 1 {0 1 {}}}}

    10 {} {| {S x}}
    {grammar::fa x {0 {1 0 {x 1}} 1 {0 1 {}}}}

    11 {} {& {S x}}
    {grammar::fa x {0 {1 0 {x 1}} 1 {0 1 {}}}}

    12 {} {& {. {S a} {* {S d}} {S c}} {. {S a} {* {S b}} {S c}}}
    {grammar::fa {a b c d} {0 {1 0 {{} 2}} 1 {0 1 {}} 2 {0 0 {a 3}} 3 {0 0 {c 4}} 4 {0 0 {{} 1}}}}
} {
    set key ${n}

    test faop-regex-${setimpl}-2.$key {fromRegex} {
	grammar::fa a
	grammar::fa::op::fromRegex a $re $over
	set res [validate_serial $fres a]
	a destroy
	set res

    } ok

    test faop-regex-${setimpl}-3.$key {fromRegex, as method} {
	grammar::fa a
	a fromRegex $re $over
	set res [validate_serial $fres a]
	a destroy
	set res
    } ok
}

# -------------------------------------------------------------------------

test faop-regex-${setimpl}-4.0 {toRegexp, error} {
    catch {grammar::fa::op::toRegexp} res
    set res
} {wrong # args: should be "grammar::fa::op::toRegexp fa"}

test faop-regex-${setimpl}-4.1 {toRegexp, error} {
    catch {grammar::fa::op::toRegexp a b} res
    set res
} {wrong # args: should be "grammar::fa::op::toRegexp fa"}

test faop-regex-${setimpl}-4.2 {toRegexp, error} {
    catch {grammar::fa::op::toRegexp a} res
    set res
} {invalid command name "a"}

test faop-regex-${setimpl}-4.3 {toRegexp} {
    grammar::fa a
    a state add 0 1 2
    a symbol add a
    a symbol add b
    a next 0 a --> 1
    a next 0 b --> 2
    a next 1 b --> 0
    a next 2 b --> 0
    a start add 0
    a final add 0

    set res [grammar::fa::op::toRegexp a]
    a destroy
    set res
} {* {| {. {S a} {S b}} {. {S b} {S b}}}}


test faop-regex-${setimpl}-5.0 {toRegexp2, error} {
    catch {grammar::fa::op::toRegexp2} res
    set res
} {wrong # args: should be "grammar::fa::op::toRegexp2 fa"}

test faop-regex-${setimpl}-5.1 {toRegexp2, error} {
    catch {grammar::fa::op::toRegexp2 a b} res
    set res
} {wrong # args: should be "grammar::fa::op::toRegexp2 fa"}

test faop-regex-${setimpl}-5.2 {toRegexp2, error} {
    catch {grammar::fa::op::toRegexp2 a} res
    set res
} {invalid command name "a"}

test faop-regex-${setimpl}-5.3 {toRegexp2} {
    grammar::fa a
    a state add 0 1 2
    a symbol add a
    a symbol add b
    a next 0 a --> 1
    a next 0 b --> 2
    a next 1 b --> 0
    a next 2 b --> 0
    a start add 0
    a final add 0

    set res [grammar::fa::op::toRegexp2 a]
    a destroy
    set res
} {* {| {. {S a} {S b}} {. {S b} {S b}}}}

# -------------------------------------------------------------------------

test faop-regex-${setimpl}-6.0 {toTclRegexp, error} {
    catch {grammar::fa::op::toTclRegexp} res
    set res
} {wrong # args: should be "grammar::fa::op::toTclRegexp re symdict"}

test faop-regex-${setimpl}-6.1 {toTclRegexp, error} {
    catch {grammar::fa::op::toTclRegexp a b c} res
    set res
} {wrong # args: should be "grammar::fa::op::toTclRegexp re symdict"}

test faop-regex-${setimpl}-6.2 {toTclRegexp, error} {
    catch {grammar::fa::op::toTclRegexp a {}} res
    set res
} {invalid command name "a"}

test faop-regex-${setimpl}-6.3 {toTclRegexp} {
    grammar::fa::op::toTclRegexp {* {| {. {S a} {S b}} {. {S b} {S b}}}} {}
} {(ab|bb)*}

# -------------------------------------------------------------------------

test faop-regex-${setimpl}-7.0 {simplifyRegexp, error} {
    catch {grammar::fa::op::simplifyRegexp} res
    set res
} {wrong # args: should be "grammar::fa::op::simplifyRegexp RE0"}

test faop-regex-${setimpl}-7.1 {simplifyRegexp, error} {
    catch {grammar::fa::op::simplifyRegexp a b} res
    set res
} {wrong # args: should be "grammar::fa::op::simplifyRegexp RE0"}

test faop-regex-${setimpl}-7.2 {simplifyRegexp} {
    set re {* {. {| {S a} {S b}} {S b}}}
    grammar::fa::op::simplifyRegexp $re
} {* {. {| {S a} {S b}} {S b}}}

test faop-regex-${setimpl}-7.3 {simplifyRegexp} {
    set re {* {| {. {S a} {S b}} {. {S b} {S b}}}}
    grammar::fa::op::simplifyRegexp $re
} {* {. {| {S a} {S b}} {S b}}}

# -------------------------------------------------------------------------
## Two larger examples

test faop-regex-${setimpl}-8.0 {to(Tcl)Regexp, match 2 mod 3, decimal} {
    set fa [grammar::fa decimal_2_mod_3]
    $fa state  add 0 1 2
    $fa symbol add 0 1 2 3 4 5 6 7 8 9
    foreach state [$fa states] {
	foreach digit [$fa symbols] {
	    $fa next $state $digit --> [expr {(10*$state + $digit) % 3}]
	}
    }
    $fa start add 0
    $fa final add 2
    set RE ^([grammar::fa::op::toTclRegexp [grammar::fa::op::toRegexp $fa] {}])\$
    # Check the generated regex for correctness. Should match all ints 2 mod 3.
    set res {}
    for {set n 0} {$n<1000} {incr n} {
	if {[regexp $RE $n] != ($n % 3 == 2)} {
	    lappend res $n
	}
    }
    $fa destroy
    set res
} {}

test faop-regex-${setimpl}-8.1 {to(Tcl)Regexp, match 1 mod 8, octal} {

    set fa [grammar::fa octal_1_mod_3]
    $fa state  add 0 1 2
    $fa symbol add 0 1 2 3 4 5 6 7
    foreach state [$fa states] {
	foreach digit [$fa symbols] {
	    $fa next $state $digit --> [expr {(8*$state + $digit) % 3}]
	}
    }
    $fa start add 0
    $fa final add 1
    set RE ^([grammar::fa::op::toTclRegexp [grammar::fa::op::toRegexp $fa] {}])\$
    set res {}
    for {set n 0} {$n<4096} {incr n} {
	if {[regexp $RE [format %o $n]] != ($n % 3 == 1)} {
	    lappend res $n
	}
    }
    $fa destroy
    set res
} {}

# -------------------------------------------------------------------------
::tcltest::cleanupTests