Unit Tests

This commit is contained in:
2025-04-25 15:18:36 +02:00
parent ca36cffd47
commit ac854ed4f1
13 changed files with 399 additions and 39 deletions

25
pom.xml
View File

@ -80,6 +80,13 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.17.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -113,6 +120,24 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -4,6 +4,7 @@ import fabrik.simulator.pic16f84.interfaces.*;
public class Commands extends PICComponent implements CommandInterface {
private int wRegister;
private long totalExecutionTime;
private double executionTimeMultiplier = 1;
public Commands(){
super();
@ -22,7 +23,7 @@ public class Commands extends PICComponent implements CommandInterface {
}
public double getTotalExecutionTime() {
return (totalExecutionTime * frontendController.getExecutionTimeMultiplier());
return (totalExecutionTime * getExecutionTimeMultiplier());
}
public void resetTotalExecutionTime() {
@ -248,8 +249,6 @@ public class Commands extends PICComponent implements CommandInterface {
else{
System.out.println("Nicht gefunden!");
}
}
public void SLEEP() {
@ -557,9 +556,39 @@ public class Commands extends PICComponent implements CommandInterface {
}
}
public double getExecutionTimeMultiplier(){
return executionTimeMultiplier;
}
public void setExecutionTimeMultiplier(String option){
switch (option) {
case "8 MHZ":
executionTimeMultiplier = 0.5;
break;
case "4 MHZ":
executionTimeMultiplier = 1;
break;
case "1 MHZ":
executionTimeMultiplier = 4;
break;
case "500 HZ":
executionTimeMultiplier = 8;
break;
case "100 HZ":
executionTimeMultiplier = 40;
break;
case "32 HZ":
executionTimeMultiplier = 125;
break;
}
}
@Override
public void initialize(PICComponents picComponents) {
System.out.println("Commands");
super.initialize(picComponents);
}
}

View File

@ -43,13 +43,6 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private int [][] read;
private int [] ind;
private PICComponents picComponents;
private double executionTimeMultiplier = 1;
public double getExecutionTimeMultiplier(){
return executionTimeMultiplier;
}
private boolean isBreakpointReached = false;
@ -160,29 +153,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private void updateExecutionTimeMultiplier() {
String selectedOption = executionTimeComboBox.getValue();
switch (selectedOption) {
case "8 MHZ":
executionTimeMultiplier = 0.5;
break;
case "4 MHZ":
executionTimeMultiplier = 1;
break;
case "1 MHZ":
executionTimeMultiplier = 4;
break;
case "500 HZ":
executionTimeMultiplier = 8;
break;
case "100 HZ":
executionTimeMultiplier = 40;
break;
case "32 HZ":
executionTimeMultiplier = 125;
break;
}
commands.setExecutionTimeMultiplier(selectedOption);
}
@FXML

View File

@ -36,6 +36,7 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
}
public long read (int address) {
if (address < 0) return 0;
FileReader reader;
try {
reader = new FileReader("eeprom.json");
@ -60,6 +61,7 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
}
public void write (int address, long data) {
if (address < 0) return;
FileReader reader;
try {
reader = new FileReader("eeprom.json");
@ -172,7 +174,6 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
@Override
public void initialize(PICComponents picComponents) {
System.out.println("EEPROM");
super.initialize(picComponents);
}
}

View File

@ -5,7 +5,7 @@ import fabrik.simulator.pic16f84.interfaces.*;
public abstract class PICComponent {
DataRegisterInterface dataRegister;
EEPROMInterface eeprom;
PreScaler preScaler;
PreScalerInterface preScaler;
IOPortInterface ioPorts;
TimerInterface timer;
InterruptInterface interrupts;
@ -22,7 +22,7 @@ public abstract class PICComponent {
dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class);
eeprom = (EEPROMInterface) picComponents.getComponent(EEPROMInterface.class);
ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class);
preScaler = (PreScaler) picComponents.getComponent(PreScalerInterface.class);
preScaler = (PreScalerInterface) picComponents.getComponent(PreScalerInterface.class);
timer = (TimerInterface) picComponents.getComponent(TimerInterface.class);
interrupts = (InterruptInterface) picComponents.getComponent(InterruptInterface.class);
//table = (TableInterface) picComponents.getComponent(TableInterface.class);

View File

@ -26,7 +26,7 @@ public class WatchdogTimer extends PICComponent implements WatchdogTimerInterfac
}
else {
rawtimer++;
realtimer = (long) (rawtimer * frontendController.getExecutionTimeMultiplier());
realtimer = (long) (rawtimer * commands.getExecutionTimeMultiplier());
}
}
}

