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) {
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;

View File

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

View File

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

View File

@ -5,9 +5,7 @@ import org.junit.Test;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
public class OperandTest {
@ -127,28 +125,106 @@ public class OperandTest {
}
//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
}
try{ new Operand("a1", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("a_", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("_a", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("_1", labels, registerSet, 0); } catch (InvalidOperandException ignored){ }
try{ new Operand("S", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("label1_", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("+label1", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[- 12]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[12+1]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[+label1", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[*12]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[-A]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[A B]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[A + B]", labels, registerSet, 0); } 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{ new Operand("[]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[A+A+]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try{ new Operand("[A+[1]]", labels, registerSet, 0); } catch (InvalidOperandException ignored){}
try {
new Operand("a1", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("a_", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("_a", labels, registerSet, 0);
fail();
} catch (InvalidOperandException ignored) {
}
try {
new Operand("_1", labels, registerSet, 0);
fail();
} 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
@ -159,6 +235,23 @@ public class OperandTest {
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
public void binaryLiteral() throws Exception {
Operand op = new Operand("0b1000", new HashMap<>(), new DefaultRegisterSet(), 0);