forked from pharo-containers/Containers-Array2D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTArray2DTest.class.st
More file actions
419 lines (355 loc) · 11.3 KB
/
CTArray2DTest.class.st
File metadata and controls
419 lines (355 loc) · 11.3 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
Class {
#name : 'CTArray2DTest',
#superclass : 'TestCase',
#category : 'Containers-Array2D-Tests',
#package : 'Containers-Array2D-Tests'
}
{ #category : 'running' }
CTArray2DTest >> arrayClass [
^ CTArray2D
]
{ #category : 'running' }
CTArray2DTest >> classUnderTest [
^ CTArray2D
]
{ #category : 'tests-converting' }
CTArray2DTest >> testAsArray [
| foo |
foo := self arrayClass width: 3 height: 3.
foo atAllPut: 0.
self assert: foo asArray equals: #(0 0 0 0 0 0 0 0 0)
]
{ #category : 'tests-converting' }
CTArray2DTest >> testAsArray2 [
self
assert: self arrayClass width2Height3 asArray
equals: #(1 2 3 4 5 6)
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testAt [
self assert: (self arrayClass width2Height3 at: 1@1) equals: 1.
self assert: (self arrayClass width2Height3 at: 1@2) equals: 3.
self assert: (self arrayClass width2Height3 at: 2@3) equals: 6
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testAtAllPut [
| foo |
foo := self arrayClass width: 5 height: 20.
foo atAllPut: 12.
foo do: [ :each | self assert: each equals: 12 ]
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtColumn [
| foo |
foo := self arrayClass width: 3 height: 5.
foo atColumn: 2 put: #(1 2 3 4 5).
self assert: (foo atColumn: 2) equals: #(1 2 3 4 5)
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtColumnAtRow [
self assert: (self arrayClass width2Height3 atColumn: 1 atRow: 1) equals: 1.
self assert: (self arrayClass width2Height3 atColumn: 1 atRow: 2) equals: 3.
self assert: (self arrayClass width2Height3 atColumn: 2 atRow: 3) equals: 6
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtColumnAtRowPut [
| foo |
foo := self arrayClass width: 5 height: 20.
foo atColumn: 1 atRow: 1 put: 12.
self assert: (foo atColumn: 1 atRow: 1) equals: 12
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtColumnNotInBounds [
| foo |
foo := self arrayClass width: 3 height: 5.
self should: [ foo atColumn: 0 ] raise: SubscriptOutOfBounds.
self should: [ foo atColumn: 6 ] raise: SubscriptOutOfBounds
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtColumnPut [
| foo |
foo := self arrayClass width: 3 height: 5.
foo atColumn: 2 put: #(1 1 1 1 1).
1 to: foo height do: [ :y | self assert: (foo atColumn: 2 atRow: y) equals: 1 ]
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testAtPut [
| foo |
foo := self arrayClass width: 5 height: 20.
foo at: 1@5 put: 12.
self assert: (foo atX: 1 atY: 5) equals: 12
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtRow [
| foo |
foo := self arrayClass width: 5 height: 3.
foo atRow: 2 put: #(1 2 3 4 5).
self assert: (foo atRow: 2) equals: #(1 2 3 4 5)
]
{ #category : 'tests-accessing rows/columns' }
CTArray2DTest >> testAtRowPut [
| foo |
foo := self arrayClass width: 5 height: 3.
foo atRow: 2 put: #(1 1 1 1 1).
1 to: foo width do: [ :x | self assert: (foo atColumn: x atRow: 2) equals: 1 ]
]
{ #category : 'tests-accessing x/y' }
CTArray2DTest >> testAtXAtY [
| matrix |
matrix := self arrayClass width2Height3.
self assert: (matrix atX: 1 atY: 1) equals: 1.
self assert: (matrix atX: 1 atY: 2) equals: 3.
self assert: (matrix atX: 2 atY: 3) equals: 6
]
{ #category : 'tests-accessing x/y' }
CTArray2DTest >> testAtXAtYPut [
| foo |
foo := self arrayClass width: 5 height: 20.
foo atX: 1 atY: 1 put: 12.
self assert: (foo atX: 1 atY: 1) equals: 12
]
{ #category : 'tests-converting' }
CTArray2DTest >> testCannotAccessWithWrongCoordinates [
self should: [ self arrayClass width2Height3 atColumn: 6 atRow: 1 ] raise: SubscriptOutOfBounds
]
{ #category : 'tests' }
CTArray2DTest >> testColumns [
| foo |
foo := self arrayClass width2Height3.
self assert: foo columns equals: #(#(1 3 5) #(2 4 6))
]
{ #category : 'tests' }
CTArray2DTest >> testColumnsDo [
| foo array columnIndex |
foo := self arrayClass width2Height3.
array := Array new: 2.
columnIndex := 1.
foo columnsDo: [ :column |
array at: columnIndex put: column.
columnIndex := columnIndex + 1 ].
self assert: array equals: #(#(1 3 5) #(2 4 6))
]
{ #category : 'tests-copying' }
CTArray2DTest >> testCopy [
| foo cop |
foo := self arrayClass width: 5 height: 3.
foo atAllPut: 0.
cop := foo copy.
self assert: cop width equals: 5.
self assert: cop height equals: 3.
1 to: cop width do: [ :x | 1 to: cop height do: [ :y | self assert: (cop atColumn: x atRow: y) equals: 0 ] ].
self deny: cop identicalTo: foo
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testDo [
| foo nb |
foo := self arrayClass width: 5 height: 3.
foo atAllPut: 0.
foo atColumn: 2 atRow: 2 put: 1.
nb := 0.
"foo looks like that: #( 0 0 0 0 0
0 1 0 0 0
0 0 0 0 0 )"
foo
do: [ :each |
each = 0
ifFalse: [ nb := each ] ].
"nb should be equal to 1"
self assert: nb equals: 1
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testDo2 [
| count |
count := 0.
self arrayClass width2Height3 do: [ :each | each even ifTrue: [ count := count + 1] ].
self assert: count equals: 3
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testExtent [
self assert: (self arrayClass width: 5 height: 20) extent equals: 5 @ 20
]
{ #category : 'tests-private' }
CTArray2DTest >> testExtentFromArray [
| foo |
foo := self arrayClass width: 2 height: 2.
foo atAllPut: 0.
foo extent: 3 @ 2 fromArray: #(1 2 3 4 5 6).
self assert: foo width equals: 3.
self assert: (foo atColumn: 1 atRow: 1) equals: 1.
self assert: (foo atColumn: 2 atRow: 1) equals: 2.
self assert: (foo atColumn: 3 atRow: 1) equals: 3.
self assert: (foo atColumn: 1 atRow: 2) equals: 4.
self assert: (foo atColumn: 2 atRow: 2) equals: 5.
self assert: (foo atColumn: 3 atRow: 2) equals: 6
]
{ #category : 'tests-private' }
CTArray2DTest >> testFromArray [
| a2 |
a2 := self arrayClass fromArray: #( 0 2 4
6 8 10 ) width: 3.
self assert: a2 width equals: 3.
self assert: a2 height equals: 2.
self assert: (a2 atRow: 1) equals: #(0 2 4).
self assert: (a2 atRow: 2) equals: #(6 8 10).
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromBottomToTopFromLeftToRightDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromBottomToTopFromLeftToRightDo: [ :each | res add: each ].
self assert: res equals: #(5 3 1 6 4 2) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromBottomToTopFromRightToLeftDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromBottomToTopFromRightToLeftDo: [ :each | res add: each ].
self assert: res equals: #(6 4 2 5 3 1) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromLeftToRightFromBottomToTopDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromLeftToRightFromBottomToTopDo: [ :each | res add: each ].
self assert: res equals: #(5 6 3 4 1 2) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromLeftToRightFromTopToBottomDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromLeftToRightFromTopToBottomDo: [ :each | res add: each ].
self assert: res equals: #(1 2 3 4 5 6) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromRightToLeftFromBottomToTopDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromRightToLeftFromBottomToTopDo: [ :each | res add: each ].
self assert: res equals: #(6 5 4 3 2 1) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromRightToLeftFromTopToBottomDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromRightToLeftFromTopToBottomDo: [ :each | res add: each ].
self assert: res equals: #(2 1 4 3 6 5) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromTopToBottomFromLeftToRightDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromTopToBottomFromLeftToRightDo: [ :each | res add: each ].
self assert: res equals: #(1 3 5 2 4 6) asOrderedCollection
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testFromTopToBottomFromRightToLeftDo [
|foo res|
foo := self arrayClass width2Height3.
res := OrderedCollection new.
foo fromTopToBottomFromRightToLeftDo: [ :each | res add: each ].
self assert: res equals: #(2 4 6 1 3 5) asOrderedCollection
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testHeight [
self assert: (self arrayClass width: 5 height: 20) height equals: 20.
]
{ #category : 'tests-private' }
CTArray2DTest >> testIndexXY [
| foo |
foo := self arrayClass width: 5 height: 20.
self assert: (foo indexX: 2 y: 3) equals: (3-1)*(foo width)+2
]
{ #category : 'tests-printing' }
CTArray2DTest >> testPrinting [
self
assert: self classUnderTest width2Height3 printString
equals: 'CTArray2D fromArray: #(1 2 3 4 5 6) width: 2'
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testRowAndColumnValuesDo [
| foo array |
array := #(1 2 3 4 5 6).
foo := self classUnderTest width2Height3.
foo rowAndColumnValuesDo: [ :y :x :value |
self assert: (array at: ((y-1) * 2 + x))
equals: value
]
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testRowsAndColumnsDo [
| foo array |
foo := self arrayClass width2Height3.
self shouldnt: [ foo rowsAndColumnsDo: [ :row :col | ] ] raise: Error.
array := #(1 2 3 4 5 6).
foo := self arrayClass width2Height3.
foo rowsAndColumnsDo: [ :y :x | self assert: (foo atColumn: x atRow: y) equals: (array at: (y - 1) * 2 + x) ]
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testRowsAndColumnsPut [
| foo array |
foo := self arrayClass width: 2 height: 3.
self
shouldnt: [ foo rowsAndColumnsPut: [ :row :col | row * 10 + col ] ]
raise: Error.
array := #( 11 12 21 22 31 32 ).
foo rowsAndColumnsDo: [ :y :x |
self
assert: (foo atColumn: x atRow: y)
equals: (array at: y - 1 * 2 + x) ]
]
{ #category : 'tests-private' }
CTArray2DTest >> testSetContents [
| foo |
foo := self arrayClass width: 2 height: 3.
foo setContents: #(1 1 3 1 1 2).
self assert: (foo atRow: 1) equals: #(1 1).
self assert: (foo atRow: 2) equals: #(3 1).
self assert: (foo atRow: 3) equals: #(1 2).
]
{ #category : 'tests-private' }
CTArray2DTest >> testSetWidth [
| foo |
foo := self arrayClass width: 2 height: 3.
foo setWidth: 3.
self assert: foo width equals: 3
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testSize [
| foo |
foo := self arrayClass width: 5 height: 20.
self assert: foo size equals: foo width * foo height
]
{ #category : 'tests-accessing' }
CTArray2DTest >> testWidth [
self assert: (self arrayClass width: 5 height: 20) width
equals: 5
]
{ #category : 'tests-enumeration' }
CTArray2DTest >> testWidthHeightTabulate [
| foo array |
foo := self arrayClass
width: 2
height: 3
tabulate: [ :x :y | y * 10 + x ].
array := #( 11 12 21 22 31 32 ).
foo rowsAndColumnsDo: [ :y :x |
self
assert: (foo atColumn: x atRow: y)
equals: (array at: y - 1 * 2 + x) ]
]
{ #category : 'tests-private' }
CTArray2DTest >> testWidthHeightType [
| foo |
foo := self arrayClass new.
foo width: 3 height: 2 type: Array.
self assert: foo width equals: 3.
self assert: foo size equals: 3 * 2.
foo do: [ :each | self assert: each equals: nil ]
]