tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

build-more-than-once.https.any.js (3680B)


      1 // META: title=ensure MLMLGraphBuilder may build at most one MLGraph
      2 // META: global=window,worker
      3 // META: variant=?cpu
      4 // META: variant=?gpu
      5 // META: variant=?npu
      6 // META: script=../resources/utils_validation.js
      7 
      8 const kExampleInputDescriptor = {
      9  dataType: 'float32',
     10  shape: [2]
     11 };
     12 
     13 promise_test(async t => {
     14  const builder = new MLGraphBuilder(context);
     15  const a = builder.input('a', kExampleInputDescriptor);
     16  const b = builder.input('b', kExampleInputDescriptor);
     17  const c = builder.add(a, b);
     18  const graph = await builder.build({c});
     19 
     20  await promise_rejects_dom(t, 'InvalidStateError', builder.build({c}));
     21 }, 'Throw if attempting to build a second graph with an MLGraphBuilder');
     22 
     23 promise_test(async t => {
     24  const builder = new MLGraphBuilder(context);
     25  const a = builder.input('a', kExampleInputDescriptor);
     26  const b = builder.input('b', kExampleInputDescriptor);
     27  const c = builder.add(a, b);
     28  const graph_promise_not_awaited = builder.build({c});
     29 
     30  await promise_rejects_dom(t, 'InvalidStateError', builder.build({c}));
     31 }, 'Throw if attempting to build a second graph without awaiting the first');
     32 
     33 promise_test(async t => {
     34  const builder = new MLGraphBuilder(context);
     35  const a = builder.input('a', kExampleInputDescriptor);
     36  const b = builder.input('b', kExampleInputDescriptor);
     37  const c = builder.add(a, b);
     38  const graph = await builder.build({c});
     39 
     40  assert_throws_dom('InvalidStateError', () => builder.sub(a, b));
     41 }, 'Throw if an operand-yielding method is called on a built MLGraphBuilder');
     42 
     43 promise_test(async t => {
     44  const builder = new MLGraphBuilder(context);
     45  const a = builder.input('a', kExampleInputDescriptor);
     46  const b = builder.input('b', kExampleInputDescriptor);
     47  const c = builder.add(a, b);
     48  const graph = await builder.build({c});
     49 
     50  assert_throws_dom(
     51      'InvalidStateError', () => builder.input('d', kExampleInputDescriptor));
     52 }, 'Throw if adding an input operand to a built MLGraphBuilder');
     53 
     54 promise_test(async t => {
     55  const builder = new MLGraphBuilder(context);
     56  const a = builder.input('a', kExampleInputDescriptor);
     57  const b = builder.input('b', kExampleInputDescriptor);
     58  const c = builder.add(a, b);
     59  const graph = await builder.build({c});
     60 
     61  const buffer = new ArrayBuffer(8);
     62  const bufferView = new Float32Array(buffer);
     63 
     64  assert_throws_dom(
     65      'InvalidStateError',
     66      () => builder.constant(kExampleInputDescriptor, bufferView));
     67 }, 'Throw if adding a constant operand to a built MLGraphBuilder');
     68 
     69 promise_test(async t => {
     70  const builder = new MLGraphBuilder(context);
     71  const a = builder.input('a', kExampleInputDescriptor);
     72  const b = builder.input('b', kExampleInputDescriptor);
     73  const c = builder.add(a, b);
     74 
     75  // Call build() with invalid parameters.
     76  await promise_rejects_js(t, TypeError, builder.build({a}));
     77 
     78  // Passing valid parameters successfully creates the graph...
     79  const graph = await builder.build({c});
     80 
     81  // ...exactly once!
     82  await promise_rejects_dom(t, 'InvalidStateError', builder.build({c}));
     83 }, 'An MLGraphBuilder remains unbuilt if build() is called with invalid paramaters');
     84 
     85 promise_test(async t => {
     86  const builder1 = new MLGraphBuilder(context);
     87  const builder2 = new MLGraphBuilder(context);
     88 
     89  const a1 = builder1.input('a', kExampleInputDescriptor);
     90  const b1 = builder1.input('b', kExampleInputDescriptor);
     91  const c1 = builder1.add(a1, b1);
     92  const graph1 = await builder1.build({c1});
     93 
     94  const a2 = builder2.input('a', kExampleInputDescriptor);
     95  const b2 = builder2.input('b', kExampleInputDescriptor);
     96  const c2 = builder2.add(a2, b2);
     97  const graph2 = await builder2.build({c2});
     98 }, 'Build two graphs with separate MLGraphBuilders');