Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 148 lines (119 sloc) 3.095 kb
db9f1ed @clarkware Initial import
authored
1 #!/usr/bin/env ruby
2
3 require 'test/unit'
4
5 class ArrayTest < Test::Unit::TestCase
6
7 def test_new_array_literal
8 a = [3.14, "pie", 99]
9 assert_equal(Array, a.class)
10 assert_equal(3, a.length)
11 assert_equal(3.14, a[0])
12 assert_equal("pie", a[1])
13 assert_equal(99, a[2])
14 assert_nil(a[3])
15 end
16
17 def test_new_array
18 a = Array.new
19 assert_equal(Array, a.class)
20 a[0] = 3.14
21 a[1] = "pie"
22 a[2] = 99
23 assert_equal(3, a.length)
24 assert_equal([3.14, "pie", 99], a)
25 end
26
27 def test_new_array_with_block
28 a = Array.new(3) { |i| i + 2 }
29 assert_equal([2, 3, 4], a)
30 end
31
32 def test_array_of_words
33 a = %w{ ireland scotland england }
34 assert_equal(["ireland", "scotland", "england"], a)
35 end
36
37 def test_index_negative
38 a = [ 1, 3, 5 ]
39 assert_equal(5, a[-1])
40 assert_equal(3, a[-2])
41 assert_equal(1, a[-3])
42 assert_nil(a[-99])
43 end
44
45 def test_index_by_start_and_count
46 a = [ 1, 3, 5, 7 ]
47 assert_equal([1], a[0, 1])
48 assert_equal([1, 3], a[0, 2])
49 assert_equal([3, 5, 7], a[1, 3])
50 assert_equal([7], a[3, 1])
51 assert_equal([3, 5], a[-3, 2])
52 end
53
54 def test_index_by_range
55 a = [ 1, 3, 5, 7 ]
56 assert_equal([1, 3, 5], a[0..2])
57 assert_equal([1, 3], a[0...2])
58 assert_equal([3], a[1..1])
59 assert_equal([], a[1...1])
60 assert_equal([3, 5, 7], a[-3..-1])
61 end
62
63 def test_single_index_change
64 a = [ 1, 3, 5, 7, 9 ]
65
66 a[1] = "bat"
67 assert_equal([1, "bat", 5, 7, 9], a)
68
69 a[-3] = "cat"
70 assert_equal([1, "bat", "cat", 7, 9], a)
71
72 a[3] = [9, 8]
73 assert_equal([1, "bat", "cat", [9, 8], 9], a)
74
75 a[6] = 99
76 assert_equal([1, "bat", "cat", [9, 8], 9, nil, 99], a)
77 end
78
79 def test_range_change
80 a = [ 1, 3, 5, 7, 9 ]
81
82 a[2, 2] = 'cat'
83 assert_equal([1, 3, "cat", 9], a)
84
85 a[2, 0] = 'dog'
86 assert_equal([1, 3, "dog", "cat", 9], a)
87
88 a[1, 1] = [9, 8, 7]
89 assert_equal([1, 9, 8, 7, "dog", "cat", 9], a)
90
91 a[0..3] = []
92 assert_equal(["dog", "cat", 9], a)
93
94 a[5..6] = 99, 98
95 assert_equal(["dog", "cat", 9, nil, nil, 99, 98], a)
96 end
97
98 def test_clear
99 a = [1, 2, 3]
100 assert_equal([], a.clear())
101 end
102
103 def test_push_pop_shift
104 a = Array.new
105 a.push("A")
106 a.push("B")
107 a.push("C")
108 assert_equal(["A", "B", "C"], a)
109 assert_equal("A", a.shift)
110 assert_equal("C", a.pop)
111 assert_equal("B", a.pop)
112 assert_equal(nil, a.pop)
113 end
114
115 def test_each
116 a = ["A", "B", "C"]
117 s = ""
118 a.each { |element| s += element }
119 assert_equal("ABC", s)
120 end
121
122 def test_sort
123 a = [3, 1, 7, 0]
124 assert_equal([0, 1, 3, 7], a.sort)
125 end
126
127 def test_reverse
128 a = [3, 1, 7, 0]
129 assert_equal([0, 7, 1, 3], a.reverse)
130 end
131
132 def test_join
133 a = ["a", "b", "c"]
134 assert_equal("abc", a.join)
135 assert_equal("a-b-c", a.join("-"))
136 end
137
138 def test_array_of_hashes
139 a = Array.new(size=2) { Hash.new(0) }
140 assert_equal([{}, {}], a)
141 end
142
143 def test_array_of_arrays
144 a = Array.new(size=2) { Array.new(count=1, obj="A") }
145 assert_equal([["A"], ["A"]], a)
146 end
147
148 end
Something went wrong with that request. Please try again.