tor-browser

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

inputs-with-special-names.https.any.js (3351B)


      1 // META: title=test input with special character names
      2 // META: global=window
      3 // META: variant=?cpu
      4 // META: variant=?gpu
      5 // META: variant=?npu
      6 // META: script=../resources/utils.js
      7 // META: timeout=long
      8 
      9 'use strict';
     10 
     11 // https://www.w3.org/TR/webnn/#api-mlgraphbuilder-input
     12 
     13 let mlContext;
     14 
     15 // Skip tests if WebNN is unimplemented.
     16 promise_setup(async () => {
     17  assert_implements(navigator.ml, 'missing navigator.ml');
     18  mlContext = await navigator.ml.createContext(contextOptions);
     19 });
     20 
     21 const specialNameArray = [
     22  '12-L#!.☺',
     23  '🤦🏼‍♂️124DS#!F',
     24 
     25  // Escape Sequence
     26  'hello\n\t\r\b\f\v\'\"\0\\webnn',
     27  '\0',
     28  '\0startWithNullCharacter',
     29 
     30  // Hexadecimal Escape Sequences
     31  // '\x41'→ 'A'
     32  '\x41\x41\x41',
     33 
     34  // Unicode & Hexadecimal Characters
     35  //   "\u00A9" → "©"
     36  //   "\xA9" → "©"
     37  //   "\u2665" → "♥"
     38  //   "\u2026" → "…"
     39  //   "\U0001F600" → 😀 (Grinning Face Emoji)
     40  '\u00A9\xA9\u2665\u2026',
     41  '\U0001F600'
     42 ];
     43 
     44 specialNameArray.forEach((name) => {
     45  promise_test(async () => {
     46    const builder = new MLGraphBuilder(mlContext);
     47    const inputOperand = builder.input(name, {dataType: 'float32', shape: [4]});
     48    const outputOperand = builder.abs(inputOperand);
     49 
     50    const [inputTensor, outputTensor, mlGraph] = await Promise.all([
     51      mlContext.createTensor({
     52        dataType: 'float32',
     53        shape: [4],
     54        readable: true,
     55        writable: true,
     56      }),
     57      mlContext.createTensor({dataType: 'float32', shape: [4], readable: true}),
     58      builder.build({'output': outputOperand})
     59    ]);
     60 
     61    const inputData = Float32Array.from([-2, -1, 1, 2]);
     62    mlContext.writeTensor(inputTensor, inputData);
     63 
     64    const inputs = {};
     65    inputs[name] = inputTensor;
     66 
     67    mlContext.dispatch(mlGraph, inputs, {'output': outputTensor});
     68 
     69    // Wait for graph execution to complete.
     70    await mlContext.readTensor(outputTensor);
     71 
     72    assert_array_equals(
     73        new Float32Array(await mlContext.readTensor(outputTensor)),
     74        Float32Array.from([2, 1, 1, 2]));
     75  }, `abs input with special character name '${name}'`);
     76 });
     77 
     78 promise_test(async () => {
     79  const builder = new MLGraphBuilder(mlContext);
     80  const inputA = builder.input('input\0a', { dataType: 'float32', shape: [2] });
     81  const inputB = builder.input('input\0b', { dataType: 'float32', shape: [2] });
     82  const output = builder.add(inputA, inputB);
     83 
     84  const [inputATensor, inputBTensor, outputTensor, mlGraph] = await Promise.all([
     85    mlContext.createTensor({ dataType: 'float32', shape: [2], writable: true }),
     86    mlContext.createTensor({ dataType: 'float32', shape: [2], writable: true }),
     87    mlContext.createTensor({ dataType: 'float32', shape: [2], readable: true }),
     88    builder.build({ 'output': output })
     89  ]);
     90 
     91  const inputAData = Float32Array.from([1, 1]);
     92  const inputBData = Float32Array.from([2, 2]);
     93  mlContext.writeTensor(inputATensor, inputAData);
     94  mlContext.writeTensor(inputBTensor, inputBData);
     95 
     96  const inputs = { 'input\0a': inputATensor, 'input\0b': inputBTensor };
     97  mlContext.dispatch(mlGraph, inputs, { 'output': outputTensor });
     98 
     99  // Wait for graph execution to complete.
    100  await mlContext.readTensor(outputTensor);
    101 
    102  assert_array_equals(
    103    new Float32Array(await mlContext.readTensor(outputTensor)),
    104    Float32Array.from([3, 3]));
    105 }, `[add] inputs with null character name in the middle`);