More tests, add octal with '0o' prefix, fixes #166

This commit is contained in:
simon987 2020-07-30 19:27:41 -04:00
parent 73dc9b1dca
commit 421a983910
4 changed files with 135 additions and 35 deletions

View File

@ -133,6 +133,14 @@ public class Operand {
} catch (NumberFormatException e2) { } catch (NumberFormatException e2) {
return false; return false;
} }
} else if (text.startsWith("0o")) {
try {
data = Integer.parseInt(text.substring(2), 8);
value = IMMEDIATE_VALUE;
return true;
} catch (NumberFormatException e2) {
return false;
}
} }
return false; return false;

View File

@ -14,10 +14,9 @@ import java.util.List;
*/ */
public class RegisterSet implements Target, MongoSerializable, Cloneable { public class RegisterSet implements Target, MongoSerializable, Cloneable {
// TODO configurable number of registers private int size = 0;
private static final int REG_COUNT = 8 + 1;
private final Register[] registers = new Register[REG_COUNT]; private final Register[] registers = new Register[16];
public RegisterSet() { public RegisterSet() {
@ -34,7 +33,7 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
name = name.toUpperCase(); name = name.toUpperCase();
for (int i = 1; i < REG_COUNT; i++) { for (int i = 1; i <= size; i++) {
if (registers[i].getName().equals(name)) { if (registers[i].getName().equals(name)) {
return i; return i;
} }
@ -80,7 +79,7 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
*/ */
@Override @Override
public int get(int address) { public int get(int address) {
if (address <= 0 || address >= REG_COUNT) { if (address <= 0 || address > size) {
return 0; return 0;
} }
@ -95,7 +94,7 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
*/ */
@Override @Override
public void set(int address, int value) { public void set(int address, int value) {
if (address <= 0 || address >= REG_COUNT) { if (address <= 0 || address > size) {
return; return;
} }
@ -120,17 +119,18 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
*/ */
public void put(int index, Register reg) { public void put(int index, Register reg) {
registers[index] = reg; registers[index] = reg;
size = Math.max(index, size);
} }
int size() { int size() {
return REG_COUNT - 1; return size;
} }
@Override @Override
public Document mongoSerialise() { public Document mongoSerialise() {
List<Document> registers = new ArrayList<>(); List<Document> registers = new ArrayList<>();
for (int i = 1; i < REG_COUNT; i++) { for (int i = 1; i <= size; i++) {
Document register = new Document(); Document register = new Document();
register.put("index", i); register.put("index", i);
@ -187,7 +187,7 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
public String toString() { public String toString() {
String str = ""; String str = "";
for (int i = 1; i < REG_COUNT; i++) { for (int i = 1; i <= size; i++) {
str += i + " " + getRegister(i).getName() + "=" + Util.toHex(getRegister(i).getValue()) + "\n"; str += i + " " + getRegister(i).getName() + "=" + Util.toHex(getRegister(i).getValue()) + "\n";
} }
@ -198,7 +198,7 @@ public class RegisterSet implements Target, MongoSerializable, Cloneable {
public RegisterSet clone() { public RegisterSet clone() {
RegisterSet rs = new RegisterSet(); RegisterSet rs = new RegisterSet();
for (int i = 1; i < REG_COUNT; i++) { for (int i = 1; i <= size; i++) {
rs.put(i, getRegister(i).clone()); rs.put(i, getRegister(i).clone());
} }
return rs; return rs;

View File

@ -184,8 +184,7 @@ function getOperandType(text, result) {
} }
//Check IMM //Check IMM
if (!isNaN(Number(text)) && Number(text) === Math.floor(Number(text)) && text.indexOf("o") === -1 if (!isNaN(Number(text)) && Number(text) === Math.floor(Number(text)) && text.indexOf("0e") !== 0) {
&& text.indexOf("0e") !== 0) {
return OPERAND_IMM; return OPERAND_IMM;
} }

View File

@ -5,9 +5,7 @@ import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.fail;
public class OperandTest { public class OperandTest {
@ -127,28 +125,106 @@ public class OperandTest {
} }
//Invalid operands //Invalid operands
try{ new Operand("aa", labels, registerSet, 0); } catch (InvalidOperandException ignored){ try {
new Operand("aa", labels, registerSet, 0);
} catch (InvalidOperandException ignored) {
//It's not a valid Operand; that's okay, just continue; VALID FOR ALL THE OTHER CATCH SENTENCES //It's not a valid Operand; that's okay, just continue; VALID FOR ALL THE OTHER CATCH SENTENCES
} }
try{ new Operand("a1", labels, registerSet, 0); } catch (InvalidOperandException ignored){} try {
try{ new Operand("a_", labels, registerSet, 0); } catch (InvalidOperandException ignored){} new Operand("a1", labels, registerSet, 0);
try{ new Operand("_a", labels, registerSet, 0); } catch (InvalidOperandException ignored){} fail();
try{ new Operand("_1", labels, registerSet, 0); } catch (InvalidOperandException ignored){ } } catch (InvalidOperandException ignored) {
try{ new Operand("S", labels, registerSet, 0); } catch (InvalidOperandException ignored){} }
try{ new Operand("label1_", labels, registerSet, 0); } catch (InvalidOperandException ignored){} try {
try{ new Operand("+label1", labels, registerSet, 0); } catch (InvalidOperandException ignored){} new Operand("a_", labels, registerSet, 0);
try{ new Operand("[- 12]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} fail();
try{ new Operand("[12+1]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} } catch (InvalidOperandException ignored) {
try{ new Operand("[+label1", labels, registerSet, 0); } catch (InvalidOperandException ignored){} }
try{ new Operand("[*12]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} try {
try{ new Operand("[-A]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} new Operand("_a", labels, registerSet, 0);
try{ new Operand("[A B]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} fail();
try{ new Operand("[A + B]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} } catch (InvalidOperandException ignored) {
try{ new Operand("[A + -1]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} }
try{ new Operand("[A + ]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} try {
try{ new Operand("[]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} new Operand("_1", labels, registerSet, 0);
try{ new Operand("[A+A+]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} fail();
try{ new Operand("[A+[1]]", labels, registerSet, 0); } catch (InvalidOperandException ignored){} } catch (InvalidOperandException ignored) {
}
try {
new Operand("S", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("label1_", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("+label1", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[- 12]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[12+1]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[+label1", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[*12]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[-A]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A B]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A + B]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A + -1]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A + ]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A+A+]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("[A+[1]]", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
} }
@Test @Test
@ -159,6 +235,23 @@ public class OperandTest {
assertEquals(384, op.getData()); assertEquals(384, op.getData());
} }
@Test
public void octalLiteralPrefix2() throws Exception {
Operand op = new Operand("0o600", new HashMap<>(), new DefaultRegisterSet(), 0);
assertEquals(OperandType.IMMEDIATE16, op.getType());
assertEquals(Operand.IMMEDIATE_VALUE, op.getValue());
assertEquals(384, op.getData());
}
@Test
public void octalLiteralPrefix2Invalid() {
try {
Operand op = new Operand("0o609", new HashMap<>(), new DefaultRegisterSet(), 0);
fail();
} catch (InvalidOperandException ignored) {
}
}
@Test @Test
public void binaryLiteral() throws Exception { public void binaryLiteral() throws Exception {
Operand op = new Operand("0b1000", new HashMap<>(), new DefaultRegisterSet(), 0); Operand op = new Operand("0b1000", new HashMap<>(), new DefaultRegisterSet(), 0);