summaryrefslogtreecommitdiff
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
authornahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-09-04 16:18:59 +0000
committernahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-09-04 16:18:59 +0000
commitaaf5d9c03cd41818edb34a154ac570b7fe61687c (patch)
tree5bc96fa0c702404154d3ba936276873cc7cc242f /test/ruby/test_array.rb
parent822a72b8ee2752051e11b3b62fc6c5cd21c9409e (diff)
* test/ruby: tests for ruby itself.
* test/ruby/test_*.rb: split sample/test.rb into 28 test/unit testcases. some tests could not be translates... search '!!' mark to see it. * test/csv/test_csv.rb: should require 'csv', not '../lib/csv'. test runner should set load path correctly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb103
1 files changed, 103 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
new file mode 100644
index 0000000000..bc2902f6ff
--- /dev/null
+++ b/test/ruby/test_array.rb
@@ -0,0 +1,103 @@
+require 'test/unit'
+
+$KCODE = 'none'
+
+class TestArray < Test::Unit::TestCase
+ def test_array
+ assert([1, 2] + [3, 4] == [1, 2, 3, 4])
+ assert([1, 2] * 2 == [1, 2, 1, 2])
+ assert([1, 2] * ":" == "1:2")
+
+ assert([1, 2].hash == [1, 2].hash)
+
+ assert([1,2,3] & [2,3,4] == [2,3])
+ assert([1,2,3] | [2,3,4] == [1,2,3,4])
+ assert([1,2,3] - [2,3] == [1])
+
+ $x = [0, 1, 2, 3, 4, 5]
+ assert($x[2] == 2)
+ assert($x[1..3] == [1, 2, 3])
+ assert($x[1,3] == [1, 2, 3])
+
+ $x[0, 2] = 10
+ assert($x[0] == 10 && $x[1] == 2)
+
+ $x[0, 0] = -1
+ assert($x[0] == -1 && $x[1] == 10)
+
+ $x[-1, 1] = 20
+ assert($x[-1] == 20 && $x.pop == 20)
+ end
+
+ def test_array_andor
+ assert(([1,2,3]&[2,4,6]) == [2])
+ assert(([1,2,3]|[2,4,6]) == [1,2,3,4,6])
+ end
+
+ def test_compact
+ $x = [nil, 1, nil, nil, 5, nil, nil]
+ $x.compact!
+ assert($x == [1, 5])
+ end
+
+ def test_uniq
+ $x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
+ $x.uniq!
+ assert($x == [1, 4, 2, 5])
+
+ # empty?
+ assert(!$x.empty?)
+ $x = []
+ assert($x.empty?)
+ end
+
+ def test_sort
+ $x = ["it", "came", "to", "pass", "that", "..."]
+ $x = $x.sort.join(" ")
+ assert($x == "... came it pass that to")
+ $x = [2,5,3,1,7]
+ $x.sort!{|a,b| a<=>b} # sort with condition
+ assert($x == [1,2,3,5,7])
+ $x.sort!{|a,b| b-a} # reverse sort
+ assert($x == [7,5,3,2,1])
+ end
+
+ def test_split
+ $x = "The Boassert of Mormon"
+ assert($x.split(//).reverse!.join == $x.reverse)
+ assert($x.reverse == $x.reverse!)
+ assert("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
+ $x = "a b c d"
+ assert($x.split == ['a', 'b', 'c', 'd'])
+ assert($x.split(' ') == ['a', 'b', 'c', 'd'])
+ end
+
+ def test_misc
+ assert(defined? "a".chomp)
+ assert("abc".scan(/./) == ["a", "b", "c"])
+ assert("1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]])
+ # non-greedy match
+ assert("a=12;b=22".scan(/(.*?)=(\d*);?/) == [["a", "12"], ["b", "22"]])
+
+ $x = [1]
+ assert(($x * 5).join(":") == '1:1:1:1:1')
+ assert(($x * 1).join(":") == '1')
+ assert(($x * 0).join(":") == '')
+
+ *$x = *(1..7).to_a
+ assert($x.size == 7)
+ assert($x == [1, 2, 3, 4, 5, 6, 7])
+
+ $x = [1,2,3]
+ $x[1,0] = $x
+ assert($x == [1,1,2,3,2,3])
+
+ $x = [1,2,3]
+ $x[-1,0] = $x
+ assert($x == [1,2,1,2,3,3])
+
+ $x = [1,2,3]
+ $x.concat($x)
+ assert($x == [1,2,3,1,2,3])
+ end
+end