Repository: apple/swift-nio
File: Tests/NIOCoreTests/ByteBufferTest.swift (Swift)

Code before:
        let expectedReadableBytes = totalBufferSize - offset
        let slice = buf.getSlice(at: offset, length: expectedReadableBytes)!
        XCTAssertEqual(expectedReadableBytes, slice.readableBytes)
        XCTAssertEqual(0, slice.readerIndex)
        XCTAssertEqual(expectedReadableBytes, slice.writerIndex)
        XCTAssertEqual(Int(UInt32(expectedReadableBytes).nextPowerOf2()), slice.capacity)

        XCTAssertEqual(0xcc, slice.getInteger(at: 0, as: UInt8.self))
        XCTAssertEqual(0xdd, slice.getInteger(at: slice.writerIndex - 1, as: UInt8.self))
    }

    func testSliceOfMassiveBufferWithAdvancedReaderIndexIsOk() throws {
        // We only want to run this test on 64-bit systems: 32-bit systems can't allocate buffers
        // large enough to run this test safely.
        guard MemoryLayout<Int>.size >= 8 else {
            throw XCTSkip("This test is only supported on 64-bit systems.")
        }

        let targetSize = Int(UInt32.max)
        var buffer = self.allocator.buffer(capacity: targetSize)

        // Move the reader index forward such that we hit the slow path.
        let offset = Int(_UInt24.max) + 1
        buffer.moveWriterIndex(to: offset)
        buffer.moveReaderIndex(to: offset)
        buffer.writeInteger(UInt32(0))

        // We're going to move the readerIndex forward by 1, and then slice.
        buffer.moveReaderIndex(forwardBy: 1)
        let slice = buffer.getSlice(at: buffer.readerIndex, length: 3)!
        XCTAssertEqual(slice.readableBytes, 3)
        XCTAssertEqual(slice.readerIndex, 0)
        XCTAssertEqual(Int(UInt32(3).nextPowerOf2()), slice.capacity)
    }
    
    func testSliceOnSliceAfterHitting16MBMark() {
        // This test ensures that a slice will get a new backing storage if its start is more than
        // 16MiB after the originating backing storage.
        
        // create a buffer with 16MiB + 1 byte
        let inputBufferLength = 16 * 1024 * 1024 + 1
        var inputBuffer = ByteBufferAllocator().buffer(capacity: inputBufferLength)
        inputBuffer.writeRepeatingByte(1, count: 8)
        inputBuffer.writeRepeatingByte(2, count: inputBufferLength - 9)
        inputBuffer.writeRepeatingByte(3, count: 1)
        // read a small slice from the inputBuffer, to create an offset of eight bytes
        XCTAssertEqual(inputBuffer.readInteger(as: UInt64.self), 0x0101010101010101)
        
        // read the remaining bytes into a new slice (this will have a length of 16MiB - 7Bbytes)
        let remainingSliceLength = inputBufferLength - 8
        XCTAssertEqual(inputBuffer.readableBytes, remainingSliceLength)

Review comment (suggestion):
@Lukasa you can still make this write happen by having the fake allocator do

```
let ptr = malloc(4)
return ptr - offset
```

And `free` would do `free(ptr + offset)`.

Code after:
        let expectedReadableBytes = totalBufferSize - offset
        let slice = buf.getSlice(at: offset, length: expectedReadableBytes)!
        XCTAssertEqual(expectedReadableBytes, slice.readableBytes)
        XCTAssertEqual(0, slice.readerIndex)
        XCTAssertEqual(expectedReadableBytes, slice.writerIndex)
        XCTAssertEqual(Int(UInt32(expectedReadableBytes).nextPowerOf2()), slice.capacity)

        XCTAssertEqual(0xcc, slice.getInteger(at: 0, as: UInt8.self))
        XCTAssertEqual(0xdd, slice.getInteger(at: slice.writerIndex - 1, as: UInt8.self))
    }

    func testSliceOfMassiveBufferWithAdvancedReaderIndexIsOk() throws {
        // We only want to run this test on 64-bit systems: 32-bit systems can't allocate buffers
        // large enough to run this test safely.
        guard MemoryLayout<Int>.size >= 8 else {
            throw XCTSkip("This test is only supported on 64-bit systems.")
        }

        // This allocator assumes that we'll never call realloc.
        let fakeAllocator = ByteBufferAllocator(
            hookedMalloc: { _ in .init(bitPattern: 0xdeadbeef) },
            hookedRealloc: { _, _ in fatalError() },
            hookedFree: { precondition($0 == .init(bitPattern: 0xdeadbeef)!) },
            hookedMemcpy: {_, _, _ in }
        )

        let targetSize = Int(UInt32.max)
        var buffer = fakeAllocator.buffer(capacity: targetSize)

        // Move the reader index forward such that we hit the slow path.
        let offset = Int(_UInt24.max) + 1
        buffer.moveWriterIndex(to: offset)
        buffer.moveReaderIndex(to: offset)

        // Pretend we wrote a UInt32.
        buffer.moveWriterIndex(forwardBy: 4)

        // We're going to move the readerIndex forward by 1, and then slice.
        buffer.moveReaderIndex(forwardBy: 1)
        let slice = buffer.getSlice(at: buffer.readerIndex, length: 3)!
        XCTAssertEqual(slice.readableBytes, 3)
        XCTAssertEqual(slice.readerIndex, 0)
        XCTAssertEqual(Int(UInt32(3).nextPowerOf2()), slice.capacity)
    }
    
    func testSliceOnSliceAfterHitting16MBMark() {
        // This test ensures that a slice will get a new backing storage if its start is more than
        // 16MiB after the originating backing storage.
        
        // create a buffer with 16MiB + 1 byte
        let inputBufferLength = 16 * 1024 * 1024 + 1