File: nodes.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (341 lines) | stat: -rw-r--r-- 10,494 bytes parent folder | download | duplicates (4)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- tcl -*-
# Graph tests - nodes
# Copyright (c) 2006 Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
# RCS: @(#) $Id: nodes.test,v 1.3 2008/03/23 19:08:26 andreas_kupries Exp $

# Syntax: graph nodes
# (1)     graph nodes -key KEY
#         graph nodes -key KEY -value VALUE
# (2)     graph nodes -filter CMDPREFIX
# (3)     graph nodes -in       NODE...
#         graph nodes -out      NODE...
#         graph nodes -adj      NODE...
#         graph nodes -inner    NODE...
#         graph nodes -embedded NODE...

# We can use one in each group (1,2,3)

# Note: node names may have a leading dash ('-').

# Heuristics: Unknown options after (3) are considered node names
# until we reach a valid option.

# -------------------------------------------------------------------------
# Wrong # args: Missing, Too many

# Cannot have missing arguments (zero is fine),
# except when switches are in use. That however
# is tested with the switches. Ditto for too many
# arguments.

# -------------------------------------------------------------------------
# Logical arguments checks and failures

test graph-${impl}-${setimpl}-nodes-1.0 {nodes, bad switch} {
    SETUP
    catch {mygraph nodes -foo} msg
    mygraph destroy
    set msg
} {bad restriction "-foo": must be -adj, -embedding, -filter, -in, -inner, -key, -out, or -value}

# -------------------------------------------------------------------------
# Ok arguments.

test graph-${impl}-${setimpl}-nodes-2.0 {nodes, empty graph} {
    SETUP
    set result [mygraph nodes]
    mygraph destroy
    set result
} {}

test graph-${impl}-${setimpl}-nodes-2.1 {nodes} {
    SETUP
    mygraph node insert 0 1 2 3 4 5
    set result [lsort [mygraph nodes]]
    mygraph destroy
    set result
} {0 1 2 3 4 5}

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

# (1) -key, -value
# -------------------------------------------------------------------------
# Wrong # args: Missing, Too many

