Skip to content

Commit

Permalink
Add network writing test
Browse files Browse the repository at this point in the history
  • Loading branch information
marecabo committed Oct 13, 2023
1 parent 6dfe858 commit ce9dd6f
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package org.matsim.core.network;

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

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.core.network.io.MatsimNetworkReader;
import org.matsim.core.network.io.NetworkWriter;

public class DisallowedNextLinksTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void testSerialization() {

Expand All @@ -29,4 +42,30 @@ public void testSerialization() {
dns1.getDisallowedLinkSequences("car").get(0).get(0));
}

@Test
public void testNetworkWriting() throws IOException {

Network n = NetworkUtils.createNetwork();
Node n0 = NetworkUtils.createAndAddNode(n, Id.createNodeId("0"), new Coord(0, 0));
Node n1 = NetworkUtils.createAndAddNode(n, Id.createNodeId("1"), new Coord(1, 0));
Node n2 = NetworkUtils.createAndAddNode(n, Id.createNodeId("2"), new Coord(2, -1));
Node n3 = NetworkUtils.createAndAddNode(n, Id.createNodeId("3"), new Coord(2, 1));
Node n4 = NetworkUtils.createAndAddNode(n, Id.createNodeId("4"), new Coord(3, 0));
Link l0 = NetworkUtils.createAndAddLink(n, Id.createLinkId("0"), n0, n1, Math.sqrt(2), 30 / 3.6, 900, 1);
Link l1 = NetworkUtils.createAndAddLink(n, Id.createLinkId("1"), n1, n2, Math.sqrt(2), 30 / 3.6, 900, 1);
Link l2 = NetworkUtils.createAndAddLink(n, Id.createLinkId("2"), n1, n3, Math.sqrt(2), 30 / 3.6, 900, 1);
Link l3 = NetworkUtils.createAndAddLink(n, Id.createLinkId("3"), n2, n4, Math.sqrt(2), 30 / 3.6, 900, 1);
Link l4 = NetworkUtils.createAndAddLink(n, Id.createLinkId("4"), n3, n4, Math.sqrt(2), 30 / 3.6, 900, 1);

DisallowedNextLinks dnl = NetworkUtils.getOrCreateDisallowedNextLinks(l1);
dnl.addDisallowedLinkSequence("car", List.of(l1.getId()));

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())));
}

}

0 comments on commit ce9dd6f

Please sign in to comment.