From a65ab6aafad36d26e52c59d3e6135c09496f670d Mon Sep 17 00:00:00 2001 From: Jur van den Berg Date: Tue, 22 May 2018 01:42:28 +0200 Subject: [PATCH] Add tests for task 1 and 2 --- files/bonus/brainfuck/dank.bf | 106 +++++++++++++++++++++++++++ files/bonus/brainfuck/hello_world.bf | 14 ++++ files/task3/IFEQ1 | 27 +++++++ files/task3/IFLT1 | 30 ++++++++ files/testmem.bin | Bin 0 -> 47 bytes src/block.rs | 10 +++ src/lib.rs | 2 + tests/task1.rs | 34 +++++++++ tests/task2.rs | 88 ++++++++++++++++++++++ 9 files changed, 311 insertions(+) create mode 100644 files/bonus/brainfuck/dank.bf create mode 100644 files/bonus/brainfuck/hello_world.bf create mode 100644 files/task3/IFEQ1 create mode 100644 files/task3/IFLT1 create mode 100644 files/testmem.bin create mode 100644 tests/task1.rs create mode 100644 tests/task2.rs diff --git a/files/bonus/brainfuck/dank.bf b/files/bonus/brainfuck/dank.bf new file mode 100644 index 0000000..9e87944 --- /dev/null +++ b/files/bonus/brainfuck/dank.bf @@ -0,0 +1,106 @@ +```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` +`````````````````````````````````````````````````````````````````/`````````````````````````````````````````````````````````````` +````````````````````````````````````````````````````````````````/[`````````````````````````````````````````````````````````````` +```````````````````````````````````````````````````````````````/-]>````````````````````````````````````````````````````````````` +```````````````````````````````````````````````````````````````/[-/````````````````````````````````````````````````````````````` +``````````````````````````````````````````````````````````````;]@@@<```````````````````````````````````````````````````````````` +``````````````````````````````````````````````````````````````>++++;```````````````````````````````````````````````````````````` +`````````````````````````````````````````````````````````````/++/;++;``````````````````````````````````````````````````````````` +````````````````````````````````````````````````````````````//+[@@@@````````````````````````````````````````````````````````` +``````````````````````````````````````````````````````````//-;;;;;]@@@<````````````````````````````````````````````````````````` +``````````````````````````````````````````````````````````/----@@@@@@@@.```````````````````````````````````````````````````````` +`````````````````````````````````````````````````````````>++++++[@@@@@@<```````````````````````````````````````````````````````` +`````````````````````````````````````````````````````````;+;;;;/;/;;;;++/``````````````````````````````````````````````````````` +````````````````````````````````````````````````````````/+;;;;;;;;/;;;++>``````````````````````````````````````````````````````` +```````````````````````````````````````````````````````/-]@<;;--@@@@@@@@@.`````````````````````````````````````````````````````` +```````````````````````````````````````````````````````>++++[@@@@@@@@@@@@;;;;;;-]@@@;;;;;;;;;;;;;/++/```````````````````````````````````````````````````` +`````````````````````````````````````````````````````;++/;;;;;;;//;;;;/;;[@<```````````````````````````````````````````````````` +````````````````````````````````````````````````````/++;;;;;/;;++@@>;//;-]<;/``````````````````````````````````````````````````` +```````````````````````````````````````````````````/;+@@@@@@@@@@@@@@@@@@@@@@.``````````````````````````````````````````````````` +```````````````````````````````````````````````````>+++++[@@@@@@<;;;-----@@@@>`````````````````````````````````````````````````` +``````````````````````````````````````````````````/-]@;/;;/;;++/;;/;+//;;;///+[<```````````````````````````````````````````````` +````````````````````````````````````````````````++++@@@@@@>;-]@<;/;+@.>;++;;/++;/``````````````````````````````````````````````` +```````````````````````````````````````````````;[@@@@;``````````````````````````````````````````````` +``````````````````````````````````````````````/-]@+++++++>++++++++++>+++>+<<<<-] multiply cells 1 2 3 4 with the 10 in 0 +>++. H +>+. e ++++++++.. ll ++++. o +>++. space +<<+++++++++++++++. W +>. o ++++. r +------. l +--------. d +>+. ! +>. new line diff --git a/files/task3/IFEQ1 b/files/task3/IFEQ1 new file mode 100644 index 0000000..70a7338 --- /dev/null +++ b/files/task3/IFEQ1 @@ -0,0 +1,27 @@ +.constant +.end-constant + +.main + +L1: + BIPUSH 0x05 +L2: + DUP + BIPUSH 0x30 + IADD + OUT + BIPUSH 0x01 + ISUB + DUP + IFEQ L3 + GOTO L2 +L3: + BIPUSH 0x65 + BIPUSH 0x6E + BIPUSH 0x6F + BIPUSH 0x64 + OUT + OUT + OUT + OUT +.end-main diff --git a/files/task3/IFLT1 b/files/task3/IFLT1 new file mode 100644 index 0000000..b029490 --- /dev/null +++ b/files/task3/IFLT1 @@ -0,0 +1,30 @@ +.constant +.end-constant + +.main + +L1: + BIPUSH 0x00 + IFLT L6 +L2: + BIPUSH 0x01 + IFLT L6 +L3: + BIPUSH 0xFF + IFLT L6 +L4: + BIPUSH 0x10 + BIPUSH 0x11 + ISUB + IFLT L6 +L5: + BIPUSH 0x00 + BIPUSH 0x00 + BIPUSH 0x00 + BIPUSH 0x00 + BIPUSH 0x00 + HALT +L6: + BIPUSH 0x37 + HALT +.end-main diff --git a/files/testmem.bin b/files/testmem.bin new file mode 100644 index 0000000000000000000000000000000000000000..a15756d505ae29911f969b0115f253a1db908f85 GIT binary patch literal 47 vcmb1rb$=}bBLfJq0I?I8#lRpgz$n0A#?ZpRD9RwfIiG=T`TuVM1_^%wsox0N literal 0 HcmV?d00001 diff --git a/src/block.rs b/src/block.rs index d34c796..f0fcb1c 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,3 +1,5 @@ +use std::ops; + use Result; use binread::BinReadable; @@ -45,6 +47,14 @@ impl Block { } } +impl ops::Index for Block { + type Output = u8; + + fn index(&self, index: usize) -> &>::Output { + return &self.source[index]; + } +} + impl BinReadable for Block { fn get(&mut self) -> Result { if self.pointer >= self.length { diff --git a/src/lib.rs b/src/lib.rs index dded12f..db5b52b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,5 @@ pub mod frame; pub mod netstack; type Result = ::std::result::Result; + +pub use machine::Machine; diff --git a/tests/task1.rs b/tests/task1.rs new file mode 100644 index 0000000..ac9d1bb --- /dev/null +++ b/tests/task1.rs @@ -0,0 +1,34 @@ +extern crate rustijvm; + +use rustijvm::binread::BinReadable; + +#[test] +fn task1_program1() { + let machine = rustijvm::Machine::new_from_file("files/task1/program1.ijvm").unwrap(); + assert_eq!(machine.block.len(), 7); + assert_eq!(machine.block[0], 0x10); // BIPUSH + assert_eq!(machine.block[2], 0x10); // BIPUSH + assert_eq!(machine.block[4], 0x60); // IADD + assert_eq!(machine.block[5], 0xFD); // OUT +} + +#[test] +fn task1_program2() { + let machine = rustijvm::Machine::new_from_file("files/task1/program2.ijvm").unwrap(); + assert_eq!(machine.block.len(), 16); + assert_eq!(machine.block[0], 0x0); + assert_eq!(machine.block[1], 0x13); + assert_eq!(machine.block[4], 0x59); + assert_eq!(machine.block[5], 0x13); + assert_eq!(machine.block[8], 0x60); + assert_eq!(machine.block[9], 0x13); + assert_eq!(machine.block[12], 0x60); + assert_eq!(machine.block[13], 0xFD); + assert_eq!(machine.block[14], 0x0); +} + +#[test] +fn task1_program_counter() { + let machine = rustijvm::Machine::new_from_file("files/task1/program1.ijvm").unwrap(); + assert_eq!(machine.get_program_counter(), 0); +} diff --git a/tests/task2.rs b/tests/task2.rs new file mode 100644 index 0000000..649de47 --- /dev/null +++ b/tests/task2.rs @@ -0,0 +1,88 @@ +extern crate rustijvm; + +fn steps(machine: &mut rustijvm::Machine, num: usize) { + for _ in 0..num { + println!("step"); + machine.step().unwrap(); + } +} + +#[test] +fn task2_simple_bipush() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestBipush1.ijvm").unwrap(); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(42)); +} + +#[test] +fn task2_signed_bipush() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestBipush2.ijvm").unwrap(); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(-42)); +} + +#[test] +fn task2_simple_iadd() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIadd1.ijvm").unwrap(); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(60)); +} + +#[test] +fn task2_signed_iadd() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIadd2.ijvm").unwrap(); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(-60)); +} + +#[test] +fn task2_simple_isub() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIsub1.ijvm").unwrap(); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(-10)); +} + +#[test] +fn task2_signed_isub() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIsub2.ijvm").unwrap(); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(10)); +} + +#[test] +fn task2_simple_iand() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIAND1.ijvm").unwrap(); + steps(&mut machine, 5); + assert_eq!(machine.cur_stack().top(), Ok(1)); +} + +#[test] +fn task2_simple_ior() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestIOR1.ijvm").unwrap(); + steps(&mut machine, 5); + assert_eq!(machine.cur_stack().top(), Ok(127)); +} + +#[test] +fn task2_swap() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestSwap1.ijvm").unwrap(); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(10)); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(20)); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(10)); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(20)); +} + +#[test] +fn task2_simple_stack_operations() { + let mut machine = rustijvm::Machine::new_from_file("files/task2/TestPop1.ijvm").unwrap(); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(10)); + steps(&mut machine, 3); + assert_eq!(machine.cur_stack().top(), Ok(50)); + steps(&mut machine, 1); + assert_eq!(machine.cur_stack().top(), Ok(10)); +}