# Commands covered:  lsearch
#
# 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) 1991-1993 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: lsearch.test,v 1.5 2000/04/10 17:19:01 ericm Exp $

source [file dirname [info script]]/testing.tcl

catch {package require regexp}
testConstraint regexp [expr {[info commands regexp] ne {}}]

set x {abcd bbcd 123 234 345}
test lsearch-1.1 {lsearch command} {
    lsearch $x 123
} 2
test lsearch-1.2 {lsearch command} {
    lsearch $x 3456
} -1
test lsearch-1.3 {lsearch command} {
    lsearch -glob $x *5
} 4
test lsearch-1.4 {lsearch command} {
    lsearch -glob $x *bc*
} 0

test lsearch-2.1 {search modes} {
    lsearch -exact {xyz bbcc *bc*} *bc*
} 2
test lsearch-2.2 {search modes} {
    lsearch -exact {b.x ^bc xy bcx} ^bc
} 1
test lsearch-2.3 {search modes} {
    lsearch -exact {foo bar cat} ba
} -1
test lsearch-2.4 {search modes} {
    lsearch -exact {foo bar cat} bart
} -1
test lsearch-2.5 {search modes} {
    lsearch -exact {foo bar cat} bar
} 1
test lsearch-2.6 {search modes} regexp {
    list [catch {lsearch -regexp {xyz bbcc *bc*} *bc*} msg]
} {1}
test lsearch-2.7 {search modes} regexp {
    lsearch -regexp {b.x ^bc xy bcx} ^bc
} 3
test lsearch-2.8 {search modes} {
    lsearch -glob {xyz bbcc *bc*} *bc*
} 1
test lsearch-2.9 {search modes} {
    lsearch -glob {b.x ^bc xy bcx} ^bc
} 1
test lsearch-2.10 {search modes} {
    list [catch {lsearch -glib {b.x bx xy bcx} b.x} msg]
} {1}
test lsearch-2.7 {search modes, -nocase} regexp {
    lsearch -nocase -regexp {b.x ^bc xy bcx} ^BC
} 3
test lsearch-2.8 {search modes, -nocase} {
    lsearch -nocase -exact {b.x ^bc xy bcx} ^BC
} 1
test lsearch-2.9 {search modes, -nocase} {
    lsearch -nocase -glob {b.x ^bc xy bcx} B*
} 0

test lsearch-2.10 {regexp with option-looking pattern} regexp {
    lsearch -regexp {-abc -def -ghi} -def
} 1

test lsearch-2.11 {regexp with option-looking pattern, -nocase} regexp {
    lsearch -nocase -regexp {-abc -def -ghi} -DEF
} 1

test lsearch-3.1 {lsearch errors} {
    list [catch lsearch msg]
} {1}
test lsearch-3.2 {lsearch errors} {
    list [catch {lsearch a} msg]
} {1}
test lsearch-3.3 {lsearch errors} {
    list [catch {lsearch a b c} msg]
} {1}
test lsearch-3.4 {lsearch errors} {
    list [catch {lsearch a b c d} msg]
} {1}

test lsearch-4.1 {binary data} {
    lsearch -exact [list foo one\000two bar] bar
} 2
test lsearch-4.2 {binary data} {
    set x one
    append x \x00
    append x two
    lsearch -exact [list foo one\000two bar] $x
} 1

test lsearch-5.1 {lsearch -all} {
    lsearch -glob -all {a1 a2 b1 b2 a3 b3} a*
} {0 1 4}

test lsearch-5.2 {lsearch -all no match} {
    lsearch -glob -all {a1 a2 b1 b2 a3 b3} B*
} {}

test lsearch-5.3 {lsearch -all -nocase} {
    lsearch -glob -all -nocase {a1 a2 b1 b2 a3 b3} B*
} {2 3 5}

test lsearch-5.4 {lsearch -all -inline} {
    lsearch -glob -all -inline -nocase {a1 a2 b1 b2 a3 b3} A*
} {a1 a2 a3}

test lsearch-5.5 {lsearch -inline} {
    lsearch -glob -inline {a1 a2 b1 b2 a3 b3} b*
} {b1}

