-
-
Notifications
You must be signed in to change notification settings - Fork 921
/
test_array.rb
216 lines (179 loc) · 4.58 KB
/
test_array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'test/unit'
class TestArray < Test::Unit::TestCase
def test_unshift_and_leftshift_op
arr = ["zero", "first"]
arr.unshift "second", "third"
assert_equal(["second", "third", "zero", "first"], arr)
assert_equal(["first"], arr[-1..-1])
assert_equal(["first"], arr[3..3])
assert_equal([], arr[3..2])
assert_equal([], arr[3..1])
assert(["third", "zero", "first"] == arr[1..4])
assert('["third", "zero", "first"]' == arr[1..4].inspect)
arr << "fourth"
assert("fourth" == arr.pop());
assert("second" == arr.shift());
end
class MyArray < Array
def [](arg)
arg
end
end
def test_aref
assert_equal(nil, [].slice(-1..1))
# test that overriding in child works correctly
assert_equal(2, MyArray.new[2])
end
def test_class
assert(Array == ["zero", "first"].class)
assert("Array" == Array.to_s)
end
def test_dup_and_reverse
arr = [1, 2, 3]
arr2 = arr.dup
arr2.reverse!
assert_equal([1,2,3], arr)
assert_equal([3,2,1], arr2)
assert_equal([1,2,3], [1,2,3,1,2,3,1,1,1,2,3,2,1].uniq)
assert_equal([1,2,3,4], [[[1], 2], [3, [4]]].flatten)
assert_equal(nil, [].flatten!)
end
def test_delete
arr = [1, 2, 3]
arr2 = []
arr.each { |x|
arr2 << x
arr.delete(x) if x == 2
}
assert_equal([1,2], arr2)
end
def test_fill
arr = [1,2,3,4]
arr.fill(1,10)
assert_equal([1,2,3,4], arr)
arr.fill(1,0)
assert_equal([1,1,1,1], arr)
end
def test_flatten
arr = []
arr << [[[arr]]]
assert_raises(ArgumentError) {
arr.flatten
}
end
# To test int coersion for indicies
class IntClass
def initialize(num); @num = num; end
def to_int; @num; end;
end
def test_conversion
arr = [1, 2, 3]
index = IntClass.new(1)
arr[index] = 4
assert_equal(4, arr[index])
eindex = IntClass.new(2)
arr[index, eindex] = 5
assert_equal([1,5], arr)
arr.delete_at(index)
assert_equal([1], arr)
arr = arr * eindex
assert_equal([1, 1], arr)
end
def test_unshift_nothing
assert_nothing_raised { [].unshift(*[]) }
assert_nothing_raised { [].unshift() }
end
##### Array#[] #####
def test_indexing
assert_equal([1], Array[1])
assert_equal([], Array[])
assert_equal([1,2], Array[1,2])
end
##### insert ####
def test_insert
a = [10, 11]
a.insert(1, 12)
assert_equal([10, 12, 11], a)
a = []
a.insert(-1, 10)
assert_equal([10], a)
a.insert(-2, 11)
assert_equal([11, 10], a)
a = [10]
a.insert(-1, 11)
assert_equal([10, 11], a)
end
##### == #####
def test_ary
o = Object.new
def o.to_ary; end
def o.==(o); true; end
assert_equal(true, [].==(o))
end
# test that extensions of the base classes are typed correctly
class ArrayExt < Array
end
def test_array_extension
assert_equal(ArrayExt, ArrayExt.new.class)
assert_equal(ArrayExt, ArrayExt[:foo, :bar].class)
end
##### flatten #####
def test_flatten
a = [2,[3,[4]]]
assert_equal([1,2,3,4],[1,a].flatten)
assert_equal([2,[3,[4]]],a)
a = [[1,2,[3,[4],[5]],6,[7,[8]]],9]
assert_equal([1,2,3,4,5,6,7,8,9],a.flatten)
assert(a.flatten!,"We did flatten")
assert(!a.flatten!,"We didn't flatten")
end
##### splat test #####
class ATest
def to_a; 1; end
end
def test_splatting
proc { |a| assert_equal(1, a) }.call(*1)
assert_raises(TypeError) { proc { |a| }.call(*ATest.new) }
end
#### index test ####
class AlwaysEqual
def ==(arg)
true
end
end
def test_index
array_of_alwaysequal = [AlwaysEqual.new]
# this should pass because index should call AlwaysEqual#== when searching
assert_equal(0, array_of_alwaysequal.index("foo"))
assert_equal(0, array_of_alwaysequal.rindex("foo"))
end
def test_spaceship
assert_equal(0, [] <=> [])
assert_equal(0, [1] <=> [1])
assert_equal(-1, [1] <=> [2])
assert_equal(1, [2] <=> [1])
assert_equal(1, [1] <=> [])
assert_equal(-1, [] <=> [1])
assert_equal(0, [1, 1] <=> [1, 1])
assert_equal(-1, [1, 1] <=> [1, 2])
assert_equal(1, [1,6,1] <=> [1,5,0,1])
assert_equal(-1, [1,5,0,1] <=> [1,6,1])
end
class BadComparator
def <=>(other)
"hello"
end
end
def test_bad_comparator
assert_equal("hello", [BadComparator.new] <=> [BadComparator.new])
end
def test_raises_stack_exception
assert_raises(SystemStackError) { a = []; a << a; a <=> a }
end
def test_multiline_array_not_really_add
assert_raises(NoMethodError) do
[1,2,3]
+[2,3]
end
end
end