# -*- tcl -*-
# graph.test:  tests for the graph structure.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# All rights reserved.
#
# RCS: @(#) $Id: graph.test,v 1.3 2004/02/09 09:32:14 andreas_kupries Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

source [file join [file dirname [info script]] graph.tcl]
namespace import ::struct::graph::graph

catch {puts "-- cgraph [package present cgraph]"}

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

test graph-0.1 {graph errors} {
    graph mygraph
    catch {graph mygraph} msg
    mygraph destroy
    set msg
} "command \"mygraph\" already exists, unable to create graph"

test graph-0.2 {graph errors} {
    graph mygraph
    catch {mygraph} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph option ?arg arg ...?\""

test graph-0.3 {graph errors} {
    graph mygraph
    catch {mygraph foo} msg
    mygraph destroy
    set msg
} "bad option \"foo\": must be arc, arcs, destroy, get, getall, keys, keyexists, node, nodes, set, swap, unset, or walk"

test graph-0.4 {graph errors} {
    catch {graph set} msg
    set msg
} "command \"set\" already exists, unable to create graph"

test graph-0.5 {graph errors} {
    graph mygraph
    catch {mygraph arc foo} msg
    mygraph destroy
    set msg
} "bad option \"foo\": must be append, delete, exists, get, getall, insert, keys, keyexists, lappend, set, source, target, or unset"

test graph-0.6 {graph errors} {
    graph mygraph
    catch {mygraph node foo} msg
    mygraph destroy
    set msg
} "bad option \"foo\": must be append, degree, delete, exists, get, getall, insert, keys, keyexists, lappend, opposite, set, or unset"

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

test graph-1.1 {create} {
    graph mygraph
    set result [string equal [info commands ::mygraph] "::mygraph"]
    mygraph destroy
    set result
} 1

test graph-1.2 {create} {
    set name [graph]
    set result [list $name [string equal [info commands ::$name] "::$name"]]
    $name destroy
    set result
} [list graph1 1]

test graph-1.3 {destroy} {
    graph mygraph
    mygraph destroy
    string equal [info commands ::mygraph] ""
} 1

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

test graph-2.1 {arc delete} {
    graph mygraph
    catch {mygraph arc delete arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-2.2 {arc delete} {
    graph mygraph

    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    mygraph arc  delete arc0

    set result [mygraph arc exists arc0]
    mygraph destroy
    set result
} {0}

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

test graph-3.1 {arc exists} {
    graph mygraph
    set     result [list]
    lappend result [mygraph arc exists arc1]
    mygraph node insert node1
    mygraph node insert node2
    mygraph arc  insert node1 node2 arc1
    lappend result [mygraph arc exists arc1]
    mygraph arc  delete arc1
    lappend result [mygraph arc exists arc1]
    mygraph destroy
    set     result
} {0 1 0}

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

test graph-4.1 {arc get gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc get arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-4.2 {arc get gives error on bogus key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc get arc0 -key bogus} msg
    mygraph destroy
    set msg
} "invalid key \"bogus\" for arc \"arc0\""

test graph-4.3 {arc get uses data as default key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 foobar
    set result [mygraph arc get arc0]
    mygraph destroy
    set result
} "foobar"

test graph-4.4 {arc get respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key boom foobar
    set result [mygraph arc get arc0 -key boom]
    mygraph destroy
    set result
} "foobar"

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

test graph-5.1 {arc insert gives error on duplicate arc name} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc insert node0 node1 arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" already exists in graph \"mygraph\""

test graph-5.2 {arc insert creates and initializes arc} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    set result [list ]
    lappend result [mygraph arc exists arc0]
    lappend result [mygraph arc source arc0]
    lappend result [mygraph arc target arc0]
    lappend result [mygraph arc set arc0]
    mygraph destroy
    set result
} {1 node0 node1 {}}

test graph-5.3 {arc insert arcs in correct location} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1

    mygraph arc insert node0 node1 arc0
    mygraph arc insert node0 node1 arc1
    mygraph arc insert node0 node1 arc2
    set result [lsort [mygraph arcs -out node0]]
    mygraph destroy
    set result
} {arc0 arc1 arc2}

test graph-5.4 {arc insert gives error when trying to insert to a fake node} {
    graph mygraph
    catch {mygraph arc insert node0 node1 arc0} msg
    mygraph destroy
    set msg
} "source node \"node0\" does not exist in graph \"mygraph\""

test graph-5.5 {arc insert gives error when trying to insert to a fake node} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph arc insert node0 node1 arc0} msg
    mygraph destroy
    set msg
} "target node \"node1\" does not exist in graph \"mygraph\""

test graph-5.6 {arc insert generates arc name when none is given} {
    graph mygraph
    mygraph node insert n0

    set     result [list [mygraph arc insert n0 n0]]
    lappend result       [mygraph arc insert n0 n0]
    mygraph                       arc insert n0 n0 arc3
    lappend result       [mygraph arc insert n0 n0]
    mygraph destroy
    set result
} [list arc1 arc2 arc4]

if {0} {
    # if feature used, fix this test...
    test graph-5.6 {arc insert generates arc name when none is given} {
	graph mygraph
	set result [list [mygraph insert root end]]
	lappend result [mygraph insert root end]
	mygraph insert root end arc3
	lappend result [mygraph insert root end]
	mygraph destroy
	set result
    } [list arc1 arc2 arc4] ; # {}
}

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