test lsearch-5.6 {lsearch -not -all} {
    lsearch -not -glob -all {a1 a2 b1 b2 a3 b3} a*
} {2 3 5}

test lsearch-5.7 {lsearch -not -all no match} {
    lsearch -not -glob -all {a1 a2 b1 b2 a3 b3} B*
} {0 1 2 3 4 5}

test lsearch-5.8 {lsearch -not -all -nocase} {
    lsearch -not -glob -all -nocase {a1 a2 b1 b2 a3 b3} B*
} {0 1 4}

test lsearch-5.9 {lsearch -not -all -inline} {
    lsearch -not -glob -all -inline -nocase {a1 a2 b1 b2 a3 b3} A*
} {b1 b2 b3}

test lsearch-5.10 {lsearch -not -inline} {
    lsearch -not -glob -inline {a1 a2 b1 b2 a3 b3} b*
} {a1}

test lsearch-5.11 {lsearch -inline, no match} {
    lsearch -glob -inline {a1 a2 b1 b2 a3 b3} C*
} {}

test lsearch-6.1 {lsearch -bool, found} jim {
    lsearch -bool {a1 a2 b1 b2 a3 b3} b1
} {1}

test lsearch-6.2 {lsearch -bool, not found} jim {
    lsearch -bool {a1 a2 b1 b2 a3 b3} c1
} {0}

test lsearch-6.3 {lsearch -not -bool, found} jim {
    lsearch -not -bool {a1 a2 b1 b2 a3 b3} b1
} {0}

test lsearch-6.4 {lsearch -not -bool, not found} jim {
    lsearch -not -bool {a1 a2 b1 b2 a3 b3} c1
} {1}

test lsearch-6.5 {lsearch -bool -all} jim {
    lsearch -bool -glob -all {a1 a2 b1 b2 a3 b3} a*
} {1 1 0 0 1 0}

test lsearch-6.6 {lsearch -bool -all no match} jim {
    lsearch -bool -glob -all {a1 a2 b1 b2 a3 b3} B*
} {0 0 0 0 0 0}

test lsearch-6.7 {lsearch -bool -all -nocase} jim {
    lsearch -bool -glob -all -nocase {a1 a2 b1 b2 a3 b3} B*
} {0 0 1 1 0 1}

test lsearch-6.8 {lsearch -not -bool -all} jim {
    lsearch -not -bool -glob -all {a1 a2 b1 b2 a3 b3} a*
} {0 0 1 1 0 1}

test lsearch-6.9 {lsearch -not -bool -all no match} jim {
    lsearch -not -bool -glob -all {a1 a2 b1 b2 a3 b3} B*
} {1 1 1 1 1 1}

test lsearch-6.10 {lsearch -not -bool -all -nocase} jim {
    lsearch -not -bool -glob -all -nocase {a1 a2 b1 b2 a3 b3} B*
} {1 1 0 0 1 0}

