void AddressRangeTest::testConstructorByWidth()
{
    AddressRange addressRange = AddressRange::fromWidth( Start, Width );

    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), End );
    QCOMPARE( addressRange.width(), Width );

    addressRange = AddressRange::fromWidth( Width );

    QCOMPARE( addressRange.start(), 0 );
    QCOMPARE( addressRange.end(), Width-1 );
    QCOMPARE( addressRange.width(), Width );
}
Exemple #2
0
void Dropper::handleInternalDrag( QDropEvent* dropEvent, AbstractByteArrayView* sourceByteArrayView )
{
    // get drag origin
    AddressRange selection = sourceByteArrayView->tableRanges()->removeSelection();

    ByteArrayTableCursor* tableCursor = mByteArrayView->tableCursor();
    AbstractByteArrayModel* byteArrayModel = mByteArrayView->byteArrayModel();

    Address insertIndex = tableCursor->realIndex();

    // is this a move?
    if( dropEvent->proposedAction() == Qt::MoveAction )
    {
        // ignore the copy hold in the event but only move
        Address newCursorIndex;
        // need to swap?
        if( selection.end() < insertIndex )
        {
            newCursorIndex = insertIndex;
            const Address firstIndex = selection.start();
            selection.set( selection.nextBehindEnd(), insertIndex-1 );
            insertIndex = firstIndex;
        }
        else
            newCursorIndex = insertIndex + selection.width();

        const bool success = byteArrayModel->swap( insertIndex, selection );
        if( success )
        {
            tableCursor->gotoCIndex( newCursorIndex );
            emit mByteArrayView->cursorPositionChanged( tableCursor->realIndex() );
        }
    }
    // is a copy
    else
    {
        // TODO: should this be a method of AbstractByteArrayModel, to reuse piece data?

        // get data
        const QByteArray data =
            dropEvent->mimeData()->data( QLatin1String(DropperOctetStreamFormatName) );

        if( !data.isEmpty() )
        {
            if( mByteArrayView->isOverwriteMode() )
            {
                const Size length = mByteArrayView->layout()->length();
                if( !tableCursor->isBehind() && length > 0 )
                {
                    AddressRange overwriteRange = AddressRange::fromWidth( insertIndex, data.size() );
                    overwriteRange.restrictEndTo( length-1 );
                    if( overwriteRange.isValid() )
                        byteArrayModel->replace( overwriteRange, reinterpret_cast<const Byte*>(data.constData()), overwriteRange.width() );
                }
            }
            else
                byteArrayModel->insert( insertIndex, reinterpret_cast<const Byte*>(data.constData()), data.size() );
        }
    }
}
void AddressRangeTest::testSplitAtLocal()
{
    AddressRange addressRange( Start, End );

    // split at start
    AddressRange splitAddressRange = addressRange.splitAtLocal( 0 );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start );
    QCOMPARE( splitAddressRange.end(),   End );

    // split at one after start
    addressRange.set( Start, End );
    splitAddressRange = addressRange.splitAtLocal( 1 );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), Start );
    QCOMPARE( splitAddressRange.start(), Start+1 );
    QCOMPARE( splitAddressRange.end(),   End );

    // split at mid
    const Address Mid = Width/2;
    addressRange.set( Start, End );
    splitAddressRange = addressRange.splitAtLocal( Mid );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), Start+Mid-1 );
    QCOMPARE( splitAddressRange.start(), Start+Mid );
    QCOMPARE( splitAddressRange.end(),   End );

    // split at one before end
    addressRange.set( Start, End );
    splitAddressRange = addressRange.splitAtLocal( Width-1 );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), End-1 );
    QCOMPARE( splitAddressRange.start(), End );
    QCOMPARE( splitAddressRange.end(),   End );

    // split at start so the split is the full
    addressRange.set( Start, End );
    splitAddressRange = addressRange.splitAtLocal( Width );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), End );
    QVERIFY( !splitAddressRange.isValid() );
}
void AddressRangeTest::testSubRange()
{
    AddressRange addressRange( Start, End );

    AddressRange subRange = addressRange.subRange( AddressRange::fromWidth(Width) );
    QCOMPARE( subRange.start(), Start );
    QCOMPARE( subRange.end(), End );

    subRange = addressRange.subRange( AddressRange(1,Width-1) );
    QCOMPARE( subRange.start(), Start+1 );
    QCOMPARE( subRange.end(), End );

    subRange = addressRange.subRange( AddressRange(0,Width-2) );
    QCOMPARE( subRange.start(), Start );
    QCOMPARE( subRange.end(), End-1 );

    subRange = addressRange.subRange( AddressRange(1,Width-2) );
    QCOMPARE( subRange.start(), Start+1 );
    QCOMPARE( subRange.end(), End-1 );
}
void AddressRangeTest::testLocalRange()
{
    AddressRange addressRange( Start, End );

    AddressRange localRange = addressRange.localRange( AddressRange(Start,End) );
    QCOMPARE( localRange.start(), 0 );
    QCOMPARE( localRange.end(), Width-1 );

    localRange = addressRange.localRange( AddressRange(Start+1,End) );
    QCOMPARE( localRange.start(), 1 );
    QCOMPARE( localRange.end(), Width-1 );

    localRange = addressRange.localRange( AddressRange(Start,End-1) );
    QCOMPARE( localRange.start(), 0 );
    QCOMPARE( localRange.end(), Width-2 );

    localRange = addressRange.localRange( AddressRange(Start+1,End-1) );
    QCOMPARE( localRange.start(), 1 );
    QCOMPARE( localRange.end(), Width-2 );
}
void AddressRangeTest::testSetGetWidth()
{
    AddressRange addressRange;
    // end, width by width
    addressRange.setStart( Start );
    addressRange.setEndByWidth( Width );
    QCOMPARE( addressRange.end(), End );
    QCOMPARE( addressRange.width(), Width );
    // start, width by width
    addressRange.setEnd( End );
    addressRange.setStartByWidth( Width );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.width(), Width );
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void OverflowSamples::visit(const AddressRange& range,
                            const OverflowSampleVisitor& visitor) const
{
    std::vector<boost::uint64_t> samples(1);
    bool terminate = false;
    
    for (std::map<Address, boost::uint64_t>::const_iterator
             i = dm_samples.lower_bound(range.begin()),
             i_end = dm_samples.upper_bound(range.end());
         !terminate && (i != i_end);
         ++i)
    {
        samples[0] = i->second;
        terminate |= !visitor(i->first, samples);
    }
}
void AddressRangeTest::testSetGetEnd()
{
    AddressRange addressRange;
    addressRange.setEnd( End );
    QCOMPARE( addressRange.end(), End );
}
void AddressRangeTest::testRemoveLocal()
{
    const Address Mid = Width/2;
    AddressRange addressRange( Start, End );

    // remove none at start
    AddressRange removeAddressRange( 0, 0-1  );
    AddressRange splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove one at start
    addressRange.set( Start, End );
    removeAddressRange.set( 0, 0 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start+1 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove many at start
    addressRange.set( Start, End );
    removeAddressRange.set( 0, Mid );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start+Mid+1 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove all except last
    addressRange.set( Start, End );
    removeAddressRange.set( 0, Width-2 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), End );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove at mid
    addressRange.set( Start, End );
    removeAddressRange.set( Mid-1, Mid+1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), Start+Mid-2 );
    QCOMPARE( splitAddressRange.start(), Start+Mid+2 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove none at end
    addressRange.set( Start, End );
    removeAddressRange.set( Width, Width-1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   End );
    QVERIFY( !splitAddressRange.isValid() );

    // remove one at end
    addressRange.set( Start, End );
    removeAddressRange.set( Width-1, Width-1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   End-1 );
    QVERIFY( !splitAddressRange.isValid() );

    // remove many at end
    addressRange.set( Start, End );
    removeAddressRange.set( Mid, Width-1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   Start+Mid-1 );
    QVERIFY( !splitAddressRange.isValid() );

    // remove all except first
    addressRange.set( Start, End );
    removeAddressRange.set( 1, Width-1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   Start );
    QVERIFY( !splitAddressRange.isValid() );

    // remove all
    addressRange.set( Start, End );
    removeAddressRange.set( 0, Width-1 );
    splitAddressRange = addressRange.removeLocal( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QVERIFY( !splitAddressRange.isValid() );
}
void AddressRangeTest::testRemove()
{
    const Address Mid = (Start+End)/2;
    AddressRange addressRange( Start, End );

    // remove none at start
    AddressRange removeAddressRange( Start, Start-1  );
    AddressRange splitAddressRange = addressRange.remove( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove one at start
    addressRange.set( Start, End );
    removeAddressRange.set( Start, Start );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Start+1 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove many at start
    addressRange.set( Start, End );
    removeAddressRange.set( Start, Mid );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), Mid+1 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove all except last
    addressRange.set( Start, End );
    removeAddressRange.set( Start, End-1 );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QCOMPARE( splitAddressRange.start(), End );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove at mid
    addressRange.set( Start, End );
    removeAddressRange.set( Mid-1, Mid+1 );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(), Mid-2 );
    QCOMPARE( splitAddressRange.start(), Mid+2 );
    QCOMPARE( splitAddressRange.end(),   End );

    // remove none at end
    addressRange.set( Start, End );
    removeAddressRange.set( End+1, End );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   End );
    QVERIFY( !splitAddressRange.isValid() );

    // remove one at end
    addressRange.set( Start, End );
    removeAddressRange.set( End, End );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   End-1 );
    QVERIFY( !splitAddressRange.isValid() );

    // remove many at end
    addressRange.set( Start, End );
    removeAddressRange.set( Mid, End );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   Mid-1 );
    QVERIFY( !splitAddressRange.isValid() );

    // remove all except first
    addressRange.set( Start, End );
    removeAddressRange.set( Start+1, End );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QCOMPARE( addressRange.start(), Start );
    QCOMPARE( addressRange.end(),   Start );
    QVERIFY( !splitAddressRange.isValid() );

    // remove all
    addressRange.set( Start, End );
    removeAddressRange.set( Start, End );
    splitAddressRange = addressRange.remove( removeAddressRange );
    QVERIFY( !addressRange.isValid() );
    QVERIFY( !splitAddressRange.isValid() );
}