test graph-6.1 {arc set gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc set arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-6.2 {arc set with arc name gets/sets "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 foobar
    set result [mygraph arc set arc0]
    mygraph destroy
    set result
} "foobar"

test graph-6.3 {arc set with arc name and key gets/sets key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key baz foobar
    set result [list [mygraph arc set arc0] [mygraph arc set arc0 -key baz]]
    mygraph destroy
    set result
} [list "" "foobar"]

test graph-6.4 {arc set with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc set arc0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph arc set arc0 ?-key key? ?value?\""

test graph-6.5 {arc set with bad args} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc set arc0 foo bar} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-6.6 {arc set with bad args} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc set arc0 foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-6.7 {arc set with bad key gives error} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc set arc0 -key foo} msg
    mygraph destroy
    set msg
} "invalid key \"foo\" for arc \"arc0\""

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

test graph-7.1 {arc source gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc source arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-7.2 {arc source} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    set result [mygraph arc source arc0]
    mygraph destroy
    set result
} node0

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

test graph-8.1 {arc target gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc target arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-8.2 {arc target} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    set result [mygraph arc target arc0]
    mygraph destroy
    set result
} node1

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

test graph-9.1 {arc unset gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc unset arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-9.2 {arc unset does not give error on bogus key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    set result [catch {mygraph arc unset arc0 -key bogus}]
    mygraph destroy
    set result
} 0

test graph-9.3 {arc unset removes a keyed value from a arc} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    mygraph arc        set arc0 -key foobar foobar
    mygraph arc      unset arc0 -key foobar
    catch {mygraph arc get arc0 -key foobar} msg
    mygraph destroy
    set msg
} "invalid key \"foobar\" for arc \"arc0\""

test graph-9.4 {arc unset requires -key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    mygraph arc        set arc0 -key foobar foobar
    catch {mygraph arc unset arc0 flaboozle foobar} msg
    mygraph destroy
    set msg
} "invalid option \"flaboozle\": should be \"mygraph arc unset arc0 ?-key key?\""

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

test graph-10.1 {arcs} {
    graph mygraph
    set result [mygraph arcs]
    mygraph destroy
    set result
} {}

test graph-10.2 {arcs} {
    graph mygraph
    catch {mygraph arcs -foo} msg
    mygraph destroy
    set msg
} {invalid restriction "-foo": should be -in, -out, -adj, -inner, -embedding, -key or -value}

test graph-10.3 {arcs} {
    graph mygraph
    catch {mygraph arcs -in} msg
    mygraph destroy
    set msg
} {no nodes specified: should be "mygraph arcs ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding node node...?"}

test graph-10.4 {arcs} {
    graph mygraph
    catch {mygraph arcs -in node0} msg
    mygraph destroy
    set msg
} {node "node0" does not exist in graph "mygraph"}

test graph-10.5 {arcs} {
    graph mygraph
    mygraph node insert node1
    mygraph node insert node2
    mygraph node insert node3
    mygraph node insert node4
    mygraph node insert node5
    mygraph node insert node6

    mygraph arc insert node4 node1 arcA
    mygraph arc insert node5 node2 arcB
    mygraph arc insert node6 node3 arcC
    mygraph arc insert node3 node1 arcD
    mygraph arc insert node1 node2 arcE
    mygraph arc insert node2 node3 arcF

    set result [list \
	    [lsort [mygraph arcs            ]] \
	    \
	    [lsort [mygraph arcs -in        node1 node2 node3]] \
	    [lsort [mygraph arcs -out       node1 node2 node3]] \
	    [lsort [mygraph arcs -adj       node1 node2 node3]] \
	    [lsort [mygraph arcs -inner     node1 node2 node3]] \
	    [lsort [mygraph arcs -embedding node1 node2 node3]] \
	    \
	    [lsort [mygraph arcs -in        node4 node5 node6]] \
	    [lsort [mygraph arcs -out       node4 node5 node6]] \
	    [lsort [mygraph arcs -adj       node4 node5 node6]] \
	    [lsort [mygraph arcs -inner     node4 node5 node6]] \
	    [lsort [mygraph arcs -embedding node4 node5 node6]] \
    ]
    mygraph destroy
    set result
} [list \
	{arcA arcB arcC arcD arcE arcF}	\
	\
	{arcA arcB arcC arcD arcE arcF}	\
	{arcD arcE arcF}		\
	{arcA arcB arcC arcD arcE arcF}	\
	{arcD arcE arcF}		\
	{arcA arcB arcC}		\
	\
	{}			\
	{arcA arcB arcC}	\
	{arcA arcB arcC}	\
	{}			\
	{arcA arcB arcC}	\
	]

test graph-10.6 {arcs} {
    graph mygraph
    mygraph node insert node1
    mygraph node insert node2
    mygraph arc insert node1 node2 arcE
    mygraph arc insert node2 node1 arcF
    set result [lsort [mygraph arcs -adj node1 node2]]
    mygraph destroy
    set result
} {arcE arcF}

test graph-10.7 {arcs} {
    graph mygraph
    mygraph node insert n0
    mygraph node insert n1
    mygraph arc insert n0 n1 a1
    mygraph arc insert n0 n1 a2
    mygraph arc set a1 -key foobar 1
    mygraph arc set a2 -key blubber 2
    catch {mygraph arcs -key foobar} msg
    mygraph destroy
    set msg
} {a1}

