MCPcopy Index your code
hub / github.com/nodejs/node / At

Function At

deps/v8/test/mjsunit/typedarray-resizablearraybuffer.js:1455–1510  ·  view source on GitHub ↗
(atHelper, oobThrows)

Source from the content-addressed store, hash-verified

1453})();
1454
1455function At(atHelper, oobThrows) {
1456 for (let ctor of ctors) {
1457 const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT,
1458 8 * ctor.BYTES_PER_ELEMENT);
1459 const fixedLength = new ctor(rab, 0, 4);
1460 const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
1461 const lengthTracking = new ctor(rab, 0);
1462 const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
1463
1464 // Write some data into the array.
1465 let ta_write = new ctor(rab);
1466 for (let i = 0; i < 4; ++i) {
1467 WriteToTypedArray(ta_write, i, i);
1468 }
1469
1470 assertEquals(3, atHelper(fixedLength, -1));
1471 assertEquals(3, atHelper(lengthTracking, -1));
1472 assertEquals(3, atHelper(fixedLengthWithOffset, -1));
1473 assertEquals(3, atHelper(lengthTrackingWithOffset, -1));
1474
1475 // Shrink so that fixed length TAs go out of bounds.
1476 rab.resize(3 * ctor.BYTES_PER_ELEMENT);
1477
1478 if (oobThrows) {
1479 assertThrows(() => { atHelper(fixedLength, -1); });
1480 assertThrows(() => { atHelper(fixedLengthWithOffset, -1); });
1481 } else {
1482 assertEquals(undefined, atHelper(fixedLength, -1));
1483 assertEquals(undefined, atHelper(fixedLengthWithOffset, -1));
1484 }
1485
1486 assertEquals(2, atHelper(lengthTracking, -1));
1487 assertEquals(2, atHelper(lengthTrackingWithOffset, -1));
1488
1489 // Shrink so that the TAs with offset go out of bounds.
1490 rab.resize(1 * ctor.BYTES_PER_ELEMENT);
1491
1492 if (oobThrows) {
1493 assertThrows(() => { atHelper(fixedLength, -1); });
1494 assertThrows(() => { atHelper(fixedLengthWithOffset, -1); });
1495 assertThrows(() => { atHelper(lengthTrackingWithOffset, -1); });
1496 } else {
1497 assertEquals(undefined, atHelper(fixedLength, -1));
1498 assertEquals(undefined, atHelper(fixedLengthWithOffset, -1));
1499 assertEquals(undefined, atHelper(lengthTrackingWithOffset, -1));
1500 }
1501 assertEquals(0, atHelper(lengthTracking, -1));
1502
1503 // Grow so that all TAs are back in-bounds. New memory is zeroed.
1504 rab.resize(6 * ctor.BYTES_PER_ELEMENT);
1505 assertEquals(0, atHelper(fixedLength, -1));
1506 assertEquals(0, atHelper(lengthTracking, -1));
1507 assertEquals(0, atHelper(fixedLengthWithOffset, -1));
1508 assertEquals(0, atHelper(lengthTrackingWithOffset, -1));
1509 }
1510}
1511At(TypedArrayAtHelper, true);
1512At(ArrayAtHelper, false);

Calls 4

WriteToTypedArrayFunction · 0.85
assertEqualsFunction · 0.50
assertThrowsFunction · 0.50

Tested by

no test coverage detected