use crate::push::instructions::Instruction;
use crate::push::instructions::InstructionCache;
use crate::push::item::Item;
use crate::push::random::CodeGenerator;
use crate::push::state::PushState;
use crate::push::state::*;
use crate::push::stack::PushPrint;
use std::collections::HashMap;
use std::fmt;
#[derive(Clone, Debug, Default)]
pub struct BoolVector {
pub values: Vec<bool>,
}
impl BoolVector {
pub fn new(arg: Vec<bool>) -> Self {
Self { values: arg }
}
pub fn from_int_array(arg: Vec<usize>) -> Self {
let mut bv = vec![false; arg.len()];
for (i, ival) in arg.iter().enumerate() {
bv[i] = ival == &1;
}
Self { values: bv }
}
}
impl PushPrint for BoolVector {
fn to_pstring(&self) -> String {
format!("{}", self.to_string())
}
}
impl fmt::Display for BoolVector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = self
.values
.clone()
.into_iter()
.fold(String::new(), |acc, num| {
acc + &num.to_string().to_uppercase() + ","
});
s.pop();
write!(f, "[{}]", s)
}
}
impl PartialEq for BoolVector {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
}
}
#[derive(Clone, Debug, Default)]
pub struct IntVector {
pub values: Vec<i32>,
}
impl IntVector {
pub fn new(arg: Vec<i32>) -> Self {
Self { values: arg }
}
}
impl PushPrint for IntVector {
fn to_pstring(&self) -> String {
format!("{}", self.to_string())
}
}
impl fmt::Display for IntVector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = self
.values
.clone()
.into_iter()
.fold(String::new(), |acc, num| acc + &num.to_string() + ",");
s.pop();
write!(f, "[{}]", s)
}
}
impl PartialEq for IntVector {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
}
}
#[derive(Clone, Debug, Default)]
pub struct FloatVector {
pub values: Vec<f32>,
}
impl FloatVector {
pub fn new(arg: Vec<f32>) -> Self {
Self { values: arg }
}
}
impl PushPrint for FloatVector {
fn to_pstring(&self) -> String {
format!("{}", self.to_string())
}
}
impl fmt::Display for FloatVector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = self
.values
.clone()
.into_iter()
.fold(String::new(), |acc, num| acc + &format!("{:.3}", num) + ",");
s.pop();
write!(f, "[{}]", s)
}
}
impl PartialEq for FloatVector {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
}
}
pub fn load_vector_instructions(map: &mut HashMap<String, Instruction>) {
map.insert(
String::from("BOOLVECTOR.GET"),
Instruction::new(bool_vector_get),
);
map.insert(
String::from("BOOLVECTOR.SET"),
Instruction::new(bool_vector_set),
);
map.insert(
String::from("BOOLVECTOR.AND"),
Instruction::new(bool_vector_and),
);
map.insert(
String::from("BOOLVECTOR.OR"),
Instruction::new(bool_vector_or),
);
map.insert(
String::from("BOOLVECTOR.NOT"),
Instruction::new(bool_vector_not),
);
map.insert(
String::from("BOOLVECTOR.COUNT"),
Instruction::new(bool_vector_count),
);
map.insert(
String::from("BOOLVECTOR.DEFINE"),
Instruction::new(bool_vector_define),
);
map.insert(
String::from("BOOLVECTOR.DUP"),
Instruction::new(bool_vector_dup),
);
map.insert(
String::from("BOOLVECTOR.EQUAL"),
Instruction::new(bool_vector_equal),
);
map.insert(
String::from("BOOLVECTOR.FLUSH"),
Instruction::new(bool_vector_flush),
);
map.insert(
String::from("BOOLVECTOR.ID"),
Instruction::new(bool_vector_id),
);
map.insert(
String::from("BOOLVECTOR.LENGTH"),
Instruction::new(bool_vector_length),
);
map.insert(
String::from("BOOLVECTOR.ONES"),
Instruction::new(bool_vector_ones),
);
map.insert(
String::from("BOOLVECTOR.POP"),
Instruction::new(bool_vector_pop),
);
map.insert(
String::from("BOOLVECTOR.RAND"),
Instruction::new(bool_vector_rand),
);
map.insert(
String::from("BOOLVECTOR.ROTATE"),
Instruction::new(bool_vector_rand),
);
map.insert(
String::from("BOOLVECTOR.SHOVE"),
Instruction::new(bool_vector_shove),
);
map.insert(
String::from("BOOLVECTOR.SORT*ASC"),
Instruction::new(bool_vector_sort_asc),
);
map.insert(
String::from("BOOLVECTOR.SORT*DESC"),
Instruction::new(bool_vector_sort_desc),
);
map.insert(
String::from("BOOLVECTOR.SWAP"),
Instruction::new(bool_vector_swap),
);
map.insert(
String::from("BOOLVECTOR.STACKDEPTH"),
Instruction::new(bool_vector_stack_depth),
);
map.insert(
String::from("BOOLVECTOR.YANK"),
Instruction::new(bool_vector_yank),
);
map.insert(
String::from("BOOLVECTOR.YANKDUP"),
Instruction::new(bool_vector_yank_dup),
);
map.insert(
String::from("BOOLVECTOR.ZEROS"),
Instruction::new(bool_vector_zeros),
);
map.insert(
String::from("INTVECTOR.APPEND"),
Instruction::new(int_vector_append),
);
map.insert(
String::from("INTVECTOR.BOOLINDEX"),
Instruction::new(int_vector_bool_index),
);
map.insert(
String::from("INTVECTOR.GET"),
Instruction::new(int_vector_get),
);
map.insert(
String::from("INTVECTOR.SET"),
Instruction::new(int_vector_set),
);
map.insert(
String::from("INTVECTOR.+"),
Instruction::new(int_vector_add),
);
map.insert(
String::from("INTVECTOR.-"),
Instruction::new(int_vector_subtract),
);
map.insert(
String::from("INTVECTOR.CONTAINS"),
Instruction::new(int_vector_contains),
);
map.insert(
String::from("INTVECTOR.DEFINE"),
Instruction::new(int_vector_define),
);
map.insert(
String::from("INTVECTOR.DUP"),
Instruction::new(int_vector_dup),
);
map.insert(
String::from("INTVECTOR.EMPTY"),
Instruction::new(int_vector_empty),
);
map.insert(
String::from("INTVECTOR.EQUAL"),
Instruction::new(int_vector_equal),
);
map.insert(
String::from("INTVECTOR.FLUSH"),
Instruction::new(int_vector_flush),
);
map.insert(
String::from("INTVECTOR.FROMINT"),
Instruction::new(int_vector_from_int),
);
map.insert(
String::from("INTVECTOR.ID"),
Instruction::new(int_vector_id),
);
map.insert(
String::from("INTVECTOR.ONES"),
Instruction::new(int_vector_ones),
);
map.insert(
String::from("INTVECTOR.MEAN"),
Instruction::new(int_vector_mean),
);
map.insert(
String::from("INTVECTOR.LENGTH"),
Instruction::new(int_vector_length),
);
map.insert(
String::from("INTVECTOR.LOOP"),
Instruction::new(int_vector_loop),
);
map.insert(
String::from("INTVECTOR.POP"),
Instruction::new(int_vector_pop),
);
map.insert(
String::from("INTVECTOR.REMOVE"),
Instruction::new(int_vector_remove),
);
map.insert(
String::from("INTVECTOR.RAND"),
Instruction::new(int_vector_rand),
);
map.insert(
String::from("INTVECTOR.ROTATE"),
Instruction::new(int_vector_rotate),
);
map.insert(
String::from("INTVECTOR.SHOVE"),
Instruction::new(int_vector_shove),
);
map.insert(
String::from("INTVECTOR.SORT*ASC"),
Instruction::new(int_vector_sort_asc),
);
map.insert(
String::from("INTVECTOR.SORT*DESC"),
Instruction::new(int_vector_sort_desc),
);
map.insert(
String::from("INTVECTOR.SWAP"),
Instruction::new(int_vector_swap),
);
map.insert(
String::from("INTVECTOR.STACKDEPTH"),
Instruction::new(int_vector_stack_depth),
);
map.insert(
String::from("INTVECTOR.SET*INSERT"),
Instruction::new(int_vector_set_insert),
);
map.insert(
String::from("INTVECTOR.SUM"),
Instruction::new(int_vector_sum),
);
map.insert(
String::from("INTVECTOR.YANK"),
Instruction::new(int_vector_yank),
);
map.insert(
String::from("INTVECTOR.YANKDUP"),
Instruction::new(int_vector_yank_dup),
);
map.insert(
String::from("INTVECTOR.ZEROS"),
Instruction::new(int_vector_zeros),
);
map.insert(
String::from("FLOATVECTOR.GET"),
Instruction::new(float_vector_get),
);
map.insert(
String::from("FLOATVECTOR.SET"),
Instruction::new(float_vector_set),
);
map.insert(
String::from("FLOATVECTOR.+"),
Instruction::new(float_vector_add),
);
map.insert(
String::from("FLOATVECTOR.-"),
Instruction::new(float_vector_subtract),
);
map.insert(
String::from("FLOATVECTOR.*"),
Instruction::new(float_vector_multiply),
);
map.insert(
String::from("FLOATVECTOR.*SCALAR"),
Instruction::new(float_vector_multiply_scalar),
);
map.insert(
String::from("FLOATVECTOR./"),
Instruction::new(float_vector_divide),
);
map.insert(
String::from("FLOATVECTOR.APPEND"),
Instruction::new(float_vector_append),
);
map.insert(
String::from("FLOATVECTOR.DEFINE"),
Instruction::new(float_vector_define),
);
map.insert(
String::from("FLOATVECTOR.DUP"),
Instruction::new(float_vector_dup),
);
map.insert(
String::from("FLOATVECTOR.EMPTY"),
Instruction::new(float_vector_empty),
);
map.insert(
String::from("FLOATVECTOR.EQUAL"),
Instruction::new(float_vector_equal),
);
map.insert(
String::from("FLOATVECTOR.FLUSH"),
Instruction::new(float_vector_flush),
);
map.insert(
String::from("FLOATVECTOR.ID"),
Instruction::new(float_vector_id),
);
map.insert(
String::from("FLOATVECTOR.LENGTH"),
Instruction::new(float_vector_length),
);
map.insert(
String::from("FLOATVECTOR.MEAN"),
Instruction::new(float_vector_mean),
);
map.insert(
String::from("FLOATVECTOR.ONES"),
Instruction::new(float_vector_ones),
);
map.insert(
String::from("FLOATVECTOR.POP"),
Instruction::new(float_vector_pop),
);
map.insert(
String::from("FLOATVECTOR.RAND"),
Instruction::new(float_vector_rand),
);
map.insert(
String::from("FLOATVECTOR.ROTATE"),
Instruction::new(float_vector_rotate),
);
map.insert(
String::from("FLOATVECTOR.SINE"),
Instruction::new(float_vector_sine),
);
map.insert(
String::from("FLOATVECTOR.SHOVE"),
Instruction::new(float_vector_shove),
);
map.insert(
String::from("FLOATVECTOR.SORT*ASC"),
Instruction::new(float_vector_sort_asc),
);
map.insert(
String::from("FLOATVECTOR.SORT*DESC"),
Instruction::new(float_vector_sort_desc),
);
map.insert(
String::from("FLOATVECTOR.SWAP"),
Instruction::new(float_vector_swap),
);
map.insert(
String::from("FLOATVECTOR.STACKDEPTH"),
Instruction::new(float_vector_stack_depth),
);
map.insert(
String::from("FLOATVECTOR.SUM"),
Instruction::new(float_vector_stack_depth),
);
map.insert(
String::from("FLOATVECTOR.YANK"),
Instruction::new(float_vector_yank),
);
map.insert(
String::from("FLOATVECTOR.YANKDUP"),
Instruction::new(float_vector_yank_dup),
);
map.insert(
String::from("FLOATVECTOR.ZEROS"),
Instruction::new(float_vector_zeros),
);
}
pub fn bool_vector_id(push_state: &mut PushState, _instruction_set: &InstructionCache) {
push_state.int_stack.push(BOOL_VECTOR_STACK_ID);
}
pub fn bool_vector_set(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(new_element) = push_state.bool_stack.pop() {
if let Some(item_to_change) = push_state.bool_vector_stack.get_mut(0) {
if item_to_change.values.len() > 0 {
let i =
i32::max(i32::min(index, item_to_change.values.len() as i32 - 1), 0) as usize;
item_to_change.values[i] = new_element;
}
}
}
}
}
pub fn bool_vector_and(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut bv) = push_state.bool_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = bv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
bv[0].values[ofs_idx] &= bv[1].values[i];
}
push_state.bool_vector_stack.push(bv[0].clone());
}
}
}
pub fn bool_vector_get(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(element) = push_state.bool_vector_stack.get(0) {
if element.values.len() >0 {
let i = i32::max(i32::min(index, element.values.len() as i32 - 1), 0) as usize;
push_state.bool_stack.push(element.values[i].clone());
}
}
}
}
pub fn bool_vector_or(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut bv) = push_state.bool_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = bv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
bv[0].values[ofs_idx] |= bv[1].values[i];
}
push_state.bool_vector_stack.push(bv[0].clone());
}
}
}
pub fn bool_vector_not(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut bvval) = push_state.bool_vector_stack.pop() {
if let Some(offset) = push_state.int_stack.pop() {
for i in 0..bvval.values.len() {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > bvval.values.len() - 1 {
continue; }
bvval.values[ofs_idx] = !bvval.values[ofs_idx];
}
push_state.bool_vector_stack.push(bvval.clone());
}
}
}
pub fn bool_vector_define(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(name) = push_state.name_stack.pop() {
if let Some(bvval) = push_state.bool_vector_stack.pop() {
push_state.name_bindings.insert(name, Item::boolvec(bvval));
}
}
}
pub fn bool_vector_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvval) = push_state.bool_vector_stack.copy(0) {
push_state.bool_vector_stack.push(bvval);
}
}
fn bool_vector_equal(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvvals) = push_state.bool_vector_stack.pop_vec(2) {
push_state.bool_stack.push(bvvals[0] == bvvals[1]);
}
}
pub fn bool_vector_flush(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.bool_vector_stack.flush();
}
pub fn bool_vector_length(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bv) = push_state.bool_vector_stack.get(0) {
push_state.int_stack.push(bv.values.len() as i32);
}
}
pub fn bool_vector_ones(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.bool_vector_stack
.push(BoolVector::from_int_array(vec![1; size as usize]));
}
}
}
pub fn bool_vector_pop(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.bool_vector_stack.pop();
}
pub fn bool_vector_rand(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if let Some(sparsity) = push_state.float_stack.pop() {
if let Some(rbvval) = CodeGenerator::random_bool_vector(size, sparsity) {
push_state.bool_vector_stack.push(rbvval);
}
}
}
}
pub fn bool_vector_rotate(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(b) = push_state.bool_stack.pop() {
if let Some(bv) = push_state.bool_vector_stack.get_mut(0) {
bv.values.rotate_left(1);
let n = bv.values.len();
bv.values[n - 1] = b;
}
}
}
pub fn bool_vector_sort_asc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvec) = push_state.bool_vector_stack.get_mut(0) {
bvec.values.sort_by(|a, b| a.partial_cmp(b).unwrap());
}
}
pub fn bool_vector_sort_desc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvec) = push_state.bool_vector_stack.get_mut(0) {
bvec.values.sort_by(|a, b| a.partial_cmp(b).unwrap());
bvec.values.reverse();
}
}
pub fn bool_vector_count(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvec) = push_state.bool_vector_stack.get(0) {
push_state
.int_stack
.push(bvec.values.iter().filter(|&n| *n == true).count() as i32);
}
}
pub fn bool_vector_shove(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(shove_index) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min(
(push_state.bool_vector_stack.size() as i32) - 1,
shove_index,
),
0,
) as usize;
push_state.bool_vector_stack.shove(corr_index);
}
}
pub fn bool_vector_stack_depth(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state
.int_stack
.push(push_state.bool_vector_stack.size() as i32);
}
pub fn bool_vector_swap(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.bool_vector_stack.shove(1);
}
pub fn bool_vector_yank(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.bool_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
push_state.bool_vector_stack.yank(corr_index as usize);
}
}
pub fn bool_vector_yank_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.bool_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
if let Some(deep_item) = push_state.bool_vector_stack.copy(corr_index as usize) {
push_state.bool_vector_stack.push(deep_item);
}
}
}
pub fn bool_vector_zeros(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.bool_vector_stack
.push(BoolVector::from_int_array(vec![0; size as usize]));
}
}
}
pub fn int_vector_append(push_state: &mut PushState, _instruction_set: &InstructionCache) {
if let Some(item) = push_state.int_vector_stack.get_mut(0) {
if let Some(to_append) = push_state.int_stack.pop() {
item.values.push(to_append);
}
}
}
pub fn int_vector_id(push_state: &mut PushState, _instruction_set: &InstructionCache) {
push_state.int_stack.push(INT_VECTOR_STACK_ID);
}
pub fn int_vector_bool_index(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(bvval) = push_state.bool_vector_stack.pop() {
let mut index_vector = vec![];
for (i, bval) in bvval.values.iter().enumerate() {
if *bval {
index_vector.push(i as i32);
}
}
push_state
.int_vector_stack
.push(IntVector::new(index_vector));
}
}
pub fn int_vector_get(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(element) = push_state.int_vector_stack.get(0) {
if element.values.len() >0 {
let i = i32::max(i32::min(index, element.values.len() as i32 - 1), 0) as usize;
push_state.int_stack.push(element.values[i].clone());
}
}
}
}
pub fn int_vector_set(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(new_element) = push_state.int_stack.pop() {
if let Some(item_to_change) = push_state.int_vector_stack.get_mut(0) {
if item_to_change.values.len() >0 {
let i =
i32::max(i32::min(index, item_to_change.values.len() as i32 - 1), 0) as usize;
item_to_change.values[i] = new_element;
}
}
}
}
}
pub fn int_vector_add(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.int_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] += iv[1].values[i];
}
push_state.int_vector_stack.push(iv[0].clone());
}
}
}
pub fn int_vector_subtract(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.int_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] -= iv[1].values[i];
}
push_state.int_vector_stack.push(iv[0].clone());
}
}
}
pub fn int_vector_multiply(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.int_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] *= iv[1].values[i];
}
push_state.int_vector_stack.push(iv[0].clone());
}
}
}
pub fn int_vector_divide(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.int_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let mut invalid = false;
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
if iv[1].values[i] == 0 {
invalid = true;
} else {
iv[0].values[ofs_idx] /= iv[1].values[i];
}
}
if !invalid {
push_state.int_vector_stack.push(iv[0].clone());
}
}
}
}
pub fn int_vector_contains(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(element) = push_state.int_stack.pop() {
if let Some(array) = push_state.int_vector_stack.pop() {
push_state.bool_stack.push(array.values.contains(&element));
}
}
}
pub fn int_vector_define(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(name) = push_state.name_stack.pop() {
if let Some(ivval) = push_state.int_vector_stack.pop() {
push_state.name_bindings.insert(name, Item::intvec(ivval));
}
}
}
pub fn int_vector_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(ivval) = push_state.int_vector_stack.copy(0) {
push_state.int_vector_stack.push(ivval);
}
}
fn int_vector_empty(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.int_vector_stack.push(IntVector::new(vec![]));
}
fn int_vector_equal(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(ivvals) = push_state.int_vector_stack.pop_vec(2) {
push_state.bool_stack.push(ivvals[0] == ivvals[1]);
}
}
pub fn int_vector_flush(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.int_vector_stack.flush();
}
pub fn int_vector_from_int(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(vector_size) = push_state.int_stack.pop() {
let size = push_state.int_stack.size() as i32;
let corr_size = i32::max(i32::min(size, vector_size), 0) as usize;
if let Some(ivec) = push_state.int_stack.pop_vec(corr_size) {
push_state.int_vector_stack.push(IntVector::new(ivec));
}
}
}
pub fn int_vector_length(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(iv) = push_state.int_vector_stack.get(0) {
push_state.int_stack.push(iv.values.len() as i32);
}
}
pub fn int_vector_loop(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut array) = push_state.int_vector_stack.pop() {
if let Some(body) = push_state.exec_stack.pop() {
if !array.values.is_empty() {
let next_element = array.values.remove(0);
let updated_loop = Item::list(vec![
body.clone(),
Item::instruction("INTVECTOR.LOOP".to_string()),
Item::intvec(array),
]);
push_state.exec_stack.push(updated_loop);
push_state.exec_stack.push(body);
push_state.int_stack.push(next_element);
}
}
}
}
pub fn int_vector_mean(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(numbers) = push_state.int_vector_stack.get(0) {
let sum = numbers.values.iter().sum::<i32>() as f32;
let size = numbers.values.len() as f32;
push_state.float_stack.push(sum / size);
}
}
pub fn int_vector_ones(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.int_vector_stack
.push(IntVector::new(vec![1; size as usize]));
}
}
}
pub fn int_vector_pop(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.int_vector_stack.pop();
}
pub fn int_vector_rand(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(params) = push_state.int_stack.pop_vec(3) {
if let Some(rbvval) = CodeGenerator::random_int_vector(params[2], params[0], params[1]) {
push_state.int_vector_stack.push(rbvval);
}
}
}
pub fn int_vector_remove(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(item) = push_state.int_vector_stack.get_mut(0) {
if let Some(to_remove) = push_state.int_stack.pop() {
item.values.retain(|x| *x != to_remove);
}
}
}
pub fn int_vector_rotate(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(i) = push_state.int_stack.pop() {
if let Some(iv) = push_state.int_vector_stack.get_mut(0) {
iv.values.rotate_left(1);
let n = iv.values.len();
iv.values[n - 1] = i;
}
}
}
pub fn int_vector_set_insert(push_state: &mut PushState, _instruction_set: &InstructionCache) {
if push_state.int_vector_stack.size() == 0 {
push_state.int_vector_stack.push(IntVector::new(vec![]));
}
if let Some(item) = push_state.int_vector_stack.get_mut(0) {
if let Some(to_insert) = push_state.int_stack.pop() {
if !item.values.contains(&to_insert) {
item.values.push(to_insert);
}
}
}
}
pub fn int_vector_shove(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(shove_index) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.int_vector_stack.size() as i32) - 1, shove_index),
0,
) as usize;
push_state.int_vector_stack.shove(corr_index as usize);
}
}
pub fn int_vector_sort_asc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(ivec) = push_state.int_vector_stack.get_mut(0) {
ivec.values.sort();
}
}
pub fn int_vector_sort_desc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(ivec) = push_state.int_vector_stack.get_mut(0) {
ivec.values.sort();
ivec.values.reverse();
}
}
pub fn int_vector_stack_depth(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state
.int_stack
.push(push_state.int_vector_stack.size() as i32);
}
pub fn int_vector_sum(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(ivec) = push_state.int_vector_stack.get(0) {
push_state.int_stack.push(ivec.values.iter().sum());
}
}
pub fn int_vector_swap(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.int_vector_stack.shove(1);
}
pub fn int_vector_yank(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.int_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
push_state.int_vector_stack.yank(corr_index as usize);
}
}
pub fn int_vector_yank_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.int_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
if let Some(deep_item) = push_state.int_vector_stack.copy(corr_index as usize) {
push_state.int_vector_stack.push(deep_item);
}
}
}
pub fn int_vector_zeros(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.int_vector_stack
.push(IntVector::new(vec![0; size as usize]));
}
}
}
pub fn float_vector_append(push_state: &mut PushState, _instruction_set: &InstructionCache) {
if let Some(item) = push_state.float_vector_stack.get_mut(0) {
if let Some(to_append) = push_state.float_stack.pop() {
item.values.push(to_append);
}
}
}
pub fn float_vector_id(push_state: &mut PushState, _instruction_set: &InstructionCache) {
push_state.int_stack.push(FLOAT_VECTOR_STACK_ID);
}
pub fn float_vector_get(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(element) = push_state.float_vector_stack.get(0) {
if element.values.len() > 0 {
let i = i32::max(i32::min(index, element.values.len() as i32 - 1), 0) as usize;
push_state.float_stack.push(element.values[i].clone());
}
}
}
}
pub fn float_vector_set(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(index) = push_state.int_stack.pop() {
if let Some(new_element) = push_state.float_stack.pop() {
if let Some(item_to_change) = push_state.float_vector_stack.get_mut(0) {
if item_to_change.values.len() > 0 {
let i = i32::max(i32::min(index, item_to_change.values.len() as i32 - 1), 0) as usize;
item_to_change.values[i] = new_element;
}
}
}
}
}
pub fn float_vector_add(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.float_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] += iv[1].values[i];
}
push_state.float_vector_stack.push(iv[0].clone());
}
}
}
pub fn float_vector_subtract(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.float_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] -= iv[1].values[i];
}
push_state.float_vector_stack.push(iv[0].clone());
}
}
}
pub fn float_vector_multiply(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.float_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
iv[0].values[ofs_idx] *= iv[1].values[i];
}
push_state.float_vector_stack.push(iv[0].clone());
}
}
}
pub fn float_vector_divide(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(mut iv) = push_state.float_vector_stack.pop_vec(2) {
if let Some(offset) = push_state.int_stack.pop() {
let mut invalid = false;
let scd_size = iv[0].values.len();
for i in 0..scd_size {
let ofs_idx = (i as i32 + offset) as usize;
if ofs_idx > scd_size - 1 {
continue; }
if iv[1].values[i] == 0.0 {
invalid = true;
} else {
iv[0].values[ofs_idx] /= iv[1].values[i];
}
}
if !invalid {
push_state.float_vector_stack.push(iv[0].clone());
}
}
}
}
pub fn float_vector_define(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(name) = push_state.name_stack.pop() {
if let Some(fvval) = push_state.float_vector_stack.pop() {
push_state.name_bindings.insert(name, Item::floatvec(fvval));
}
}
}
pub fn float_vector_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fvval) = push_state.float_vector_stack.copy(0) {
push_state.float_vector_stack.push(fvval);
}
}
fn float_vector_empty(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.float_vector_stack.push(FloatVector::new(vec![]));
}
fn float_vector_equal(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fvvals) = push_state.float_vector_stack.pop_vec(2) {
push_state.bool_stack.push(fvvals[0] == fvvals[1]);
}
}
pub fn float_vector_flush(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.float_vector_stack.flush();
}
pub fn float_vector_length(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fv) = push_state.float_vector_stack.get(0) {
push_state.int_stack.push(fv.values.len() as i32);
}
}
pub fn float_vector_mean(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(numbers) = push_state.float_vector_stack.get(0) {
let sum = numbers.values.iter().sum::<f32>();
let size = numbers.values.len() as f32;
push_state.float_stack.push(sum / size);
}
}
pub fn float_vector_multiply_scalar(
push_state: &mut PushState,
_instruction_cache: &InstructionCache,
) {
if let Some(f) = push_state.float_stack.pop() {
if let Some(fv) = push_state.float_vector_stack.get_mut(0) {
fv.values.iter_mut().for_each(|x| *x *= f);
}
}
}
pub fn float_vector_ones(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.float_vector_stack
.push(FloatVector::new(vec![1.0; size as usize]));
}
}
}
pub fn float_vector_pop(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.float_vector_stack.pop();
}
pub fn float_vector_rand(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if let Some(gauss_params) = push_state.float_stack.pop_vec(2) {
if let Some(rfvval) =
CodeGenerator::random_float_vector(size, gauss_params[1], gauss_params[0])
{
push_state.float_vector_stack.push(rfvval);
}
}
}
}
pub fn float_vector_rotate(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(f) = push_state.float_stack.pop() {
if let Some(fv) = push_state.float_vector_stack.get_mut(0) {
fv.values.rotate_left(1);
let n = fv.values.len();
fv.values[n - 1] = f;
}
}
}
pub fn float_vector_sine(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(sine_params) = push_state.float_stack.pop_vec(3) {
if let Some(vector_size) = push_state.int_stack.pop() {
let mut sine_vector = vec![];
for i in 0..vector_size as usize {
sine_vector.push(
sine_params[2]
* (2.0 * std::f32::consts::PI * sine_params[1] * i as f32 + sine_params[0])
.sin(),
)
}
push_state
.float_vector_stack
.push(FloatVector::new(sine_vector));
}
}
}
pub fn float_vector_shove(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(shove_index) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min(
(push_state.float_vector_stack.size() as i32) - 1,
shove_index,
),
0,
) as usize;
push_state.float_vector_stack.shove(corr_index as usize);
}
}
pub fn float_vector_sort_asc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fvec) = push_state.float_vector_stack.get_mut(0) {
fvec.values.sort_by(|a, b| a.partial_cmp(b).unwrap());
}
}
pub fn float_vector_sort_desc(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fvec) = push_state.float_vector_stack.get_mut(0) {
fvec.values.sort_by(|a, b| a.partial_cmp(b).unwrap());
fvec.values.reverse();
}
}
pub fn float_vector_stack_depth(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state
.int_stack
.push(push_state.float_vector_stack.size() as i32);
}
pub fn float_vector_sum(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(fvec) = push_state.float_vector_stack.get(0) {
push_state.float_stack.push(fvec.values.iter().sum());
}
}
pub fn float_vector_swap(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
push_state.float_vector_stack.shove(1);
}
pub fn float_vector_yank(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.float_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
push_state.float_vector_stack.yank(corr_index as usize);
}
}
pub fn float_vector_yank_dup(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(idx) = push_state.int_stack.pop() {
let corr_index = i32::max(
i32::min((push_state.float_vector_stack.size() as i32) - 1, idx),
0,
) as usize;
if let Some(deep_item) = push_state.float_vector_stack.copy(corr_index as usize) {
push_state.float_vector_stack.push(deep_item);
}
}
}
pub fn float_vector_zeros(push_state: &mut PushState, _instruction_cache: &InstructionCache) {
if let Some(size) = push_state.int_stack.pop() {
if size > 0 {
push_state
.float_vector_stack
.push(FloatVector::new(vec![0.0; size as usize]));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
pub fn icache() -> InstructionCache {
InstructionCache::new(vec![])
}
#[test]
fn bool_vector_prints_values() {
let bv = BoolVector::new(vec![true, false, true]);
assert_eq!(bv.to_string(), "[TRUE,FALSE,TRUE]");
}
#[test]
fn bool_vector_and_with_different_overlaps() {
let test_vec1 = BoolVector::from_int_array(vec![1, 1, 1, 1, 0, 0, 0, 0]);
let test_vec2 = BoolVector::from_int_array(vec![1, 0, 1, 0, 1, 0, 1, 0]);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(0);
bool_vector_and(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 0, 1, 0, 0, 0, 0, 0])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(-4);
bool_vector_and(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![0, 0, 0, 0, 1, 0, 1, 0])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(8);
bool_vector_and(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 0, 1, 0, 1, 0, 1, 0])
);
}
#[test]
fn bool_vector_get_pushes_vector_element() {
let test_vec1 = BoolVector::from_int_array(vec![1, 1, 1, 0, 1, 1, 1, 1]);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec1);
test_state.int_stack.push(3);
bool_vector_get(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), false);
test_state.int_stack.push(15);
bool_vector_get(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), true);
}
#[test]
fn bool_vector_set_modifies_vector() {
let test_vec1 = BoolVector::from_int_array(vec![1, 1, 1, 1, 1, 1, 1, 1]);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec1);
test_state.int_stack.push(5);
test_state.bool_stack.push(false);
bool_vector_set(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 1, 1, 1, 1, 0, 1, 1])
);
}
#[test]
fn bool_vector_or_with_different_overlaps() {
let test_vec1 = BoolVector::from_int_array(vec![1, 1, 1, 1, 0, 0, 0, 0]);
let test_vec2 = BoolVector::from_int_array(vec![1, 0, 1, 0, 1, 0, 1, 0]);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(0);
bool_vector_or(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 1, 1, 1, 1, 0, 1, 0])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(-4);
bool_vector_or(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 0, 1, 0, 1, 0, 1, 0])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec2.clone());
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(8);
bool_vector_or(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 0, 1, 0, 1, 0, 1, 0])
);
}
#[test]
fn bool_vector_not_with_different_overlaps() {
let test_vec1 = BoolVector::from_int_array(vec![1, 1, 1, 1, 0, 0, 0, 0]);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(0);
bool_vector_not(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![0, 0, 0, 0, 1, 1, 1, 1])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(-4);
bool_vector_not(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![0, 0, 0, 0, 0, 0, 0, 0])
);
let mut test_state = PushState::new();
test_state.bool_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(8);
bool_vector_not(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 1);
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1, 1, 1, 1, 0, 0, 0, 0])
);
}
#[test]
fn bool_vector_define_creates_name_binding() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true, false]));
test_state.name_stack.push(String::from("TEST"));
bool_vector_define(&mut test_state, &icache());
assert_eq!(
*test_state.name_bindings.get("TEST").unwrap().to_string(),
Item::boolvec(BoolVector::new(vec![true, false])).to_string()
);
}
#[test]
fn bool_vector_equal_pushes_result() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
bool_vector_equal(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), true);
}
#[test]
fn bool_vector_ones_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
bool_vector_ones(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
bool_vector_ones(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1; test_size as usize])
);
}
#[test]
fn bool_vector_rand_pushes_new_item() {
let mut test_state = PushState::new();
let test_size = 92;
let test_sparsity = 0.07;
test_state.int_stack.push(test_size);
test_state.float_stack.push(test_sparsity);
bool_vector_rand(&mut test_state, &icache());
if let Some(rbv) = test_state.bool_vector_stack.pop() {
assert_eq!(rbv.values.len(), test_size as usize);
assert_eq!(
rbv.values.iter().filter(|&n| *n == true).count(),
(test_sparsity * test_size as f32) as usize
);
} else {
assert!(false, "Expected to find bool vector");
}
}
#[test]
fn bool_vector_rotate_shifts_elements_left() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::from_int_array(vec![1, 1, 1, 1, 0, 0, 0, 0]));
test_state.bool_stack.push(true);
bool_vector_rotate(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.get(0).unwrap(),
&BoolVector::from_int_array(vec![1, 1, 1, 0, 0, 0, 0, 1])
);
test_state.bool_stack.push(false);
bool_vector_rotate(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.get(0).unwrap(),
&BoolVector::from_int_array(vec![1, 1, 0, 0, 0, 0, 1, 0])
);
}
#[test]
fn bool_vector_shove_inserts_at_right_position() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[TRUE] [FALSE] [FALSE] [FALSE]"
);
test_state.int_stack.push(2);
bool_vector_shove(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[FALSE] [FALSE] [TRUE] [FALSE]"
);
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state.int_stack.push(25);
bool_vector_shove(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[FALSE] [FALSE] [TRUE] [FALSE] [TRUE]"
);
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state.int_stack.push(-2);
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[TRUE] [FALSE] [FALSE] [TRUE] [FALSE] [TRUE]"
);
}
#[test]
fn bool_vector_sort_top_item() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true, false, false, true, false]));
bool_vector_sort_asc(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.to_string(), "[FALSE,FALSE,FALSE,TRUE,TRUE]");
bool_vector_sort_desc(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.to_string(), "[TRUE,TRUE,FALSE,FALSE,FALSE]");
}
#[test]
fn bool_vector_stack_depth_returns_size() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
bool_vector_stack_depth(&mut test_state, &icache());
assert_eq!(test_state.int_stack.to_string(), "4");
}
#[test]
fn bool_vector_count_pushes_aggregation_value() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true, false, false, true, false]));
bool_vector_count(&mut test_state, &icache());
assert_eq!(test_state.int_stack.to_string(), "2");
}
#[test]
fn bool_vector_swaps_top_elements() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
assert_eq!(test_state.bool_vector_stack.to_string(), "[FALSE] [TRUE]");
bool_vector_swap(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.to_string(), "[TRUE] [FALSE]");
}
#[test]
fn bool_vector_yank_brings_item_to_top() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[TRUE] [TRUE] [TRUE] [FALSE] [TRUE]"
);
test_state.int_stack.push(3);
bool_vector_yank(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[FALSE] [TRUE] [TRUE] [TRUE] [TRUE]"
);
}
#[test]
fn bool_vector_yank_dup_copies_item_to_top() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![false]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
test_state
.bool_vector_stack
.push(BoolVector::new(vec![true]));
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[TRUE] [TRUE] [TRUE] [FALSE] [TRUE]"
);
test_state.int_stack.push(3);
bool_vector_yank_dup(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.to_string(),
"[FALSE] [TRUE] [TRUE] [TRUE] [FALSE] [TRUE]"
);
}
#[test]
fn bool_vector_zeros_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
bool_vector_ones(&mut test_state, &icache());
assert_eq!(test_state.bool_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
bool_vector_ones(&mut test_state, &icache());
assert_eq!(
test_state.bool_vector_stack.pop().unwrap(),
BoolVector::from_int_array(vec![1; test_size as usize])
);
}
#[test]
fn int_vector_prints_values() {
let iv = IntVector::new(vec![1, 2, -3]);
assert_eq!(iv.to_string(), "[1,2,-3]");
}
#[test]
fn int_vector_bool_index_pushes_indices_of_active_bits() {
let mut test_state = PushState::new();
test_state
.bool_vector_stack
.push(BoolVector::from_int_array(vec![1, 0, 0, 1, 0]));
int_vector_bool_index(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![0, 3])
);
}
#[test]
fn int_vector_get_pushes_vector_element() {
let test_vec1 = IntVector::new(vec![1, 1, 1, 0, 1, 1, 1, 2]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec1);
test_state.int_stack.push(3);
int_vector_get(&mut test_state, &icache());
assert_eq!(test_state.int_stack.pop().unwrap(), 0);
test_state.int_stack.push(-15);
int_vector_get(&mut test_state, &icache());
assert_eq!(test_state.int_stack.pop().unwrap(), 1);
test_state.int_stack.push(15);
int_vector_get(&mut test_state, &icache());
assert_eq!(test_state.int_stack.pop().unwrap(), 2);
}
#[test]
fn int_vector_set_modifies_vector() {
let test_vec1 = IntVector::new(vec![1, 1, 1, 1, 1, 1, 1, 1]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec1);
test_state.int_stack.push(12); test_state.int_stack.push(5); int_vector_set(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1, 1, 1, 1, 1, 12, 1, 1])
);
}
#[test]
fn int_vector_add_with_different_overlaps() {
let test_vec1 = IntVector::new(vec![1, 1, 1, 1, 0, 0, 0, 0]);
let test_vec2 = IntVector::new(vec![1, 0, 1, 0, 1, 0, 1, 0]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(0);
int_vector_add(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![2, 1, 2, 1, 1, 0, 1, 0])
);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(-4);
int_vector_add(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1, 0, 1, 0, 1, 0, 1, 0])
);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(8);
int_vector_add(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1, 0, 1, 0, 1, 0, 1, 0])
);
}
#[test]
fn int_vector_subtract_with_partial_overlap() {
let test_vec1 = IntVector::new(vec![1, 1, 1, 1, 0, 0, 0, 0]);
let test_vec2 = IntVector::new(vec![1, 0, 1, 0, 1, 0, 1, 0]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
int_vector_subtract(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1, 0, 1, 0, 0, -1, 0, -1])
);
}
#[test]
fn int_vector_multiply_with_partial_overlap() {
let test_vec1 = IntVector::new(vec![-1, -1, -1, -1, 1, 1, 1, 1]);
let test_vec2 = IntVector::new(vec![1, 2, 1, 2, 1, 2, 1, 2]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
int_vector_multiply(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1, 2, 1, 2, -1, -2, -1, -2])
);
}
#[test]
fn int_vector_divide_with_partial_overlap() {
let test_vec1 = IntVector::new(vec![1, 2, 1, 2, 1, 2, 1, 2]);
let test_vec2 = IntVector::new(vec![2, 2, 2, 2, 1, 1, 1, 1]);
let mut test_state = PushState::new();
test_state.int_vector_stack.push(test_vec2.clone());
test_state.int_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
int_vector_divide(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 1);
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![2, 2, 2, 2, 1, 0, 1, 0])
);
}
#[test]
fn int_vector_contains_pushes_to_bool() {
let mut test_state = PushState::new();
test_state
.int_vector_stack
.push(IntVector::new(vec![3, 4, 1, 2]));
test_state.int_stack.push(4);
int_vector_contains(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), true);
assert_eq!(test_state.int_vector_stack.size(), 0);
test_state.int_stack.push(5);
test_state
.int_vector_stack
.push(IntVector::new(vec![3, 4, 1, 2]));
int_vector_contains(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), false);
assert_eq!(test_state.int_vector_stack.size(), 0);
assert_eq!(test_state.int_stack.size(), 0);
}
#[test]
fn int_vector_define_creates_name_binding() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![1, 2]));
test_state.name_stack.push(String::from("TEST"));
int_vector_define(&mut test_state, &icache());
assert_eq!(
*test_state.name_bindings.get("TEST").unwrap().to_string(),
Item::intvec(IntVector::new(vec![1, 2])).to_string()
);
}
#[test]
fn int_vector_equal_pushes_result() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![4]));
test_state.int_vector_stack.push(IntVector::new(vec![4]));
int_vector_equal(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), true);
}
#[test]
fn int_vector_from_int_pushes_item() {
let mut test_state = PushState::new();
for i in 0..10 {
test_state.int_stack.push(i);
}
test_state.int_stack.push(11);
int_vector_from_int(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
);
}
#[test]
fn int_vector_ones_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
int_vector_ones(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
int_vector_ones(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![1; test_size as usize])
);
}
#[test]
fn int_vector_rotate_shifts_elements_left() {
let mut test_state = PushState::new();
test_state
.int_vector_stack
.push(IntVector::new(vec![1, 2, 3, 4, 0, 0, 0, 0]));
test_state.int_stack.push(5);
int_vector_rotate(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.get(0).unwrap(),
&IntVector::new(vec![2, 3, 4, 0, 0, 0, 0, 5])
);
}
#[test]
fn int_vector_rand_pushes_new_item() {
let mut test_state = PushState::new();
let test_size = 92;
let test_min = -7;
let test_max = 77;
test_state.int_stack.push(test_min);
test_state.int_stack.push(test_max);
test_state.int_stack.push(test_size);
int_vector_rand(&mut test_state, &icache());
if let Some(riv) = test_state.int_vector_stack.pop() {
assert_eq!(riv.values.len(), test_size as usize);
assert_eq!(
riv.values
.iter()
.filter(|&n| (*n >= test_min && *n <= test_max) == true)
.count(),
test_size as usize
);
} else {
assert!(false, "Expected to find bool vector");
}
}
#[test]
fn int_vector_remove_elements() {
let mut test_state = PushState::new();
let test_input = IntVector::new(vec![1,2,3,2]);
test_state.int_vector_stack.push(test_input.clone());
test_state.int_stack.push(5);
int_vector_remove(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.get(0).unwrap(), &IntVector::new(vec![1,2,3,2]));
test_state.int_stack.push(2);
int_vector_remove(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.get(0).unwrap(), &IntVector::new(vec![1,3]));
}
#[test]
fn int_vector_set_insert_does_not_allow_duplicates() {
let mut test_state = PushState::new();
let test_input = IntVector::new(vec![1,2,3,4]);
test_state.int_vector_stack.push(test_input.clone());
test_state.int_stack.push(1);
int_vector_set_insert(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.get(0).unwrap(), &test_input);
test_state.int_stack.push(5);
int_vector_set_insert(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.get(0).unwrap(), &IntVector::new(vec![1,2,3,4,5]));
}
#[test]
fn int_vector_shove_inserts_at_right_position() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![4]));
test_state.int_vector_stack.push(IntVector::new(vec![3]));
test_state.int_vector_stack.push(IntVector::new(vec![2]));
test_state.int_vector_stack.push(IntVector::new(vec![1]));
assert_eq!(
test_state.int_vector_stack.to_string(),
"[1] [2] [3] [4]"
);
test_state.int_stack.push(2);
int_vector_shove(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.to_string(),
"[2] [3] [1] [4]"
);
}
#[test]
fn int_vector_sort_top_item() {
let mut test_state = PushState::new();
test_state
.int_vector_stack
.push(IntVector::new(vec![34, 0, -28, 111, -1]));
int_vector_sort_asc(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.to_string(),
"[-28,-1,0,34,111]"
);
int_vector_sort_desc(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.to_string(),
"[111,34,0,-1,-28]"
);
}
#[test]
fn int_vector_stack_depth_returns_size() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![4]));
test_state.int_vector_stack.push(IntVector::new(vec![3]));
test_state.int_vector_stack.push(IntVector::new(vec![2]));
test_state.int_vector_stack.push(IntVector::new(vec![1]));
int_vector_stack_depth(&mut test_state, &icache());
assert_eq!(test_state.int_stack.to_string(), "4");
}
#[test]
fn int_vector_swaps_top_elements() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![0]));
test_state.int_vector_stack.push(IntVector::new(vec![1]));
assert_eq!(test_state.int_vector_stack.to_string(), "[1] [0]");
int_vector_swap(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.to_string(), "[0] [1]");
}
#[test]
fn int_vector_sum_pushes_aggregation_value() {
let mut test_state = PushState::new();
test_state
.int_vector_stack
.push(IntVector::new(vec![1, 3, -2, 5, 7]));
int_vector_sum(&mut test_state, &icache());
assert_eq!(test_state.int_stack.to_string(), "14");
}
#[test]
fn int_vector_yank_brings_item_to_top() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![5]));
test_state.int_vector_stack.push(IntVector::new(vec![4]));
test_state.int_vector_stack.push(IntVector::new(vec![3]));
test_state.int_vector_stack.push(IntVector::new(vec![2]));
test_state.int_vector_stack.push(IntVector::new(vec![1]));
assert_eq!(
test_state.int_vector_stack.to_string(),
"[1] [2] [3] [4] [5]"
);
test_state.int_stack.push(3);
int_vector_yank(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.to_string(),
"[4] [1] [2] [3] [5]"
);
}
#[test]
fn int_vector_yank_dup_copies_item_to_top() {
let mut test_state = PushState::new();
test_state.int_vector_stack.push(IntVector::new(vec![5]));
test_state.int_vector_stack.push(IntVector::new(vec![4]));
test_state.int_vector_stack.push(IntVector::new(vec![3]));
test_state.int_vector_stack.push(IntVector::new(vec![2]));
test_state.int_vector_stack.push(IntVector::new(vec![1]));
assert_eq!(
test_state.int_vector_stack.to_string(),
"[1] [2] [3] [4] [5]"
);
test_state.int_stack.push(3);
int_vector_yank_dup(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.to_string(),
"[4] [1] [2] [3] [4] [5]"
);
}
#[test]
fn int_vector_zeros_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
int_vector_zeros(&mut test_state, &icache());
assert_eq!(test_state.int_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
int_vector_zeros(&mut test_state, &icache());
assert_eq!(
test_state.int_vector_stack.pop().unwrap(),
IntVector::new(vec![0; test_size as usize])
);
}
#[test]
fn float_vector_prints_values() {
let fv = FloatVector::new(vec![1.2, 3.4, -4.5]);
assert_eq!(fv.to_string(), "[1.200,3.400,-4.500]");
}
#[test]
fn float_vector_get_pushes_vector_element() {
let test_vec1 = FloatVector::new(vec![2.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 4.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec1);
test_state.int_stack.push(3);
float_vector_get(&mut test_state, &icache());
assert_eq!(test_state.float_stack.pop().unwrap(), 0.0);
test_state.int_stack.push(-115);
float_vector_get(&mut test_state, &icache());
assert_eq!(test_state.float_stack.pop().unwrap(), 2.0);
test_state.int_stack.push(15);
float_vector_get(&mut test_state, &icache());
assert_eq!(test_state.float_stack.pop().unwrap(), 4.0);
}
#[test]
fn float_vector_set_modifies_vector() {
let test_vec1 = FloatVector::new(vec![1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec1);
test_state.float_stack.push(12.0);
test_state.int_stack.push(5); float_vector_set(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![1.0, 1.0, 1.0, 1.0, 1.0, 12.0, 1.0, 1.0])
);
}
#[test]
fn float_vector_add_with_partial() {
let test_vec1 = FloatVector::new(vec![1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]);
let test_vec2 = FloatVector::new(vec![1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec2.clone());
test_state.float_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(0);
float_vector_add(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 1);
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![2.0, 1.0, 2.0, 1.0, 1.0, 0.0, 1.0, 0.0])
);
}
#[test]
fn float_vector_sine_generates_2pi_angle() {
let mut test_state = PushState::new();
test_state.int_stack.push(1000); test_state.float_stack.push(0.0); test_state.float_stack.push(0.001); test_state.float_stack.push(1.0); float_vector_sine(&mut test_state, &icache());
let sine_vector = test_state.float_vector_stack.pop().unwrap().values;
assert_eq!(sine_vector.len(), 1000);
assert!(f32::abs(sine_vector[0]) < 0.01f32);
assert!(f32::abs(sine_vector[249] - 1.0) < 0.01f32);
assert!(f32::abs(sine_vector[499]) < 0.01f32);
assert!(f32::abs(sine_vector[749] + 1.0) < 0.01f32);
assert!(f32::abs(sine_vector[999]) < 0.01f32);
}
#[test]
fn float_vector_subtract_with_partial_overlap() {
let test_vec1 = FloatVector::new(vec![1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]);
let test_vec2 = FloatVector::new(vec![1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec2.clone());
test_state.float_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
float_vector_subtract(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 1);
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![1.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, -1.0])
);
}
#[test]
fn float_vector_multiply_with_partial_overlap() {
let test_vec1 = FloatVector::new(vec![2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]);
let test_vec2 = FloatVector::new(vec![1.0, 3.0, 1.0, 3.0, 1.0, 3.0, 1.0, 3.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec2.clone());
test_state.float_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
float_vector_multiply(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 1);
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![1.0, 3.0, 1.0, 3.0, 2.0, 6.0, 2.0, 6.0])
);
}
#[test]
fn float_vector_multiply_scalar_to_each_element() {
let mut test_state = PushState::new();
test_state.int_stack.push(4);
float_vector_ones(&mut test_state, &icache());
test_state.float_stack.push(3.0);
float_vector_multiply_scalar(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![3.0, 3.0, 3.0, 3.0])
);
}
#[test]
fn float_vector_divide_with_partial_overlap() {
let test_vec1 = FloatVector::new(vec![2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0]);
let test_vec2 = FloatVector::new(vec![6.0, 4.0, 6.0, 4.0, 6.0, 4.0, 6.0, 4.0]);
let mut test_state = PushState::new();
test_state.float_vector_stack.push(test_vec2.clone());
test_state.float_vector_stack.push(test_vec1.clone());
test_state.int_stack.push(4);
float_vector_divide(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 1);
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![6.0, 4.0, 6.0, 4.0, 3.0, 2.0, 3.0, 2.0])
);
}
#[test]
fn float_vector_define_creates_name_binding() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0, 2.0]));
test_state.name_stack.push(String::from("TEST"));
float_vector_define(&mut test_state, &icache());
assert_eq!(
*test_state.name_bindings.get("TEST").unwrap().to_string(),
Item::floatvec(FloatVector::new(vec![1.0, 2.0])).to_string()
);
}
#[test]
fn float_vector_equal_pushes_result() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
float_vector_equal(&mut test_state, &icache());
assert_eq!(test_state.bool_stack.pop().unwrap(), true);
}
#[test]
fn float_vector_shove_inserts_at_right_position() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![3.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![2.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0]));
assert_eq!(
test_state.float_vector_stack.to_string(),
"[1.000] [2.000] [3.000] [4.000]"
);
test_state.int_stack.push(2);
float_vector_shove(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.to_string(),
"[2.000] [3.000] [1.000] [4.000]"
);
}
#[test]
fn float_vector_ones_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
float_vector_ones(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
float_vector_ones(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![1.0; test_size as usize])
);
}
#[test]
fn float_vector_rand_pushes_new_item() {
let mut test_state = PushState::new();
let test_size = 1000;
let test_mean = vec![-7.0, 0.0, 12.0];
let test_stddev = vec![0.77, 1.23];
for tm in &test_mean {
for ts in &test_stddev {
test_state.int_stack.push(test_size);
test_state.float_stack.push(*ts);
test_state.float_stack.push(*tm);
float_vector_rand(&mut test_state, &icache());
if let Some(fvs) = test_state.float_vector_stack.pop() {
assert_eq!(fvs.values.len(), test_size as usize);
let sum = fvs.values.iter().sum::<f32>();
let count = fvs.values.len() as f32;
assert!(f32::abs(sum / count - tm) < *ts);
} else {
assert!(false, "Expected to find bool vector");
}
}
}
}
#[test]
fn float_vector_sort_top_item() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![34.2, 0.0, -28.1, 111.1, -1.5]));
float_vector_sort_asc(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.to_string(),
"[-28.100,-1.500,0.000,34.200,111.100]"
);
float_vector_sort_desc(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.to_string(),
"[111.100,34.200,0.000,-1.500,-28.100]"
);
}
#[test]
fn float_vector_rotate_shifts_elements_left() {
let mut test_state = PushState::new();
test_state.float_vector_stack.push(FloatVector::new(vec![
1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0,
]));
test_state.float_stack.push(5.0);
float_vector_rotate(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.get(0).unwrap(),
&FloatVector::new(vec![2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 5.0])
);
}
#[test]
fn float_vector_stack_depth_returns_size() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![3.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![2.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0]));
float_vector_stack_depth(&mut test_state, &icache());
assert_eq!(test_state.int_stack.to_string(), "4");
}
#[test]
fn float_vector_sum_pushes_aggregation_value() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0, 3.0, -2.0, 5.0, 7.0]));
float_vector_sum(&mut test_state, &icache());
assert_eq!(test_state.float_stack.to_string(), "14.0");
}
#[test]
fn float_vector_swaps_top_elements() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![0.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0]));
assert_eq!(test_state.float_vector_stack.to_string(), "[1.000] [0.000]");
float_vector_swap(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.to_string(), "[0.000] [1.000]");
}
#[test]
fn float_vector_yank_brings_item_to_top() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![5.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![3.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![2.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0]));
assert_eq!(
test_state.float_vector_stack.to_string(),
"[1.000] [2.000] [3.000] [4.000] [5.000]"
);
test_state.int_stack.push(3);
float_vector_yank(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.to_string(),
"[4.000] [1.000] [2.000] [3.000] [5.000]"
);
}
#[test]
fn float_vector_yank_dup_copies_item_to_top() {
let mut test_state = PushState::new();
test_state
.float_vector_stack
.push(FloatVector::new(vec![5.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![4.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![3.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![2.0]));
test_state
.float_vector_stack
.push(FloatVector::new(vec![1.0]));
assert_eq!(
test_state.float_vector_stack.to_string(),
"[1.000] [2.000] [3.000] [4.000] [5.000]"
);
test_state.int_stack.push(3);
float_vector_yank_dup(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.to_string(),
"[4.000] [1.000] [2.000] [3.000] [4.000] [5.000]"
);
}
#[test]
fn float_vector_zeros_creates_item() {
let mut test_state = PushState::new();
let mut test_size = -11;
test_state.int_stack.push(test_size);
float_vector_zeros(&mut test_state, &icache());
assert_eq!(test_state.float_vector_stack.size(), 0);
test_size = 11;
test_state.int_stack.push(test_size);
float_vector_zeros(&mut test_state, &icache());
assert_eq!(
test_state.float_vector_stack.pop().unwrap(),
FloatVector::new(vec![0.0; test_size as usize])
);
}
}