summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/typedarray/TypedArrayTest.scala
blob: 1dfbbd47e9a0e9e0f753a186ef37c169281c06c8 (plain) (blame)
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
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.typedarray

import org.scalajs.jasminetest.JasmineTest
import org.scalajs.jasmine.JasmineExpectation

import scala.scalajs.js
import js.typedarray._

/** Shallow test for TypedArrays. Basically just tests that the method exist and
 *  return something which could be a right result. It is probably sufficient to
 *  test whether a runtime supports TypedArrays.
 */
object TypedArrayTest extends JasmineTest {

  /** Generalized tests for all TypedArrays */
  def tests[V, T <: TypedArray[V, T]](name: String,
      bytesPerElement: => Int,
      lenCtor: Int => T,
      tarrCtor: TypedArray[_, _] => T,
      arrCtor: js.Array[_] => T,
      bufCtor1: (ArrayBuffer) => T,
      bufCtor2: (ArrayBuffer, Int) => T,
      bufCtor3: (ArrayBuffer, Int, Int) => T,
      hasType: Any => Boolean,
      expectV: V => JasmineExpectation,
      intToV: Int => V
  ) = {

    when("typedarray").
    describe(name) {

      it(s"should allow constructing a new $name with length") {
        val x = lenCtor(10)
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(10)
      }

      it(s"should allow constructing a new $name from an Int8Array") {
        val x = tarrCtor(new Float32Array(js.Array(3, 7)))
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(2)

        expectV(x(0)).toEqual(3)
        expectV(x(1)).toEqual(7)
      }

      it(s"should allow constructing a new $name from a js.Array") {
        val x = arrCtor(js.Array(5,6,7))
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(3)

        expectV(x(0)).toEqual(5)
        expectV(x(1)).toEqual(6)
        expectV(x(2)).toEqual(7)
      }

      it(s"should allow constructing a new $name from an ArrayBuffer (1 arg)") {
        val buf = arrCtor(js.Array(5, 6, 7, 8)).buffer
        val x = bufCtor1(buf)
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(4)

        expectV(x(0)).toEqual(5)
        expectV(x(1)).toEqual(6)
        expectV(x(2)).toEqual(7)
        expectV(x(3)).toEqual(8)
      }

      it(s"should allow constructing a new $name from an ArrayBuffer (2 arg)") {
        val buf = arrCtor(js.Array(5, 6, 7, 8)).buffer
        val x = bufCtor2(buf, bytesPerElement)
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(3)

        expectV(x(0)).toEqual(6)
        expectV(x(1)).toEqual(7)
        expectV(x(2)).toEqual(8)
      }

      it(s"should allow constructing a new $name from an ArrayBuffer (3 arg)") {
        val buf = arrCtor(js.Array(5, 6, 7, 8)).buffer
        val x = bufCtor3(buf, bytesPerElement, 2)
        expect(hasType(x)).toBeTruthy
        expect(x.length).toBe(2)

        expectV(x(0)).toEqual(6)
        expectV(x(1)).toEqual(7)
      }

      it("should allow retrieving the length") {
        val x = lenCtor(100)
        expect(x.length).toBe(100)
      }

      it("should allow retrieving an element") {
        val x = arrCtor(js.Array(5))
        expectV(x(0)).toBe(5)
      }

      it("should allow setting an element") {
        val x = lenCtor(2)

        x(0) = intToV(5)
        x(1) = intToV(10)

        expectV(x(0)).toBe(5)
        expectV(x(1)).toBe(10)
      }

      it("should provide `get`") {
        val x = arrCtor(js.Array(10))
        expectV(x.get(0)).toBe(10)
      }

      it("should provide `set` for a single element") {
        val x = lenCtor(10)
        x.set(0, intToV(5))
        x.set(1, intToV(6))

        expectV(x(0)).toBe(5)
        expectV(x(1)).toBe(6)
        expectV(x(2)).toBe(0)
      }

      it("should provide `set` for a js.Array with one arguments") {
        val x = lenCtor(10)
        x.set(js.Array(5,6,7))
        expectV(x(0)).toBe(5)
        expectV(x(1)).toBe(6)
        expectV(x(2)).toBe(7)
        expectV(x(3)).toBe(0)
        expectV(x(4)).toBe(0)
        expectV(x(5)).toBe(0)
      }

      it("should provide `set` for a js.Array with two arguments") {
        val x = lenCtor(10)
        x.set(js.Array(5,6,7), 2)
        expectV(x(0)).toBe(0)
        expectV(x(1)).toBe(0)
        expectV(x(2)).toBe(5)
        expectV(x(3)).toBe(6)
        expectV(x(4)).toBe(7)
        expectV(x(5)).toBe(0)
      }

      it("should provide `set` for a TypedArray with one argument") {
        val x = lenCtor(10)
        x.set(new Int8Array(js.Array(5,6,7)))
        expectV(x(0)).toBe(5)
        expectV(x(1)).toBe(6)
        expectV(x(2)).toBe(7)
        expectV(x(3)).toBe(0)
        expectV(x(4)).toBe(0)
        expectV(x(5)).toBe(0)
      }

      it("should provide `set` for a TypedArray with two arguments") {
        val x = lenCtor(10)
        x.set(new Int8Array(js.Array(5,6,7)), 2)
        expectV(x(0)).toBe(0)
        expectV(x(1)).toBe(0)
        expectV(x(2)).toBe(5)
        expectV(x(3)).toBe(6)
        expectV(x(4)).toBe(7)
        expectV(x(5)).toBe(0)
      }

      it("should provide `subarray` with one argument") {
        val x = arrCtor(js.Array(1,2,3,4,5,6,7,8,9))
        val y = x.subarray(2)

        expect(y.length).toBe(7)
        expectV(y(0)).toBe(3)

        x(2) = intToV(100)

        expectV(y(0)).toBe(100)
      }

      it("should provide `subarray` with two arguments") {
        val x = arrCtor(js.Array(1,2,3,4,5,6,7,8,9))
        val y = x.subarray(2, 4)

        expect(y.length).toBe(2)
        expectV(y(0)).toBe(3)

        x(2) = intToV(100)

        expectV(y(0)).toBe(100)
      }

      it("should provide `buffer`") {
        val x = arrCtor(js.Array(1,2,3,4,5,6,7,8,9))
        val y = bufCtor3(x.buffer, 0, 2)

        expect(x.buffer eq y.buffer).toBeTruthy
      }

      it("should provide `byteLength`") {
        val x = arrCtor(js.Array(0 until bytesPerElement * 4: _*))
        val y = bufCtor3(x.buffer, bytesPerElement, 3)

        expect(y.byteLength).toBe(3 * bytesPerElement)
      }

      it("should provide `byteOffset`") {
        val x = arrCtor(js.Array(0 until bytesPerElement * 4: _*))
        val y = bufCtor3(x.buffer, bytesPerElement, 3)

        expect(y.byteOffset).toBe(bytesPerElement)
      }

    }
  }

