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

Code before:
        self.buf.setBytes([UInt8(0x3F)], at: 1023)
        self.buf.setBytes([UInt8(0x3F)], at: 2)
        let view = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertEqual(1001, view?.lastIndex(of: UInt8(0x59)))
        XCTAssertNil(view?.lastIndex(of: UInt8(0x3F)))
    }

    func testBufferViewLastIndexWithWhereClosure() {
        self.buf.clear()
        self.buf.writeBytes(Array(repeating: UInt8(0x4E), count: 1024))
        self.buf.setBytes([UInt8(0x59)], at: 1000)
        self.buf.setBytes([UInt8(0x59)], at: 1001)
        self.buf.setBytes([UInt8(0x59)], at: 1022)
        self.buf.setBytes([UInt8(0x59)], at: 3)
        self.buf.setBytes([UInt8(0x3F)], at: 1023)
        self.buf.setBytes([UInt8(0x3F)], at: 2)
        let view = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertEqual(1001, view?.lastIndex(where: { $0 == UInt8(0x59) }))
        XCTAssertNil(view?.lastIndex(where: { $0 == UInt8(0x3F) }))
    }

    func testBufferViewContains() {
        self.buf.clear()
        self.buf.writeBytes(Array(repeating: UInt8(0x4E), count: 1024))
        self.buf.setBytes([UInt8(0x59)], at: 1000)
        let view: ByteBufferView? = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertTrue(view?.contains(UInt8(0x4E)) == true)
        XCTAssertTrue(view?.contains(UInt8(0x59)) == true)
        XCTAssertTrue(view?.contains(UInt8(0x3F)) == false)
    }

    func testByteBuffersCanBeInitializedFromByteBufferViews() throws {
        self.buf.writeString("hello")

        let readableByteBuffer = ByteBuffer(self.buf.readableBytesView)
        XCTAssertEqual(self.buf, readableByteBuffer)

        let sliceByteBuffer = self.buf.viewBytes(at: 1, length: 3).map(ByteBuffer.init)
        XCTAssertEqual("ell", sliceByteBuffer.flatMap { $0.getString(at: 0, length: $0.readableBytes) })

        self.buf.clear()

        let emptyByteBuffer = ByteBuffer(self.buf.readableBytesView)
        XCTAssertEqual(self.buf, emptyByteBuffer)

        var fixedCapacityBuffer = self.allocator.buffer(capacity: 16)
        let capacity = fixedCapacityBuffer.capacity
        fixedCapacityBuffer.writeString(String(repeating: "x", count: capacity))
        XCTAssertEqual(capacity, fixedCapacityBuffer.capacity)
        XCTAssertEqual(fixedCapacityBuffer, ByteBuffer(fixedCapacityBuffer.readableBytesView))
    }

Review comment (suggestion):
Can you amend this test to drop some bytes from the front of the buffer? It's useful to validate that the index calculations are right.

Code after:
        self.buf.setBytes([UInt8(0x61)], at: 1002)
        self.buf.setBytes([UInt8(0x59)], at: 1003)
        self.buf.setBytes([UInt8(0x61)], at: 1004)
        self.buf.setBytes([UInt8(0x59)], at: 1023)
        self.buf.setBytes([UInt8(0x3F)], at: 1024)
        let view = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertEqual(1003, view?.lastIndex(of: UInt8(0x59)))
        XCTAssertNil(view?.lastIndex(of: UInt8(0x3F)))
    }

    func testBufferViewLastIndexWithWhereClosure() {
        self.buf.clear()
        self.buf.writeBytes(Array(repeating: UInt8(0x4E), count: 1024))
        self.buf.setBytes([UInt8(0x3F)], at: 2)
        self.buf.setBytes([UInt8(0x59)], at: 3)
        self.buf.setBytes([UInt8(0x61)], at: 1000)
        self.buf.setBytes([UInt8(0x59)], at: 1001)
        self.buf.setBytes([UInt8(0x61)], at: 1002)
        self.buf.setBytes([UInt8(0x59)], at: 1003)
        self.buf.setBytes([UInt8(0x61)], at: 1004)
        self.buf.setBytes([UInt8(0x59)], at: 1023)
        self.buf.setBytes([UInt8(0x3F)], at: 1024)
        let view = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertEqual(1003, view?.lastIndex(where: { $0 == UInt8(0x59) }))
        XCTAssertNil(view?.lastIndex(where: { $0 == UInt8(0x3F) }))
    }

    func testBufferViewContains() {
        self.buf.clear()
        self.buf.writeBytes(Array(repeating: UInt8(0x4E), count: 1024))
        self.buf.setBytes([UInt8(0x59)], at: 1000)
        let view: ByteBufferView? = self.buf.viewBytes(at: 5, length: 1010)
        XCTAssertTrue(view?.contains(UInt8(0x4E)) == true)
        XCTAssertTrue(view?.contains(UInt8(0x59)) == true)
        XCTAssertTrue(view?.contains(UInt8(0x3F)) == false)
    }

    func testByteBuffersCanBeInitializedFromByteBufferViews() throws {
        self.buf.writeString("hello")

        let readableByteBuffer = ByteBuffer(self.buf.readableBytesView)
        XCTAssertEqual(self.buf, readableByteBuffer)

        let sliceByteBuffer = self.buf.viewBytes(at: 1, length: 3).map(ByteBuffer.init)
        XCTAssertEqual("ell", sliceByteBuffer.flatMap { $0.getString(at: 0, length: $0.readableBytes) })

        self.buf.clear()

        let emptyByteBuffer = ByteBuffer(self.buf.readableBytesView)
        XCTAssertEqual(self.buf, emptyByteBuffer)