tor-browser

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

helper-validation.window.js (6393B)


      1 // META: title=Validate WebNN test helpers
      2 // META: script=resources/utils.js
      3 
      4 // This doesn't validate the WebNN API itself, it just verifies behavior of
      5 // non-trivial helper functions.
      6 
      7 'use strict';
      8 
      9 // Largest integer uniquely representable as a float32.
     10 const MAX_FLOAT32_INTEGER = 2 ** 24;
     11 
     12 test(t => {
     13  const dataType = 'float32';
     14  [[0.0, 0.0, 0n],
     15   [0.0, 1e-46, 0n],
     16   [0.0, 1e-36, 61482021n],
     17   [1.0, 1.0, 0n],
     18   [1.0, -1.0, 2130706432n],
     19   [1.0, 2.0, 8388608n],
     20   [1.000001, 1.000002, 9n],
     21   [1.0000001, 1.0000002, 1n],
     22   [-1.000001, 1.000002, 2130706457n],
     23   [-1.0000001, 1.0000002, 2130706435n],
     24   [0.0, 1.401298464324817e-45, 1n],
     25   [99.28312683105469, 39.03501892089844, 11169050n],
     26   [MAX_FLOAT32_INTEGER - 1, MAX_FLOAT32_INTEGER, 1n],
     27  ].forEach(([a, b, expected]) => {
     28    assert_equals(
     29        ulpDistance(a, b, dataType), expected,
     30        `ULP distance between ${a} and ${b}`);
     31    assert_equals(
     32        ulpDistance(b, a, dataType), expected,
     33        `ULP distance between ${b} and ${a} (commutative)`);
     34    assert_equals(
     35        ulpDistance(-a, -b, dataType), expected,
     36        `ULP distance between ${- a} and ${- b} (negated)`);
     37  });
     38 }, 'ULP Distance - float32');
     39 
     40 // TODO: Add test cases for 'float16' data type.
     41 
     42 test(t => {
     43  const dataType = 'int64';
     44  [[0n, 0n, 0n],
     45   [1n, 0n, 1n],
     46   [1n, 2n, 1n],
     47   [10n, 11n, 1n],
     48   [10n, 20n, 10n],
     49   [100000001n, 100000002n, 1n],
     50   [0x7FFFFFFFFFFFFFFEn, 0x7FFFFFFFFFFFFFFFn, 1n],
     51   [-0x7FFFFFFFFFFFFFFFn, 0x7FFFFFFFFFFFFFFFn, 0xFFFFFFFFFFFFFFFEn],
     52  ].forEach(([a, b, expected]) => {
     53    assert_equals(
     54        ulpDistance(a, b, dataType), expected,
     55        `ULP distance between ${a} and ${b}`);
     56    assert_equals(
     57        ulpDistance(b, a, dataType), expected,
     58        `ULP distance between ${b} and ${a} (commutative)`);
     59    assert_equals(
     60        ulpDistance(-a, -b, dataType), expected,
     61        `ULP distance between ${- a} and ${- b} (negated)`);
     62  });
     63  assert_equals(
     64      ulpDistance(-0x8000000000000000n, 0x7FFFFFFFFFFFFFFFn, dataType),
     65      0xFFFFFFFFFFFFFFFFn, 'ULP distance between min and max int64');
     66 }, 'ULP Distance - int64');
     67 
     68 test(t => {
     69  const dataType = 'uint64';
     70  [[0n, 0n, 0n],
     71   [1n, 0n, 1n],
     72   [1n, 2n, 1n],
     73   [10n, 11n, 1n],
     74   [10n, 20n, 10n],
     75   [100000001n, 100000002, 1n],
     76   [0xFFFFFFFFFFFFFFFEn, 0xFFFFFFFFFFFFFFFFn, 1n],
     77   [0n, 0xFFFFFFFFFFFFFFFFn, 0xFFFFFFFFFFFFFFFFn],
     78  ].forEach(([a, b, expected]) => {
     79    assert_equals(
     80        ulpDistance(a, b, dataType), expected,
     81        `ULP distance between ${a} and ${b}`);
     82    assert_equals(
     83        ulpDistance(b, a, dataType), expected,
     84        `ULP distance between ${b} and ${a} (commutative)`);
     85  });
     86 }, 'ULP Distance - uint64');
     87 
     88 test(t => {
     89  const dataType = 'int32';
     90  [[0, 0, 0],
     91   [1, 0, 1],
     92   [1, 2, 1],
     93   [10, 11, 1],
     94   [10, 20, 10],
     95   [100000001, 100000002, 1],
     96   [0x7FFFFFFE, 0x7FFFFFFF, 1],
     97   [-0x7FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFE],
     98  ].forEach(([a, b, expected]) => {
     99    assert_equals(
    100        ulpDistance(a, b, dataType), expected,
    101        `ULP distance between ${a} and ${b}`);
    102    assert_equals(
    103        ulpDistance(b, a, dataType), expected,
    104        `ULP distance between ${b} and ${a} (commutative)`);
    105    assert_equals(
    106        ulpDistance(-a, -b, dataType), expected,
    107        `ULP distance between ${- a} and ${- b} (negated)`);
    108  });
    109  assert_equals(
    110      ulpDistance(-0x80000000, 0x7FFFFFFF, dataType), 0xFFFFFFFF,
    111      'ULP distance between min and max int32');
    112 }, 'ULP Distance - int32');
    113 
    114 test(t => {
    115  const dataType = 'uint32';
    116  [[0, 0, 0],
    117   [1, 0, 1],
    118   [1, 2, 1],
    119   [10, 11, 1],
    120   [10, 20, 10],
    121   [100000001, 100000002, 1],
    122   [0xFFFFFFFE, 0xFFFFFFFF, 1],
    123   [0, 0xFFFFFFFF, 0xFFFFFFFF],
    124  ].forEach(([a, b, expected]) => {
    125    assert_equals(
    126        ulpDistance(a, b, dataType), expected,
    127        `ULP distance between ${a} and ${b}`);
    128    assert_equals(
    129        ulpDistance(b, a, dataType), expected,
    130        `ULP distance between ${b} and ${a} (commutative)`);
    131  });
    132 }, 'ULP Distance - uint32');
    133 
    134 test(t => {
    135  const dataType = 'int8';
    136  [[0, 0, 0],
    137   [1, 0, 1],
    138   [1, 2, 1],
    139   [10, 11, 1],
    140   [10, 20, 10],
    141   [101, 102, 1],
    142   [0x7E, 0x7F, 1],
    143   [-0x7F, 0x7F, 0xFE],
    144  ].forEach(([a, b, expected]) => {
    145    assert_equals(
    146        ulpDistance(a, b, dataType), expected,
    147        `ULP distance between ${a} and ${b}`);
    148    assert_equals(
    149        ulpDistance(b, a, dataType), expected,
    150        `ULP distance between ${b} and ${a} (commutative)`);
    151    assert_equals(
    152        ulpDistance(-a, -b, dataType), expected,
    153        `ULP distance between ${- a} and ${- b} (negated)`);
    154  });
    155  assert_equals(
    156      ulpDistance(-0x80, 0x7F, dataType), 0xFF,
    157      'ULP distance between min and max int8');
    158 }, 'ULP Distance - int8');
    159 
    160 test(t => {
    161  const dataType = 'uint8';
    162  [[0, 0, 0],
    163   [1, 0, 1],
    164   [1, 2, 1],
    165   [10, 11, 1],
    166   [10, 20, 10],
    167   [101, 102, 1],
    168   [0xFE, 0xFF, 1],
    169   [0, 0xFF, 0xFF],
    170  ].forEach(([a, b, expected]) => {
    171    assert_equals(
    172        ulpDistance(a, b, dataType), expected,
    173        `ULP distance between ${a} and ${b}`);
    174    assert_equals(
    175        ulpDistance(b, a, dataType), expected,
    176        `ULP distance between ${b} and ${a} (commutative)`);
    177  });
    178 }, 'ULP Distance - uint8');
    179 
    180 test(t => {
    181  const dataType = 'int4';
    182  [[0, 0, 0],
    183   [1, 0, 1],
    184   [1, 2, 1],
    185   [10, 11, 1],
    186   [1, 10, 9],
    187   [6, 7, 1],
    188   [-7, 7, 14],
    189  ].forEach(([a, b, expected]) => {
    190    assert_equals(
    191        ulpDistance(a, b, dataType), expected,
    192        `ULP distance between ${a} and ${b}`);
    193    assert_equals(
    194        ulpDistance(b, a, dataType), expected,
    195        `ULP distance between ${b} and ${a} (commutative)`);
    196    assert_equals(
    197        ulpDistance(-a, -b, dataType), expected,
    198        `ULP distance between ${- a} and ${- b} (negated)`);
    199  });
    200  assert_equals(
    201      ulpDistance(-0x8, 0x7, dataType), 0xF,
    202      'ULP distance between min and max int4');
    203 }, 'ULP Distance - int4');
    204 
    205 test(t => {
    206  const dataType = 'uint4';
    207  [[0, 0, 0],
    208   [1, 0, 1],
    209   [1, 2, 1],
    210   [10, 11, 1],
    211   [1, 10, 9],
    212   [0xE, 0xF, 1],
    213   [0, 0xF, 0xF],
    214  ].forEach(([a, b, expected]) => {
    215    assert_equals(
    216        ulpDistance(a, b, dataType), expected,
    217        `ULP distance between ${a} and ${b}`);
    218    assert_equals(
    219        ulpDistance(b, a, dataType), expected,
    220        `ULP distance between ${b} and ${a} (commutative)`);
    221  });
    222 }, 'ULP Distance - uint4');