Skip to content
Tcl Source Code Artifact [bf692a5f03]
Login

Artifact [bf692a5f03]

Artifact bf692a5f03b5fb121ca6ebbde4719ba683f96b786d92c71fd23157e7f03eb21a:


# Commands covered:  range
#
# 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 © 2003 Simon Geard.
#
# 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::*
}

## Arg errors
test range-1.1 {error cases} {
    -body {
	range
    }
    -returnCodes 1
    -result {wrong # args: should be "range start op end ?by step?"}
}

test range-1.2 {step magnitude} {
    -body {
	range 10 .. 1 by 2 ;# or this could be an error - or not
    }
    -result {10 8 6 4 2}
}

test range-1.3 {step wrong sign} {
    -body {
	range 25. to 5. by 5 ;# ditto - maybe this is an error
    }
    -result {25.0 20.0 15.0 10.0 5.0}
}

test range-1.4 {integer decreasing} {
    -body {
	range 10 .. 1
    }
    -result {10 9 8 7 6 5 4 3 2 1}
}

test range-1.5 {integer increasing} {
    -body {
	range 1 .. 10
    }
    -result {1 2 3 4 5 6 7 8 9 10}
}

test range-1.6 {integer decreasing with step} {
    -body {
	range 10 .. 1 by -2
    }
    -result {10 8 6 4 2}
}

test range-1.7 {real increasing range} {
    -body {
	range 5.0 to 15.
    }
    -result {5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0}
}

test range-1.8 {real increasing range with step} {
    -body {
	range 5.0 to 25. by 5
    }
    -result {5.0 10.0 15.0 20.0 25.0}
}

test range-1.9 {real decreasing with step} {
    -body {
	range 25. to 5. by -5
    }
    -result {25.0 20.0 15.0 10.0 5.0}
}

# note, 10 cannot be in such a list, but allowed
test range-1.10 {integer range with step} {
    -body {
	range 1 to 10 by 2
    }
    -result {1 3 5 7 9}
}

test range-1.11 {error case: increasing wrong step direction} {
    -body {
	range 1 to 10 by -2
    }
    -result {1 3 5 7 9}
}

test range-1.12 {decreasing range with step} {
    -body {
	range 25. to -25. by -5
    }
    -result { 25.0 20.0 15.0 10.0 5.0 0.0 -5.0 -10.0 -15.0 -20.0 -25.0}
}

test range-1.13 {count operation} {
    -body {
	range 5 -count 5
    }
    -result {5 6 7 8 9}
}

test range-1.14 {count with step} {
    -body {
	range 5 -count 5 by 2
    }
    -result {5 7 9 11 13}
}

test range-1.15 {count with decreasing step} {
    -body {
	range 5 -count 5 by -2
    }
    -result {5 3 1 -1 -3}
}

#
# Short-hand use cases
#
test range-2.2 {step magnitude} {
    -body {
	range 10 1 2 ;# or this could be an error - or not
    }
    -result {10 8 6 4 2}
}

test range-2.3 {step wrong sign} {
    -body {
	range 25. 5. 5 ;# ditto - maybe this is an error
    }
    -result {25.0 20.0 15.0 10.0 5.0}
}

test range-2.4 {integer decreasing} {
    -body {
	range 10 1
    }
    -result {10 9 8 7 6 5 4 3 2 1}
}

test range-2.5 {integer increasing} {
    -body {
	range 1 10
    }
    -result {1 2 3 4 5 6 7 8 9 10}
}

test range-2.6 {integer decreasing with step} {
    -body {
	range 10 1 by -2
    }
    -result {10 8 6 4 2}
}

test range-2.7 {real increasing range} {
    -body {
	range 5.0 15.
    }
    -result {5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0}
}

test range-2.8 {real increasing range with step} {
    -body {
	range 5.0 25. 5
    }
    -result {5.0 10.0 15.0 20.0 25.0}
}

test range-2.9 {real decreasing with step} {
    -body {
	range 25. 5. -5
    }
    -result {25.0 20.0 15.0 10.0 5.0}
}

# note, 10 cannot be in such a list, but allowed
test range-2.10 {integer range with step} {
    -body {
	range 1 10 2
    }
    -result {1 3 5 7 9}
}

test range-2.11 {error case: increasing wrong step direction} {
    -body {
	range 1 10 -2
    }
    -result {1 3 5 7 9}
}

test range-2.12 {decreasing range with step} {
    -body {
	range 25. -25. -5
    }
    -result { 25.0 20.0 15.0 10.0 5.0 0.0 -5.0 -10.0 -15.0 -20.0 -25.0}
}

test range-2.13 {count only operation} {
    -body {
	range 5 
    }
    -result {0 1 2 3 4}
}

test range-2.14 {count with step} {
    -body {
	range 5 -count 5 2
    }
    -result {5 7 9 11 13}
}

test range-2.15 {count with decreasing step} {
    -body {
	range 5 -count 5 -2
    }
    -result {5 3 1 -1 -3}
}