test graph-${impl}-${setimpl}-nodes-key-1.0 {nodes, wrong#args, missing} {
    SETUP
    catch {mygraph nodes -key} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"$MY nodes ?-key key? ?-value value? ?-filter cmd? ?-in|-out|-adj|-inner|-embedding node node...?\""

test graph-${impl}-${setimpl}-nodes-key-1.1 {nodes, wrong#args, missing} {
    SETUP
    catch {mygraph nodes -value} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"$MY nodes ?-key key? ?-value value? ?-filter cmd? ?-in|-out|-adj|-inner|-embedding node node...?\""

# -------------------------------------------------------------------------
# Logical arguments checks and failures

test graph-${impl}-${setimpl}-nodes-key-2.0 {nodes, multiple -key} {
    SETUP
    catch {mygraph nodes -key foobar -value 1 -key foo} msg
    mygraph destroy
    set msg
} {invalid restriction: illegal multiple use of "-key"}

test graph-${impl}-${setimpl}-nodes-key-2.1 {nodes, multiple -value} {
    SETUP
    catch {mygraph nodes -key foobar -value 1 -value foo} msg
    mygraph destroy
    set msg
} {invalid restriction: illegal multiple use of "-value"}

test graph-${impl}-${setimpl}-nodes-key-2.2 {nodes, -value without -key} {
    SETUP
    catch {mygraph nodes -value 1} msg
    mygraph destroy
    set msg
} {invalid restriction: use of "-value" without "-key"}

# -------------------------------------------------------------------------
# Ok arguments.

test graph-${impl}-${setimpl}-nodes-key-3.0 {nodes, -key} {
    SETUP
    mygraph node insert n0 n1
    mygraph node set n0 foobar  1
    mygraph node set n1 blubber 2

    catch {mygraph nodes -key foobar} msg
    mygraph destroy
    set msg
} n0

test graph-${impl}-${setimpl}-nodes-key-3.1 {nodes, -key, -value} {
    SETUP
    mygraph node insert n0 n1
    mygraph node set n0 foobar 1
    mygraph node set n1 foobar 2

    catch {mygraph nodes -key foobar -value 1} msg
    mygraph destroy
    set msg
} n0

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

# (2) -filter
# -------------------------------------------------------------------------
# Wrong # args: Missing, Too many

test graph-${impl}-${setimpl}-nodes-filter-1.0 {nodes, wrong#args, missing} {
    SETUP
    catch {mygraph nodes -filter} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"$MY nodes ?-key key? ?-value value? ?-filter cmd? ?-in|-out|-adj|-inner|-embedding node node...?\""

# -------------------------------------------------------------------------
# Logical arguments checks and failures

test graph-${impl}-${setimpl}-nodes-filter-2.0 {nodes, multiple -filter} {
    SETUP
    catch {mygraph nodes -filter foobar -filter foo} msg
    mygraph destroy
    set msg
} {invalid restriction: illegal multiple use of "-filter"}

# -------------------------------------------------------------------------
# Ok arguments.

test graph-${impl}-${setimpl}-nodes-filter-3.0 {nodes, -filter} {
    SETUP

    mygraph node insert 1 2 3 4 5 6
    mygraph node set 1 volume 30
    mygraph node set 3 volume 50

    proc vol {g n} {
	$g node keyexists $n volume
    }

    set result [lsort [mygraph nodes -filter vol]]
    mygraph destroy
    rename vol {}

    set result
} {1 3}

test graph-${impl}-${setimpl}-nodes-filter-3.1 {nodes, -filter} {
    SETUP

    mygraph node insert 1 2 3 4 5 6
    mygraph node set 1 volume 30
    mygraph node set 3 volume 50

    proc vol {g n} {
	if {![$g node keyexists $n volume]} {return 0}
	expr {[$g node get $n volume] > 40}
    }

    set result [mygraph nodes -filter vol]
    mygraph destroy
    rename vol {}

    set result
} 3

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

# (3) -in, -out, -adj, -inner, -embedding
# -------------------------------------------------------------------------
# Wrong # args: Missing, Too many

set n 0
foreach switch {-in -out -adj -inner -embedding} {

    test graph-${impl}-${setimpl}-nodes-ioaie-1.$n "nodes, $switch, wrong#args, missing" {
	SETUP
	catch {mygraph nodes $switch} msg
	mygraph destroy
	set msg
    } "wrong # args: should be \"$MY nodes ?-key key? ?-value value? ?-filter cmd? ?-in|-out|-adj|-inner|-embedding node node...?\"" ; # {}

    incr n
}

# -------------------------------------------------------------------------
# Logical arguments checks and failures

set n 0
foreach switch {-in -out -adj -inner -embedding} {

    test graph-${impl}-${setimpl}-nodes-ioaie-2.$n "nodes, $switch, missing node" {
	SETUP
	catch {mygraph nodes $switch x} msg
	mygraph destroy
	set msg
    } [MissingNode $MY x] ; # {}

    incr n

    foreach switchB {-in -out -adj -inner -embedding} {

	test graph-${impl}-${setimpl}-nodes-ioaie-2.$n "nodes, $switch, $switchB together" {
	    SETUP
	    catch {mygraph nodes $switch $switchB x} msg
	    mygraph destroy
	    set msg
	} {invalid restriction: illegal multiple use of "-in"|"-out"|"-adj"|"-inner"|"-embedding"} ; # {}

	incr n
    }
}

# -------------------------------------------------------------------------
# Ok arguments.

set n 0
foreach {switch nodes expected} {
    -in        {1 2 3} {-1 1 2 3 4 5 6} -in        {4 5 6} {}
    -out       {1 2 3} {1 2 3}          -out       {4 5 6} {1 2 3}
    -adj       {1 2 3} {-1 1 2 3 4 5 6} -adj       {4 5 6} {1 2 3}
    -inner     {1 2 3} {1 2 3}          -inner     {4 5 6} {}
    -embedding {1 2 3} {-1 4 5 6}       -embedding {4 5 6} {1 2 3}
    -in        {1 2}   {-1 1 3 4 5}     -in        {4 5}   {}
    -out       {1 2}   {2 3}            -out       {4 5}   {1 2}
    -adj       {1 2}   {-1 1 2 3 4 5}   -adj       {4 5}   {1 2}
    -inner     {1 2}   {1 2}            -inner     {4 5}   {}
    -embedding {1 2}   {-1 3 4 5}       -embedding {4 5}   {1 2}
    -in        {1}     {3 4}            -in        {4}     {}
    -out       {1}     {2}              -out       {4}     {1}
    -adj       {1}     {2 3 4}          -adj       {4}     {1}
    -inner     {1}     {}               -inner     {4}     {}
    -embedding {1}     {2 3 4}          -embedding {4}     {1}
    -in        {1 4}   {3 4}            -in        {4 2}   {-1 1 5}
    -out       {1 4}   {1 2}            -out       {4 2}   {1 3}
    -adj       {1 4}   {1 2 3 4}        -adj       {4 2}   {-1 1 3 5}
    -inner     {1 4}   {1 4}            -inner     {4 2}   {}
    -embedding {1 4}   {2 3}            -embedding {4 2}   {-1 1 3 5}
    -in        {1 -1}  {3 4}
    -out       {1 -1}  2
    -adj       {1 -1}  {2 3 4}
    -inner     {1 -1}  {}
    -embedding {1 -1}  {2 3 4}
} {
    lappend expected $switch $nodes

    test graph-${impl}-${setimpl}-nodes-ioaie-3.$n "nodes, $switch ($nodes)" {
	SETUP
	# Nodes (digits -1,1-6) and arcs (letters A-G), no direction
	#        4
	#        |A
	#        1     -1
	#      D/ \   /G
	#      /   \E/
	#     3 --- 2
	#   C/  F    \B
	#   6         5
	
	mygraph node insert 1 2 3 4 5 6 -1
	mygraph arc  insert 4 1 A
	mygraph arc  insert 5 2 B
	mygraph arc  insert 6 3 C
	mygraph arc  insert 3 1 D
	mygraph arc  insert 1 2 E
	mygraph arc  insert 2 3 F
	mygraph arc  insert -1 2 G

	set result [lsort [eval [linsert $nodes 0 mygraph nodes $switch]]]
	mygraph destroy
	lappend result $switch $nodes
	set result
    } $expected ; # {}

    incr n
}
unset n

# ---------------------------------------------------
# Test with many parallel arcs, beyond the number of nodes, i.e. lots
# of duplicated sources and destinations, to check that the dup
# removal works correctly. See bug [SF 1923685].

set n 0
foreach {switch nodes expected} {
    -in        2       1
    -out       2       3
    -adj       2       {1 3}
    -inner     {1 2 3} {1 2 3}
    -embedding 2       {1 3}
} {
    lappend expected $switch $nodes

    test graph-${impl}-${setimpl}-nodes-parallel-ioaie-bug1923685-4.$n "nodes, $switch, parallel arcs, bug 1923685" {
	SETUP

	mygraph node insert 1 2 3 4
	mygraph arc  insert 1 2 A ; mygraph arc  insert 2 3 A.
	mygraph arc  insert 1 2 B ; mygraph arc  insert 2 3 B.
	mygraph arc  insert 1 2 C ; mygraph arc  insert 2 3 C.
	mygraph arc  insert 1 2 D ; mygraph arc  insert 2 3 D.
	mygraph arc  insert 1 2 E ; mygraph arc  insert 2 3 E.
	mygraph arc  insert 1 2 F ; mygraph arc  insert 2 3 F.
	mygraph arc  insert 1 2 G ; mygraph arc  insert 2 3 G.
	mygraph arc  insert 1 2 H ; mygraph arc  insert 2 3 H.

	set result [lsort [eval [linsert $nodes 0 mygraph nodes $switch]]]
	mygraph destroy
	lappend result $switch $nodes
	set result
    } $expected ; # {}

    incr n
}
unset n

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