more code instructions/tests, start on common

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-07 14:07:45 -05:00
parent 8eeef3a928
commit a6094bbed4
3 changed files with 67 additions and 6 deletions

View File

@ -99,10 +99,10 @@ make_instruction_clone!(code, code, _wrap_block, Gene, 1);
fn _combine(vals: Vec<Gene>) -> Option<Gene> { fn _combine(vals: Vec<Gene>) -> Option<Gene> {
match (&vals[0], &vals[1]) { match (&vals[0], &vals[1]) {
(Gene::Block(x), Gene::Block(y)) => { (Gene::Block(x), Gene::Block(y)) => {
let mut x_clone = x.clone(); let x_clone = x.clone();
let y_clone = y.clone(); let mut y_clone = y.clone();
x_clone.extend(y_clone.into_iter()); y_clone.extend(x_clone.into_iter());
Some(Gene::Block(x_clone)) Some(Gene::Block(y_clone))
} }
(Gene::Block(x), y) => { (Gene::Block(x), y) => {
let mut x_clone = x.clone(); let mut x_clone = x.clone();
@ -117,7 +117,7 @@ fn _combine(vals: Vec<Gene>) -> Option<Gene> {
(x, y) => Some(Gene::Block(Box::new(vec![x.clone(), y.clone()]))), (x, y) => Some(Gene::Block(Box::new(vec![x.clone(), y.clone()]))),
} }
} }
make_instruction_clone!(code, code, _combine, Gene, 1); make_instruction_clone!(code, code, _combine, Gene, 2);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -291,6 +291,63 @@ mod tests {
#[test] #[test]
fn combine_test() { fn combine_test() {
// TODO: This later let mut test_state = EMPTY_STATE;
test_state
.code
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
test_state.code.push(Gene::Block(Box::new(vec![
Gene::GeneFloat(dec!(3.8)),
Gene::GeneBoolean(true),
])));
code_combine(&mut test_state);
assert_eq!(
vec![Gene::Block(Box::new(vec![
Gene::GeneInt(1),
Gene::GeneFloat(dec!(3.8)),
Gene::GeneBoolean(true),
]))],
test_state.code
);
test_state.code.clear();
test_state
.code
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
test_state.code.push(Gene::GeneFloat(dec!(4.0)));
code_combine(&mut test_state);
assert_eq!(
vec![Gene::Block(Box::new(vec![
Gene::GeneInt(1),
Gene::GeneFloat(dec!(4.0)),
]))],
test_state.code
);
test_state.code.clear();
test_state.code.push(Gene::GeneFloat(dec!(4.0)));
test_state
.code
.push(Gene::Block(Box::new(vec![Gene::GeneInt(1)])));
code_combine(&mut test_state);
assert_eq!(
vec![Gene::Block(Box::new(vec![
Gene::GeneInt(1),
Gene::GeneFloat(dec!(4.0)),
]))],
test_state.code
);
test_state.code.clear();
test_state.code.push(Gene::GeneFloat(dec!(4.0)));
test_state.code.push(Gene::GeneChar('z'));
code_combine(&mut test_state);
assert_eq!(
vec![Gene::Block(Box::new(vec![
Gene::GeneChar('z'),
Gene::GeneFloat(dec!(4.0)),
]))],
test_state.code
);
} }
} }

View File

@ -0,0 +1,3 @@
use crate::push::state::{Gene, PushState};
/// Swaps the top two values

View File

@ -103,6 +103,7 @@ pub mod macros {
} }
pub mod code; pub mod code;
pub mod common;
pub mod logical; pub mod logical;
pub mod numeric; pub mod numeric;
pub mod utils; pub mod utils;