Skip to content
Tcl Source Code Artifact [8cc4824c3c]
Login

Artifact [8cc4824c3c]

Artifact 8cc4824c3cc2735e0198643b95ccdf0980789f0b628678ae0cd5fe2a51d852f4:


# Copyright © 2021 Nathan Coulter

# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2.5
    namespace import -force ::tcltest::*
}

::tcltest::loadTestedCommands
testConstraint testindexhex [expr {[namespace which testindexhex] ne {}}]

variable list
variable res

proc data1 iterations {
    for {set i 0} {$i < $iterations} {incr i} {
	lappend expected [format %x $i]
    }
    return $expected
}


test {list length} {INST_LIST_INDEX_IMM} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	llength $list
    } -cleanup {
	catch {unset list}
    } -result -1 

test {list lindex constant} {INST_LIST_INDEX_IMM} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lindex $list 731
    } -cleanup {
	catch {unset list}
    } -result 2db


test {list lindex constant end} {INST_LIST_INDEX_IMM} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	after 100
	lindex $list end
    } -cleanup {
	catch {unset list}
	catch {unset res}
    } -returnCodes 1 -result {list length indeterminate}



test {list lindex dynamic} {INST_LIST_INDEX} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	set val [expr {731 + 0}]
	after 100
	lindex $list $val
    } -cleanup {
	catch {unset list}
    } -result 2db


test {list lindex dynamic end} {INST_LIST_INDEX} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	append index e n d
	lindex $list $index
    } -cleanup {
	catch {unset list}
	catch {unset res}
    } -returnCodes 1 -result {list length indeterminate}


test {list lindex drill} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lindex $list 731 0 0 0
    } -cleanup {
	catch {unset list}
    } -result 2db


test {list lrange constant} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lrange $list 10 15
    } -cleanup {
	catch {unset list}
    } -result {a b c d e f}


test {list lrange dynamic} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	set first [expr {10 + 0}]
	set last [expr {15 + 0}]
	lrange $list $first $last
    } -cleanup {
	catch {unset list}
    } -result {a b c d e f}


test {list lrange end constant} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lrange $list 10 end
    } -cleanup {
	catch {unset list}
    } -returnCodes 1 -result {list length indeterminate}


test {list lrange end dynamic} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	set back [expr {-5 + 0}]
	lrange $list 10 end-$back
    } -cleanup {
	catch {unset list}
    } -returnCodes 1 -result {list length indeterminate}


test {list lrange end minus constant} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lrange $list 10 end-1
    } -cleanup {
	catch {unset list}
    } -returnCodes 1 -result {list length indeterminate}


test {list lsearch} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lsearch $list ff
    } -cleanup {
	catch {unset list}
    } -result 255


test {list lsearch sorted} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lsearch -sorted $list ff
    } -cleanup {
	catch {unset list}
    } -returnCodes 1 -result {sorted list is incoherent}


test {list lsearch start} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	lsearch -start 5171 -glob $list a*
    } -cleanup {
	catch {unset list}
    } -result 40960


test {string index} {} \
    -constraints testindexhex \
    -body {
	set iterations 4097
	set expected [data1 $iterations]
	set list [testindexhex]
	set progres {}
	set iterations [string length $expected]
	for {set i 0} {$i < $iterations} {incr i} {
	    set eitem [string index $expected $i]
	    set item [string index $list $i]
	    if {$item ne $eitem} {
		error [list {failed at index} $i [
		    format %x $i] expected $eitem got $item]
	    }
	    lappend progress $item
	}
	return success
    } -cleanup {
	catch {unset i}
	catch {unset list}
    } -result success


test {string index end} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	string index $list end
	return success
    } -cleanup {
	catch {unset list}
    } -returnCodes 1 -result {list length indeterminate}


test {string length} {} \
    -constraints testindexhex \
    -body {
	set list [testindexhex]
	string length $list
    } -cleanup {
	catch {unset list}
    } -result -1


test {string range} {} \
    -constraints testindexhex \
    -body {
	set res {}
	set iterations 4097
	set data1 [data1 $iterations]
	set data1Length [string length $data1]
	set list [testindexhex]
	for {set first 0} {$first < $data1Length } {
	    set first [expr {($first + 1) * 2}]} {

	    for {set last $first} {$last < $data1Length} {
		set last [expr {($last + 1) * 3}]} {

		set expected [string range $data1 $first $last]
		set range [string range $list $first $last]
		if {$range ne $expected} {
		    set length [expr {
			max([string length $expected], [string length $range])
		    }]
		    for {set i 0} {$i < $length} {incr i} {
			set item1 [string index $range $i]
			set item2 [string index $expected $i]
			if {$item1 ne $item2} {
			    error [list {failed at} $first $last $i \
				expected $item2 got $item1]
			}
		    }
		}
	    }
	}
	lappend res success
	after 100

	# The largest string index currently allowed.
	lappend res [string range $list 2147483640 2147483647]

	# This produces an error until index ranges are expanded in some later
	# version of Tcl.
	set status [catch {string range $list 2147483640 2147483648} cres copts]

	lappend res $status $cres
	return $res
    } -cleanup {
	catch {unset list}
    } -result {success {d73ac8f } 1 {}}


# cleanup
::tcltest::cleanupTests
return