void tst_QRingBuffer::readPointerAtPositionWriteRead() { //create some data QBuffer inData; inData.open(QIODevice::ReadWrite); inData.putChar(0x42); inData.putChar(0x23); inData.write("Qt rocks!"); for (int i = 0; i < 5000; i++) inData.write(QString("Number %1").arg(i).toUtf8()); inData.reset(); QVERIFY(inData.size() > 0); //put the inData in the QRingBuffer QRingBuffer ringBuffer; qint64 remaining = inData.size(); while (remaining > 0) { // write in chunks of 50 bytes // this ensures there will be multiple QByteArrays inside the QRingBuffer // since QRingBuffer is then only using individual arrays of around 4000 bytes qint64 thisWrite = qMin(remaining, qint64(50)); char *pos = ringBuffer.reserve(thisWrite); inData.read(pos, thisWrite); remaining -= thisWrite; } // was data put into it? QVERIFY(ringBuffer.size() > 0); QCOMPARE(qint64(ringBuffer.size()), inData.size()); //read from the QRingBuffer in loop, put back into another QBuffer QBuffer outData; outData.open(QIODevice::ReadWrite); remaining = ringBuffer.size(); while (remaining > 0) { qint64 thisRead; // always try to read as much as possible const char *buf = ringBuffer.readPointerAtPosition(ringBuffer.size() - remaining, thisRead); outData.write(buf, thisRead); remaining -= thisRead; } outData.reset(); QVERIFY(outData.size() > 0); // was the data read from the QRingBuffer the same as the one written into it? QCOMPARE(outData.size(), inData.size()); QVERIFY(outData.buffer().startsWith(inData.buffer())); }
void tst_QRingBuffer::sizeWhenReserved() { QRingBuffer ringBuffer; ringBuffer.reserve(5); QCOMPARE(ringBuffer.size(), 5); }
void tst_QRingBuffer::sizeWhenReservedAndChopped() { QRingBuffer ringBuffer; ringBuffer.reserve(31337); ringBuffer.chop(31337); QCOMPARE(ringBuffer.size(), 0); }
/*! \reimp */ qint64 QProcess::readData(char *data, qint64 maxlen) { Q_D(QProcess); QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError) ? &d->errorReadBuffer : &d->outputReadBuffer; if (maxlen == 1) { int c = readBuffer->getChar(); if (c == -1) { #if defined QPROCESS_DEBUG qDebug("QProcess::readData(%p \"%s\", %d) == -1", data, qt_prettyDebug(data, maxlen, 1).constData(), 1); #endif return -1; } *data = (char) c; #if defined QPROCESS_DEBUG qDebug("QProcess::readData(%p \"%s\", %d) == 1", data, qt_prettyDebug(data, maxlen, 1).constData(), 1); #endif return 1; } qint64 bytesToRead = qint64(qMin(readBuffer->size(), (int)maxlen)); qint64 readSoFar = 0; while (readSoFar < bytesToRead) { char *ptr = readBuffer->readPointer(); int bytesToReadFromThisBlock = qMin<qint64>(bytesToRead - readSoFar, readBuffer->nextDataBlockSize()); memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); readSoFar += bytesToReadFromThisBlock; readBuffer->free(bytesToReadFromThisBlock); } #if defined QPROCESS_DEBUG qDebug("QProcess::readData(%p \"%s\", %lld) == %lld", data, qt_prettyDebug(data, readSoFar, 16).constData(), maxlen, readSoFar); #endif return readSoFar; }
void tst_QRingBuffer::sizeWhenEmpty() { QRingBuffer ringBuffer; QCOMPARE(ringBuffer.size(), 0); }