# -*- tcl -*-
# Testsuite for pt::ast.
# Called by the ../pt_ast.test driver file.

# TODO :: new, new0

test pt-ast-1.0 {verify, wrong#args} -body {
    pt::ast verify
} -returnCodes error -result {wrong # args: should be "pt::ast verify serial ?canonvar?"}

test pt-ast-1.1 {verify, wrong#args} -body {
    pt::ast verify PE PFX XXX
} -returnCodes error -result {wrong # args: should be "pt::ast verify serial ?canonvar?"}

test pt-ast-2.0 {print, wrong#args} -body {
    pt::ast print
} -returnCodes error -result {wrong # args: should be "pt::ast print serial"}

test pt-ast-2.1 {print, wrong#args} -body {
    pt::ast print PE XXX
} -returnCodes error -result {wrong # args: should be "pt::ast print serial"}

test pt-ast-5.1 {equal, wrong#args} -body {
    pt::ast equal
} -returnCodes error -result {wrong # args: should be "pt::ast equal seriala serialb"}

test pt-ast-5.2 {equal, wrong#args} -body {
    pt::ast equal PE
} -returnCodes error -result {wrong # args: should be "pt::ast equal seriala serialb"}

test pt-ast-5.3 {equal, wrong#args} -body {
    pt::ast equal PE PEB XXX
} -returnCodes error -result {wrong # args: should be "pt::ast equal seriala serialb"}

test pt-ast-6.0 {verify-as-canonical, wrong#args} -body {
    pt::ast verify-as-canonical
} -returnCodes error -result {wrong # args: should be "pt::ast verify-as-canonical serial"}

test pt-ast-6.1 {verify-as-canonical, wrong#args} -body {
    pt::ast verify-as-canonical PE XXX
} -returnCodes error -result {wrong # args: should be "pt::ast verify-as-canonical serial"}

test pt-ast-7.0 {canonicalize, wrong#args} -body {
    pt::ast canonicalize
} -returnCodes error -result {wrong # args: should be "pt::ast canonicalize serial"}

test pt-ast-7.1 {canonicalize, wrong#args} -body {
    pt::ast canonicalize PE XXX
} -returnCodes error -result {wrong # args: should be "pt::ast canonicalize serial"}

# -------------------------------------------------------------------------
# Various bad serials.
set n 0
foreach {badserial expected} {
    {}            {expected at least 3 elements for node}
    {{}}          {expected at least 3 elements for node}
    {{} {}}       {expected at least 3 elements for node}
    {{} {} {}}    {expected integer >= 0 as start of range, got ""}
    {{} -1 {}}    {expected integer >= 0 as start of range, got "-1"}
    {{} aa {}}    {expected integer >= 0 as start of range, got "aa"}
    {{} 20 {}}    {expected integer >= 0 as end of range, got ""}
    {{} 20 -1}    {expected integer >= 0 as end of range, got "-1"}
    {{} 20 aa}    {expected integer >= 0 as end of range, got "aa"}
    {{} 20 10}    {expected start <= end for range}
    {SY 20 10}    {expected start <= end for range}
} {
    test pt-ast-8.$n {verify, error} -body {
	pt::ast verify $badserial
    } -returnCodes error -result "error in serialization: $expected"
    incr n
}

set n 0
foreach {badserial expected} {
    {}            {expected at least 3 elements for node}
    {{}}          {expected at least 3 elements for node}
    {{} {}}       {expected at least 3 elements for node}
    {{} {} {}}    {expected integer >= 0 as start of range, got ""}
    {{} -1 {}}    {expected integer >= 0 as start of range, got "-1"}
    {{} aa {}}    {expected integer >= 0 as start of range, got "aa"}
    {{} 20 {}}    {expected integer >= 0 as end of range, got ""}
    {{} 20 -1}    {expected integer >= 0 as end of range, got "-1"}
    {{} 20 aa}    {expected integer >= 0 as end of range, got "aa"}
    {{} 20 10}    {expected start <= end for range}
    {SY 20 10}    {expected start <= end for range}
    { SY 10 20}   {has irrelevant whitespace}
    { {} 10 10}   {has irrelevant whitespace}
} {
    test pt-ast-9.$n {verify-as-canonical, error} -body {
	pt::ast verify-as-canonical $badserial
    } -returnCodes error -result "error in serialization: $expected"
    incr n
}

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

TestFilesProcess $mytestdir ok ast_serial ast_serial-print -> n label input data expected {
    # The 'expected' data is irrelevant here, only used to satisfy
    # TestFilesProcess' syntax.
    test pt-ast-10.$n "verify, $label, ok :- $input" -body {
	pt::ast verify $data
    } -result {}

    test pt-ast-11.$n "verify, $label, ok :- $input" -body {
	pt::ast verify $data IGNORED
    } -result {}
}

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

TestFilesProcess $mytestdir ok ast_serial ast_serial-print -> n label input data expected {
    # The 'expected' data is irrelevant here, only used to satisfy
    # TestFilesProcess' syntax.
    test pt-ast-12.$n "print, $label :- $input" -body {
	pt::ast print $data
    } -result $expected
}

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

TestFilesProcess $mytestdir ok ast_serial ast_serial-tddump -> n label input data expected {
    # The 'expected' data is irrelevant here, only used to satisfy
    # TestFilesProcess' syntax.
    test pt-ast-13.$n "topdown, $label :- $input" -setup {
	proc DUMP {ast} { global res ; lappend res $ast }
	set res {}
    } -body {
	pt::ast topdown DUMP $data
	join $res \n
    } -cleanup {
	rename DUMP {}
	unset res
    } -result $expected
}

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

TestFilesProcess $mytestdir ok ast_serial ast_serial-budump -> n label input data expected {
    # The 'expected' data is irrelevant here, only used to satisfy
    # TestFilesProcess' syntax.
    test pt-ast-14.$n "bottomup, $label :- $input" -setup {
	proc DUMP {ast} { global res ; lappend res $ast ; return $ast }
	set res {}
    } -body {
	pt::ast bottomup DUMP $data
	join $res \n
    } -cleanup {
	rename DUMP {}
	unset res
    } -result $expected
}

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

test pt-ast-15.0 {equal, yes} -body {
    pt::ast equal {{} 1 1} {{} 1 1}
} -result 1

test pt-ast-15.1 {equal, nested, yes} -body {
    pt::ast equal {SY 5 5 {{} 5 5}} {  SY 5 5 {  {} 5 5}}
} -result 1

test pt-ast-15.2 {equal, no} -body {
    pt::ast equal {{} 1 1} {{} 5 5}
} -result 0

test pt-ast-15.3 {equal, nested, no} -body {
    pt::ast equal {SY 5 5 {{} 5 5}} {  XX 5 5 {  {} 5 5}}
} -result 0

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

test pt-ast-16.0 {verify-as-canonical, ok} -body {
    pt::ast verify-as-canonical {{} 1 1}
} -result {}

test pt-ast-16.1 {verify-as-canonical, ok} -body {
    pt::ast verify-as-canonical {X 10 20 {{} 10 10} {{} 16 17}}
} -result {}

test pt-ast-16.2 {verify-as-canonical, bad} -body {
    pt::ast verify-as-canonical { {} 1 5}
} -returnCodes error -result {error in serialization: has irrelevant whitespace}

test pt-ast-16.3 {verify-as-canonical, bad} -body {
    pt::ast verify-as-canonical {X
	10 20}
} -returnCodes error -result {error in serialization: has irrelevant whitespace}

test pt-ast-16.4 {verify-as-canonical, bad} -body {
    pt::ast verify-as-canonical {
	X
	10 20
	{{} 14 16}
	{{} 18 20}
    }
} -returnCodes error -result {error in serialization: has irrelevant whitespace}

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

test pt-ast-17.0 {canonicalize} -body {
    pt::ast canonicalize {{} 1 1}
} -result {{} 1 1}

test pt-ast-17.1 {canonicalize} -body {
    pt::ast canonicalize {X 10 20 {{} 11 11} {{} 16 16}}
} -result {X 10 20 {{} 11 11} {{} 16 16}}

test pt-ast-17.2 {canonicalize} -body {
    pt::ast canonicalize { {} 1 5}
} -result {{} 1 5}

test pt-ast-17.3 {canonicalize} -body {
    pt::ast canonicalize {X
	10 20}
} -result {X 10 20}

test pt-ast-17.4 {canonicalize} -body {
    pt::ast canonicalize {
	X
	10 20
	{{} 16 17}
	{{} 19 20}
    }
} -result {X 10 20 {{} 16 17} {{} 19 20}}

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

test pt-ast-18.0 {equal} -body {
    pt::ast equal \
	{{} 1 2} \
	{{} 1 2}
} -result 1

test pt-ast-18.1 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 11 12} {{} 16 16}} \
	{X 10 20 {{} 11 12} {{} 16 16}}
} -result 1

test pt-ast-18.2 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 11 11} {{} 16 16}} \
	{X 10 20
	    {{} 11 11}
	    {{} 16 16}}
} -result 1

test pt-ast-18.3 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 11 11}
	    {{} 16 16}} \
	{X 10 20
	    {{} 11 11}
	    {{} 16 16}}
} -result 1

test pt-ast-18.4 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 11 11}
	    {{} 16 16}} \
	{X 10 20
	    {{} 11 11}
	    {{} 16
		16}}
} -result 1

test pt-ast-18.5 {equal} -body {
    pt::ast equal \
	{{} 1 1} \
	{X 10 20 {{} 11 11} {{} 16 16}}
} -result 0

test pt-ast-18.6 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 11 11} {{} 16 16}} \
	{X 10 20 {{} 11 11} {{} 20 20}}
} -result 0

test pt-ast-18.7 {equal} -body {
    pt::ast equal \
	{Z 10 20 {{} 11 11} {{} 16 16}} \
	{X 10 20 {{} 11 11} {{} 16 16}}
} -result 0

test pt-ast-18.8 {equal} -body {
    pt::ast equal \
	{Z 10 20 {{} 11 11} {{} 16 16}} \
	{X 10 20 {{} 11 11} {{}
	    20 20}}
} -result 0

test pt-ast-18.9 {equal} -body {
    pt::ast equal \
	{X 10 20 {{} 10 10} {{} 16 16}} \
	{X 10 20 {{} 11 11} {{}
	    16 16}}
} -result 0

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

unset n badserial expected label input data
