Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marecabo committed Oct 14, 2023
1 parent 6dae9b6 commit 4ebdf4d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
Expand All @@ -32,16 +33,32 @@
*/
public class DisallowedNextLinks {

// Actually, it could be Map<String, Set<List<Id<Link>>>>, as the order of the
// next links sequences does not matter. However, we choose to store them in a
// list in favor of a smaller memory footprint.
private final Map<String, List<List<Id<Link>>>> linkIdSequencesMap = new HashMap<>();

/**
* Add a sequence of subsequent links to be disallowed.
*
* @param mode
* @param linkSequence sequence of links that shall not be passed after passing
* the link where this object is attached
* @return true, if linkSequence was actually added
*/
public boolean addDisallowedLinkSequence(String mode, Collection<Id<Link>> linkSequence) {
List<List<Id<Link>>> sequences = this.linkIdSequencesMap.computeIfAbsent(mode, m -> new ArrayList<>());
List<List<Id<Link>>> linkSequences = this.linkIdSequencesMap.computeIfAbsent(mode, m -> new ArrayList<>());

boolean result = false;
// prevent adding empty/duplicate link id sequences, or duplicate link ids
if (!linkSequence.isEmpty() && new HashSet<>(linkSequence).size() == linkSequence.size()
&& !sequences.contains(linkSequence)) {
return sequences.add(ImmutableList.copyOf(linkSequence));
&& !linkSequences.contains(linkSequence)) {
result = linkSequences.add(ImmutableList.copyOf(linkSequence));
if (result) { // sorting is required for a.equals(b) <=> a.hashCode() == b.hashCode()
Collections.sort(linkSequences, (l, r) -> l.toString().compareTo(r.toString()));
}
}
return false;
return result;
}

public List<List<Id<Link>>> getDisallowedLinkSequences(String mode) {
Expand All @@ -65,8 +82,23 @@ public void clear() {

@Override
public boolean equals(Object obj) {
if (obj instanceof DisallowedNextLinks dls) {
return this.linkIdSequencesMap.equals(dls.linkIdSequencesMap);
if (obj instanceof DisallowedNextLinks dnl) {
if (!this.linkIdSequencesMap.keySet().equals(dnl.linkIdSequencesMap.keySet())) {
return false;
}
for (Entry<String, List<List<Id<Link>>>> entry : this.linkIdSequencesMap.entrySet()) {
String mode = entry.getKey();
List<List<Id<Link>>> linkSequences = entry.getValue();
List<List<Id<Link>>> otherLinkSequences = dnl.linkIdSequencesMap.get(mode);
// because we store next link sequences in a list, even though their order has
// no meaning, we need to ignore the order when comparing objects.
if (linkSequences.size() != otherLinkSequences.size()
|| !linkSequences.containsAll(otherLinkSequences)
|| !otherLinkSequences.containsAll(linkSequences)) {
return false;
}
}
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Rule;
Expand All @@ -22,40 +24,119 @@ public class DisallowedNextLinksTest {
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void testSerialization() {
public void testEquals() {
DisallowedNextLinks dnl0 = new DisallowedNextLinks();
DisallowedNextLinks dnl1 = new DisallowedNextLinks();
dnl0.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));
dnl1.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));

DisallowedNextLinks dns0 = new DisallowedNextLinks();
dns0.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("3")));
dns0.addDisallowedLinkSequence("car",
List.of(Id.createLinkId("0"), Id.createLinkId("1"), Id.createLinkId("2")));
dns0.addDisallowedLinkSequence("bike", List.of(Id.createLinkId("10"), Id.createLinkId("11")));
Assert.assertEquals(dnl0, dnl1);
Assert.assertEquals(dnl1, dnl0);

DisallowedNextLinks.DisallowedNextLinksAttributeConverter ac = new DisallowedNextLinks.DisallowedNextLinksAttributeConverter();
dnl1.addDisallowedLinkSequence("bike", List.of(Id.createLinkId("0")));

Assert.assertNotEquals(dnl0, dnl1);
Assert.assertNotEquals(dnl1, dnl0);
}

@Test
public void testAdding() {
DisallowedNextLinks dnl = new DisallowedNextLinks();
dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));
dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0")));
dnl.addDisallowedLinkSequence("bike", List.of(Id.createLinkId("0")));

Map<String, List<List<Id<Link>>>> map = Map.of(
"bike", List.of(List.of(Id.createLinkId("0"))),
"car", List.of(List.of(Id.createLinkId("0"), Id.createLinkId("1")), List.of(Id.createLinkId("0"))));
Assert.assertEquals(map, dnl.getAsMap());
}

@Test
public void testRemoving() {
DisallowedNextLinks dnl = new DisallowedNextLinks();
dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));
dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0")));
dnl.addDisallowedLinkSequence("bike", List.of(Id.createLinkId("0")));

dnl.removeDisallowedLinkSequences("bike");

