OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 if (self.importScripts) { |
| 4 self.importScripts('/resources/testharness.js'); |
| 5 } |
| 6 |
| 7 // Due to the limitations of floating-point precision, the calculation of desire
dSize sometimes gives different answers |
| 8 // than adding up the items in the queue would. It is important that implementat
ions give the same result in these edge |
| 9 // cases so that developers do not come to depend on non-standard behaviour. See |
| 10 // https://github.com/whatwg/streams/issues/582 and linked issues for further di
scussion. |
| 11 |
| 12 promise_test(() => { |
| 13 const writer = setupTestStream(); |
| 14 |
| 15 const writePromises = [ |
| 16 writer.write(2), |
| 17 writer.write(Number.MAX_SAFE_INTEGER) |
| 18 ]; |
| 19 |
| 20 assert_equals(writer.desiredSize, 0 - 2 - Number.MAX_SAFE_INTEGER, |
| 21 'desiredSize must be calculated using double-precision floating-point arithm
etic (after writing two chunks)'); |
| 22 |
| 23 return Promise.all(writePromises).then(() => { |
| 24 assert_equals(writer.desiredSize, 0, '[[queueTotalSize]] must clamp to 0 if
it becomes negative'); |
| 25 }); |
| 26 }, 'Floating point arithmetic must manifest near NUMBER.MAX_SAFE_INTEGER (total
ends up positive)'); |
| 27 |
| 28 promise_test(() => { |
| 29 const writer = setupTestStream(); |
| 30 |
| 31 const writePromises = [ |
| 32 writer.write(1e-16), |
| 33 writer.write(1) |
| 34 ]; |
| 35 |
| 36 assert_equals(writer.desiredSize, 0 - 1e-16 - 1, |
| 37 'desiredSize must be calculated using double-precision floating-point arithm
etic (after writing two chunks)'); |
| 38 |
| 39 return Promise.all(writePromises).then(() => { |
| 40 assert_equals(writer.desiredSize, 0, '[[queueTotalSize]] must clamp to 0 if
it becomes negative'); |
| 41 }); |
| 42 }, 'Floating point arithmetic must manifest near 0 (total ends up positive, but
clamped)'); |
| 43 |
| 44 promise_test(() => { |
| 45 const writer = setupTestStream(); |
| 46 |
| 47 const writePromises = [ |
| 48 writer.write(1e-16), |
| 49 writer.write(1), |
| 50 writer.write(2e-16) |
| 51 ]; |
| 52 |
| 53 assert_equals(writer.desiredSize, 0 - 1e-16 - 1 - 2e-16, |
| 54 'desiredSize must be calculated using double-precision floating-point arithm
etic (after writing three chunks)'); |
| 55 |
| 56 return Promise.all(writePromises).then(() => { |
| 57 assert_equals(writer.desiredSize, 0 - 1e-16 - 1 - 2e-16 + 1e-16 + 1 + 2e-16, |
| 58 'desiredSize must be calculated using floating-point arithmetic (after the
three chunks have finished writing)'); |
| 59 }); |
| 60 }, 'Floating point arithmetic must manifest near 0 (total ends up positive, and
not clamped)'); |
| 61 |
| 62 promise_test(() => { |
| 63 const writer = setupTestStream(); |
| 64 |
| 65 const writePromises = [ |
| 66 writer.write(2e-16), |
| 67 writer.write(1) |
| 68 ]; |
| 69 |
| 70 assert_equals(writer.desiredSize, 0 - 2e-16 - 1, |
| 71 'desiredSize must be calculated using double-precision floating-point arithm
etic (after writing two chunks)'); |
| 72 |
| 73 return Promise.all(writePromises).then(() => { |
| 74 assert_equals(writer.desiredSize, 0 - 2e-16 - 1 + 2e-16 + 1, |
| 75 'desiredSize must be calculated using floating-point arithmetic (after the
two chunks have finished writing)'); |
| 76 }); |
| 77 }, 'Floating point arithmetic must manifest near 0 (total ends up zero)'); |
| 78 |
| 79 function setupTestStream() { |
| 80 const strategy = { |
| 81 size(x) { |
| 82 return x; |
| 83 }, |
| 84 highWaterMark: 0 |
| 85 }; |
| 86 |
| 87 const ws = new WritableStream({}, strategy); |
| 88 |
| 89 return ws.getWriter(); |
| 90 } |
OLD | NEW |