test graph-10.8 {arcs} {
    graph mygraph
    mygraph node insert n0
    mygraph node insert n1
    mygraph arc insert n0 n1 a1
    mygraph arc insert n0 n1 a2
    mygraph arc set a1 -key foobar 1
    mygraph arc set a2 -key foobar 2
    catch {mygraph arcs -key foobar -value 1} msg
    mygraph destroy
    set msg
} {a1}

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

test graph-11.1 {node degree} {
    graph mygraph
    catch {mygraph node degree} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph node degree ?-in|-out? node\""

test graph-11.2 {node degree} {
    graph mygraph
    catch {mygraph node degree foo bar baz} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph node degree ?-in|-out? node\""

test graph-11.3 {node degree} {
    graph mygraph
    catch {mygraph node degree node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-11.4 {node degree} {
    graph mygraph
    catch {mygraph node degree -foo node0} msg
    mygraph destroy
    set msg
} "invalid option \"-foo\": should be -in or -out"

test graph-11.5 {node degree} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph node insert node2
    mygraph node insert node3
    mygraph node insert node4
    mygraph node insert node5

    mygraph arc insert node1 node2 arc0
    mygraph arc insert node3 node3 arc1
    mygraph arc insert node4 node5 arc2
    mygraph arc insert node4 node5 arc3
    mygraph arc insert node4 node5 arc4
    mygraph arc insert node5 node2 arc5

    set result [list	\
	    [mygraph node degree      node0]	\
	    [mygraph node degree -in  node0]	\
	    [mygraph node degree -out node0]	\
	    [mygraph node degree      node1]	\
	    [mygraph node degree -in  node1]	\
	    [mygraph node degree -out node1]	\
	    [mygraph node degree      node2]	\
	    [mygraph node degree -in  node2]	\
	    [mygraph node degree -out node2]	\
	    [mygraph node degree      node3]	\
	    [mygraph node degree -in  node3]	\
	    [mygraph node degree -out node3]	\
	    [mygraph node degree      node4]	\
	    [mygraph node degree -in  node4]	\
	    [mygraph node degree -out node4]	\
	    [mygraph node degree      node5]	\
	    [mygraph node degree -in  node5]	\
	    [mygraph node degree -out node5]	\
	    ]

    mygraph destroy
    set result
} [list	0 0 0 \
	1 0 1 \
	2 2 0 \
	2 1 1 \
	3 0 3 \
	4 3 1
	]

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

test graph-12.1 {node delete} {
    graph mygraph
    catch {mygraph node delete node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-12.2 {node delete} {
    graph mygraph
    mygraph node insert node0
    mygraph node delete node0
    set result [mygraph node exists node0]
    mygraph destroy
    set result
} {0}

test graph-12.3 {node delete} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    mygraph node delete node0

    set result [list \
	    [mygraph node exists node0] \
	    [mygraph node exists node1] \
	    [mygraph arc exists arc0]	\
	    ]
    mygraph destroy
    set result
} {0 1 0}

test graph-12.4 {node delete} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0
    mygraph node delete node1

    set result [list \
	    [mygraph node exists node0] \
	    [mygraph node exists node1] \
	    [mygraph arc exists arc0]	\
	    ]
    mygraph destroy
    set result
} {1 0 0}

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

test graph-13.1 {node exists} {
    graph mygraph
    set     result [list]
    lappend result [mygraph node exists node1]
    mygraph node insert node1
    lappend result [mygraph node exists node1]
    mygraph node delete node1
    lappend result [mygraph node exists node1]
    mygraph destroy
    set     result
} {0 1 0}

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

test graph-14.1 {node get gives error on bogus node} {
    graph mygraph
    catch {mygraph node get node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-14.2 {node get gives error on bogus key} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node get node0 -key bogus} msg
    mygraph destroy
    set msg
} "invalid key \"bogus\" for node \"node0\""

test graph-14.3 {node get uses data as default key} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 foobar
    set result [mygraph node get node0]
    mygraph destroy
    set result
} "foobar"

test graph-14.4 {node get respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key boom foobar
    set result [mygraph node get node0 -key boom]
    mygraph destroy
    set result
} "foobar"

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

test graph-15.1 {node insert gives error on duplicate node name} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node insert node0} msg
    mygraph destroy
    set msg
} "node \"node0\" already exists in graph \"mygraph\""

test graph-15.2 {node insert creates and initializes node} {
    graph mygraph
    mygraph node insert node0
    set result [list ]
    lappend result [mygraph node exists node0]
    lappend result [mygraph node set    node0]
    mygraph destroy
    set result
} {1 {}}

test graph-15.3 {node insert generates node name when none is given} {
    graph mygraph
    set result [list [mygraph node insert]]

    lappend result [mygraph node insert]
    mygraph node insert node3
    lappend result [mygraph node insert]
    mygraph destroy
    set result
} [list node1 node2 node4]

if {0} {
    # fix if this feature is used ...
    test graph-15.x {node insert generates node name when none is given} {
	graph mygraph
	set result [list [mygraph node insert root end]]
	lappend result [mygraph node insert root end]
	mygraph node insert root end node3
	lappend result [mygraph node insert root end]
	mygraph destroy
	set result
    } [list node1 node2 node4] ; # {}
}

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

