1
0
mirror of https://github.com/sethm/symon.git synced 2024-11-18 23:10:05 +00:00

Much saner implementation of 'overlaps' method.

This commit is contained in:
Seth Morabito 2008-12-08 22:12:28 -08:00
parent 5bbd72d44e
commit af7743b385
3 changed files with 34 additions and 43 deletions

View File

@ -20,8 +20,9 @@ public class AddressDecoder {
throws MemoryRangeException { throws MemoryRangeException {
// Make sure there's no memory overlap. // Make sure there's no memory overlap.
for (MemoryRange memRange : ioMap.keySet()) { for (MemoryRange memRange : ioMap.keySet()) {
if (d.getMemoryRange().overlapsWith(memRange)) { if (d.getMemoryRange().overlaps(memRange)) {
throw new MemoryRangeException("The device being added overlaps with an existing device."); throw new MemoryRangeException("The device being added overlaps " +
"with an existing device.");
} }
} }

View File

@ -10,7 +10,8 @@ public class MemoryRange implements Comparable<MemoryRange> {
throw new MemoryRangeException("Addresses cannot be less than 0."); throw new MemoryRangeException("Addresses cannot be less than 0.");
} }
if (startAddress >= endAddress) { if (startAddress >= endAddress) {
throw new MemoryRangeException("End address must be greater than start address."); throw new MemoryRangeException("End address must be greater " +
"than start address.");
} }
this.startAddress = startAddress; this.startAddress = startAddress;
this.endAddress = endAddress; this.endAddress = endAddress;
@ -40,15 +41,9 @@ public class MemoryRange implements Comparable<MemoryRange> {
* *
* @returns true if this range overlaps in any way with the other. * @returns true if this range overlaps in any way with the other.
*/ */
public boolean overlapsWith(MemoryRange other) { public boolean overlaps(MemoryRange other) {
return ((this.getEndAddress() >= other.getStartAddress() && return (this.includes(other.getStartAddress()) ||
this.getEndAddress() <= other.getEndAddress()) || other.includes(this.getStartAddress()));
(other.getEndAddress() >= this.getStartAddress() &&
other.getEndAddress() <= this.getEndAddress()) ||
(this.getStartAddress() <= other.getStartAddress() &&
this.getEndAddress() >= other.getEndAddress()) ||
(other.getStartAddress() <= this.getStartAddress() &&
other.getEndAddress() >= this.getEndAddress()));
} }
// Implementation of Comparable interface // Implementation of Comparable interface

View File

@ -46,43 +46,38 @@ public class MemoryRangeTest extends TestCase {
assertEquals(0x202, r.getEndAddress()); assertEquals(0x202, r.getEndAddress());
} }
public void testOverlapsWith() throws MemoryRangeException { public void testOverlaps() throws MemoryRangeException {
MemoryRange a = new MemoryRange(0x0000, 0x0fff); MemoryRange a = new MemoryRange(0x0100, 0x01ff);
MemoryRange b = new MemoryRange(0x2000, 0x2fff); MemoryRange b = new MemoryRange(0x0200, 0x02ff);
MemoryRange c = new MemoryRange(0x1000, 0x1fff); // fits between a and b
MemoryRange d = new MemoryRange(0x0f00, 0x10ff); // overlaps with a
MemoryRange e = new MemoryRange(0x1fff, 0x20ff); // overlaps with b
MemoryRange f = new MemoryRange(0x0fff, 0x2000); // overlaps a and b
MemoryRange g = new MemoryRange(0x00ff, 0x0100); // Overlaps (inside) a, below b
MemoryRange h = new MemoryRange(0x20ff, 0x2100); // Overlaps (inside) b, above a
assertFalse(c.overlapsWith(a)); MemoryRange c = new MemoryRange(0x01a0, 0x01af); // inside a
assertFalse(c.overlapsWith(b)); MemoryRange d = new MemoryRange(0x00ff, 0x01a0); // overlaps a
assertFalse(a.overlapsWith(c)); MemoryRange e = new MemoryRange(0x01a0, 0x02ff); // overlaps a
assertFalse(b.overlapsWith(c));
assertFalse(a.overlapsWith(b));
assertFalse(b.overlapsWith(a));
assertTrue(d.overlapsWith(a)); MemoryRange f = new MemoryRange(0x02a0, 0x02af); // inside b
assertTrue(a.overlapsWith(d)); MemoryRange g = new MemoryRange(0x01ff, 0x02a0); // overlaps b
MemoryRange h = new MemoryRange(0x02a0, 0x03ff); // overlaps b
assertTrue(e.overlapsWith(b)); assertFalse(a.overlaps(b));
assertTrue(b.overlapsWith(e)); assertFalse(b.overlaps(a));
assertTrue(f.overlapsWith(a)); assertTrue(a.overlaps(c));
assertTrue(a.overlapsWith(f)); assertTrue(c.overlaps(a));
assertTrue(f.overlapsWith(b));
assertTrue(b.overlapsWith(f));
assertTrue(a.overlapsWith(g)); assertTrue(a.overlaps(d));
assertTrue(g.overlapsWith(a)); assertTrue(d.overlaps(a));
assertFalse(b.overlapsWith(g));
assertFalse(g.overlapsWith(b));
assertFalse(a.overlapsWith(h)); assertTrue(a.overlaps(e));
assertFalse(h.overlapsWith(a)); assertTrue(e.overlaps(a));
assertTrue(b.overlapsWith(h));
assertTrue(h.overlapsWith(b)); assertTrue(b.overlaps(f));
assertTrue(f.overlaps(b));
assertTrue(b.overlaps(g));
assertTrue(g.overlaps(b));
assertTrue(b.overlaps(h));
assertTrue(h.overlaps(b));
} }
public void testIncluded() throws MemoryRangeException { public void testIncluded() throws MemoryRangeException {