Skip to content

Commit

Permalink
feat(lih_tests): separate tests and add tests for each register;
Browse files Browse the repository at this point in the history
  • Loading branch information
5-pebbles committed Sep 12, 2024
1 parent 7d12eb4 commit 8d69c1d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 22 deletions.
55 changes: 34 additions & 21 deletions src/tests/logic_is_hard.rs → src/tests/lih/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,52 @@ use arbitrary_int::{u12, u6};
use crate::{compilation::compile_to_binary, emulation::InteractiveState, utils::tuple_as_u12};

#[test]
fn jump_if_eq() {
fn jump_not_taken_when_a_is_zero() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [A == 0] TEST\nHLT\nLAB TEST\nHLT");
let mc_result = compile_to_binary("LIH [A == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
// reset
state = InteractiveState::new();
}

#[test]
fn jump_taken_when_a_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [A == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.a = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_if_not_eq() {
fn jump_not_taken_when_b_is_zero() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [A != 0] TEST\nHLT\nLAB TEST\nHLT");
let mc_result = compile_to_binary("LIH [B == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
// reset
state = InteractiveState::new();
}

#[test]
fn jump_taken_when_b_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [B == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.a = u6::new(1);
state.b = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
Expand All @@ -47,25 +57,28 @@ fn jump_if_not_eq() {
}

#[test]
fn multiple_jumps() {
fn jump_not_taken_when_c_is_zero() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary(
"LIH [A == 0] TEST1\nLIH [A != 0] TEST2\nHLT\nLAB TEST1\nHLT\nLAB TEST2\nHLT",
);
let mc_result = compile_to_binary("LIH [C == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST1")
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
// reset
state = InteractiveState::new();
}

#[test]
fn jump_taken_when_c_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [C == 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.a = u6::new(1);
state.c = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST2")
mc_result.symbol_table.get("TEST")
);
}
Empty file added src/tests/lih/less_or_equal.rs
Empty file.
2 changes: 2 additions & 0 deletions src/tests/lih/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod equal;
mod not_equal;
83 changes: 83 additions & 0 deletions src/tests/lih/not_equal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::{compilation::compile_to_binary, emulation::InteractiveState, utils::tuple_as_u12};
use arbitrary_int::{u12, u6};

#[test]
fn jump_taken_when_a_is_not_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [A != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_not_taken_when_a_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [A != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.a = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_taken_when_b_is_not_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [B != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_not_taken_when_b_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [B != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.b = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_taken_when_c_is_not_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [C != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.consume_until_halt();
assert_eq!(
Some(&tuple_as_u12(state.program_counter.as_tuple())),
mc_result.symbol_table.get("TEST")
);
}

#[test]
fn jump_not_taken_when_c_is_one() {
let mut state = InteractiveState::new();
let mc_result = compile_to_binary("LIH [C != 1] TEST\nHLT\nLAB TEST\nHLT");
assert_eq!(mc_result.diagnostics.len(), 0);
state.memory.store_array(0, &mc_result.binary);
state.c = u6::new(1);
state.consume_until_halt();
assert_eq!(
Some(&(tuple_as_u12(state.program_counter.as_tuple()) + u12::new(1))),
mc_result.symbol_table.get("TEST")
);
}
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod halt_and_nop;
mod lab_and_pc;
mod logic_is_hard;
mod lih;
mod nor_and_or;
mod nxor_and_xor;
mod shift_and_rotate;

0 comments on commit 8d69c1d

Please sign in to comment.