test graph-16.1 {node opposite gives error on bogus node} {
    graph mygraph
    catch {mygraph node opposite node0 arc0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-16.2 {node opposite gives error on bogus arc} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node opposite node0 arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""

test graph-16.3 {node opposite gives error on bogus node/arc combination} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph node insert node2
    mygraph arc  insert node1 node2 arc0

    catch {mygraph node opposite node0 arc0} msg
    mygraph destroy
    set msg
} "node \"node0\" and arc \"arc0\" are not connected in graph \"mygraph\""

test graph-16.4 {node opposite} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc  insert node0 node1 arc0

    set result [list	\
	    [mygraph node opposite node0 arc0]	\
	    [mygraph node opposite node1 arc0]	\
	    ]
    mygraph destroy
    set result
} {node1 node0}

test graph-16.5 {node opposite} {
    graph mygraph
    mygraph node insert node0
    mygraph arc  insert node0 node0 arc0
    set result [mygraph node opposite node0 arc0]
    mygraph destroy
    set result
} {node0}

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

test graph-17.1 {node set gives error on bogus node} {
    graph mygraph
    catch {mygraph node set node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-17.2 {node set with node name gets/sets "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 foobar
    set result [mygraph node set node0]
    mygraph destroy
    set result
} "foobar"

test graph-17.3 {node set with node name and key gets/sets key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key baz foobar
    set result [list [mygraph node set node0] [mygraph node set node0 -key baz]]
    mygraph destroy
    set result
} [list "" "foobar"]

test graph-17.4 {node set with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node set node0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph node set node0 ?-key key? ?value?\""

test graph-17.5 {node set with bad args} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node set node0 foo bar} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-17.6 {node set with bad args} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node set node0 foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-17.7 {node set with bad key gives error} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node set node0 -key foo} msg
    mygraph destroy
    set msg
} "invalid key \"foo\" for node \"node0\""

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

test graph-18.1 {node unset gives error on bogus node} {
    graph mygraph
    catch {mygraph node unset node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-18.2 {node unset does not give error on bogus key} {
    graph mygraph
    mygraph node insert node0
    set result [catch {mygraph node unset node0 -key bogus}]
    mygraph destroy
    set result
} 0

test graph-18.3 {node unset removes a keyed value from a node} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key foobar foobar
    mygraph node unset node0 -key foobar
    catch {mygraph node get node0 -key foobar} msg
    mygraph destroy
    set msg
} "invalid key \"foobar\" for node \"node0\""

test graph-18.4 {unset requires -key} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key foobar foobar
    catch {mygraph node unset node0 flaboozle foobar} msg
    mygraph destroy
    set msg
} "invalid option \"flaboozle\": should be \"mygraph node unset node0 ?-key key?\""

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

test graph-19.1 {nodes} {
    graph mygraph
    set result [mygraph nodes]
    mygraph destroy
    set result
} {}

test graph-19.2 {nodes} {
    graph mygraph
    catch {mygraph nodes -foo} msg
    mygraph destroy
    set msg
} {invalid restriction "-foo": should be -in, -out, -adj, -inner, -embedding, -key or -value}

test graph-19.3 {nodes} {
    graph mygraph
    catch {mygraph nodes -in} msg
    mygraph destroy
    set msg
} {no nodes specified: should be "mygraph nodes ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding node node...?"}

test graph-19.4 {nodes} {
    graph mygraph
    catch {mygraph nodes -in node0} msg
    mygraph destroy
    set msg
} {node "node0" does not exist in graph "mygraph"}

test graph-19.5 {nodes} {
    graph mygraph
    mygraph node insert node1
    mygraph node insert node2
    mygraph node insert node3
    mygraph node insert node4
    mygraph node insert node5
    mygraph node insert node6

    mygraph arc insert node4 node1 arcA
    mygraph arc insert node5 node2 arcB
    mygraph arc insert node6 node3 arcC
    mygraph arc insert node3 node1 arcD
    mygraph arc insert node1 node2 arcE
    mygraph arc insert node2 node3 arcF

    set result [list \
	    [lsort [mygraph nodes            ]] \
	    \
	    [lsort [mygraph nodes -in        node1 node2 node3]] \
	    [lsort [mygraph nodes -out       node1 node2 node3]] \
	    [lsort [mygraph nodes -adj       node1 node2 node3]] \
	    [lsort [mygraph nodes -inner     node1 node2 node3]] \
	    [lsort [mygraph nodes -embedding node1 node2 node3]] \
	    \
	    [lsort [mygraph nodes -in        node4 node5 node6]] \
	    [lsort [mygraph nodes -out       node4 node5 node6]] \
	    [lsort [mygraph nodes -adj       node4 node5 node6]] \
	    [lsort [mygraph nodes -inner     node4 node5 node6]] \
	    [lsort [mygraph nodes -embedding node4 node5 node6]] \
    ]
    mygraph destroy
    set result
} [list \
	{node1 node2 node3 node4 node5 node6} \
	\
	{node1 node2 node3 node4 node5 node6} \
	{node1 node2 node3} \
	{node1 node2 node3 node4 node5 node6} \
	{node1 node2 node3} \
	{node4 node5 node6} \
	\
	{} \
	{node1 node2 node3} \
	{node1 node2 node3} \
	{} \
	{node1 node2 node3} \
	]

