###############################################################################
#
# list.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################

runTest {test list-1.1.1 {build lists, pre-TIP #148 (Tcl)} -body {
  list [list [list a b c] [list x y z] [list d e f] [list a b [list a b [list \
      a b [list a b c]]]] [list 1 2 3] [list 0x1 0x2 0x3] [list \{ \} \[ \]]] \
      [list [list [list a b c] [list x y z] [list 1 2 3]] [list [list X Y Z] \
      [list 1 2 3] [list A B C]] [list [list - + ^] [list # # #] \
      [list $ $ $]]] [list a A b B z f]
} -constraints {tcl tcl84} -result {{{a b c} {x y z} {d e f}\
{a b {a b {a b {a b c}}}} {1 2 3} {0x1 0x2 0x3} {\{ \} {[} \]}}\
{{{a b c} {x y z} {1 2 3}} {{X Y Z} {1 2 3} {A B C}} {{- + ^}\
{# # #} {{$} {$} {$}}}} {a A b B z f}}}

###############################################################################

runTest {test list-1.1.2 {build lists, post-TIP #148} -body {
  list [list [list a b c] [list x y z] [list d e f] [list a b [list a b [list \
      a b [list a b c]]]] [list 1 2 3] [list 0x1 0x2 0x3] [list \{ \} \[ \]]] \
      [list [list [list a b c] [list x y z] [list 1 2 3]] [list [list X Y Z] \
      [list 1 2 3] [list A B C]] [list [list - + ^] [list # # #] \
      [list $ $ $]]] [list a A b B z f]
} -constraints {tcl85Feature} -result {{{a b c} {x y z} {d e f}\
{a b {a b {a b {a b c}}}} {1 2 3} {0x1 0x2 0x3} {\{ \} {[} \]}}\
{{{a b c} {x y z} {1 2 3}} {{X Y Z} {1 2 3} {A B C}} {{- + ^}\
{{#} # #} {{$} {$} {$}}}} {a A b B z f}}}

###############################################################################

runTest {test lindex-1.1.1 {lindex with bad index} -setup {
  unset -nocomplain msg
} -body {
  list [catch {lindex one 0} msg] $msg \
      [catch {lindex one bad} msg] $msg \
      [catch {lindex one 0 end} msg] $msg \
      [catch {lindex one 0 bad} msg] $msg \
      [catch {lindex one 0 end end} msg] $msg \
      [catch {lindex one 0 bad end} msg] $msg
} -cleanup {
  unset -nocomplain msg
} -constraints {tcl tcl84} -result \
{0 one 1 {bad index "bad": must be integer or end?-integer?} 0 one 1 {bad index\
"bad": must be integer or end?-integer?} 0 one 1 {bad index "bad": must be\
integer or end?-integer?}}}

###############################################################################

runTest {test lindex-1.1.2 {lindex with bad index} -setup {
  unset -nocomplain msg
} -body {
  list [catch {lindex one 0} msg] $msg \
      [catch {lindex one bad} msg] $msg \
      [catch {lindex one 0 end} msg] $msg \
      [catch {lindex one 0 bad} msg] $msg \
      [catch {lindex one 0 end end} msg] $msg \
      [catch {lindex one 0 bad end} msg] $msg
} -cleanup {
  unset -nocomplain msg
} -constraints {tcl tcl85} -result \
{0 one 1 {bad index "bad": must be integer?[+-]integer? or end?[+-]integer?} 0\
one 1 {bad index "bad": must be integer?[+-]integer? or end?[+-]integer?} 0 one\
1 {bad index "bad": must be integer?[+-]integer? or end?[+-]integer?}}}

###############################################################################

runTest {test lindex-1.1.3 {lindex with bad index} -setup {
  unset -nocomplain msg
} -body {
  list [catch {lindex one 0} msg] $msg \
      [catch {lindex one bad} msg] $msg \
      [catch {lindex one 0 end} msg] $msg \
      [catch {lindex one 0 bad} msg] $msg \
      [catch {lindex one 0 end end} msg] $msg \
      [catch {lindex one 0 bad end} msg] $msg
} -cleanup {
  unset -nocomplain msg
} -constraints {eagle} -result \
{0 one 1 {bad index "bad": must be end|integer?[+-]end|integer?} 0\
one 1 {bad index "bad": must be end|integer?[+-]end|integer?} 0 one\
1 {bad index "bad": must be end|integer?[+-]end|integer?}}}

###############################################################################

runTest {test lindex-2.1 {lindex with index list} -setup {
  unset -nocomplain msg
} -body {
  list [catch {lindex one 0} msg] $msg \
      [catch {lindex one bad} msg] $msg \
      [catch {lindex one {0 end}} msg] $msg \
      [catch {lindex one {0 bad}} msg] $msg \
      [catch {lindex one {0 end end}} msg] $msg \
      [catch {lindex one {0 bad end}} msg] $msg
} -cleanup {
  unset -nocomplain msg
} -constraints {eagle} -result \
{0 one 1 {bad index "bad": must be end|integer?[+-]end|integer?} 0 {one one} 1\
{bad index "0 bad": must be end|integer?[+-]end|integer?} 0 {one one one} 1\
{bad index "0 bad end": must be end|integer?[+-]end|integer?}}}

###############################################################################

runTest {test lpermute-1.1 {list permutations} -body {
  lpermute [list a b c d e]
} -result {{a b c d e} {a b c e d} {a b d c e} {a b d e c} {a b e c d} {a b e d\
c} {a c b d e} {a c b e d} {a c d b e} {a c d e b} {a c e b d} {a c e d b} {a d\
b c e} {a d b e c} {a d c b e} {a d c e b} {a d e b c} {a d e c b} {a e b c d}\
{a e b d c} {a e c b d} {a e c d b} {a e d b c} {a e d c b} {b a c d e} {b a c\
e d} {b a d c e} {b a d e c} {b a e c d} {b a e d c} {b c a d e} {b c a e d} {b\
c d a e} {b c d e a} {b c e a d} {b c e d a} {b d a c e} {b d a e c} {b d c a\
e} {b d c e a} {b d e a c} {b d e c a} {b e a c d} {b e a d c} {b e c a d} {b e\
c d a} {b e d a c} {b e d c a} {c a b d e} {c a b e d} {c a d b e} {c a d e b}\
{c a e b d} {c a e d b} {c b a d e} {c b a e d} {c b d a e} {c b d e a} {c b e\
a d} {c b e d a} {c d a b e} {c d a e b} {c d b a e} {c d b e a} {c d e a b} {c\
d e b a} {c e a b d} {c e a d b} {c e b a d} {c e b d a} {c e d a b} {c e d b\
a} {d a b c e} {d a b e c} {d a c b e} {d a c e b} {d a e b c} {d a e c b} {d b\
a c e} {d b a e c} {d b c a e} {d b c e a} {d b e a c} {d b e c a} {d c a b e}\
{d c a e b} {d c b a e} {d c b e a} {d c e a b} {d c e b a} {d e a b c} {d e a\
c b} {d e b a c} {d e b c a} {d e c a b} {d e c b a} {e a b c d} {e a b d c} {e\
a c b d} {e a c d b} {e a d b c} {e a d c b} {e b a c d} {e b a d c} {e b c a\
d} {e b c d a} {e b d a c} {e b d c a} {e c a b d} {e c a d b} {e c b a d} {e c\
b d a} {e c d a b} {e c d b a} {e d a b c} {e d a c b} {e d b a c} {e d b c a}\
{e d c a b} {e d c b a}}}

###############################################################################

runTest {test lpermute-1.2 {fast list permutations} -setup {
  set list(1) [object create -alias StringList a b c d e]
} -body {
  set lists(1) [lpermute [$list(1) ToString]]

  set lists(2) [object invoke -create -flags +NonPublic \
      Eagle._Components.Private.ListOps Permute $list(1)]

  set lists(3) [list]

  object foreach -alias list(2) $lists(2) {
    lappend lists(3) [$list(2) ToString]
  }

  expr {[llength $lists(1)] == 120 && \
      [llength $lists(1)] == [llength $lists(3)] && \
      [lsort $lists(1)] eq [lsort $lists(3)]}
} -cleanup {
  unset -nocomplain lists list; # dispose
} -constraints {eagle} -result {True}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