Map<String, List<List<Id<Link>>>> map = Map.of(
"car", List.of(List.of(Id.createLinkId("0"), Id.createLinkId("1")), List.of(Id.createLinkId("0"))));
Assert.assertEquals(map, dnl.getAsMap());
}

@Test
public void testNotAddingDuplicates() {
DisallowedNextLinks dnl = new DisallowedNextLinks();

Assert.assertTrue(dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1"))));
Assert.assertFalse(dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1"))));
Assert.assertFalse(dnl.addDisallowedLinkSequence("car", Collections.emptyList()));
}

@Test
public void testNotAddingEmpty() {
DisallowedNextLinks dnl = new DisallowedNextLinks();

Assert.assertFalse(dnl.addDisallowedLinkSequence("car", Collections.emptyList()));
}

@Test
public void testNotAddingSequenceWithDuplicates() {
DisallowedNextLinks dnl = new DisallowedNextLinks();

Assert.assertFalse(dnl.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("0"))));
}

String s = ac.convertToString(dns0);
Assert.assertEquals("{\"car\":[[\"0\",\"3\"],[\"0\",\"1\",\"2\"]],\"bike\":[[\"10\",\"11\"]]}", s);
System.out.println(s);
@Test
public void testEqualAndHashCode() {
DisallowedNextLinks dnl0 = new DisallowedNextLinks();
dnl0.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));
dnl0.addDisallowedLinkSequence("car", List.of(Id.createLinkId("4"), Id.createLinkId("5")));
DisallowedNextLinks dnl1 = new DisallowedNextLinks();
dnl1.addDisallowedLinkSequence("car", List.of(Id.createLinkId("4"), Id.createLinkId("5")));
dnl1.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("1")));

Assert.assertEquals(dnl0, dnl1);
Assert.assertEquals(dnl0.hashCode(), dnl1.hashCode());
}

@Test
public void testSerialization() {
DisallowedNextLinks dnl0 = new DisallowedNextLinks();
dnl0.addDisallowedLinkSequence("car", List.of(Id.createLinkId("0"), Id.createLinkId("3")));
dnl0.addDisallowedLinkSequence("car",
List.of(Id.createLinkId("0"), Id.createLinkId("1"), Id.createLinkId("2")));
dnl0.addDisallowedLinkSequence("bike", List.of(Id.createLinkId("10"), Id.createLinkId("11")));
DisallowedNextLinks.DisallowedNextLinksAttributeConverter ac = new DisallowedNextLinks.DisallowedNextLinksAttributeConverter();
String s = ac.convertToString(dnl0);
DisallowedNextLinks dnl1 = ac.convert(s);

DisallowedNextLinks dns1 = ac.convert(s);
Assert.assertEquals(dns0, dns1);
Assert.assertSame(dns0.getDisallowedLinkSequences("car").get(0).get(0),
dns1.getDisallowedLinkSequences("car").get(0).get(0));
Assert.assertEquals("{\"car\":[[\"0\",\"1\",\"2\"],[\"0\",\"3\"]],\"bike\":[[\"10\",\"11\"]]}", s);
Assert.assertEquals(dnl0, dnl1);
Assert.assertEquals(dnl0.hashCode(), dnl1.hashCode());
Assert.assertSame(dnl0.getDisallowedLinkSequences("car").get(0).get(0),
dnl1.getDisallowedLinkSequences("car").get(0).get(0));
}

@Test
public void testNetworkWriting() throws IOException {
public void testNetworkWritingAndReading() throws IOException {

Network n = createNetwork();
Link l1 = n.getLinks().get(Id.createLinkId("1"));
DisallowedNextLinks dnl = NetworkUtils.getOrCreateDisallowedNextLinks(l1);
dnl.addDisallowedLinkSequence("car", List.of(l1.getId()));
DisallowedNextLinks dnl0 = NetworkUtils.getOrCreateDisallowedNextLinks(l1);
dnl0.addDisallowedLinkSequence("car", List.of(l1.getId(), Id.createLinkId("2")));

File tempFile = tempFolder.newFile("network.xml");
new NetworkWriter(n).write(tempFile.toString());
Network network = NetworkUtils.createNetwork();
new MatsimNetworkReader(network).readFile(tempFile.toString());

Assert.assertEquals(dnl, NetworkUtils.getDisallowedNextLinks(network.getLinks().get(l1.getId())));
DisallowedNextLinks dnl1 = NetworkUtils.getDisallowedNextLinks(network.getLinks().get(l1.getId()));
Assert.assertEquals(dnl0, dnl1);
Assert.assertEquals(dnl0.hashCode(), dnl1.hashCode());
Assert.assertSame(l1.getId(), dnl1.getDisallowedLinkSequences("car").get(0).get(0));
}

static Network createNetwork() {
Expand Down

0 comments on commit 4ebdf4d

Please sign in to comment.