Skip to content

Commit

Permalink
Oops, fixed test cases related to last commit :P
Browse files Browse the repository at this point in the history
  • Loading branch information
mnordahl committed Sep 25, 2024
1 parent 4c75ecc commit 7549754
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/test/java/instruction/LdATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,38 @@ void setup() {

@Test
void testLoadFromAddressOperation() {
int operand = Registry.nameToIdx(Registry.REG_R1); // Operand: register to load value into
int op1 = Registry.nameToIdx(Registry.REG_R1);
int op2 = Registry.nameToIdx(Registry.REG_RES);
int operand = (op1 << 4) | op2;
int value = 123; // The value to be loaded from memory
int memoryAddress = 33; // The address in memory where value is stored

// Simulate the program counter pointing to the next memory address
when(mockPC.next()).thenReturn(1);

// Simulate memory returning the memory address
when(mockMemory.getValueAt(1)).thenReturn(memoryAddress);
// Simulate fetching the operand from memory
when(mockMemory.getValueAt(1)).thenReturn(operand);

// Simulate memory returning the value from the memory address
// Simulate fetching the value from the register specified by the operand
when(mockRegistry.getValueAt(op1)).thenReturn(memoryAddress);
when(mockMemory.getValueAt(memoryAddress)).thenReturn(value);

LdA ldaInstruction = new LdA(operand);
LdA ldaInstruction = new LdA(0);
ldaInstruction.execute(mockMemory, mockRegistry, mockPC, null);

// Verify the registry's setRegister method is called with the correct value
verify(mockRegistry).setValueAt(operand, value);
verify(mockRegistry).setValueAt(op2, value);
}

@Test
void testPrettyPrint() {
when(mockMemory.getValueAt(1)).thenReturn(81);

LdA load = new LdA(Registry.nameToIdx(Registry.REG_R1));
LdA load = new LdA(0);
int leftOp = Registry.nameToIdx(Registry.REG_R2);
int rightOp = Registry.nameToIdx(Registry.REG_OP1);
int operand = (leftOp << 4) | rightOp;
when(mockMemory.getValueAt(1)).thenReturn(operand);
assertEquals(
InstructionFactory.INST_NAME_LDA + " (m[81] " + Instruction.RIGHT_ARROW_CHAR + " R1)",
InstructionFactory.INST_NAME_LDA + " (*R2 " + Instruction.RIGHT_ARROW_CHAR + " OP1)",
load.prettyPrint(mockMemory, mockRegistry, 0));
}
}

0 comments on commit 7549754

Please sign in to comment.