File: disjointset.testsuite

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 (401 lines) | stat: -rw-r--r-- 11,023 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# -*- tcl -*-
# Tests for the 'disjointset' module in the 'struct' library. -*- tcl -*-
#
# This file contains a collection of tests for one or more of the Tcllib
# procedures.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2008 by Alejandro Eduardo Cruz Paz
# Copyright (c) 2008 by Andreas Kupries (extended for API changes and
# error conditions)
# Copyright (c) 2018 by Kevin B. Kenny - reworked to a proper disjoint-sets
# data structure, added 'add-element', 'exemplars' and 'find-exemplar'.
#
# RCS: @(#) $Id: disjointset.testsuite,v 1.1 2008/09/10 16:23:14 andreas_kupries Exp $

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

test disjointset-1.0 {disjointset creation} {
    ::struct::disjointset DS
    set result [djstate DS]
    DS destroy
    set result
} {{} 0}

test disjointset-1.1 {disjointset creation error} {
    catch {::struct::disjointset DS other} msg
    set result $msg
} {wrong # args: should be "::struct::disjointset ?name?"}

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

test disjointset-2.0 {disjointset add-partition error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS add-partition} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS add-partition" {items} 1]

test disjointset-2.1 {disjointset add-partition error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS add-partition x y} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS add-partition" {items}]

test disjointset-2.2 {disjointset add-partition error, elements already known} {
    testset
    catch {DS add-partition {1}} msg
    DS destroy
    set msg
} {The element "1" is already known to the disjoint set ::DS}

test disjointset-2.3 {disjointset add-partition, ok} {
    testset
    set result [list [DS add-partition {11 14}] [djstate DS]]
    DS destroy
    set result
} {{} {{0 {1 2 3 4} {5 6} {7 10} {8 9} {11 14}} 6}}

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

test disjointset-3.0 {disjointset partitions error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS partitions x} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS partitions" {}]

test disjointset-3.1 {disjointset partitions, ok} {
    testset
    set result [djstate DS]
    DS destroy
    set result
} {{0 {1 2 3 4} {5 6} {7 10} {8 9}} 5}

test disjointset-3.2 {disjointset partitions, empty} {
    ::struct::disjointset DS
    set result [DS partitions]
    DS destroy
    set result
} {}

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

test disjointset-4.0 {disjointset equal error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS equal} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS equal" {a b} 1]

test disjointset-4.1 {disjointset equal error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS equal x} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS equal" {a b} 2]

test disjointset-4.2 {disjointset equal error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS equal x y z} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS equal" {a b}]

test disjointset-4.3 {disjointset equal error, unknown elements} {
    testset
    catch {DS equal x 1} msg
    DS destroy
    set msg
} {The element "x" is not known to the disjoint set ::DS}

test disjointset-4.4 {disjointset equal error, unknown elements} {
    testset
    catch {DS equal 1 x} msg
    DS destroy
    set msg
} {The element "x" is not known to the disjoint set ::DS}

test disjointset-4.5 {disjointset equal ok, unequal elements} {
    testset
    set res [DS equal 1 5]
    DS destroy
    set res
} 0

test disjointset-4.6 {disjointset equal ok, equal elements} {
    testset
    set res [DS equal 4 1]
    DS destroy
    set res
} 1

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

test disjointset-5.0 {disjointset merge error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS merge} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS merge" {a b} 1]

test disjointset-5.1 {disjointset merge error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS merge x} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS merge" {a b} 2]

test disjointset-5.2 {disjointset merge error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS merge x y z} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS merge" {a b}]

test disjointset-5.3 {disjointset merge error, unknown elements} {
    testset
    catch {DS merge x 1} msg
    DS destroy
    set msg
} {The element "x" is not known to the disjoint set ::DS}

test disjointset-5.4 {disjointset merge error, unknown elements} {
    testset
    catch {DS merge 1 x} msg
    DS destroy
    set msg
} {The element "x" is not known to the disjoint set ::DS}

test disjointset-5.5 {disjointset merge ok, different partitions} {
    testset
    DS merge 1 5
    set result [djstate DS]
    DS destroy
    set result
} {{0 {1 2 3 4 5 6} {7 10} {8 9}} 4}

test disjointset-5.6 {disjointset merge ok, same partition, no change} {
    testset
    DS merge 4 3
    set result [djstate DS]
    DS destroy
    set result
} {{0 {1 2 3 4} {5 6} {7 10} {8 9}} 5}

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

test disjointset-6.0 {disjointset find error, wrong#args, missing} {
    ::struct::disjointset DS
    catch {DS find} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS find" {item} 1]

test disjointset-6.1 {disjointset find error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS find x y} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS find" {item}]

test disjointset-6.2 {disjointset find, unknown element} {
    testset
    set result [DS find 11]
    DS destroy
    set result
} {}

test disjointset-6.3 {disjointset find, known element} {
    testset
    set result [lsort -dict [DS find 3]]
    DS destroy
    set result
} {1 2 3 4}

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

test disjointset-7.0 {disjointset num-partitions error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS num-partitions x} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS num-partitions" {}]

test disjointset-7.1 {disjointset num-partitions, ok} {
    testset
    set result [DS num-partitions]
    DS destroy
    set result
} 5

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

test disjointset-8.0 {disjointset add-element error, wrongArgs, none} {
    ::struct::disjointset DS
    catch {DS add-element} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS add-element" {item} 1]

test disjointset-8.1 {disjointset add-element error, wrongArgs, too many} {
    ::struct::disjointset DS
    catch {DS add-element p q} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS add-element" {item}]

test disjointset-8.2 {disjointset add-element error, duplicate element} {
    testset
    catch {DS add-element 0} message
    DS destroy
    set message
} {The element "0" is already known to the disjoint set ::DS}

test disjointset-8.3 {disjointset add-element ok} {
    testset
    DS add-element 11
    set result [djstate DS]
    DS destroy
    set result
} {{0 {1 2 3 4} {5 6} {7 10} {8 9} 11} 6}

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

test disjointset-9.0 {disjointset find-exemplar error, wrongArgs, none} {
    ::struct::disjointset DS
    catch {DS find-exemplar} msg
    DS destroy
    set msg
} [tcltest::wrongNumArgs "DS find-exemplar" {item} 1]

test disjointset-9.1 {disjointset find-exemplar error, wrongArgs, too many} {
    ::struct::disjointset DS
    catch {DS find-exemplar p q} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS find-exemplar" {item}]

test disjointset-9.2 {disjointset find-exemplar error, not found} {
    testset
    catch {DS find-exemplar x} message
    DS destroy
    set message
} {The element "x" is not known to the disjoint set ::DS}    

test disjointset-9.3 {disjointset find-exemplar ok} {
    testset
    set result [DS find-exemplar 3]
    DS destroy
    expr {$result in {1 2 3 4}}
} {1}

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

test disjointset-10.0 {disjointset exemplars error, wrong#args, too many} {
    ::struct::disjointset DS
    catch {DS exemplars x} msg
    DS destroy
    set msg
} [tcltest::tooManyArgs "DS exemplars" {}]

test disjointset-10.1 {disjointset exemplars, ok} {
    ::struct::disjointset DS
    DS add-element 0
    set result [DS exemplars]
    DS destroy
    set result
} 0

test disjointset-10.2 {disjointset exemplars, empty} {
    ::struct::disjointset DS
    set result [DS exemplars]
    DS destroy
    set result
} {}

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

test disjointset-11.0 {disjointset merge - larger randomized set of merges} {
    struct::disjointset DS
    foreach item {a b c d e f g h i j k l m n o p q r s t u v w x y z} {
	DS add-partition [list $item]
    }
    DS merge g a
    DS merge o n
    DS merge v o
    DS merge c w
    DS merge r h
    DS merge s y
    DS merge g i
    DS merge d f
    DS merge m q
    DS merge a z
    DS merge k e
    DS merge x k
    DS merge r s
    DS merge h m
    DS merge d l
    DS merge e a
    DS merge o t
    DS merge q p
    DS merge u c
    DS merge o a
    DS merge p j
    DS merge b l
    DS merge p c
    DS merge f e
    set result [lsort [lmap x [DS partitions] {lsort $x}]]
    DS destroy
    set result
} {{a b d e f g i k l n o t v x z} {c h j m p q r s u w y}}

test disjointset-11.1 {disjointset merge - larger randomized set of merges} {
    struct::disjointset DS
    foreach item {a b c d e f g h i j k l m n o p q r s t u v w x y z} {
	DS add-partition [list $item]
    }
    DS merge g a
    DS merge o n
    DS merge v o
    DS merge c w
    DS merge r h
    DS merge s y
    DS merge g i
    DS merge d f
    DS merge m q
    DS merge a z
    DS merge k e
    DS merge x k
    DS merge r s
    DS merge h m
    DS merge d l
    DS merge e a
    DS merge o t
    DS merge q p
    DS merge u c
    DS merge o a
    DS merge p j
    DS merge b l
    DS merge p c
    DS merge f e
    set result [DS exemplars]
    DS destroy
    set trouble {}
    if {[llength $result] ne 2} {
	append trouble "\nShould be two exemplars, found $result"
    }
    lassign $result e1 e2
    set l1 {a b d e f g i k l n o t v x z}
    set l2 {c h j m p q r s u w y}
    if {!(($e1 in $l1) ^ ($e2 in $l1))} {
	append trouble "\nExactly one of $e1 and $e2\
                          should be in the first set"
    }
    if {!(($e1 in $l2) ^ ($e2 in $l2))} {
	append trouble "\nExactly one of $e1 and $e2\
                          should be in the second set"
    }
    set trouble
} {}