Switches + Tests
This commit is contained in:
parent
803145617b
commit
e5c694fc53
5
pom.xml
5
pom.xml
@ -25,6 +25,11 @@
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.11.0-M1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -3,9 +3,12 @@ package de.ph87.electro.circuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -36,15 +39,37 @@ public class Circuit {
|
||||
return light;
|
||||
}
|
||||
|
||||
public void connect(final Junction a, final Junction b) {
|
||||
wires.add(new Wire(a, b));
|
||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y) {
|
||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, x, y);
|
||||
parts.add(switch1x1);
|
||||
others.add(switch1x1);
|
||||
return switch1x1;
|
||||
}
|
||||
|
||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y) {
|
||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, x, y);
|
||||
parts.add(switch1x2);
|
||||
others.add(switch1x2);
|
||||
return switch1x2;
|
||||
}
|
||||
|
||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y) {
|
||||
final PartSwitchCross switchCross = new PartSwitchCross(name, x, y);
|
||||
parts.add(switchCross);
|
||||
others.add(switchCross);
|
||||
return switchCross;
|
||||
}
|
||||
|
||||
public Wire connect(final Junction a, final Junction b) {
|
||||
final Wire wire = new Wire(a, b);
|
||||
wires.add(wire);
|
||||
return wire;
|
||||
}
|
||||
|
||||
public void evaluate() {
|
||||
wires.forEach(Wire::reset);
|
||||
parts.forEach(Part::reset);
|
||||
batteries.forEach(PartBattery::propagate);
|
||||
others.forEach(PartOther::evaluate);
|
||||
batteries.forEach(PartBattery::startPropagation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package de.ph87.electro.circuit.junction;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
@ -13,6 +14,8 @@ import java.util.List;
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class Junction {
|
||||
|
||||
private final Part owner;
|
||||
|
||||
@ToString.Include
|
||||
private final String name;
|
||||
|
||||
@ -24,7 +27,8 @@ public class Junction {
|
||||
|
||||
private final List<Wire> wires = new ArrayList<>();
|
||||
|
||||
public Junction(final String name, final int x, final int y) {
|
||||
public Junction(final Part owner, final String name, final int x, final int y) {
|
||||
this.owner = owner;
|
||||
this.name = name;
|
||||
this.position = new Point(x, y);
|
||||
}
|
||||
@ -33,28 +37,15 @@ public class Junction {
|
||||
voltage = Double.NaN;
|
||||
}
|
||||
|
||||
public void injectVoltage(final double newVoltage) throws ShortCircuit {
|
||||
public void propagate(final double newVoltage) throws ShortCircuit {
|
||||
if (voltage == newVoltage) {
|
||||
return;
|
||||
}
|
||||
if (Double.isNaN(voltage)) {
|
||||
voltage = newVoltage;
|
||||
owner.propagate(this);
|
||||
for (Wire wire : wires) {
|
||||
wire.propagateVoltage(this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
throw new ShortCircuit();
|
||||
}
|
||||
|
||||
public void propagateVoltage(final Wire source) throws ShortCircuit {
|
||||
if (voltage == source.getVoltage()) {
|
||||
return;
|
||||
}
|
||||
if (Double.isNaN(voltage)) {
|
||||
voltage = source.getVoltage();
|
||||
for (Wire wire : this.wires) {
|
||||
wire.propagateVoltage(this);
|
||||
wire.propagate(newVoltage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@ -26,14 +26,14 @@ public class Wire {
|
||||
voltage = Double.NaN;
|
||||
}
|
||||
|
||||
public void propagateVoltage(final Junction source) throws ShortCircuit {
|
||||
if (voltage == source.getVoltage()) {
|
||||
public void propagate(final double newVoltage) throws ShortCircuit {
|
||||
if (voltage == newVoltage) {
|
||||
return;
|
||||
}
|
||||
if (Double.isNaN(this.voltage)) {
|
||||
voltage = source.getVoltage();
|
||||
junction0.propagateVoltage(this);
|
||||
junction1.propagateVoltage(this);
|
||||
if (Double.isNaN(voltage)) {
|
||||
voltage = newVoltage;
|
||||
junction0.propagate(voltage);
|
||||
junction1.propagate(voltage);
|
||||
return;
|
||||
}
|
||||
throw new ShortCircuit();
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -30,4 +31,6 @@ public abstract class Part {
|
||||
junctions.forEach(Junction::reset);
|
||||
}
|
||||
|
||||
public abstract void propagate(final Junction source) throws ShortCircuit;
|
||||
|
||||
}
|
||||
|
||||
@ -20,20 +20,25 @@ public class PartBattery extends Part {
|
||||
public PartBattery(final String name, final int x, final int y, final double voltage) {
|
||||
super(name, x, y);
|
||||
this.voltage = voltage;
|
||||
minus = new Junction("-", 0, 1);
|
||||
plus = new Junction("+", 2, 1);
|
||||
minus = new Junction(this, "-", 0, 1);
|
||||
plus = new Junction(this, "+", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
}
|
||||
|
||||
public void propagate() {
|
||||
public void startPropagation() {
|
||||
try {
|
||||
shortCircuit = null;
|
||||
minus.injectVoltage(0);
|
||||
plus.injectVoltage(voltage);
|
||||
minus.propagate(0);
|
||||
plus.propagate(voltage);
|
||||
} catch (ShortCircuit e) {
|
||||
shortCircuit = e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -6,6 +6,4 @@ public abstract class PartOther extends Part {
|
||||
super(name, x, y);
|
||||
}
|
||||
|
||||
public abstract void evaluate();
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
@ -24,8 +25,8 @@ public class PartLight extends PartOther {
|
||||
public PartLight(final String name, final int x, final int y, final double maxVoltage) {
|
||||
super(name, x, y);
|
||||
this.maxVoltage = maxVoltage;
|
||||
minus = new Junction("", 0, 1);
|
||||
plus = new Junction("", 2, 1);
|
||||
minus = new Junction(this, "", 0, 1);
|
||||
plus = new Junction(this, "", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
}
|
||||
@ -35,7 +36,7 @@ public class PartLight extends PartOther {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
public void propagate(final Junction source) {
|
||||
voltage = abs(plus.getVoltage() - minus.getVoltage());
|
||||
if (voltage > maxVoltage) {
|
||||
defect = true;
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitch1x1 extends PartOther {
|
||||
|
||||
private final Junction common;
|
||||
|
||||
private final Junction output;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
|
||||
public PartSwitch1x1(final String name, final int x, final int y) {
|
||||
super(name, x, y);
|
||||
common = new Junction(this, "", 0, 1);
|
||||
output = new Junction(this, "", 2, 1);
|
||||
junctions.add(common);
|
||||
junctions.add(output);
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common) {
|
||||
if (state) {
|
||||
output.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == output) {
|
||||
if (state) {
|
||||
common.propagate(source.getVoltage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitch1x2 extends PartOther {
|
||||
|
||||
private final Junction common;
|
||||
|
||||
private final Junction output0;
|
||||
|
||||
private final Junction output1;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
|
||||
public PartSwitch1x2(final String name, final int x, final int y) {
|
||||
super(name, x, y);
|
||||
common = new Junction(this, "", 0, 1);
|
||||
output0 = new Junction(this, "", 2, 1);
|
||||
output1 = new Junction(this, "", 2, 1);
|
||||
junctions.add(common);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common) {
|
||||
if (state) {
|
||||
output1.propagate(source.getVoltage());
|
||||
} else {
|
||||
output0.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == output0) {
|
||||
if (!state) {
|
||||
common.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == output1) {
|
||||
if (state) {
|
||||
common.propagate(source.getVoltage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.junction.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartSwitchCross extends PartOther {
|
||||
|
||||
private final Junction common0;
|
||||
|
||||
private final Junction common1;
|
||||
|
||||
private final Junction output0;
|
||||
|
||||
private final Junction output1;
|
||||
|
||||
@Setter
|
||||
private boolean state = false;
|
||||
|
||||
public PartSwitchCross(final String name, final int x, final int y) {
|
||||
super(name, x, y);
|
||||
common0 = new Junction(this, "", 0, 1);
|
||||
common1 = new Junction(this, "", 0, 1);
|
||||
output0 = new Junction(this, "", 2, 1);
|
||||
output1 = new Junction(this, "", 2, 1);
|
||||
junctions.add(common0);
|
||||
junctions.add(common1);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) throws ShortCircuit {
|
||||
if (source == common0) {
|
||||
if (state) {
|
||||
output1.propagate(source.getVoltage());
|
||||
} else {
|
||||
output0.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == common1) {
|
||||
if (state) {
|
||||
output0.propagate(source.getVoltage());
|
||||
} else {
|
||||
output1.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == output0) {
|
||||
if (state) {
|
||||
common1.propagate(source.getVoltage());
|
||||
} else {
|
||||
common0.propagate(source.getVoltage());
|
||||
}
|
||||
} else if (source == output1) {
|
||||
if (state) {
|
||||
common0.propagate(source.getVoltage());
|
||||
} else {
|
||||
common1.propagate(source.getVoltage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
41
src/test/java/de/ph87/electro/circuit/BatteryLightTest.java
Normal file
41
src/test/java/de/ph87/electro/circuit/BatteryLightTest.java
Normal file
@ -0,0 +1,41 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class BatteryLightTest {
|
||||
|
||||
private static final double VOLTAGE = 3;
|
||||
|
||||
private final Circuit circuit = new Circuit();
|
||||
|
||||
private final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
|
||||
private final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
|
||||
private final Wire plus = circuit.connect(battery.getPlus(), light.getPlus());
|
||||
|
||||
private final Wire minus = circuit.connect(light.getMinus(), battery.getMinus());
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, plus.getVoltage());
|
||||
|
||||
assertEquals(VOLTAGE, light.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, light.getVoltage());
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
assertEquals(0, minus.getVoltage());
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class BatterySwitcher1x1Test {
|
||||
|
||||
private static final double VOLTAGE = 3;
|
||||
|
||||
private final Circuit circuit = new Circuit();
|
||||
|
||||
private final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
|
||||
private final PartSwitch1x1 switcher = circuit.addSwitch1x1("Ein-/Ausschalter", 1, 1);
|
||||
|
||||
private final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
|
||||
private final Wire plus = circuit.connect(battery.getPlus(), switcher.getCommon());
|
||||
|
||||
private final Wire output = circuit.connect(switcher.getOutput(), light.getPlus());
|
||||
|
||||
private final Wire minus = circuit.connect(light.getMinus(), battery.getMinus());
|
||||
|
||||
@Test
|
||||
public void test0() {
|
||||
test(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
test(true);
|
||||
}
|
||||
|
||||
private void test(final boolean state) {
|
||||
final double voltage = state ? VOLTAGE : Double.NaN;
|
||||
|
||||
switcher.setState(state);
|
||||
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(state, switcher.isState());
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, plus.getVoltage());
|
||||
|
||||
assertEquals(VOLTAGE, switcher.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher.getOutput().getVoltage());
|
||||
assertEquals(voltage, output.getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, minus.getVoltage());
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,80 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class BatterySwitcher1x2Test {
|
||||
|
||||
private static final double VOLTAGE = 3;
|
||||
|
||||
private final Circuit circuit = new Circuit();
|
||||
|
||||
private final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
|
||||
private final PartSwitch1x2 switcher = circuit.addSwitch1x2("Wechselschalter", 1, 1);
|
||||
|
||||
private final PartLight light0 = circuit.addLight("Licht 0", 1, 1, VOLTAGE);
|
||||
|
||||
private final PartLight light1 = circuit.addLight("Licht 1", 1, 1, VOLTAGE);
|
||||
|
||||
private final Wire plus = circuit.connect(battery.getPlus(), switcher.getCommon());
|
||||
|
||||
private final Wire output0 = circuit.connect(switcher.getOutput0(), light0.getPlus());
|
||||
|
||||
private final Wire minus0 = circuit.connect(light0.getMinus(), battery.getMinus());
|
||||
|
||||
private final Wire output1 = circuit.connect(switcher.getOutput1(), light1.getPlus());
|
||||
|
||||
private final Wire minus1 = circuit.connect(light1.getMinus(), battery.getMinus());
|
||||
|
||||
@Test
|
||||
public void test0() {
|
||||
test(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
test(true);
|
||||
}
|
||||
|
||||
private void test(final boolean state) {
|
||||
final double voltage0 = state ? Double.NaN : VOLTAGE;
|
||||
final double voltage1 = state ? VOLTAGE : Double.NaN;
|
||||
|
||||
switcher.setState(state);
|
||||
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(state, switcher.isState());
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, plus.getVoltage());
|
||||
|
||||
assertEquals(VOLTAGE, switcher.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage0, switcher.getOutput0().getVoltage());
|
||||
assertEquals(voltage0, output0.getVoltage());
|
||||
assertEquals(voltage0, light0.getPlus().getVoltage());
|
||||
assertEquals(voltage0, light0.getVoltage());
|
||||
assertEquals(0, light0.getMinus().getVoltage());
|
||||
assertEquals(0, minus0.getVoltage());
|
||||
assertFalse(light0.isDefect());
|
||||
|
||||
assertEquals(voltage1, switcher.getOutput1().getVoltage());
|
||||
assertEquals(voltage1, output1.getVoltage());
|
||||
assertEquals(voltage1, light1.getPlus().getVoltage());
|
||||
assertEquals(voltage1, light1.getVoltage());
|
||||
assertEquals(0, light1.getMinus().getVoltage());
|
||||
assertEquals(0, minus1.getVoltage());
|
||||
assertFalse(light1.isDefect());
|
||||
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,94 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class BatterySwitcher2x2Test {
|
||||
|
||||
private static final double VOLTAGE = 3;
|
||||
|
||||
private final Circuit circuit = new Circuit();
|
||||
|
||||
private final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
|
||||
private final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 1, 1);
|
||||
|
||||
private final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1);
|
||||
|
||||
private final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
|
||||
private final Wire plus = circuit.connect(battery.getPlus(), switcher0.getCommon());
|
||||
|
||||
private final Wire switcher00 = circuit.connect(switcher0.getOutput0(), switcher1.getOutput0());
|
||||
|
||||
private final Wire switcher01 = circuit.connect(switcher0.getOutput1(), switcher1.getOutput1());
|
||||
|
||||
private final Wire output = circuit.connect(switcher1.getCommon(), light.getPlus());
|
||||
|
||||
private final Wire minus = circuit.connect(light.getMinus(), battery.getMinus());
|
||||
|
||||
@Test
|
||||
public void test00() {
|
||||
test(false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test10() {
|
||||
test(true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test01() {
|
||||
test(false, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test11() {
|
||||
test(true, true);
|
||||
}
|
||||
|
||||
private void test(final boolean state0, final boolean state1) {
|
||||
final double voltage0 = state0 ? Double.NaN : VOLTAGE;
|
||||
final double voltage1 = state0 ? VOLTAGE : Double.NaN;
|
||||
final double voltage = state1 ? voltage1 : voltage0;
|
||||
|
||||
switcher0.setState(state0);
|
||||
switcher1.setState(state1);
|
||||
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(state0, switcher0.isState());
|
||||
assertEquals(state1, switcher1.isState());
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, plus.getVoltage());
|
||||
|
||||
assertEquals(VOLTAGE, switcher0.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage0, switcher0.getOutput0().getVoltage());
|
||||
assertEquals(voltage0, switcher00.getVoltage());
|
||||
assertEquals(voltage0, switcher1.getOutput0().getVoltage());
|
||||
|
||||
assertEquals(voltage1, switcher0.getOutput1().getVoltage());
|
||||
assertEquals(voltage1, switcher01.getVoltage());
|
||||
assertEquals(voltage1, switcher1.getOutput1().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher1.getCommon().getVoltage());
|
||||
assertEquals(voltage, output.getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, minus.getVoltage());
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,132 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.junction.Wire;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class BatterySwitcherCrossTest {
|
||||
|
||||
private static final double VOLTAGE = 3;
|
||||
|
||||
private final Circuit circuit = new Circuit();
|
||||
|
||||
private final PartBattery battery = circuit.addBattery("Batterie", 1, 0, VOLTAGE);
|
||||
|
||||
private final PartSwitch1x2 switcher0 = circuit.addSwitch1x2("Wechselschalter 0", 1, 1);
|
||||
|
||||
private final PartSwitchCross switcherX = circuit.addSwitchCross("Kreuzschalter", 1, 1);
|
||||
|
||||
private final PartSwitch1x2 switcher1 = circuit.addSwitch1x2("Wechselschalter 1", 1, 1);
|
||||
|
||||
private final PartLight light = circuit.addLight("Licht", 1, 1, VOLTAGE);
|
||||
|
||||
private final Wire plus = circuit.connect(battery.getPlus(), switcher0.getCommon());
|
||||
|
||||
private final Wire switcher00 = circuit.connect(switcher0.getOutput0(), switcherX.getCommon0());
|
||||
|
||||
private final Wire switcher01 = circuit.connect(switcher0.getOutput1(), switcherX.getCommon1());
|
||||
|
||||
private final Wire switcherX0 = circuit.connect(switcherX.getOutput0(), switcher1.getOutput0());
|
||||
|
||||
private final Wire switcherX1 = circuit.connect(switcherX.getOutput1(), switcher1.getOutput1());
|
||||
|
||||
private final Wire output = circuit.connect(switcher1.getCommon(), light.getPlus());
|
||||
|
||||
private final Wire minus = circuit.connect(light.getMinus(), battery.getMinus());
|
||||
|
||||
@Test
|
||||
public void test000() {
|
||||
test(false, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test100() {
|
||||
test(true, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test010() {
|
||||
test(false, true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test110() {
|
||||
test(true, true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test001() {
|
||||
test(false, false, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test101() {
|
||||
test(true, false, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test011() {
|
||||
test(false, true, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test111() {
|
||||
test(true, true, true);
|
||||
}
|
||||
|
||||
private void test(final boolean state0, final boolean stateX, final boolean state1) {
|
||||
final double voltage00 = state0 ? Double.NaN : VOLTAGE;
|
||||
final double voltage01 = state0 ? VOLTAGE : Double.NaN;
|
||||
final double voltageX0 = stateX ? voltage01 : voltage00;
|
||||
final double voltageX1 = stateX ? voltage00 : voltage01;
|
||||
final double voltage = state1 ? voltageX1 : voltageX0;
|
||||
|
||||
switcher0.setState(state0);
|
||||
switcherX.setState(stateX);
|
||||
switcher1.setState(state1);
|
||||
|
||||
circuit.evaluate();
|
||||
|
||||
assertEquals(state0, switcher0.isState());
|
||||
assertEquals(stateX, switcherX.isState());
|
||||
assertEquals(state1, switcher1.isState());
|
||||
|
||||
assertEquals(VOLTAGE, battery.getPlus().getVoltage());
|
||||
assertEquals(VOLTAGE, plus.getVoltage());
|
||||
assertEquals(VOLTAGE, switcher0.getCommon().getVoltage());
|
||||
|
||||
assertEquals(voltage00, switcher0.getOutput0().getVoltage());
|
||||
assertEquals(voltage00, switcher00.getVoltage());
|
||||
assertEquals(voltage00, switcherX.getCommon0().getVoltage());
|
||||
|
||||
assertEquals(voltage01, switcher0.getOutput1().getVoltage());
|
||||
assertEquals(voltage01, switcher01.getVoltage());
|
||||
assertEquals(voltage01, switcherX.getCommon1().getVoltage());
|
||||
|
||||
assertEquals(voltageX0, switcherX.getOutput0().getVoltage());
|
||||
assertEquals(voltageX0, switcherX0.getVoltage());
|
||||
assertEquals(voltageX0, switcher1.getOutput0().getVoltage());
|
||||
|
||||
assertEquals(voltageX1, switcherX.getOutput1().getVoltage());
|
||||
assertEquals(voltageX1, switcherX1.getVoltage());
|
||||
assertEquals(voltageX1, switcher1.getOutput1().getVoltage());
|
||||
|
||||
assertEquals(voltage, switcher1.getCommon().getVoltage());
|
||||
assertEquals(voltage, output.getVoltage());
|
||||
assertEquals(voltage, light.getPlus().getVoltage());
|
||||
assertEquals(voltage, light.getVoltage());
|
||||
|
||||
assertEquals(0, light.getMinus().getVoltage());
|
||||
assertEquals(0, minus.getVoltage());
|
||||
assertFalse(light.isDefect());
|
||||
|
||||
assertEquals(0, battery.getMinus().getVoltage());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user