[View] [Edit] [Lock] [References] [Attachments] [History] [Home] [Changes] [Search] [Help]
sunit[library] TypedArrays - 01 Views
TestCase
subclass: #TestTypedArray
category: 'sunit-library'!
! TestTypedArray methodsFor: #constants !
int32View: multiplier on: buffer
| int32View |
int32View := Int32Array on: buffer.
1 to: int32View size do: [:i|
int32View at: i put: i * multiplier
].
^int32View! !
! TestTypedArray methodsFor: #test !
testBPE
self assert: Int8Array bytesPerElement = 1.
self assert: Uint8Array bytesPerElement = 1.
self assert: Uint8ClampedArray bytesPerElement = 1.
self assert: Int16Array bytesPerElement = 2.
self assert: Uint16Array bytesPerElement = 2.
self assert: Int32Array bytesPerElement = 4.
self assert: Uint32Array bytesPerElement = 4.
self assert: Float32Array bytesPerElement = 4.
self assert: Float64Array bytesPerElement = 8.! !
! TestTypedArray methodsFor: #test !
test1
| view |
view := self int32View: 2 on: (ArrayBuffer new: 16).
self assert: view asArray = #(2 4 6 8)! !
! TestTypedArray methodsFor: #test !
test2
| buffer view |
buffer := ArrayBuffer new: 16.
self int32View: 2 on: buffer.
view := Int16Array on: buffer.
self assert: (view asArray copyFrom: 1 to: 2) = #(2 0)! !
! TestTypedArray methodsFor: #test !
testBuffer
| buffer view |
buffer := ArrayBuffer new: 16.
view := self int32View: 2 on: buffer.
self assert: view buffer size = 16! !
! TestTypedArray methodsFor: #test !
testByteLength
| buffer view |
buffer := ArrayBuffer new: 8.
view := Uint8Array on: buffer.
self assert: view byteLength = 8.
view := Uint8Array on: buffer from: 1 length: 5.
self assert: view byteLength = 5.
view := Uint8Array on: buffer from: 2.
self assert: view byteLength = 6.! !
! TestTypedArray methodsFor: #test !
testByteOffset
| buffer view |
buffer := ArrayBuffer new: 8.
view := Uint8Array on: buffer.
self assert: view byteOffset = 0.
view := Uint8Array on: buffer from: 3.
self assert: view byteOffset = 3.! !
! TestTypedArray methodsFor: #test !
testSize
| buffer view |
buffer := ArrayBuffer new: 8.
view := Uint8Array on: buffer.
self assert: view size = 8.
view := Uint8Array on: buffer from: 1 length: 5.
self assert: view size = 5.
view := Uint8Array on: buffer from: 2.
self assert: view size = 6.! !
! TestTypedArray methodsFor: #test !
testCopyWithin
| buffer view |
buffer := ArrayBuffer new: 8.
view := Uint8Array on: buffer.
view set: #(1 2 3).
view copyWithin: 3 start: 0 end: 3.
self assert: view asArray = #(1 2 3 1 2 3 0 0).! !
! TestTypedArray methodsFor: #test !
testEvery
| view |
view := Uint8Array new: #(12 5 8 130 44).
self assert: (view every: [:each| each >= 10]) not.
view := Uint8Array new: #(12 54 18 130 44).
self assert: (view every: [:each| each >= 10]).! !
! TestTypedArray methodsFor: #test !
testFill
self assert: ((Uint8Array new: #(1 2 3))
fill: 4; yourself) asArray = #(4 4 4).
self assert: ((Uint8Array new: #(1 2 3))
fill: 4 start:1; yourself) asArray = #(1 4 4).
self assert: ((Uint8Array new: #(1 2 3))
fill: 4 start: 1 end: 2; yourself) asArray = #(1 4 3).
self assert: ((Uint8Array new: #(1 2 3))
fill: 4 start: 1 end: 1; yourself) asArray = #(1 2 3).
self assert: ((Uint8Array new: #(1 2 3))
fill: 4 start: -3 end: -2; yourself) asArray = #(4 2 3).! !
! TestTypedArray methodsFor: #test !
testFilter
self assert: ((Uint8Array new: #(0 10 5 20 30))
filter: [:x| x > 10]) asArray = #(20 30).! !
! TestTypedArray methodsFor: #test !
testFind
self assert: ((Uint8Array new: #(4 5 8 12))
find: [:x| x > 5 ]) = 8.! !
! TestTypedArray methodsFor: #test !
testFindIndex
self assert: ((Uint8Array new: #(4 5 8 12))
findIndex: [:x| x > 5 ]) = 2.
self assert: ((Uint8Array new: #(4 5 8 12))
findIndex: [:x| x > 500 ]) = -1.! !
! TestTypedArray methodsFor: #test !
testForEach
(Uint8Array new: #(0 2 4 6)) forEach: [:x :offset|
self assert: offset * 2 = x
]! !
! TestTypedArray methodsFor: #test !
testIncludes
self assert: ((Uint8Array new: #(1 2 3)) includes: 2).
self assert: ((Uint8Array new: #(1 2 3)) includes: 4) not.
self assert: ((Uint8Array new: #(1 2 3)) includes: 3 fromOffset: 3) not.! !
! TestTypedArray methodsFor: #test !
testIndexOf
self assert: ((Uint8Array new: #(1 2 3)) indexOf: 2) = 1.
self assert: ((Uint8Array new: #(1 2 3)) indexOf: 7) = -1.
self assert: ((Uint8Array new: #(2 5 9)) indexOf: 9 fromOffset: 2) = 2.! !
! TestTypedArray methodsFor: #test !
testJoin
self assert: (Uint8Array new: #(1 2 3)) join = '1,2,3'.
self assert: ((Uint8Array new: #(1 2 3)) join: ' / ') = '1 / 2 / 3'.
self assert: ((Uint8Array new: #(2 5 9)) join: '') = #259.! !
! TestTypedArray methodsFor: #test !
testLastIndexOf
| view |
view := Uint8Array new: #(2 5 9 2).
self assert: (view lastIndexOf: 2) = 3.
self assert: (view lastIndexOf: 7) = -1.
self assert: (view lastIndexOf: 2 fromOffset: 3) = 3.
self assert: (view lastIndexOf: 2 fromOffset: 2) = 0.
self assert: (view lastIndexOf: 2 fromOffset: -2) = 0.
self assert: (view lastIndexOf: 2 fromOffset: -1) = 3.! !
! TestTypedArray methodsFor: #test !
testMap
| view |
view := Uint8Array new: #(1 4 9).
self assert: (view map: [:x| x sqrt ]) asArray = #(1 2 3).! !
! TestTypedArray methodsFor: #test !
testReduce
| view |
view := Uint8Array new: #(0 1 2 3).
self assert: (view reduce: [:a :b| a + b ]) = 6.! !
! TestTypedArray methodsFor: #test !
testReverse
| array view |
array := #( 1 205 4 5 9 ).
view := Uint8Array new: array.
view reverse.
self assert: view asArray = array reversed.! !
! TestTypedArray methodsFor: #test !
testReduce
| view |
view := Uint8Array on: (ArrayBuffer new: 8).
view set: #(1 2 3) offset: 3.
self assert: view asArray = #( 0 0 0 1 2 3 0 0 ).! !
! TestTypedArray methodsFor: #test !
testSlice
| view |
view := Uint8Array new: #(1 2 3).
self assert: (view slice: 1) asArray = #(2 3).
self assert: (view slice: 2) asArray = #(3).
self assert: (view slice: -2) asArray = #(2 3).
self assert: (view slice: 0 end: 1) asArray = #(1).! !
! TestTypedArray methodsFor: #test !
testSome
| view |
view := Uint8Array new: #(2 5 8 1 4).
self assert: (view some: [:x| x > 10]) not.
self assert: (view some: [:x| x > 6]).! !
! TestTypedArray methodsFor: #test !
testSort
| view |
view := Uint8Array new: #(40 1 5 200).
view sort.
self assert: view asArray = #(1 5 40 200).
self assert: (view some: [:x| x > 6]).! !
! TestTypedArray methodsFor: #test !
testSubarray
| view |
view := Uint8Array new: #(1 2 3 0 0 0 0 0).
self assert: (view subarray: 0 end: 4) asArray = #(1 2 3 0).
self assert: (view subarray: 2) asArray = #(3 0 0 0 0 0).! !
! TestTypedArray methodsFor: #test !
testToLocaleString
| view |
view := Uint32Array new: #(2000 500 8123 12 4212).
self assert: view toLocaleString = '2.000,500,8.123,12,4.212'.
self assert: (view toLocaleString: #en-US) = '2.000,500,8.123,12,4.212'.
" self assert: (view
toLocaleString: #ja-JP
options: (#style -> #currency
,(#currency -> #JPY))
) = '¥2,000,¥500,¥8,123,¥12,¥4,212'."! !