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 {
|
||||
private static final long serialVersionUID = 1;
|
||||
|
||||
private Address minAddress; // mimimum address in this range.
|
||||
private Address maxAddress; // maximum address in this range.
|
||||
private final Address minAddress; // minimum address in this range.
|
||||
private final Address maxAddress; // maximum address in this 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.");
|
||||
}
|
||||
|
||||
if (start.compareTo(end) < 0) {
|
||||
minAddress = start;
|
||||
maxAddress = end;
|
||||
|
||||
} else {
|
||||
// swap them if out of order
|
||||
if (minAddress.compareTo(maxAddress) > 0) {
|
||||
minAddress = end;
|
||||
maxAddress = start;
|
||||
}
|
||||
|
@ -233,28 +233,28 @@ public class AddressRangeImpl implements AddressRange, Serializable {
|
|||
|
||||
@Override
|
||||
public Iterator<Address> iterator() {
|
||||
return new MyAddressIterator(minAddress, maxAddress);
|
||||
return new MyAddressIterator();
|
||||
}
|
||||
|
||||
private class MyAddressIterator implements Iterator<Address> {
|
||||
|
||||
private Address curr;
|
||||
private Address limit;
|
||||
|
||||
public MyAddressIterator(Address start, Address end) {
|
||||
this.curr = start;
|
||||
this.limit = end;
|
||||
public MyAddressIterator() {
|
||||
this.curr = minAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return curr.add(1).compareTo(limit) <= 0;
|
||||
return curr != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Address next() {
|
||||
Address next = curr;
|
||||
curr = curr.add(1);
|
||||
if (curr != null) {
|
||||
curr = curr.equals(maxAddress) ? null : curr.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) {
|
||||
return new GenericAddress(space, a);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue