From 6520983de4607cc38050d7b5ae370119c275ddce Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Sat, 17 Jan 2015 19:47:58 -0500 Subject: [PATCH] Add range_incl.rs and use range_incl() in the tests. --- src/lib.rs | 1 + src/machine.rs | 17 +++++++++-------- src/range_incl.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/range_incl.rs diff --git a/src/lib.rs b/src/lib.rs index 1fdeba4..624314a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,4 +33,5 @@ pub mod address; pub mod instruction; pub mod machine; pub mod memory; +pub mod range_incl; pub mod registers; diff --git a/src/machine.rs b/src/machine.rs index 3ecc105..d0c95c3 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -31,6 +31,7 @@ use address::{Address, AddressDiff}; use instruction; use instruction::{DecodedInstr, Instruction, OpInput}; use memory::Memory; +use range_incl::range_incl; use registers::{ Registers, StackPointer, Status, StatusArgs }; use registers::{ PS_NEGATIVE, PS_DECIMAL_MODE, PS_OVERFLOW, PS_ZERO, PS_CARRY, PS_DISABLE_INTERRUPTS }; @@ -1230,13 +1231,13 @@ fn compare_with_y_register_test() { fn exclusive_or_test() { let mut machine = Machine::new(); - for a_before in 0us..256 { - for val in 0us..256 { + for a_before in range_incl(0u8, 255u8) { + for val in range_incl(0u8, 255u8) { machine.execute_instruction( - (Instruction::LDA, OpInput::UseImmediate(a_before as u8)) + (Instruction::LDA, OpInput::UseImmediate(a_before)) ); - machine.exclusive_or(val as u8); + machine.exclusive_or(val); let a_after = a_before ^ val; assert_eq!(machine.registers.accumulator, a_after as i8); @@ -1260,13 +1261,13 @@ fn exclusive_or_test() { fn inclusive_or_test() { let mut machine = Machine::new(); - for a_before in 0us..256 { - for val in 0us..256 { + for a_before in range_incl(0u8, 255u8) { + for val in range_incl(0u8, 255u8) { machine.execute_instruction( - (Instruction::LDA, OpInput::UseImmediate(a_before as u8)) + (Instruction::LDA, OpInput::UseImmediate(a_before)) ); - machine.inclusive_or(val as u8); + machine.inclusive_or(val); let a_after = a_before | val; assert_eq!(machine.registers.accumulator, a_after as i8); diff --git a/src/range_incl.rs b/src/range_incl.rs new file mode 100644 index 0000000..ea52263 --- /dev/null +++ b/src/range_incl.rs @@ -0,0 +1,36 @@ +#![allow(unstable)] + +use std::num::Int; + +pub struct RangeIncl { + state: Option, + end: T, +} + +pub fn range_incl(begin: T, end: T) -> RangeIncl { + RangeIncl { state: Some(begin), end: end } +} + +trait One : Int { + fn my_one(_: Option) -> Self { + Int::one() + } +} +impl One for T where T: Int {} + +impl Iterator for RangeIncl { + type Item = T; + fn next(&mut self) -> Option<::Item> { + match self.state { + Some(current) => { + self.state = + if current == self.end { None } + else { Some(current + One::my_one(None::)) }; + Some(current) + }, + None => { + None + } + } + } +}