View File

@ -10,4 +10,10 @@ public interface CommandInterface extends PICComponentInterface {
void decode(int i);
void resetTotalExecutionTime();
void addExecutionTime(int i);
double getExecutionTimeMultiplier();
void setExecutionTimeMultiplier(String option);
}

View File

@ -4,4 +4,8 @@ public interface EEPROMInterface extends PICComponentInterface {
void registerTime(boolean b);
void parse(int i, int content, int i1);
long read(int address);
void write (int address, long data);
}

View File

@ -1,7 +1,6 @@
package fabrik.simulator.pic16f84.interfaces;
public interface FrontendControllerInterface extends PICComponentInterface {
double getExecutionTimeMultiplier();
void sleep();

View File

@ -0,0 +1,88 @@
package fabrik.simulator.pic16f84;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import fabrik.simulator.pic16f84.interfaces.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class CommandsTests {
PICComponents picComponents;
public CommandsTests() {}
@BeforeEach
void resetComponents() {
picComponents = new PICComponents();
}
@Test
@DisplayName("Execution Time wird korrekt addiert")
void executionTimeIsAddedCorrectly() {
TimerInterface timerMock = mock(TimerInterface.class);
picComponents.registerComponent(TimerInterface.class, timerMock);
EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();
commands.addExecutionTime(5);
assertEquals(5, commands.getTotalExecutionTime());
}
@Test
@DisplayName("Execution Time wird korrekt zurückgesetzt")
void executionTimeResetWorks() {
TimerInterface timerMock = mock(TimerInterface.class);
picComponents.registerComponent(TimerInterface.class, timerMock);
EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();
commands.addExecutionTime(5);
commands.resetTotalExecutionTime();
assertEquals(0, commands.getTotalExecutionTime());
}
@Test
@DisplayName("Execution Time Faktor wird berücksichtigt")
void executionTimeMultiplierIsUsed() {
TimerInterface timerMock = mock(TimerInterface.class);
picComponents.registerComponent(TimerInterface.class, timerMock);
EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();
commands.addExecutionTime(5);
commands.setExecutionTimeMultiplier("100 HZ");
assertEquals(5 * 40, commands.getTotalExecutionTime());
}
@Test
@DisplayName("Decode mit ungültigem Opcode wirft keine Exception")
void decodeInvalidOpcodeDoesNotCrash() {
CommandInterface commands = new Commands();
assertDoesNotThrow(() -> commands.decode(1));
}
}

View File

@ -0,0 +1,50 @@
package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
class DataRegisterTests {
PICComponents picComponents;
public DataRegisterTests() {}
@BeforeEach
void resetComponents() {
picComponents = new PICComponents();
}
@Test
@DisplayName("DataRegister wird durch Components initialisiert und enthält dann die korrekten Startwerte")
void registerGetsInitializedThroughComponents() {
IOPortInterface mockIOPorts = mock(IOPortInterface.class);
picComponents.registerComponent(IOPortInterface.class, mockIOPorts);
DataRegisterInterface dataRegister = new DataRegister();
picComponents.registerComponent(DataRegisterInterface.class, dataRegister);
picComponents.initAll();
int [] expected = {0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 255, 0, 24, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int [] actual = dataRegister.getDataRegister();
assertEquals(expected.length, actual.length);
for (int i = 0; i<expected.length; i++){
assertEquals(expected[i], actual[i]);
}
}
}

View File

@ -0,0 +1,137 @@
package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.CommandInterface;
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
import fabrik.simulator.pic16f84.interfaces.EEPROMInterface;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
class EEPROMTests {
PICComponents picComponents;
String savedEEPROM;
public EEPROMTests() {}
@BeforeEach
void resetComponentsAndReadEEPROM() throws IOException {
picComponents = new PICComponents();
savedEEPROM = Files.readString(Path.of("eeprom.json"));
}
@AfterEach
public void rewriteEEPROM() throws IOException {
FileWriter writer = new FileWriter("eeprom.json");
writer.write(savedEEPROM);
writer.close();
}
@Test
@DisplayName("EEPROM read gibt gültigen Wert zurück")
void eepromReadReturnsValidLong() throws IOException {
FileWriter writer = new FileWriter("eeprom.json");
writer.write("{\"0\": 255}");
writer.close();
DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class);
picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister);
EEPROMInterface eeprom = new EEPROM();
picComponents.registerComponent(EEPROMInterface.class, eeprom);
picComponents.initAll();
long value = eeprom.read(0);
assertEquals(255, value);
}
@Test
@DisplayName("EEPROM write schreibt an die korrekte Adresse in eeprom.json")
void eepromWritesCorrectly() throws IOException {
FileWriter writer = new FileWriter("eeprom.json");
writer.write("{}");
writer.close();
DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class);
picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister);
CommandInterface mockCommands = mock(CommandInterface.class);
picComponents.registerComponent(CommandInterface.class, mockCommands);
EEPROMInterface eeprom = new EEPROM();
picComponents.registerComponent(EEPROMInterface.class, eeprom);
picComponents.initAll();
eeprom.write(3, 123);
assertEquals("{\"3\":123}", Files.readString(Path.of("eeprom.json")));
}
@Test
@DisplayName("EEPROM write schreibt nicht an negative Adressen")
void eepromDoesntWriteNegative() throws IOException {
FileWriter writer = new FileWriter("eeprom.json");
writer.write("{}");
writer.close();
DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class);
picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister);
CommandInterface mockCommands = mock(CommandInterface.class);
picComponents.registerComponent(CommandInterface.class, mockCommands);
EEPROMInterface eeprom = new EEPROM();
picComponents.registerComponent(EEPROMInterface.class, eeprom);
picComponents.initAll();
eeprom.write(-1, 1);
assertEquals("{}", Files.readString(Path.of("eeprom.json")));
}
@Test
@DisplayName("EEPROM read mit negativem Wert liefert 0")
void eepromReadWithNegativeAddress() throws IOException {
FileWriter writer = new FileWriter("eeprom.json");
writer.write("{\"-1\":5}");
writer.close();
DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class);
picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister);
CommandInterface mockCommands = mock(CommandInterface.class);
picComponents.registerComponent(CommandInterface.class, mockCommands);
EEPROMInterface eeprom = new EEPROM();
picComponents.registerComponent(EEPROMInterface.class, eeprom);
picComponents.initAll();
long result = eeprom.read(-1);
assertEquals(0, result);
}
}

View File

@ -0,0 +1,50 @@
package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class WatchDogTimerTests {
private PICComponents picComponents;
@BeforeEach
void resetComponents() {
picComponents = new PICComponents();
}
@Test
@DisplayName("WatchDogTimer testet ob er triggern soll, triggert korrekt nicht")
void watchDogIncrementWithoutTriggering() {
PreScalerInterface mockPreScaler = mock(PreScalerInterface.class);
when(
mockPreScaler.isPrescalerOnTimer()
).thenReturn(
true
);
picComponents.registerComponent(PreScalerInterface.class, mockPreScaler);
CommandInterface mockCommands = mock(CommandInterface.class);
when(
mockCommands.getExecutionTimeMultiplier()
).thenReturn(
1.0
);
picComponents.registerComponent(CommandInterface.class, mockCommands);
WatchdogTimerInterface watchDogTimer = new WatchdogTimer();
picComponents.registerComponent(WatchdogTimerInterface.class, watchDogTimer);
picComponents.initAll();
watchDogTimer.enable();
watchDogTimer.testAndTrigger();
assertEquals(1, watchDogTimer.get());
}
}