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

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());
}
}