-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of ShortNameComparator.
- Loading branch information
James Cover jdcove2
committed
Dec 19, 2024
1 parent
379db5e
commit a279775
Showing
2 changed files
with
67 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 27 additions & 75 deletions
102
src/test/java/emissary/util/ShortNameComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,47 @@ | ||
package emissary.util; | ||
|
||
import emissary.core.BaseDataObject; | ||
import emissary.core.DataObjectFactory; | ||
import emissary.core.Family; | ||
import emissary.core.IBaseDataObject; | ||
import emissary.test.core.junit5.UnitTest; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
class ShortNameComparatorTest extends UnitTest { | ||
private final byte[] nobytes = new byte[0]; | ||
private static final String b = "foo"; | ||
private final String ba = b + Family.SEP; | ||
|
||
@Test | ||
void testOrdering() { | ||
final List<IBaseDataObject> l = new ArrayList<>(); | ||
|
||
fillList(l); | ||
l.sort(new ShortNameComparator()); | ||
checkList(l); | ||
} | ||
|
||
@Test | ||
void testNonsenseNames() { | ||
final List<IBaseDataObject> l = new ArrayList<>(); | ||
l.add(DataObjectFactory.getInstance(null, "foo-att-def")); | ||
l.add(DataObjectFactory.getInstance(null, "foo-att-abc")); | ||
l.sort(new ShortNameComparator()); | ||
assertEquals("foo-att-abc", l.get(0).shortName(), "Bad components sort in alpha order"); | ||
} | ||
|
||
private void fillList(final List<IBaseDataObject> l) { | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "1")); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "3")); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, b)); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "3" + Family.getSep(2))); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "3" + Family.getSep(1))); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "2")); | ||
l.add(DataObjectFactory.getInstance(this.nobytes, this.ba + "3" + Family.getSep(1) + Family.getSep(1))); | ||
} | ||
|
||
private void checkList(final List<IBaseDataObject> l) { | ||
assertEquals(b, l.get(0).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "1", l.get(1).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "2", l.get(2).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "3", l.get(3).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "3" + Family.getSep(1), l.get(4).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "3" + Family.getSep(1) + Family.getSep(1), l.get(5).shortName(), "Ordering of sort"); | ||
assertEquals(this.ba + "3" + Family.getSep(2), l.get(6).shortName(), "Ordering of sort"); | ||
} | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class ShortNameComparatorTest { | ||
private static final String ABC = "abc"; | ||
private static final String DEF = "def"; | ||
private static final String PARENT = "foo"; | ||
private static final String CHILD_ABC = PARENT + Family.SEP + ABC; | ||
private static final String CHILD_DEF = PARENT + Family.SEP + DEF; | ||
private static final String CHILD_1 = PARENT + Family.SEP + "1"; | ||
private static final String CHILD_2 = PARENT + Family.SEP + "2"; | ||
private static final String GRANDCHILD_1_1 = CHILD_1 + Family.SEP + "1"; | ||
|
||
@Test | ||
void testImplComparator() { | ||
final List<IBaseDataObject> l = new ArrayList<>(); | ||
void testParameters() { | ||
final ShortNameComparator comparator = new ShortNameComparator(); | ||
final IBaseDataObject ibdo = new BaseDataObject(); | ||
|
||
fillList(l); | ||
l.sort(new ShortNameComparator()); | ||
checkList(l); | ||
assertThrows(IllegalArgumentException.class, () -> comparator.compare(null, ibdo)); | ||
assertThrows(IllegalArgumentException.class, () -> comparator.compare(ibdo, null)); | ||
} | ||
|
||
@Test | ||
void testSubclassedComparator() { | ||
final String defaultPayloadClass = DataObjectFactory.getImplementingClass(); | ||
DataObjectFactory.setImplementingClass(MyDataObject.class.getName()); | ||
try { | ||
final List<IBaseDataObject> l = new ArrayList<>(); | ||
|
||
fillList(l); | ||
l.sort(new ShortNameComparator()); | ||
checkList(l); | ||
} catch (RuntimeException ex) { | ||
fail("Cannot operate Comparator in subclass", ex); | ||
} finally { | ||
DataObjectFactory.setImplementingClass(defaultPayloadClass); | ||
} | ||
void testComparator() { | ||
check(ABC, DEF, 0, 0); | ||
check(CHILD_ABC, CHILD_DEF, -3, 3); | ||
check(CHILD_1, CHILD_2, -1, 1); | ||
check(CHILD_1, GRANDCHILD_1_1, -1, 1); | ||
} | ||
|
||
// An extension of BaseDataObject for testing the | ||
// lower bounded generics on the comparator | ||
public static class MyDataObject extends BaseDataObject { | ||
static final long serialVersionUID = 7872122417333007868L; | ||
private static void check(final String name1, final String name2, final int forwardResult, final int reverseResult) { | ||
final IBaseDataObject ibdo1 = new BaseDataObject(null, name1); | ||
final IBaseDataObject ibdo2 = new BaseDataObject(null, name2); | ||
final ShortNameComparator snc = new ShortNameComparator(); | ||
|
||
public MyDataObject(final byte[] data, final String name) { | ||
super(data, name); | ||
} | ||
assertEquals(forwardResult, snc.compare(ibdo1, ibdo2)); | ||
assertEquals(reverseResult, snc.compare(ibdo2, ibdo1)); | ||
} | ||
} |