test graph-19.6 {nodes} {
    graph mygraph
    mygraph node insert node1
    mygraph node insert node2
    mygraph node insert node3

    mygraph arc insert node1 node2 arcE
    mygraph arc insert node1 node2 arcD
    mygraph arc insert node2 node3 arcF
    mygraph arc insert node2 node3 arcG

    set result [lsort [mygraph nodes -embedding node1 node3]]
    mygraph destroy
    set result
} {node2}


test graph-19.7 {nodes} {
    graph mygraph
    mygraph node insert n0
    mygraph node insert n1
    mygraph node set n0 -key foobar 1
    mygraph node set n1 -key blubber 2
    catch {mygraph nodes -key foobar} msg
    mygraph destroy
    set msg
} {n0}

test graph-19.8 {nodes} {
    graph mygraph
    mygraph node insert n0
    mygraph node insert n1
    mygraph node set n0 -key foobar 1
    mygraph node set n1 -key foobar 2
    catch {mygraph nodes -key foobar -value 1} msg
    mygraph destroy
    set msg
} {n0}


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

test graph-20.1 {swap gives error when trying to swap non existant node} {
    graph mygraph
    catch {mygraph swap node0 node1} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-20.2 {swap gives error when trying to swap non existant node} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph swap node0 node1} msg
    mygraph destroy
    set msg
} "node \"node1\" does not exist in graph \"mygraph\""

test graph-20.3 {swap gives error when trying to swap node with self} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph swap node0 node0} msg
    mygraph destroy
    set msg
} "cannot swap node \"node0\" with itself"

test graph-20.4 {swap swaps node relationships correctly} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node0.1
    mygraph node insert node0.2
    mygraph node insert node0.1.1
    mygraph node insert node0.1.2

    mygraph arc insert node0 node0.1     a1
    mygraph arc insert node0 node0.2     a2
    mygraph arc insert node0.1 node0.1.1 a3
    mygraph arc insert node0.1 node0.1.2 a4

    mygraph swap node0 node0.1

    set result [list \
	    [lsort [mygraph nodes -out node0]]   \
	    [lsort [mygraph nodes -out node0.1]] \
	    ]
    mygraph destroy
    set result
} {{node0.1.1 node0.1.2} {node0 node0.2}}

test graph-20.5 {swap swaps node relationships correctly} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node0.1
    mygraph node insert node0.2
    mygraph node insert node0.1.1
    mygraph node insert node0.1.2

    mygraph arc insert node0   node0.1   a1
    mygraph arc insert node0   node0.2   a2
    mygraph arc insert node0.1 node0.1.1 a3
    mygraph arc insert node0.1 node0.1.2 a4

    mygraph swap node0 node0.1.1

    set result [list \
	    [lsort [mygraph nodes -out node0]]   \
	    [lsort [mygraph nodes -out node0.1.1]] \
	    ]
    mygraph destroy
    set result
} {{} {node0.1 node0.2}}

test graph-20.6 {swap swaps node relationships correctly} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node0.1
    mygraph node insert node0.2
    mygraph node insert node0.1.1
    mygraph node insert node0.1.2

    mygraph arc insert node0 node0.1     a1
    mygraph arc insert node0 node0.2     a2
    mygraph arc insert node0.1 node0.1.1 a3
    mygraph arc insert node0.1 node0.1.2 a4

    mygraph swap node0.1 node0

    set result [list \
	    [lsort [mygraph nodes -out node0]]   \
	    [lsort [mygraph nodes -out node0.1]] \
	    ]
    mygraph destroy
    set result
} {{node0.1.1 node0.1.2} {node0 node0.2}}

test graph-22.1 {arc getall gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc getall arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""
test graph-22.2 {arc getall gives error when key specified} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc getall arc0 -key data} msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-22.3 {arc getall with node name returns list of key/value pairs} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 foobar
    mygraph arc set arc0 -key other thing
    set results [mygraph arc getall arc0]
    mygraph destroy
    lsort $results
} "data foobar other thing"   

test graph-23.1 {node getall gives error on bogus node} {
    graph mygraph
    catch {mygraph node getall node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""
test graph-23.2 {node getall gives error when key specified} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node getall node0 -key data} msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-23.3 {node getall with node name returns list of key/value pairs} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 foobar
    mygraph node set node0 -key other thing
    set results [mygraph node getall node0]
    mygraph destroy
    lsort $results
} "data foobar other thing"   

test graph-24.1 {arc keys gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc keys arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""
test graph-24.2 {arc keys gives error when key specified} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch { mygraph arc keys arc0 -key bogus } msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-24.3 {arc keys with arc name returns list of keys} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key other things
    set results [mygraph arc keys arc0]
    mygraph destroy
    lsort $results
} "data other"
  
