Refactor switch tests to separate classes

This commit is contained in:
Bane Stojkovic
2022-10-09 22:04:44 +02:00
parent f9759b8cc4
commit 7d597c5ca2
8 changed files with 301 additions and 0 deletions
@@ -0,0 +1,28 @@
package org.diylc.netlist;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import org.diylc.core.IDIYComponent;
import org.diylc.core.ISwitch;
public abstract class AbstractSwitchTests {
protected void testSwitch(ISwitch sw, String[] validCombinations) {
Arrays.sort(validCombinations);
int count = 0;
for (int p = 0; p < sw.getPositionCount(); p++)
for (int i = 0; i < ((IDIYComponent<?>) sw).getControlPointCount(); i++)
for (int j = 0; j < ((IDIYComponent<?>) sw).getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = sw.arePointsConnected(i, j, p);
if (isConnected)
count++;
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0)
|| !isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
assertEquals(validCombinations.length, count);
}
}
@@ -0,0 +1,14 @@
package org.diylc.netlist;
import org.diylc.components.guitar.LPSwitch;
import org.junit.Test;
public class LPSwitchTests extends AbstractSwitchTests {
@Test
public void testLPSwitch() {
LPSwitch lpSwitch = new LPSwitch();
String[] validCombinations = new String[] {"0,1,2", "1,1,2", "1,2,3", "1,1,3", "2,2,3"};
testSwitch(lpSwitch, validCombinations);
}
}
@@ -0,0 +1,52 @@
package org.diylc.netlist;
import org.diylc.components.guitar.LeverSwitch;
import org.diylc.components.guitar.LeverSwitchType;
import org.junit.Test;
public class LeverSwitchTests extends AbstractSwitchTests {
@Test
public void testLeverSwitchDP5T() {
LeverSwitch leverSwitch = new LeverSwitch();
leverSwitch.setValue(LeverSwitchType.DP5T);
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "1,0,2", "1,7,11", "2,0,3",
"2,8,11", "3,0,4", "3,9,11", "4,0,5", "4,10,11"};
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitch4P5T() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "0,12,13", "0,18,23", "1,0,2",
"1,7,11", "1,12,14", "1,19,23", "2,0,3", "2,8,11", "2,12,15", "2,20,23", "3,0,4", "3,9,11",
"3,12,16", "3,21,23", "4,0,5", "4,10,11", "4,12,17", "4,22,23"};
leverSwitch.setValue(LeverSwitchType._4P5T);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitchImport() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,3", "0,4,5", "1,0,3", "1,4,5", "1,1,3",
"1,4,6", "2,1,3", "2,4,6", "3,1,3", "3,2,3", "3,4,6", "3,4,7", "4,2,3", "4,4,7"};
leverSwitch.setValue(LeverSwitchType.DP3T_5pos_Import);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitch6Way() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,8", "0,1,3", "0,1,5", "1,0,8", "1,2,8", "1,1,5", "2,2,8", "2,1,5", "2,1,7",
"3,2,8", "3,4,8", "3,1,7", "4,4,8", "4,1,7", "4,1,9", "5,4,8", "5,6,8", "5,1,9"};
leverSwitch.setValue(LeverSwitchType._6_WAY_OG);
testSwitch(leverSwitch, validCombinations);
}
}
@@ -0,0 +1,24 @@
package org.diylc.netlist;
import org.diylc.components.electromechanical.MiniToggleSwitch;
import org.diylc.components.electromechanical.ToggleSwitchType;
import org.junit.Test;
public class MiniToggleSwitchTests extends AbstractSwitchTests {
@Test
public void testMiniToggleSwitchDPDTononon1() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_1);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,0,1", "1,4,5", "2,1,2", "2,4,5"};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDTononon2() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_2);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,1,2", "1,3,4", "2,1,2", "2,4,5"};
testSwitch(toggleSwitch, validCombinations);
}
}
@@ -0,0 +1,58 @@
package org.diylc.netlist;
import org.diylc.components.electromechanical.RotarySwitchSealed;
import org.diylc.components.electromechanical.RotarySwitchType;
import org.junit.Test;
public abstract class RotarySwitchTests extends AbstractSwitchTests {
@Test
public void testRotarySwitchSP12T() {
RotarySwitchSealed sw = new RotarySwitchSealed();
String[] validCombinations = new String[] {"0,1,13", "1,2,13", "2,3,13", "3,4,13", "4,5,13", "5,6,13", "6,7,13", "7,8,13",
"8,9,13", "9,10,13", "10,11,13", "11,12,13"};
sw.setValue(RotarySwitchType._1P12T);
testSwitch(sw, validCombinations);
}
@Test
public void testRotarySwitchDP6T() {
RotarySwitchSealed sw = new RotarySwitchSealed();
String[] validCombinations = new String[] {
"0,1,13", "1,2,13", "2,3,13", "3,4,13", "4,5,13", "5,6,13",
"0,7,14", "1,8,14", "2,9,14", "3,10,14", "4,11,14", "5,12,14"};
sw.setValue(RotarySwitchType.DP6T);
testSwitch(sw, validCombinations);
}
@Test
public void testRotarySwitch3P4T() {
RotarySwitchSealed sw = new RotarySwitchSealed();
String[] validCombinations = new String[] {
"0,1,13", "1,2,13", "2,3,13", "3,4,13",
"0,5,14", "1,6,14", "2,7,14", "3,8,14",
"0,9,15", "1,10,15", "2,11,15", "3,12,15"};
sw.setValue(RotarySwitchType._3P4T);
testSwitch(sw, validCombinations);
}
@Test
public void testRotarySwitch4P3T() {
RotarySwitchSealed sw = new RotarySwitchSealed();
String[] validCombinations = new String[] {
"0,1,13", "1,2,13", "2,3,13",
"0,4,14", "1,5,14", "2,6,14",
"0,7,15", "1,8,15", "2,9,15",
"0,10,16", "1,11,16", "2,12,16",};
sw.setValue(RotarySwitchType._4P3T);
testSwitch(sw, validCombinations);
}
}
@@ -0,0 +1,16 @@
package org.diylc.netlist;
import org.diylc.components.guitar.S1Switch;
import org.junit.Test;
public class S1SwitchTests extends AbstractSwitchTests {
@Test
public void testS1Switch() {
S1Switch s1Switch = new S1Switch();
String[] validCombinations = new String[] {"0,1,2", "0,4,5", "0,7,8", "0,10,11", "1,2,3", "1,5,6", "1,8,9", "1,11,12"};
testSwitch(s1Switch, validCombinations);
}
}
@@ -0,0 +1,77 @@
package org.diylc.netlist;
import org.diylc.components.guitar.SchallerMegaSwitch;
import org.diylc.components.guitar.SchallerMegaSwitch.MegaSwitchType;
import org.junit.Test;
public class SchallerMegaSwitchTests extends AbstractSwitchTests {
@Test
public void testMegaSwitchE() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.E);
String[] validCombinations = new String[] {"0,2,6", "1,3,5", "1,2,6", "1,0,2", "2,3,4",
"2,2,5", "2,1,2", "3,3,4", "3,0,2", "3,1,2", "4,1,2"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchEPlus() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.E_PLUS);
String[] validCombinations = new String[] {"0,2,8", "1,3,6", "1,2,8", "1,0,2", "2,3,5",
"2,2,7", "2,1,2", "3,3,4", "3,0,2", "3,1,2", "4,1,2"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchP() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.P);
String[] validCombinations = new String[] {"0,3,4", "0,1,6", "1,3,4", "1,1,2", "1,0,4", "1,1,5",
"2,0,2", "2,1,6", "3,0,1", "3,3,4", "3,2,4", "3,1,6", "4,1,5"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchS() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.S);
String[] validCombinations = new String[] {"0,6,7", "0,2,3", "1,4,7", "1,6,7", "1,0,3", "1,2,3",
"2,4,7", "2,0,3", "3,4,7", "3,5,7", "3,0,3", "3,1,3", "4,5,7", "4,1,3"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchM() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.M);
String[] validCombinations = new String[] {"0,4,5", "0,10,11", "0,16,17", "0,22,23",
"1,3,5", "1,9,11", "1,15,17", "1,21,23",
"2,2,5", "2,8,11", "2,14,17", "2,20,23",
"3,1,5", "3,7,11", "3,13,17", "3,19,23",
"4,0,5", "4,6,11", "4,12,17", "4,18,23"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchT() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.T);
String[] validCombinations = new String[] {"0,6,7", "0,2,3", "1,4,7", "1,0,3",
"2,5,7", "2,1,3"};
testSwitch(sw, validCombinations);
}
}
@@ -0,0 +1,32 @@
package org.diylc.netlist;
import org.diylc.components.electromechanical.SlideSwitch;
import org.diylc.components.electromechanical.SlideSwitchType;
import org.junit.Test;
public abstract class SlideSwitchTests extends AbstractSwitchTests {
@Test
public void testSlideSwitchSPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.SPDT);
String[] validCombinations = new String[] {"0,0,1", "1,1,2"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DPDT);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDP3T() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DP3T);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5", "2,4,6", "2,5,7"};
testSwitch(slideSwitch, validCombinations);
}
}