mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 02:39:44 +02:00
Incorporating suggestions from review
This commit is contained in:
parent
560dbe306c
commit
2dbf262295
2 changed files with 89 additions and 36 deletions
|
@ -30,8 +30,8 @@ import java.util.Iterator;
|
||||||
public class AddressRangeImpl implements AddressRange, Serializable {
|
public class AddressRangeImpl implements AddressRange, Serializable {
|
||||||
private static final long serialVersionUID = 1;
|
private static final long serialVersionUID = 1;
|
||||||
|
|
||||||
private Address minAddress; // mimimum address in this range.
|
private final Address minAddress; // minimum address in this range.
|
||||||
private Address maxAddress; // maximum address in this range.
|
private final Address maxAddress; // maximum address in this range.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new AddressRangeImpl from the given range.
|
* Construct a new AddressRangeImpl from the given range.
|
||||||
|
@ -57,11 +57,11 @@ public class AddressRangeImpl implements AddressRange, Serializable {
|
||||||
throw new IllegalArgumentException("Start and end addresses are not in the same space.");
|
throw new IllegalArgumentException("Start and end addresses are not in the same space.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (start.compareTo(end) < 0) {
|
||||||
minAddress = start;
|
minAddress = start;
|
||||||
maxAddress = end;
|
maxAddress = end;
|
||||||
|
} else {
|
||||||
// swap them if out of order
|
// swap them if out of order
|
||||||
if (minAddress.compareTo(maxAddress) > 0) {
|
|
||||||
minAddress = end;
|
minAddress = end;
|
||||||
maxAddress = start;
|
maxAddress = start;
|
||||||
}
|
}
|
||||||
|
@ -233,28 +233,28 @@ public class AddressRangeImpl implements AddressRange, Serializable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Address> iterator() {
|
public Iterator<Address> iterator() {
|
||||||
return new MyAddressIterator(minAddress, maxAddress);
|
return new MyAddressIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MyAddressIterator implements Iterator<Address> {
|
private class MyAddressIterator implements Iterator<Address> {
|
||||||
|
|
||||||
private Address curr;
|
private Address curr;
|
||||||
private Address limit;
|
|
||||||
|
|
||||||
public MyAddressIterator(Address start, Address end) {
|
public MyAddressIterator() {
|
||||||
this.curr = start;
|
this.curr = minAddress;
|
||||||
this.limit = end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return curr.add(1).compareTo(limit) <= 0;
|
return curr != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Address next() {
|
public Address next() {
|
||||||
Address next = curr;
|
Address next = curr;
|
||||||
curr = curr.add(1);
|
if (curr != null) {
|
||||||
|
curr = curr.equals(maxAddress) ? null : curr.next();
|
||||||
|
}
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -228,6 +228,59 @@ public class AddressRangeImplTest extends AbstractGenericTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddressRange_BoundsOrdering() {
|
||||||
|
|
||||||
|
int size = 15;
|
||||||
|
Address start = addr(5);
|
||||||
|
Address limit = start.add(size);
|
||||||
|
|
||||||
|
AddressRange r1 = new AddressRangeImpl(start, limit);
|
||||||
|
assertTrue(r1.getMinAddress().compareTo(r1.getMaxAddress()) < 0);
|
||||||
|
|
||||||
|
AddressRange r2 = new AddressRangeImpl(limit, start);
|
||||||
|
assertTrue(r2.getMinAddress().compareTo(r2.getMaxAddress()) < 0);
|
||||||
|
|
||||||
|
assertTrue(r1.compareTo(r2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddressRangeIteration_RangeEnumeration() {
|
||||||
|
int size = 15;
|
||||||
|
Address start = addr(5);
|
||||||
|
Address limit = start.add(size);
|
||||||
|
|
||||||
|
AddressRange r1 = new AddressRangeImpl(start, limit);
|
||||||
|
int addrCount = 0;
|
||||||
|
Iterator<Address> addrItr = r1.iterator();
|
||||||
|
while (addrItr.hasNext()) {
|
||||||
|
addrItr.next();
|
||||||
|
addrCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
"Address Iterator does not properly enumerate address range: " +
|
||||||
|
String.format("%s (%d long) -- found %d", r1.toString(), r1.getLength(), addrCount),
|
||||||
|
addrCount == (size + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddressRangeIteration_Extent() {
|
||||||
|
int size = 15;
|
||||||
|
Address start = addr(5);
|
||||||
|
Address limit = start.add(size);
|
||||||
|
|
||||||
|
AddressRange r1 = new AddressRangeImpl(start, limit);
|
||||||
|
|
||||||
|
Iterator<Address> addrItr = r1.iterator();
|
||||||
|
Address lastAddr = Address.NO_ADDRESS;
|
||||||
|
while (addrItr.hasNext()) {
|
||||||
|
lastAddr = addrItr.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue("Address Iterator extent does not match end of range", lastAddr.equals(limit));
|
||||||
|
}
|
||||||
|
|
||||||
private Address addr(int a) {
|
private Address addr(int a) {
|
||||||
return new GenericAddress(space, a);
|
return new GenericAddress(space, a);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue