From e72e8b45c57f1018ec9d7584b132038a842e3cae Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Wed, 23 Jan 2019 10:08:30 +0100 Subject: [PATCH] implement setcc tests --- .../instruction/SetccInstructionTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Server/src/test/java/net/simon987/server/assembly/instruction/SetccInstructionTest.java diff --git a/Server/src/test/java/net/simon987/server/assembly/instruction/SetccInstructionTest.java b/Server/src/test/java/net/simon987/server/assembly/instruction/SetccInstructionTest.java new file mode 100644 index 0000000..8d6e524 --- /dev/null +++ b/Server/src/test/java/net/simon987/server/assembly/instruction/SetccInstructionTest.java @@ -0,0 +1,70 @@ +package net.simon987.server.assembly.instruction; + +import net.simon987.server.assembly.Operand; +import net.simon987.server.assembly.OperandType; + +import net.simon987.server.assembly.exception.*; + +import org.junit.Test; + +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; + +public class SetccInstructionTest { + /** + * Since SETCC is not an actual valid mnemonic, encoding the SetccInstruction class should throw an exception + */ + @Test + public void throwsInvalidMnemonicException() { + SetccInstruction instruction = new SetccInstruction(); + + boolean hasThrown = false; + try { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Operand operand = new Operand(OperandType.MEMORY_REG16, 1); + instruction.encode(stream, operand, 0); + } catch (AssemblyException exception) { + if (exception instanceof InvalidMnemonicException) { + hasThrown = true; + } + } + assertTrue(hasThrown); + } + + @Test + public void throwsIllegalOperandException() { + SetccInstruction instruction = new SetccInstruction(); + + boolean hasThrownForZeroOperands = false; + boolean oneOperandImmediatIsInvalid = false; + boolean hasThrownForTwoOperands = false; + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + try { + instruction.encode(stream, 0); + } catch (AssemblyException exception) { + if (exception instanceof IllegalOperandException) { + hasThrownForZeroOperands = true; + } + } + + try { + Operand o1 = new Operand(OperandType.MEMORY_REG16, 1); + Operand o2= new Operand(OperandType.MEMORY_REG16, 1); + instruction.encode(stream, o1, o2, 0); + } catch (AssemblyException exception) { + if (exception instanceof IllegalOperandException) { + hasThrownForTwoOperands = true; + } + } + + Operand invalidOperand = new Operand(OperandType.IMMEDIATE16, 0); + oneOperandImmediatIsInvalid = !instruction.operandValid(invalidOperand); + + assertTrue(hasThrownForZeroOperands); + assertTrue(hasThrownForTwoOperands); + assertTrue(oneOperandImmediatIsInvalid); + } +}