Skip to content

Commit

Permalink
added reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattRoss committed Apr 15, 2024
1 parent aaff300 commit 5e89d7c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
5 changes: 0 additions & 5 deletions rezasm-app/rezasm-tauri/src/tauri_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ impl TauriReader {
buffer: VecDeque::new(),
}
}

pub fn expand_buffer(&mut self, new_input: &str) {
self.write(new_input.as_bytes()).unwrap();
self.buffer.push_back(b'\n');
}
}

impl Reader for TauriReader {}
Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod writer;
pub mod reader;
46 changes: 46 additions & 0 deletions tests/common/reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::io::{Read, Write};
use std::collections::VecDeque;

use rezasm_core::simulation::reader::Reader;
use rezasm_core::util::as_any::AsAny;

#[derive(Debug)]
pub struct TestReader {
buffer: VecDeque<u8>,
}

impl TestReader {
pub fn new() -> TestReader {
TestReader {
buffer: VecDeque::new()
}
}
}

impl Reader for TestReader {}

impl Read for TestReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.buffer.read(buf)
}
}

impl Write for TestReader {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.write(buf)
}

fn flush(&mut self) -> std::io::Result<()> {
self.buffer.flush()
}
}

impl AsAny for TestReader {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
39 changes: 39 additions & 0 deletions tests/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod common;

use crate::common::writer::TestWriter;
use common::reader::TestReader;
use rezasm_core::instructions::implementation::arithmetic_instructions::ADD;
use rezasm_core::instructions::implementation::register_instructions;
use rezasm_core::parser::lexer::{
Expand All @@ -13,6 +14,7 @@ use rezasm_core::simulation::reader_cell::ReaderCell;
use rezasm_core::simulation::registry::Registry;
use rezasm_core::simulation::registry::{self, get_register_number};
use rezasm_core::simulation::simulator::Simulator;
use rezasm_core::simulation::writer::DummyWriter;
use rezasm_core::util::error::ParserError;
use rezasm_core::util::io::RezasmFileReader;
use rezasm_core::util::raw_data::RawData;
Expand Down Expand Up @@ -174,6 +176,43 @@ pub fn test_print_instructions() {
assert_eq!(output.as_str(), "3\n1.5\nPrint Instructions Work!\n");
}

#[test]
pub fn test_read_instructions() { // Test assumes all other instructions work properly
register_instructions();
let buffer = "10\n10.5\na\nHello"; //doesn't cover everything, should be close enough
let program = "
readi $t0
readf $t1
alloc $s0 $t0
readc $t2
reads $s0
printi $t0
printc '\\n'
printf $t1
printc '\\n'
printc $t2
printc '\\n'
prints $s0".to_string();

let mut reader = ReaderCell::new(TestReader::new());
let _ = reader.write(buffer.as_bytes()).unwrap();
let writer = Box::new(TestWriter::new());
let mut simulator: Simulator = Simulator::new_custom_reader_writer(reader, writer);
let lines = parse_lines(&program, simulator.get_word_size()).unwrap();
simulator.add_lines(lines, "".to_string()).unwrap();
while !simulator.is_done() {
simulator.run_line_from_pc().unwrap();
}
let writer = simulator.get_writer();
let output = writer
.deref()
.as_any()
.downcast_ref::<TestWriter>()
.unwrap()
.get_data();
assert_eq!(output.as_str(), "10\n10.5\na\nHello");
}

#[test]
pub fn test_simulator_labels() {
register_instructions();
Expand Down

0 comments on commit 5e89d7c

Please sign in to comment.