summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/readable-streams/floating-point-total-queue-size.any.js
blob: 8b88c21d7f0b531266bbec7cb6d764b51d63db66 (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
// META: global=window,worker,shadowrealm
'use strict';

// Due to the limitations of floating-point precision, the calculation of desiredSize sometimes gives different answers
// than adding up the items in the queue would. It is important that implementations give the same result in these edge
// cases so that developers do not come to depend on non-standard behaviour. See
// https://github.com/whatwg/streams/issues/582 and linked issues for further discussion.

promise_test(() => {
  const { reader, controller } = setupTestStream();

  controller.enqueue(2);
  assert_equals(controller.desiredSize, 0 - 2, 'desiredSize must be -2 after enqueueing such a chunk');

  controller.enqueue(Number.MAX_SAFE_INTEGER);
  assert_equals(controller.desiredSize, 0 - Number.MAX_SAFE_INTEGER - 2,
    'desiredSize must be calculated using double-precision floating-point arithmetic (adding a second chunk)');

  return reader.read().then(() => {
    assert_equals(controller.desiredSize, 0 - Number.MAX_SAFE_INTEGER - 2 + 2,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a chunk)');

    return reader.read();
  }).then(() => {
    assert_equals(controller.desiredSize, 0, '[[queueTotalSize]] must clamp to 0 if it becomes negative');
  });
}, 'Floating point arithmetic must manifest near NUMBER.MAX_SAFE_INTEGER (total ends up positive)');

promise_test(() => {
  const { reader, controller } = setupTestStream();

  controller.enqueue(1e-16);
  assert_equals(controller.desiredSize, 0 - 1e-16, 'desiredSize must be -1e16 after enqueueing such a chunk');

  controller.enqueue(1);
  assert_equals(controller.desiredSize, 0 - 1e-16 - 1,
    'desiredSize must be calculated using double-precision floating-point arithmetic (adding a second chunk)');

  return reader.read().then(() => {
    assert_equals(controller.desiredSize, 0 - 1e-16 - 1 + 1e-16,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a chunk)');

    return reader.read();
  }).then(() => {
    assert_equals(controller.desiredSize, 0, '[[queueTotalSize]] must clamp to 0 if it becomes negative');
  });
}, 'Floating point arithmetic must manifest near 0 (total ends up positive, but clamped)');

promise_test(() => {
  const { reader, controller } = setupTestStream();

  controller.enqueue(1e-16);
  assert_equals(controller.desiredSize, 0 - 1e-16, 'desiredSize must be -2e16 after enqueueing such a chunk');

  controller.enqueue(1);
  assert_equals(controller.desiredSize, 0 - 1e-16 - 1,
    'desiredSize must be calculated using double-precision floating-point arithmetic (adding a second chunk)');

  controller.enqueue(2e-16);
  assert_equals(controller.desiredSize, 0 - 1e-16 - 1 - 2e-16,
    'desiredSize must be calculated using double-precision floating-point arithmetic (adding a third chunk)');

  return reader.read().then(() => {
    assert_equals(controller.desiredSize, 0 - 1e-16 - 1 - 2e-16 + 1e-16,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a chunk)');

    return reader.read();
  }).then(() => {
    assert_equals(controller.desiredSize, 0 - 1e-16 - 1 - 2e-16 + 1e-16 + 1,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a second chunk)');

    return reader.read();
  }).then(() => {
    assert_equals(controller.desiredSize, 0 - 1e-16 - 1 - 2e-16 + 1e-16 + 1 + 2e-16,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a third chunk)');
  });
}, 'Floating point arithmetic must manifest near 0 (total ends up positive, and not clamped)');

promise_test(() => {
  const { reader, controller } = setupTestStream();

  controller.enqueue(2e-16);
  assert_equals(controller.desiredSize, 0 - 2e-16, 'desiredSize must be -2e16 after enqueueing such a chunk');

  controller.enqueue(1);
  assert_equals(controller.desiredSize, 0 - 2e-16 - 1,
    'desiredSize must be calculated using double-precision floating-point arithmetic (adding a second chunk)');

  return reader.read().then(() => {
    assert_equals(controller.desiredSize, 0 - 2e-16 - 1 + 2e-16,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a chunk)');

    return reader.read();
  }).then(() => {
    assert_equals(controller.desiredSize, 0,
      'desiredSize must be calculated using double-precision floating-point arithmetic (subtracting a second chunk)');
  });
}, 'Floating point arithmetic must manifest near 0 (total ends up zero)');

function setupTestStream() {
  const strategy = {
    size(x) {
      return x;
    },
    highWaterMark: 0
  };

  let controller;
  const rs = new ReadableStream({
    start(c) {
      controller = c;
    }
  }, strategy);

  return { reader: rs.getReader(), controller };
}