test lsearch-17.1 {lsearch -index option, basic functionality} {
    lsearch -index 1 {{a c} {a b} {a a}} a
} 2
test lsearch-17.2 {lsearch -index option, basic functionality} {
    lsearch -index 1 -exact {{a c} {a b} {a a}} a
} 2
test lsearch-17.3 {lsearch -index option, basic functionality} {
    lsearch -index 1 -glob {{ab cb} {ab bb} {ab ab}} b*
} 1
test lsearch-17.4 {lsearch -index option, basic functionality} {
    lsearch -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
} 0
test lsearch-17.5 {lsearch -index option, basic functionality} {
    lsearch -all -index 0 -exact {{a c} {a b} {d a}} a
} {0 1}
test lsearch-17.6 {lsearch -index option, basic functionality} {
    lsearch -all -index 1 -glob {{ab cb} {ab bb} {db bx}} b*
} {1 2}
test lsearch-17.7 {lsearch -index option, basic functionality} {
    lsearch -all -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
} {0 1}
test lsearch-17.8 {lsearch -index option, empty argument} {
    lsearch -index {} a a
} 0
test lsearch-17.9 {lsearch -index option, empty argument} {
    lsearch -index {} a a
} [lsearch a a]
test lsearch-17.10 {lsearch -index option, empty argument} {
    lsearch -index {} [list \{] \{
} 0
test lsearch-17.11 {lsearch -index option, empty argument} {
    lsearch -index {} [list \{] \{
} [lsearch [list \{] \{]
test lsearch-17.12 {lsearch -index option, encoding aliasing} -body {
    lsearch -index -2 a a
} -returnCodes error -result {index "-2" out of range}
test lsearch-17.13 {lsearch -index option, encoding aliasing} -body {
    lsearch -index -1-1 a a
} -returnCodes error -result {index "-1-1" out of range}
test lsearch-17.14 {lsearch -index option, encoding aliasing} -body {
    lsearch -index end--1 a a
} -returnCodes error -result {index "end--1" out of range}
test lsearch-17.15 {lsearch -index option, encoding aliasing} -body {
    lsearch -index end+1 a a
} -returnCodes error -result {index "end+1" out of range}
test lsearch-17.16 {lsearch -index option, encoding aliasing} -body {
    lsearch -index end+2 a a
} -returnCodes error -result {index "end+2" out of range}

test lsearch-20.1 {lsearch -index option, index larger than sublists} -body {
    lsearch -index 2 {{a c} {a b} {a a}} a
} -returnCodes error -result {element 2 missing from sublist "a c"}
test lsearch-20.2 {lsearch -index option, malformed index} -body {
    lsearch -index foo {{a c} {a b} {a a}} a
} -returnCodes error -match glob -result {bad index *}

test lsearch-23.1 {lsearch -stride option, errors} -body {
    lsearch -stride {a b} a
} -returnCodes error -match glob -result {*}
test lsearch-23.2 {lsearch -stride option, errors} -body {
    lsearch -stride 0 {a b} a
} -returnCodes error -result {stride length must be at least 1}
test lsearch-23.3 {lsearch -stride option, errors} -body {
    lsearch -stride 2 {a b c} a
} -returnCodes error -result {list size must be a multiple of the stride length}
test lsearch-23.4 {lsearch -stride option, errors} -body {
    lsearch -stride 5 {a b c} a
} -returnCodes error -result {list size must be a multiple of the stride length}
test lsearch-23.5 {lsearch -stride option, errors} -body {
    # Stride equal to length is ok
    lsearch -stride 3 {a b c} a
} -result 0

test lsearch-24.1 {lsearch -stride option} -body {
    lsearch -stride 2 {a b c d e f g h} d
} -result -1
test lsearch-24.2 {lsearch -stride option} -body {
    lsearch -stride 2 {a b c d e f g h} e
} -result 4
test lsearch-24.3 {lsearch -stride option} -body {
    lsearch -stride 3 {a b c d e f g h i} e
} -result -1
test lsearch-24.4 {lsearch -stride option} -body {
    # Result points first in group
    lsearch -stride 3 -index 1 {a b c d e f g h i} e
} -result 3
test lsearch-24.5 {lsearch -stride option} -body {
    lsearch -inline -stride 2 {a b c d e f g h} d
} -result {}
test lsearch-24.6 {lsearch -stride option} -body {
    # Inline result is a "single element" strided list
    lsearch -inline -stride 2 {a b c d e f g h} e
} -result "e f"
test lsearch-24.7 {lsearch -stride option} -body {
    lsearch -inline -stride 3 {a b c d e f g h i} e
} -result {}
test lsearch-24.8 {lsearch -stride option} -body {
    lsearch -inline -stride 3 -index 1 {a b c d e f g h i} e
} -result "d e f"
test lsearch-24.9 {lsearch -stride option} -body {
    lsearch -all -inline -stride 3 -index 1 {a b c d e f g e i} e
} -result "d e f g e i"
test lsearch-24.10 {lsearch -stride option} -body {
    lsearch -all -inline -stride 3 -index 0 {a b c d e f a e i} a
} -result "a b c a e i"
test lsearch-24.11 {lsearch -stride option} -body {
    # Stride 1 is same as no stride
    lsearch -stride 1 {a b c d e f g h} d
} -result 3
test lsearch-24.12 {lsearch -stride -index with missing elements} -body {
    lsearch -stride 1 -index {1 1} {a b c} c
} -returnCodes error -result {element 1 missing from sublist "a"}

testreport
