From 604772da125a2844723280ee0e2eb8be25b24ebf Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 28 Jul 2020 20:21:26 +0200 Subject: [PATCH] Add some tests --- src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0478c45..4ce0679 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,70 @@ mod tapemachine; #[cfg(test)] mod tests { + use crate::tapemachine::{CharTapeMachine, TapeError}; + use crate::tapemachine::TapeResult; + + const TEST_STRING: &str = "TEST STRING 1234 \\l \\n"; + + #[test] + fn it_returns_the_next_char() { + let mut ctm = CharTapeMachine::new(TEST_STRING.chars().collect()); + let mut test_chars: Vec = TEST_STRING.chars().collect(); + + let mut next = ctm.next_char().unwrap(); + assert_eq!(next, *test_chars.get(1).unwrap()); + + next = ctm.next_char().unwrap(); + assert_eq!(next, *test_chars.get(2).unwrap()); + + next = ctm.next_char().unwrap(); + next = ctm.next_char().unwrap(); + next = ctm.next_char().unwrap(); + next = ctm.next_char().unwrap(); + assert_eq!(next, *test_chars.get(6).unwrap()); + } + + #[test] + fn it_rewinds() { + let mut ctm = CharTapeMachine::new(TEST_STRING.chars().collect()); + let mut test_chars: Vec = TEST_STRING.chars().collect(); + + ctm.next_char().unwrap(); + ctm.next_char().unwrap(); + assert_eq!(ctm.next_char(), Some(*test_chars.get(3).unwrap())); + + ctm.rewind(1); + assert_eq!(ctm.next_char(), Some(*test_chars.get(2).unwrap())); + } + + #[test] + fn it_seeks() { + let mut ctm = CharTapeMachine::new(TEST_STRING.chars().collect()); + let mut test_chars: Vec = TEST_STRING.chars().collect(); + + assert_eq!(ctm.next_char(), Some(*test_chars.get(1).unwrap())); + ctm.seek_one().unwrap(); + assert_eq!(ctm.next_char(), Some(*test_chars.get(3).unwrap())); + ctm.seek_one().unwrap(); + ctm.seek_whitespace(); + assert_eq!(ctm.next_char(), Some(*test_chars.get(6).unwrap())); + } + + #[test] + fn it_asserts_chars() -> TapeResult<()> { + let mut ctm = CharTapeMachine::new(TEST_STRING.chars().collect()); + ctm.assert_any(&['A', 'B', 'T'], None)?; + ctm.seek_one(); + ctm.assert_char(&'E', None)?; + ctm.seek_one(); + ctm.assert_sequence(&['S', 'T', ' '], None)?; + ctm.seek_one(); + ctm.assert_any_sequence(&[&['C'], &['A'], &['A', 'B'], &['S', 'T', 'R']], None)?; + + if let Ok(_) = ctm.assert_any_sequence(&[&['C'], &['A'], &['A', 'B'], &['S', 'T', 'R']], None) { + Err(TapeError::new(0)) + } else { + Ok(()) + } + } }