2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestArray < Test::Unit::TestCase
|
|
|
|
def test_array
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
|
|
|
|
assert_equal([1, 2, 1, 2], [1, 2] * 2)
|
|
|
|
assert_equal("1:2", [1, 2] * ":")
|
|
|
|
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal([1, 2].hash, [1, 2].hash)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
|
|
|
assert_equal([2,3], [1,2,3] & [2,3,4])
|
|
|
|
assert_equal([1,2,3,4], [1,2,3] | [2,3,4])
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal([1,2,3] - [2,3], [1])
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [0, 1, 2, 3, 4, 5]
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(2, $x[2])
|
|
|
|
assert_equal([1, 2, 3], $x[1..3])
|
|
|
|
assert_equal([1, 2, 3], $x[1,3])
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x[0, 2] = 10
|
|
|
|
assert($x[0] == 10 && $x[1] == 2)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x[0, 0] = -1
|
|
|
|
assert($x[0] == -1 && $x[1] == 10)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x[-1, 1] = 20
|
|
|
|
assert($x[-1] == 20 && $x.pop == 20)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_andor
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([2], ([1,2,3]&[2,4,6]))
|
|
|
|
assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
def test_compact
|
|
|
|
$x = [nil, 1, nil, nil, 5, nil, nil]
|
|
|
|
$x.compact!
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1, 5], $x)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uniq
|
|
|
|
$x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
|
|
|
|
$x.uniq!
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1, 4, 2, 5], $x)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
# empty?
|
|
|
|
assert(!$x.empty?)
|
|
|
|
$x = []
|
|
|
|
assert($x.empty?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sort
|
|
|
|
$x = ["it", "came", "to", "pass", "that", "..."]
|
|
|
|
$x = $x.sort.join(" ")
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal("... came it pass that to", $x)
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [2,5,3,1,7]
|
|
|
|
$x.sort!{|a,b| a<=>b} # sort with condition
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1,2,3,5,7], $x)
|
2003-09-04 12:18:59 -04:00
|
|
|
$x.sort!{|a,b| b-a} # reverse sort
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([7,5,3,2,1], $x)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split
|
|
|
|
$x = "The Boassert of Mormon"
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal($x.reverse, $x.split(//).reverse!.join)
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal($x.reverse, $x.reverse!)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal("g:n:i:r:t:s: :e:t:y:b: :1", "1 byte string".split(//).reverse.join(":"))
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = "a b c d"
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(['a', 'b', 'c', 'd'], $x.split)
|
|
|
|
assert_equal(['a', 'b', 'c', 'd'], $x.split(' '))
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_misc
|
|
|
|
assert(defined? "a".chomp)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(["a", "b", "c"], "abc".scan(/./))
|
|
|
|
assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
|
2003-09-04 12:18:59 -04:00
|
|
|
# non-greedy match
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([["a", "12"], ["b", "22"]], "a=12;b=22".scan(/(.*?)=(\d*);?/))
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [1]
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal('1:1:1:1:1', ($x * 5).join(":"))
|
|
|
|
assert_equal('1', ($x * 1).join(":"))
|
|
|
|
assert_equal('', ($x * 0).join(":"))
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
*$x = *(1..7).to_a
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(7, $x.size)
|
|
|
|
assert_equal([1, 2, 3, 4, 5, 6, 7], $x)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [1,2,3]
|
|
|
|
$x[1,0] = $x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1,1,2,3,2,3], $x)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [1,2,3]
|
|
|
|
$x[-1,0] = $x
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1,2,1,2,3,3], $x)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [1,2,3]
|
|
|
|
$x.concat($x)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal([1,2,3,1,2,3], $x)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|