detects if names are used twice

This commit is contained in:
hneemann 2017-08-13 00:15:49 +02:00
parent 11a1acb239
commit b1dda04f70
2 changed files with 27 additions and 5 deletions

View File

@ -1,6 +1,9 @@
package de.neemann.digital.hdl.model;
import de.neemann.digital.lang.Lang;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
/**
@ -10,6 +13,7 @@ public class Ports implements Iterable<Port> {
private ArrayList<Port> ports;
private ArrayList<Port> outputs;
private ArrayList<Port> inputs;
private HashSet<String> lowerCaseNames;
/**
* creates a new instance
@ -18,6 +22,7 @@ public class Ports implements Iterable<Port> {
ports = new ArrayList<>();
inputs = new ArrayList<>();
outputs = new ArrayList<>();
lowerCaseNames = new HashSet<>();
}
@Override
@ -30,8 +35,14 @@ public class Ports implements Iterable<Port> {
*
* @param port the port to add
* @return this for chained calls
* @throws HDLException if name is used twice
*/
public Ports add(Port port) {
public Ports add(Port port) throws HDLException {
String lowerCaseName = port.getName().toLowerCase();
if (lowerCaseNames.contains(lowerCaseName))
throw new HDLException(Lang.get("err_nameUsedTwice_N", port.getName()));
lowerCaseNames.add(lowerCaseName);
ports.add(port);
if (port.getDirection() == Port.Direction.in)
inputs.add(port);

View File

@ -1,11 +1,9 @@
package de.neemann.digital.hdl.model;
import org.junit.Test;
import junit.framework.TestCase;
import static org.junit.Assert.*;
public class PortTest extends TestCase {
public class PortTest {
@Test
public void testIsNameValid() throws Exception {
assertTrue(Port.isNameValid("test"));
assertTrue(Port.isNameValid("test_2"));
@ -17,4 +15,17 @@ public class PortTest {
assertFalse(Port.isNameValid(""));
}
public void testDuplicateName() throws HDLException {
Ports p = new Ports();
p.add(new Port("a", Port.Direction.out));
p.add(new Port("b", Port.Direction.out));
try {
p.add(new Port("A", Port.Direction.out));
fail();
} catch (HDLException e) {
// expected
}
}
}