summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/typedarrays/ArrayBuffer_properties.html
blob: a4472980a210e4f92c326bb8d7d03e38abb1b56c (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Typed Arrays Test: ArrayBuffer properties</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://www.khronos.org/registry/typedarray/specs/latest/#5">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>

test(function () {
  var intLen = 8;
  var abuffer = new ArrayBuffer(intLen);

  //byteLength
  test(function () {
    var len = abuffer.byteLength;
    abuffer.byteLength = len + 1;
    assert_equals(len, intLen, "the byteLength of ArrayBuffer");
    assert_equals(abuffer.byteLength, len, "the byteLength of ArrayBuffer");
  }, "Check if the byteLength is fixed at construction time and readonly");

  //slice()
  test(function () {
    var b = abuffer.slice(2);
    assert_true(b instanceof ArrayBuffer, "Returns a new ArrayBuffer");
    assert_equals(b.byteLength, intLen-2, "The byteLength of ArrayBuffer");
  }, "Check if the new ArrayBuffer contains all bytes from begin to the end of this ArrayBuffer when the end is unspecified");

  test(function () {
    var b = abuffer.slice(2, 6);
    assert_true(b instanceof ArrayBuffer, "Returns a new ArrayBuffer");
    assert_equals(b.byteLength, 4, "The byteLength of ArrayBuffer");
  }, "Check if the new ArrayBuffer contains the bytes from begin to the end of this ArrayBuffer when the end is specified");

  test(function () {
    b = abuffer.slice(2, 10);
    assert_equals(b.byteLength, 6, "The byteLength of ArrayBuffer");
  }, "Check if the slice range specified by the begin and end values is clamped to the valid index range for the current array");

  test(function () {
    var b = abuffer.slice(2, -1);
    assert_equals(b.byteLength, 5, "The byteLength of ArrayBuffer");
    b = abuffer.slice(-2, -1);
    assert_equals(b.byteLength, 1, "The byteLength of ArrayBuffer");
  }, "Check if it refers to an index from the end of the array when either begin or end is negative");

  test(function () {
    var b = abuffer.slice(4, 2);
    assert_equals(b.byteLength, 0, "The byteLength of ArrayBuffer");
    b = abuffer.slice(-1, -2);
    assert_equals(b.byteLength, 0, "The byteLength of ArrayBuffer");
  }, "Check if the length is clamped to zero when the computed length of the new ArrayBuffer is negative");

  //static isView()
  test(function () {
    var int8a = new Int8Array(8);
    assert_true(ArrayBuffer.isView(int8a), "The return value of ArrayBufferView.isView");
  }, "Check if the isView returns true when the value is an object implementing the ArrayBufferView interface");

  test(function () {
    var arr = new Array(8);
    assert_false(ArrayBuffer.isView(arr), "The return value of ArrayBufferView.isView");
  }, "Check if the isView returns false when the value is an object not implementing the ArrayBufferView interface");
});

</script>