test graph-25.1 {node keys gives error on bogus node} {
    graph mygraph
    catch {mygraph node keys node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""
test graph-25.2 {node keys gives error when key specified} {
    graph mygraph
    mygraph node insert node0
    catch { mygraph node keys node0 -key bogus } msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-25.3 {node keys with node name returns list of keys} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key other things
    set results [mygraph node keys node0]
    mygraph destroy
    lsort $results
} "data other"

test graph-26.1 {arc keyexists gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc keyexists arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""
test graph-26.2 {arc keyexists returns false on non-existant key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    set result [mygraph arc keyexists arc0 -key bogus]
    mygraph destroy
    set result
} "0"
test graph-26.3 {arc keyexists uses data as default key} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    set result [mygraph arc keyexists arc0]
    mygraph destroy
    set result
} "1"
test graph-26.4 {arc keyexists respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key boom foobar
    set result [mygraph arc keyexists arc0 -key boom]
    mygraph destroy
    set result
} "1"

test graph-27.1 {node keyexists gives error on bogus node} {
    graph mygraph
    catch {mygraph node keyexists node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""
test graph-27.2 {node keyexists returns false on non-existant key} {
    graph mygraph
    mygraph node insert node0
    set result [mygraph node keyexists node0 -key bogus]
    mygraph destroy
    set result
} "0"
test graph-27.3 {node keyexists uses data as default key} {
    graph mygraph
    mygraph node insert node0
    set result [mygraph node keyexists node0]
    mygraph destroy
    set result
} "1"
test graph-27.4 {node keyexists respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key boom foobar
    set result [mygraph node keyexists node0 -key boom]
    mygraph destroy
    set result
} "1"

test graph-28.1 {arc append gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc append arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""
test graph-28.2 {arc append with arc name appends to "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 foo
    set result [mygraph arc append arc0 bar]
    mygraph destroy
    set result
} "foobar"
test graph-28.3 {arc append with arc name and key appends key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key baz foo
    set result [mygraph arc append arc0 -key baz bar]
    mygraph destroy
    set result
} "foobar"
test graph-28.4 {arc append with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc append arc0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph arc append arc0 ?-key key? value\""
test graph-28.5 {arc append with bad args} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc append arc0 -foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"-foo\": should be -key"
test graph-28.6 {arc append respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key baz foo
    set result [mygraph arc append arc0 -key baz bar]
    mygraph destroy
    set result
} "foobar"

test graph-29.1 {arc lappend gives error on bogus arc} {
    graph mygraph
    catch {mygraph arc lappend arc0} msg
    mygraph destroy
    set msg
} "arc \"arc0\" does not exist in graph \"mygraph\""
test graph-29.2 {arc lappend with node arc lappends to "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 foo
    set result [mygraph arc lappend arc0 bar]
    mygraph destroy
    set result
} "foo bar"
test graph-29.3 {arc lappend with arc name and key lappends key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key baz foo
    set result [mygraph arc lappend arc0 -key baz bar]
    mygraph destroy
    set result
} "foo bar"
test graph-29.4 {arc lappend with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc lappend arc0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph arc lappend arc0 ?-key key? value\""
test graph-29.5 {arc lappend with bad args} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    catch {mygraph arc lappend arc0 -foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"-foo\": should be -key"
test graph-29.6 {arc lappend respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node insert node1
    mygraph arc insert node0 node1 arc0
    mygraph arc set arc0 -key baz foo
    set result [mygraph arc lappend arc0 -key baz bar]
    mygraph destroy
    set result
} "foo bar"

test graph-30.1 {node append gives error on bogus node} {
    graph mygraph
    catch {mygraph node append node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""
test graph-30.2 {node append with node name appends to "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 foo
    set result [mygraph node append node0 bar]
    mygraph destroy
    set result
} "foobar"
test graph-30.3 {node append with node name and key appends key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key baz foo
    set result [mygraph node append node0 -key baz bar]
    mygraph destroy
    set result
} "foobar"
test graph-30.4 {node append with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node append node0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph node append node0 ?-key key? value\""
test graph-30.5 {node append with bad args} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node append node0 -foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"-foo\": should be -key"
test graph-30.6 {node append respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key baz foo
    set result [mygraph node append node0 -key baz bar]
    mygraph destroy
    set result
} "foobar"

test graph-31.1 {node lappend gives error on bogus node} {
    graph mygraph
    catch {mygraph node lappend node0} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""
test graph-32.2 {node lappend with node name lappends to "data" value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 foo
    set result [mygraph node lappend node0 bar]
    mygraph destroy
    set result
} "foo bar"
test graph-32.3 {node lappend with node name and key lappends key value} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key baz foo
    set result [mygraph node lappend node0 -key baz bar]
    mygraph destroy
    set result
} "foo bar"
test graph-32.4 {node lappend with too many args gives error} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node lappend node0 foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph node lappend node0 ?-key key? value\""
test graph-32.5 {node lappend with bad args} {
    graph mygraph
    mygraph node insert node0
    catch {mygraph node lappend node0 -foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"-foo\": should be -key"
test graph-32.6 {node lappend respects -key flag} {
    graph mygraph
    mygraph node insert node0
    mygraph node set node0 -key baz foo
    set result [mygraph node lappend node0 -key baz bar]
    mygraph destroy
    set result
} "foo bar"


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

proc makegraph {} {
    graph mygraph

    mygraph node insert i
    mygraph node insert ii
    mygraph node insert iii
    mygraph node insert iv
    mygraph node insert v
    mygraph node insert vi
    mygraph node insert vii
    mygraph node insert viii
    mygraph node insert ix

    mygraph arc insert   i    ii  1
    mygraph arc insert   ii  iii  2
    mygraph arc insert   ii  iii  3
    mygraph arc insert   ii  iii  4
    mygraph arc insert  iii   iv  5
    mygraph arc insert  iii   iv  6
    mygraph arc insert   iv    v  7
    mygraph arc insert    v   vi  8
    mygraph arc insert   vi viii  9
    mygraph arc insert viii    i 10
    mygraph arc insert    i   ix 11
    mygraph arc insert   ix   ix 12
    mygraph arc insert    i  vii 13
    mygraph arc insert  vii   vi 14
}


test graph-21.1 {walk with too few args} {badTest} {
    graph mygraph
    catch {mygraph walk} msg
    mygraph destroy
    set msg
} "no value given for parameter \"node\" to \"::struct::graph::_walk\""

test graph-21.2 {walk with too few args} {
    graph mygraph
    catch {mygraph walk node0} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph walk node0 ?-dir forward|backward? ?-order pre|post|both? ?-type {bfs|dfs}? -command cmd\""

test graph-21.3 {walk with too many args} {
    graph mygraph
    catch {mygraph walk node0 -foo bar -baz boo -foo2 boo -foo3 baz -foo4 baz} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph walk node0 ?-dir forward|backward? ?-order pre|post|both? ?-type {bfs|dfs}? -command cmd\""

test graph-21.4 {walk with fake node} {
    graph mygraph
    catch {mygraph walk node0 -command {}} msg
    mygraph destroy
    set msg
} "node \"node0\" does not exist in graph \"mygraph\""

test graph-21.5 {walk using unknown option} {
    makegraph
    catch {mygraph walk i -foo x -command {}} msg
    mygraph destroy
    set msg
} "unknown option \"-foo\": should be \"mygraph walk i ?-dir forward|backward? ?-order pre|post|both? ?-type {bfs|dfs}? -command cmd\""

test graph-21.6 {walk with empty command} {
    makegraph
    catch {mygraph walk i -command {}} msg
    mygraph destroy
    set msg
} "no command specified: should be \"mygraph walk i ?-dir forward|backward? ?-order pre|post|both? ?-type {bfs|dfs}? -command cmd\""

test graph-21.7 {walk with illegal specifications} {
    makegraph
    catch {mygraph walk i -command foo -type foo} msg
    mygraph destroy
    set msg
} "invalid search type \"foo\": should be dfs, or bfs"

test graph-21.8 {walk with illegal specifications} {
    makegraph
    catch {mygraph walk i -command foo -type dfs -dir oneway} msg
    mygraph destroy
    set msg
} "invalid search direction \"oneway\": should be forward or backward"

test graph-21.9 {walk with illegal specifications} {
    makegraph
    catch {mygraph walk i -command foo -type dfs -dir forward -order none} msg
    mygraph destroy
    set msg
} "invalid search order \"none\": should be both, pre or post"


test graph-21.10 {forward pre-order dfs is default walk} {
    makegraph
    set t [list ]
    mygraph walk i -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph    i enter mygraph ii enter mygraph iii	\
	enter mygraph   iv enter mygraph  v enter mygraph  vi	\
	enter mygraph viii enter mygraph ix enter mygraph vii	\
	]

test graph-21.11 {forward post-order dfs walk} {
    makegraph
    set t [list ]
    mygraph walk i -order post -command {lappend t}
    mygraph destroy
    set t
} [list \
	leave mygraph viii leave mygraph  vi leave mygraph  v	\
	leave mygraph   iv leave mygraph iii leave mygraph ii	\
	leave mygraph   ix leave mygraph vii leave mygraph  i	\
	]

test graph-21.12 {forward both-order dfs walk} {
    makegraph
    set t [list ]
    mygraph walk i -order both -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph    i enter mygraph   ii enter mygraph iii	\
	enter mygraph   iv enter mygraph    v enter mygraph  vi	\
	enter mygraph viii leave mygraph viii leave mygraph  vi \
	leave mygraph    v leave mygraph   iv leave mygraph iii	\
	leave mygraph   ii enter mygraph   ix leave mygraph  ix	\
	enter mygraph  vii leave mygraph  vii leave mygraph   i	\
	]

test graph-21.13 {forward pre-order bfs walk} {
    makegraph
    set t [list ]
    mygraph walk i -type bfs -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph   i enter mygraph   ii enter mygraph ix	\
	enter mygraph vii enter mygraph  iii enter mygraph vi	\
	enter mygraph  iv enter mygraph viii enter mygraph  v	\
	]

test graph-21.14 {backward pre-order bfs walk} {
    makegraph
    set t [list ]
    mygraph walk ix -type bfs -dir backward -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph ix enter mygraph   i enter mygraph viii	\
	enter mygraph vi enter mygraph   v enter mygraph  vii	\
	enter mygraph iv enter mygraph iii enter mygraph   ii	\
	]

test graph-21.15 {backward pre-order dfs walk} {
    makegraph
    set t [list ]
    mygraph walk ix -dir backward -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph  ix enter mygraph  i enter mygraph viii	\
	enter mygraph  vi enter mygraph  v enter mygraph   iv	\
	enter mygraph iii enter mygraph ii enter mygraph  vii	\
	]

test graph-21.16 {backward post-order dfs walk} {
    makegraph
    set t [list ]
    mygraph walk ix -dir backward -order post -command {lappend t}
    mygraph destroy
    set t
} [list \
	leave mygraph   ii leave mygraph iii leave mygraph   iv	\
	leave mygraph    v leave mygraph vii leave mygraph   vi	\
	leave mygraph viii leave mygraph   i leave mygraph   ix	\
	]

test graph-21.17 {backward both-order dfs walk} {
    makegraph
    set t [list ]
    mygraph walk ix -dir backward -order both -command {lappend t}
    mygraph destroy
    set t
} [list \
	enter mygraph   ix enter mygraph   i enter mygraph viii	\
	enter mygraph   vi enter mygraph   v enter mygraph   iv	\
	enter mygraph  iii enter mygraph  ii leave mygraph   ii	\
	leave mygraph  iii leave mygraph  iv leave mygraph    v	\
	enter mygraph  vii leave mygraph vii leave mygraph   vi	\
	leave mygraph viii leave mygraph   i leave mygraph   ix	\
	]

test graph-21.18 {walk, option without value} {
    makegraph
    catch {mygraph walk ix -type dfs -order} msg
    mygraph destroy
    set msg
} "value for \"-order\" missing: should be \"mygraph walk ix ?-dir forward|backward? ?-order pre|post|both? ?-type {bfs|dfs}? -command cmd\""

test graph-21.19 {forward post-order bfs walk not implemented} {
    makegraph
    catch {mygraph walk i -order post -type bfs -command {lappend t}} msg
    mygraph destroy
    set msg
} {unable to do a post-order breadth first walk}

test graph-21.20 {forward both-order bfs walk not implemented} {
    makegraph
    catch {mygraph walk i -order both -type bfs -command {lappend t}} msg
    mygraph destroy
    set msg
} {unable to do a both-order breadth first walk}

test graph-21.21 {backward post-order bfs walk not implemented} {
    makegraph
    catch {mygraph walk i -dir backward -order post -type bfs -command {lappend t}} msg
    mygraph destroy
    set msg
} {unable to do a post-order breadth first walk}

test graph-21.22 {backward both-order bfs walk not implemented} {
    makegraph
    catch {mygraph walk i -dir backward -order both -type bfs -command {lappend t}} msg
    mygraph destroy
    set msg
} {unable to do a both-order breadth first walk}


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

test graph-33.1 {get gives error on bogus key} {
    graph mygraph
    catch {mygraph get -key bogus} msg
    mygraph destroy
    set msg
} "invalid key \"bogus\" for graph \"mygraph\""

test graph-33.2 {get uses data as default key} {
    graph mygraph
    mygraph set foobar
    set result [mygraph get]
    mygraph destroy
    set result
} "foobar"

test graph-33.3 {get respects -key flag} {
    graph mygraph
    mygraph set -key boom foobar
    set result [mygraph get -key boom]
    mygraph destroy
    set result
} "foobar"

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

test graph-34.1 {set alone gets/sets "data" value} {
    graph mygraph
    mygraph set foobar
    set result [mygraph set]
    mygraph destroy
    set result
} "foobar"

test graph-34.2 {set with key gets/sets key value} {
    graph mygraph
    mygraph set -key baz foobar
    set result [list [mygraph set] [mygraph set -key baz]]
    mygraph destroy
    set result
} [list "" "foobar"]

test graph-34.3 {set with too many args gives error} {
    graph mygraph
    catch {mygraph set foo bar baz boo} msg
    mygraph destroy
    set msg
} "wrong # args: should be \"mygraph set ?-key key? ?value?\""

test graph-34.4 {set with bad args} {
    graph mygraph
    catch {mygraph set foo bar} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-34.5 {set with bad args} {
    graph mygraph
    catch {mygraph set foo bar baz} msg
    mygraph destroy
    set msg
} "invalid option \"foo\": should be key"

test graph-34.6 {set with bad key gives error} {
    graph mygraph
    catch {mygraph set -key foo} msg
    mygraph destroy
    set msg
} "invalid key \"foo\" for graph \"mygraph\""

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

test graph-35.1 {unset does not give error on bogus key} {
    graph mygraph
    set result [catch {mygraph unset -key bogus}]
    mygraph destroy
    set result
} 0

test graph-35.2 {unset removes a keyed value} {
    graph mygraph
    mygraph set -key foobar foobar
    mygraph unset -key foobar
    catch {mygraph get -key foobar} msg
    mygraph destroy
    set msg
} "invalid key \"foobar\" for graph \"mygraph\""

test graph-35.3 {unset requires -key} {
    graph mygraph
    mygraph set -key foobar foobar
    catch {mygraph unset flaboozle foobar} msg
    mygraph destroy
    set msg
} "invalid option \"flaboozle\": should be \"mygraph unset ?-key key?\""

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

test graph-36.1 {getall gives error when key specified} {
    graph mygraph
    catch {mygraph getall -key data} msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-36.2 {getall returns list of key/value pairs} {
    graph mygraph
    mygraph set foobar
    mygraph set -key other thing
    set results [mygraph getall]
    mygraph destroy
    lsort $results
} "data foobar other thing"

test graph-37.1 {keys gives error when key specified} {
    graph mygraph
    catch { mygraph keys -key bogus } msg
    mygraph destroy
    set msg
} "wrong # args: should be none"
test graph-37.2 {keys returns list of keys} {
    graph mygraph
    mygraph set -key other things
    set results [mygraph keys]
    mygraph destroy
    lsort $results
} "data other"
  
test graph-38.1 {keyexists returns false on non-existant key} {
    graph mygraph
    set result [mygraph keyexists -key bogus]
    mygraph destroy
    set result
} "0"
test graph-38.2 {keyexists uses data as default key} {
    graph mygraph
    set result [mygraph keyexists]
    mygraph destroy
    set result
} "1"
test graph-38.3 {keyexists respects -key flag} {
    graph mygraph
    mygraph set -key boom foobar
    set result [mygraph keyexists -key boom]
    mygraph destroy
    set result
} "1"

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