mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
fix tests
This commit is contained in:
@@ -44,7 +44,9 @@ public class SwitchUtils {
|
||||
List<Integer> commonPoints = new ArrayList<>();
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
final int finalI = i;
|
||||
if (switchingMatrix.stream().allMatch(
|
||||
if (!switchingMatrix.stream().allMatch(
|
||||
l -> l.isEmpty()) &&
|
||||
switchingMatrix.stream().allMatch(
|
||||
l -> l.isEmpty() ||
|
||||
l.stream().anyMatch(
|
||||
s -> s.contains(finalI)))) {
|
||||
@@ -71,69 +73,4 @@ public class SwitchUtils {
|
||||
}
|
||||
return markers;
|
||||
}
|
||||
|
||||
public static List<Set<Integer>> getConnectedTerminals(ISwitch switchComponent, int numPoints) {
|
||||
|
||||
if (!Boolean.TRUE.equals(switchComponent.getHighlightConnectedTerminals())) {
|
||||
return List.of();
|
||||
}
|
||||
List<Set<Integer>> connectedGroups = new ArrayList<>();
|
||||
|
||||
// Only process if we have a selected position and should render connections
|
||||
if (switchComponent.getSelectedPosition() == null || !Boolean.TRUE.equals(
|
||||
switchComponent.getHighlightConnectedTerminals())) {
|
||||
return connectedGroups;
|
||||
}
|
||||
|
||||
// First, collect all direct connections
|
||||
List<int[]> connections = new ArrayList<>();
|
||||
for (int i = 0; i < numPoints; i++) {
|
||||
for (int j = i + 1; j < numPoints; j++) {
|
||||
if (switchComponent.arePointsConnected(i, j, switchComponent.getSelectedPosition())) {
|
||||
connections.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a map to track which points are in which group
|
||||
Map<Integer, Set<Integer>> pointToGroup = new HashMap<>();
|
||||
|
||||
// Process each connection
|
||||
for (int[] connection : connections) {
|
||||
int p1 = connection[0];
|
||||
int p2 = connection[1];
|
||||
|
||||
Set<Integer> group1 = pointToGroup.get(p1);
|
||||
Set<Integer> group2 = pointToGroup.get(p2);
|
||||
|
||||
if (group1 == null && group2 == null) {
|
||||
// Create new group
|
||||
Set<Integer> newGroup = new HashSet<>();
|
||||
newGroup.add(p1);
|
||||
newGroup.add(p2);
|
||||
pointToGroup.put(p1, newGroup);
|
||||
pointToGroup.put(p2, newGroup);
|
||||
} else if (group1 == null) {
|
||||
// Add p1 to p2's group
|
||||
group2.add(p1);
|
||||
pointToGroup.put(p1, group2);
|
||||
} else if (group2 == null) {
|
||||
// Add p2 to p1's group
|
||||
group1.add(p2);
|
||||
pointToGroup.put(p2, group1);
|
||||
} else if (group1 != group2) {
|
||||
// Merge groups
|
||||
group1.addAll(group2);
|
||||
for (Integer p : group2) {
|
||||
pointToGroup.put(p, group1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Collect unique groups
|
||||
Set<Set<Integer>> uniqueGroups = new HashSet<>(pointToGroup.values());
|
||||
connectedGroups.addAll(uniqueGroups);
|
||||
|
||||
return connectedGroups;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,22 +10,25 @@ public class SwitchUtilsTest {
|
||||
|
||||
// Mock switch implementation for testing
|
||||
private static class MockSwitch implements ISwitch {
|
||||
private final List<int[]> connections;
|
||||
private final List<List<int[]>> connectionsByPosition;
|
||||
private final Integer selectedPosition;
|
||||
private final boolean renderConnected;
|
||||
private final int positionCount;
|
||||
|
||||
public MockSwitch(List<int[]> connections, Integer selectedPosition, boolean renderConnected) {
|
||||
this.connections = connections;
|
||||
public MockSwitch(List<List<int[]>> connectionsByPosition, Integer selectedPosition, boolean renderConnected) {
|
||||
this.connectionsByPosition = connectionsByPosition;
|
||||
this.selectedPosition = selectedPosition;
|
||||
this.renderConnected = renderConnected;
|
||||
this.positionCount = connectionsByPosition.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean arePointsConnected(int index1, int index2, int position) {
|
||||
if (position != selectedPosition) return false;
|
||||
return connections.stream()
|
||||
.anyMatch(conn -> (conn[0] == index1 && conn[1] == index2) ||
|
||||
(conn[0] == index2 && conn[1] == index1));
|
||||
if (position >= connectionsByPosition.size()) return false;
|
||||
return connectionsByPosition.get(position).stream()
|
||||
.anyMatch(conn ->
|
||||
(conn[0] == index1 && conn[1] == index2) ||
|
||||
(conn[0] == index2 && conn[1] == index1));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,99 +40,147 @@ public class SwitchUtilsTest {
|
||||
return renderConnected;
|
||||
}
|
||||
|
||||
// Other interface methods not needed for testing
|
||||
@Override public int getPositionCount() { return 0; }
|
||||
@Override public String getPositionName(int position) { return null; }
|
||||
@Override
|
||||
public int getPositionCount() {
|
||||
return positionCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPositionName(int position) {
|
||||
return String.valueOf(position + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleSPSTSwitch() {
|
||||
// Create a simple SPST switch with 2 points
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}) // Position 0
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 2, true);
|
||||
|
||||
// Both points are common in the only position
|
||||
assertEquals("A", markers[0]);
|
||||
assertEquals("B", markers[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSPDTSwitch() {
|
||||
// Create an SPDT switch with 3 points
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}), // Position 0
|
||||
Arrays.asList(new int[]{0, 2}) // Position 1
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 3, true);
|
||||
|
||||
// Point 0 is common in all positions
|
||||
assertEquals("A", markers[0]);
|
||||
// Point 1 is connected to point 0 in position 0
|
||||
assertEquals("A1", markers[1]);
|
||||
// Point 2 is connected to point 0 in position 1
|
||||
assertEquals("A2", markers[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDPDTSwitch() {
|
||||
// Create a DPDT switch with 6 points
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}, new int[]{3, 4}), // Position 0
|
||||
Arrays.asList(new int[]{0, 2}, new int[]{3, 5}) // Position 1
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 6, true);
|
||||
|
||||
// Points 0 and 3 are common in all positions
|
||||
assertEquals("A", markers[0]);
|
||||
assertEquals("A1", markers[1]);
|
||||
assertEquals("A2", markers[2]);
|
||||
assertEquals("B", markers[3]);
|
||||
assertEquals("B1", markers[4]);
|
||||
assertEquals("B2", markers[5]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonVerboseMarkers() {
|
||||
// Create a DPDT switch with 6 points
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}, new int[]{3, 4}), // Position 0
|
||||
Arrays.asList(new int[]{0, 2}, new int[]{3, 5}) // Position 1
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 6, false);
|
||||
|
||||
// Points 0 and 3 are common in all positions
|
||||
assertEquals("A", markers[0]);
|
||||
assertEquals("1", markers[1]);
|
||||
assertEquals("2", markers[2]);
|
||||
assertEquals("B", markers[3]);
|
||||
assertEquals("1", markers[4]);
|
||||
assertEquals("2", markers[5]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiplePositions() {
|
||||
// Test switch with multiple positions
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}), // Position 0
|
||||
Arrays.asList(new int[]{0, 2}), // Position 1
|
||||
Arrays.asList(new int[]{0, 3}) // Position 2
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 4, true);
|
||||
|
||||
// Point 0 is common in all positions
|
||||
assertEquals("A", markers[0]);
|
||||
// Points 1, 2, 3 are connected to point 0 in positions 0, 1, 2 respectively
|
||||
assertEquals("A1", markers[1]);
|
||||
assertEquals("A2", markers[2]);
|
||||
assertEquals("A3", markers[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoConnections() {
|
||||
MockSwitch mockSwitch = new MockSwitch(Collections.emptyList(), 0, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 5);
|
||||
assertTrue("Should return empty list when no connections", groups.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleConnection() {
|
||||
List<int[]> connections = Arrays.asList(new int[]{0, 1});
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, 0, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 5);
|
||||
|
||||
assertEquals("Should have one group", 1, groups.size());
|
||||
assertTrue("Group should contain points 0 and 1",
|
||||
groups.get(0).containsAll(Arrays.asList(0, 1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransitiveConnections() {
|
||||
// Test case where 1-2-3 are connected through 2
|
||||
List<int[]> connections = Arrays.asList(
|
||||
new int[]{1, 2},
|
||||
new int[]{2, 3}
|
||||
// Test switch with no connections
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Collections.emptyList() // Position 0 with no connections
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, 0, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 5);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
assertEquals("Should have one group", 1, groups.size());
|
||||
assertTrue("Group should contain points 1, 2, and 3",
|
||||
groups.get(0).containsAll(Arrays.asList(1, 2, 3)));
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 3, true);
|
||||
|
||||
// No common points when there are no connections
|
||||
assertNull(markers[0]);
|
||||
assertNull(markers[1]);
|
||||
assertNull(markers[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleGroups() {
|
||||
// Test case with two separate groups: {1,2,3} and {4,6,7}
|
||||
List<int[]> connections = Arrays.asList(
|
||||
new int[]{2, 3},
|
||||
new int[]{1, 3},
|
||||
new int[]{4, 7},
|
||||
new int[]{4, 6}
|
||||
public void testComplexSwitch() {
|
||||
// Test a more complex switch with multiple poles and positions
|
||||
List<List<int[]>> connectionsByPosition = Arrays.asList(
|
||||
Arrays.asList(new int[]{0, 1}, new int[]{3, 4}, new int[]{6, 7}), // Position 0
|
||||
Arrays.asList(new int[]{0, 2}, new int[]{3, 5}, new int[]{6, 8}) // Position 1
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, 0, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 8);
|
||||
MockSwitch mockSwitch = new MockSwitch(connectionsByPosition, 0, true);
|
||||
|
||||
assertEquals("Should have two groups", 2, groups.size());
|
||||
assertTrue("Groups should contain {1,2,3} and {4,6,7}",
|
||||
groups.stream().anyMatch(g -> g.containsAll(Arrays.asList(1, 2, 3))) &&
|
||||
groups.stream().anyMatch(g -> g.containsAll(Arrays.asList(4, 6, 7))));
|
||||
}
|
||||
String[] markers = SwitchUtils.getSwitchingMarkers(mockSwitch, 9, true);
|
||||
|
||||
@Test
|
||||
public void testRenderConnectedFalse() {
|
||||
List<int[]> connections = Arrays.asList(new int[]{0, 1});
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, 0, false);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 5);
|
||||
assertTrue("Should return empty list when renderConnected is false", groups.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSelectedPosition() {
|
||||
List<int[]> connections = Arrays.asList(new int[]{0, 1});
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, null, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 5);
|
||||
assertTrue("Should return empty list when no position selected", groups.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexConnections() {
|
||||
// Test case with multiple interconnected points
|
||||
List<int[]> connections = Arrays.asList(
|
||||
new int[]{0, 1},
|
||||
new int[]{1, 2},
|
||||
new int[]{2, 3},
|
||||
new int[]{3, 0}, // Creates a circle
|
||||
new int[]{4, 5},
|
||||
new int[]{5, 6},
|
||||
new int[]{7, 8} // Separate pair
|
||||
);
|
||||
MockSwitch mockSwitch = new MockSwitch(connections, 0, true);
|
||||
List<Set<Integer>> groups = SwitchUtils.getConnectedTerminals(mockSwitch, 9);
|
||||
|
||||
assertEquals("Should have three groups", 3, groups.size());
|
||||
assertTrue("Should have group with points 0,1,2,3",
|
||||
groups.stream().anyMatch(g -> g.containsAll(Arrays.asList(0, 1, 2, 3))));
|
||||
assertTrue("Should have group with points 4,5,6",
|
||||
groups.stream().anyMatch(g -> g.containsAll(Arrays.asList(4, 5, 6))));
|
||||
assertTrue("Should have group with points 7,8",
|
||||
groups.stream().anyMatch(g -> g.containsAll(Arrays.asList(7, 8))));
|
||||
// Points 0, 3, and 6 are common in all positions
|
||||
assertEquals("A", markers[0]);
|
||||
assertEquals("A1", markers[1]);
|
||||
assertEquals("A2", markers[2]);
|
||||
assertEquals("B", markers[3]);
|
||||
assertEquals("B1", markers[4]);
|
||||
assertEquals("B2", markers[5]);
|
||||
assertEquals("C", markers[6]);
|
||||
assertEquals("C1", markers[7]);
|
||||
assertEquals("C2", markers[8]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user