aboutsummaryrefslogtreecommitdiffstats
path: root/harness/testAtomics.js
blob: 81f6af3a60226e28a75abd43e02384e3cb13a6c6 (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
// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
    Collection of functions used to assert the correctness of SharedArrayBuffer objects.
---*/


/**
 * Calls the provided function for a each bad index that should throw a
 * RangeError when passed to an Atomics method on a SAB-backed view where
 * index 125 is out of range.
 *
 * @param f - the function to call for each bad index.
 */
function testWithAtomicsOutOfBoundsIndices(f) {
  var bad_indices = [
    (view) => -1,
    (view) => view.length,
    (view) => view.length*2,
    (view) => Number.POSITIVE_INFINITY,
    (view) => Number.NEGATIVE_INFINITY,
    (view) => ({ valueOf: () => 125 }),
    (view) => ({ toString: () => '125', valueOf: false }) // non-callable valueOf triggers invocation of toString
  ];

  for (let IdxGen of bad_indices) {
    try {
      f(IdxGen);
    } catch (e) {
      e.message += " (Testing with index gen " + IdxGen + ".)";
      throw e;
    }
  }
}

/**
 * Calls the provided function for each good index that should not throw when
 * passed to an Atomics method on a SAB-backed view.
 *
 * The view must have length greater than zero.
 *
 * @param f - the function to call for each good index.
 */
function testWithAtomicsInBoundsIndices(f) {
  // Most of these are eventually coerced to +0 by ToIndex.
  var good_indices = [
    (view) => 0/-1,
    (view) => '-0',
    (view) => undefined,
    (view) => NaN,
    (view) => 0.5,
    (view) => '0.5',
    (view) => -0.9,
    (view) => ({ password: "qumquat" }),
    (view) => view.length - 1,
    (view) => ({ valueOf: () => 0 }),
    (view) => ({ toString: () => '0', valueOf: false }) // non-callable valueOf triggers invocation of toString
  ];

  for (let IdxGen of good_indices) {
    try {
      f(IdxGen);
    } catch (e) {
      e.message += " (Testing with index gen " + IdxGen + ".)";
      throw e;
    }
  }
}

/**
 * Calls the provided function for each value that should throw a TypeError
 * when passed to an Atomics method as a view.
 *
 * @param f - the function to call for each non-view value.
 */

function testWithAtomicsNonViewValues(f) {
  var values = [
    null,
    undefined,
    true,
    false,
    new Boolean(true),
    10,
    3.14,
    new Number(4),
    "Hi there",
    new Date,
    /a*utomaton/g,
    { password: "qumquat" },
    new DataView(new ArrayBuffer(10)),
    new ArrayBuffer(128),
    new SharedArrayBuffer(128),
    new Error("Ouch"),
    [1,1,2,3,5,8],
    ((x) => -x),
    new Map(),
    new Set(),
    new WeakMap(),
    new WeakSet(),
    Symbol("halleluja"),
    // TODO: Proxy?
    Object,
    Int32Array,
    Date,
    Math,
    Atomics
  ];

  for (let nonView of values) {
    try {
      f(nonView);
    } catch (e) {
      e.message += " (Testing with non-view value " + nonView + ".)";
      throw e;
    }
  }
}