summaryrefslogtreecommitdiffhomepage
path: root/test/t/array.rb
blob: 90fa85c7fea4423d63f9f42f5cf02d0b7cf4570a (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
##
# Array ISO Test

assert('Array', '15.2.12') do
  Array.class == Class
end

assert('Array superclass', '15.2.12.2') do
  Array.superclass == Object
end

assert('Array.[]', '15.2.12.4.1') do
  Array.[](1,2,3) == [1, 2, 3]
end

assert('Array#*', '15.2.12.5.1') do
  e2 = nil
  begin
    # this will cause an exception due to the wrong argument
    [1].*(-1)
  rescue => e1
    e2 = e1
  end
  a = [1].*(3)
  b = [1].*(0)
  a == [1, 1, 1] and b == [] and
    e2.class == ArgumentError
end

assert('Array#+', '15.2.12.5.2') do
  [1].+([1]) == [1, 1]
end

assert('Array#<<', '15.2.12.5.3') do
  [1].<<(1) == [1, 1]
end

assert('Array#[]', '15.2.12.5.4') do
  e2 = nil
  e3 = nil
  a = Array.new
  begin
    # this will cause an exception due to the wrong arguments
    a.[]()
  rescue => e1
    e2 = e1
  end
  begin
    # this will cause an exception due to the wrong arguments
    a.[](1,2,3)
  rescue => e1
    e3 = e1
  end

  [1,2,3].[](1) == 2 and
    e2.class == ArgumentError and
    e3.class == ArgumentError
end

assert('Array#[]=', '15.2.12.5.5') do
  e2 = nil
  e3 = nil
  a = Array.new
  begin
    # this will cause an exception due to the wrong arguments
    a.[]=()
  rescue => e1
    e2 = e1
  end
  begin
    # this will cause an exception due to the wrong arguments
    a.[]=(1,2,3,4)
  rescue => e1
    e3 = e1
  end

  [1,2,3].[]=(1,4) == 4 and
  [1,2,3].[]=(1,2,3) == 3 and
    e2.class == ArgumentError and
    e3.class == ArgumentError
end

assert('Array#clear', '15.2.12.5.6') do
  a = [1]
  a.clear
  a == []
end

assert('Array#collect!', '15.2.12.5.7') do
  a = [1,2,3]
  a.collect! { |i| i + i }
  a == [2,4,6]
end

assert('Array#concat', '15.2.12.5.8') do
  a = [1,2]
  b = [3,4]
  a.concat(b) == [1,2,3,4]
end

assert('Array#delete_at', '15.2.12.5.9') do
  a = [1,2,3]
  a.delete_at(1)
  a == [1,3]
end

assert('Array#each', '15.2.12.5.10') do
  a = [1,2,3]
  b = 0
  a.each {|i| b += i}
  b == 6
end

assert('Array#each_index', '15.2.12.5.11') do
  a = [1]
  b = nil
  a.each_index {|i| b = i}
  b == 0
end

assert('Array#empty?', '15.2.12.5.12') do
  a = []
  b = [b]
  a.empty? and not b.empty?
end

assert('Array#first', '15.2.12.5.13') do
  a = []
  b = [1,2,3]

  e2 = nil
  e3 = nil
  begin
    # this will cause an exception due to the wrong argument
    [1,2,3].first(-1)
  rescue => e1
    e2 = e1
  end
  begin
    # this will cause an exception due to the wrong argument
    [1,2,3].first(1,2)
  rescue => e1
    e3 = e1
  end

  a.first == nil and b.first == 1 and b.first(0) == [] and
    b.first(1) == [1] and b.first(4) == [1,2,3] and
    e2.class == ArgumentError and e3.class == ArgumentError
end

assert('Array#index', '15.2.12.5.14') do
  a = [1,2,3]

  a.index(2) == 1
end

assert('Array#initialize', '15.2.12.5.15') do
  a = [].initialize(1)
  b = [].initialize(2)
  c = [].initialize(2, 1)
  d = [].initialize(2) {|i| i}

  a == [nil] and b == [nil,nil] and c == [1,1] and d == [0,1]
end

assert('Array#initialize_copy', '15.2.12.5.16') do
  a = [1,2,3]
  b = [].initialize_copy(a)

  b == [1,2,3]
end

assert('Array#join', '15.2.12.5.17') do
  a = [1,2,3].join
  b = [1,2,3].join(',')

  a == '123' and b == '1,2,3'
end

assert('Array#last', '15.2.12.5.18') do
  a = [1,2,3]

  e2 = nil
  begin
    # this will cause an exception due to the wrong argument
    [1,2,3].last(-1)
  rescue => e1
    e2 = e1
  end

  a.last == 3 and [].last == nil and e2.class == ArgumentError
end

assert('Array#length', '15.2.12.5.19') do
  a = [1,2,3]

  a.length == 3
end

assert('Array#map!', '15.2.12.5.20') do
  a = [1,2,3]
  a.map! { |i| i + i }
  a == [2,4,6]
end

assert('Array#pop', '15.2.12.5.21') do
  a = [1,2,3]
  b = a.pop

  [].pop == nil and a == [1,2] and b = 3
end

assert('Array#push', '15.2.12.5.22') do
  a = [1,2,3]
  b = a.push(4)

  a == [1,2,3,4] and b = [1,2,3,4]
end

assert('Array#replace', '15.2.12.5.23') do
  a = [1,2,3]
  b = [].replace(a)

  b == [1,2,3]
end

assert('Array#reverse', '15.2.12.5.24') do
  a = [1,2,3]
  b = a.reverse

  a == [1,2,3] and b == [3,2,1]
end

assert('Array#reverse!', '15.2.12.5.25') do
  a = [1,2,3]
  b = a.reverse!

  a == [3,2,1] and b == [3,2,1]
end

assert('Array#rindex', '15.2.12.5.26') do
  a = [1,2,3]

  a.rindex(2) == 1
end

assert('Array#shift', '15.2.12.5.27') do
  a = [1,2,3]
  b = a.shift

  [].shift == nil and a == [2,3] and b == 1
end

assert('Array#size', '15.2.12.5.28') do
  a = [1,2,3]

  a.size == 3
end

assert('Array#slice', '15.2.12.5.29') do
  a = "12345".slice(1, 3)
  b = a.slice(0)
  "#{b}:" == "2:" and [1,2,3].[](1) == 2
end

assert('Array#unshift', '15.2.12.5.30') do
  a = [2,3]
  b = a.unshift(1)
  c = [2,3]
  d = c.unshift(0, 1)

  a == [1,2,3] and b == [1,2,3] and c == [0,1,2,3] and d == [0,1,2,3] 
end

assert('Array#to_s', '15.2.12.5.31') do
  a = [2, 3,   4, 5]
  r1 = a.to_s
  r2 = a.inspect

  r1 == r2 and r1 == "[2, 3, 4, 5]"
end

assert('Array#==', '15.2.12.5.33') do
  r1 = [ "a", "c" ]    == [ "a", "c", 7 ]     #=> false
  r2 = [ "a", "c", 7 ] == [ "a", "c", 7 ]     #=> true
  r3 = [ "a", "c", 7 ] == [ "a", "d", "f" ]   #=> false

  r1 == false and r2 == true and r3 == false
end

assert('Array#<=>', '15.2.12.5.36') do
  r1 = [ "a", "a", "c" ]    <=> [ "a", "b", "c" ]   #=> -1
  r2 = [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ]            #=> +1
  r3 = [ "a", "b", "c" ]    <=> [ "a", "b", "c" ]   #=> 0

  r1 == -1 and r2 == +1 and r3 == 0
end

# Not ISO specified

assert("Array (Shared Array Corruption)") do
  a = [ "a", "b", "c", "d", "e", "f" ]
  b = a.slice(1, 3)
  a.clear
  b.clear
end