# -*- tcl -*-
# finite_automaton.test:  tests for the grammar::fa container.
#
# Copyright (c) 2004 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
#
# RCS: @(#) $Id: fa_start.test,v 1.4 2005/09/28 04:51:22 andreas_kupries Exp $

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

test fa-start-1.0 {start states, error} {
    grammar::fa a
    catch {a startstates x} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodstartstates type selfns win self"}


test fa-start-1.1 {start query, error} {
    grammar::fa a
    catch {a start?} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodstart? type selfns win self s"}


test fa-start-1.2 {start query, error} {
    grammar::fa a
    catch {a start? x y} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodstart? type selfns win self s"}


test fa-start-1.3 {start query, error} {
    grammar::fa a
    catch {a start? x} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-1.4 {start query set, error} {
    grammar::fa a
    catch {a start?set} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodstart?set type selfns win self states"}


test fa-start-1.5 {start query set, error} {
    grammar::fa a
    catch {a start?set x y} res
    a destroy
    set res
} {wrong # args: should be "::grammar::fa::Snit_methodstart?set type selfns win self states"}


test fa-start-1.6 {start query set, error} {
    grammar::fa a
    catch {a start?set x} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-1.7 {start query set, error} {
    grammar::fa a
    a state add x
    catch {a start?set {x y}} res
    a destroy
    set res
} {Illegal state "y"}


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


test fa-start-2.0 {start, error} {
    grammar::fa a
    catch {a start} res
    a destroy
    set res
} [tcltest::wrongNumArgs ::grammar::fa::Snit_methodstart {type selfns win self cmd args} 5]


test fa-start-2.1 {start, error} {
    grammar::fa a
    catch {a start foo} res
    a destroy
    set res
} {Expected add, clear, remove, or set, got "foo"}


test fa-start-2.2 {start, error} {
    grammar::fa a
    catch {a start add} res
    a destroy
    set res
} {wrong#args: start add state ...}


test fa-start-2.3 {start, error} {
    grammar::fa a
    catch {a start add x y} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-2.4 {start, error} {
    grammar::fa a
    catch {a start add x} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-2.5 {start states} {
    grammar::fa a
    catch {a start remove} res
    a destroy
    set res
} {wrong#args: start remove state ...}


test fa-start-2.6 {start states} {
    grammar::fa a
    catch {a start remove x y} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-2.7 {start states} {
    grammar::fa a
    catch {a start remove x} res
    a destroy
    set res
} {Illegal state "x"}


test fa-start-2.8 {start states} {
    grammar::fa a
    catch {a start set} res
    a destroy
    set res
} {wrong#args: start set states}


test fa-start-2.9 {start states} {
    grammar::fa a
    a state add x
    catch {a start set {x y}} res
    a destroy
    set res
} {Illegal state "y"}


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


test fa-start-3.0 {start states, empty fa} {
    grammar::fa a
    set res [a startstates]
    a destroy
    set res
} {}


test fa-start-3.1 {start states, plain state} {
    grammar::fa a
    a state add x
    set res [a startstates]
    a destroy
    set res
} {}


test fa-start-3.2 {start states, state addition} {
    grammar::fa a
    a state add x
    a start add x
    set res [a startstates]
    a destroy
    set res
} x


test fa-start-3.3 {start states, state addition} {
    grammar::fa a
    a state add x y
    a start add x y
    set res [lsort [a startstates]]
    a destroy
    set res
} {x y}


test fa-start-3.4 {start states, state addition, and remova;} {
    grammar::fa a
    a state add x y
    a start add x y
    set     res {}
    lappend res [a startstates]
    a start remove y
    lappend res [a startstates]
    a start remove x
    lappend res [a startstates]
    a destroy
    set res
} {{x y} x {}}


test fa-start-3.5 {start states, state addition, and remova;} {
    grammar::fa a
    a state add x y
    a start add x y
    set     res {}
    lappend res [a startstates]
    a state delete y
    lappend res [a startstates]
    a state delete x
    lappend res [a startstates]
    a destroy
    set res
} {{x y} x {}}


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


test fa-start-4.0 {start?} {
    grammar::fa a
    a state add x
    set res [a start? x]
    a destroy
    set res
} 0


test fa-start-4.1 {start?} {
    grammar::fa a
    a state add x
    a start add x
    set res [a start? x]
    a destroy
    set res
} 1


test fa-start-4.2 {start?} {
    grammar::fa a
    a state add x
    a start add x
    set     res [a start? x]
    a start remove x
    lappend res [a start? x]
    a destroy
    set res
} {1 0}


test fa-start-4.3 {start?} {
    grammar::fa a
    a state add x
    a start add x
    set     res [a start? x]
    a state delete x
    catch {a start? x} msg
    lappend res $msg
    a destroy
    set res
} {1 {Illegal state "x"}}


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


test fa-start-5.0 {start?set} {
    grammar::fa a
    a state add x
    set res [a start?set x]
    a destroy
    set res
} 0


test fa-start-5.1 {start?set} {
    grammar::fa a
    a state add x
    a start add x
    set res [a start?set x]
    a destroy
    set res
} 1


test fa-start-5.2 {start?set} {
    grammar::fa a
    set res {}
    a state add x
    a start add x
    lappend res [a start?set x]
    a start remove x
    lappend res [a start?set x]
    a destroy
    set res
} {1 0}


test fa-start-5.3 {start?set} {
    grammar::fa a
    set res {}
    a state add x y
    a start add x
    lappend res [a start?set y]
    lappend res [a start?set {x y}]
    a destroy
    set res
} {0 1}


test fa-start-5.4 {start?set} {
    grammar::fa a
    a state add x
    set     res {}
    lappend res [a start?  x]
    lappend res [a start remove x]
    lappend res [a start?  x]
    a destroy
    set res
} {0 {} 0}


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

test fa-start-6.0 {start clear} {
    grammar::fa a
    a state add x
    a start add x
    set     res {}
    lappend res [a startstates]
    a start clear
    lappend res [a startstates]
    a destroy
    set res
} {x {}}


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

test fa-start-7.0 {start set} {
    grammar::fa a
    a state add x
    a start set x
    set res [a startstates]
    a destroy
    set res
} x


test fa-start-7.1 {start set} {
    grammar::fa a
    a state add x y
    a start set {x y}
    set res [lsort [a startstates]]
    a destroy
    set res
} {x y}


test fa-start-7.2 {start set} {
    grammar::fa a
    set res {}
    a state add x y z
    a start add z
    lappend res [a startstates]
    a start set {x y}
    lappend res [lsort [a startstates]]
    a destroy
    set res
} {z {x y}}


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