  tests("Int8Array",
      Int8Array.BYTES_PER_ELEMENT,
      len => new Int8Array(len),
      tarr => new Int8Array(tarr),
      arr => new Int8Array(arr),
      buf => new Int8Array(buf),
      (buf, start) => new Int8Array(buf, start),
      (buf, start, end) => new Int8Array(buf, start, end),
      _.isInstanceOf[Int8Array],
      expect (_: Byte),
      _.toByte)

  tests("Uint8Array",
      Uint8Array.BYTES_PER_ELEMENT,
      len => new Uint8Array(len),
      tarr => new Uint8Array(tarr),
      arr => new Uint8Array(arr),
      buf => new Uint8Array(buf),
      (buf, start) => new Uint8Array(buf, start),
      (buf, start, end) => new Uint8Array(buf, start, end),
      _.isInstanceOf[Uint8Array],
      expect (_: Short),
      _.toShort)

  tests("Uint8ClampedArray",
      Uint8ClampedArray.BYTES_PER_ELEMENT,
      len => new Uint8ClampedArray(len),
      tarr => new Uint8ClampedArray(tarr),
      arr => new Uint8ClampedArray(arr),
      buf => new Uint8ClampedArray(buf),
      (buf, start) => new Uint8ClampedArray(buf, start),
      (buf, start, end) => new Uint8ClampedArray(buf, start, end),
      _.isInstanceOf[Uint8ClampedArray],
      expect (_: Int),
      _.toInt)

  tests("Int16Array",
      Int16Array.BYTES_PER_ELEMENT,
      len => new Int16Array(len),
      tarr => new Int16Array(tarr),
      arr => new Int16Array(arr),
      buf => new Int16Array(buf),
      (buf, start) => new Int16Array(buf, start),
      (buf, start, end) => new Int16Array(buf, start, end),
      _.isInstanceOf[Int16Array],
      expect (_: Short),
      _.toShort)

  tests("Uint16Array",
      Uint16Array.BYTES_PER_ELEMENT,
      len => new Uint16Array(len),
      tarr => new Uint16Array(tarr),
      arr => new Uint16Array(arr),
      buf => new Uint16Array(buf),
      (buf, start) => new Uint16Array(buf, start),
      (buf, start, end) => new Uint16Array(buf, start, end),
      _.isInstanceOf[Uint16Array],
      expect (_: Int),
      _.toInt)

  tests("Int32Array",
      Int32Array.BYTES_PER_ELEMENT,
      len => new Int32Array(len),
      tarr => new Int32Array(tarr),
      arr => new Int32Array(arr),
      buf => new Int32Array(buf),
      (buf, start) => new Int32Array(buf, start),
      (buf, start, end) => new Int32Array(buf, start, end),
      _.isInstanceOf[Int32Array],
      expect (_: Int),
      _.toInt)

  tests("Uint32Array",
      Uint32Array.BYTES_PER_ELEMENT,
      len => new Uint32Array(len),
      tarr => new Uint32Array(tarr),
      arr => new Uint32Array(arr),
      buf => new Uint32Array(buf),
      (buf, start) => new Uint32Array(buf, start),
      (buf, start, end) => new Uint32Array(buf, start, end),
      _.isInstanceOf[Uint32Array],
      expect (_: Double),
      _.toDouble)

  tests("Float32Array",
      Float32Array.BYTES_PER_ELEMENT,
      len => new Float32Array(len),
      tarr => new Float32Array(tarr),
      arr => new Float32Array(arr),
      buf => new Float32Array(buf),
      (buf, start) => new Float32Array(buf, start),
      (buf, start, end) => new Float32Array(buf, start, end),
      _.isInstanceOf[Float32Array],
      expect (_: Float),
      _.toFloat)

  tests("Float64Array",
      Float64Array.BYTES_PER_ELEMENT,
      len => new Float64Array(len),
      tarr => new Float64Array(tarr),
      arr => new Float64Array(arr),
      buf => new Float64Array(buf),
      (buf, start) => new Float64Array(buf, start),
      (buf, start, end) => new Float64Array(buf, start, end),
      _.isInstanceOf[Float64Array],
      expect (_: Double),
      _.toDouble)

}