chore: add some unit tests to stack

This commit is contained in:
Kasey White 2023-03-30 11:32:18 -04:00
parent 6a4f62d074
commit 8fad5b77c6
3 changed files with 113 additions and 1 deletions

View File

@ -9,7 +9,7 @@ use crate::{
use super::scope::Scope;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Air {
// Primitives
Int {

View File

@ -1,6 +1,12 @@
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct Scope(pub(self) Vec<u64>);
impl From<Vec<u64>> for Scope {
fn from(value: Vec<u64>) -> Self {
Self(value)
}
}
impl Scope {
pub fn push(&mut self, value: u64) {
self.0.push(value);

View File

@ -683,3 +683,109 @@ impl AirStack {
});
}
}
#[cfg(test)]
mod test {
use std::rc::Rc;
use crate::{gen_uplc::air::Air, IdGenerator};
use super::AirStack;
#[test]
fn merge_different_scopes() {
let id_gen: Rc<IdGenerator> = IdGenerator::new().into();
let scope = vec![id_gen.next(), id_gen.next()];
let scope2 = vec![id_gen.next(), id_gen.next()];
let air = vec![Air::Int {
scope: scope.clone().into(),
value: "1".to_string(),
}];
let air2 = vec![Air::Int {
scope: scope2.clone().into(),
value: "2".to_string(),
}];
let mut stack1 = AirStack {
id_gen: id_gen.clone(),
scope: scope.into(),
air,
};
let stack2 = AirStack {
id_gen,
scope: scope2.into(),
air: air2,
};
stack1.merge(stack2);
assert_eq!(stack1.scope, vec![0, 1].into());
assert_eq!(
stack1.air,
vec![
Air::Int {
scope: vec![0, 1].into(),
value: "1".to_string(),
},
Air::Int {
scope: vec![2, 3].into(),
value: "2".to_string(),
},
],
)
}
#[test]
fn merge_child_common_ancestor() {
let id_gen: Rc<IdGenerator> = IdGenerator::new().into();
let scope = vec![id_gen.next(), id_gen.next()];
let mut scope2 = scope.clone();
scope2.push(id_gen.next());
let air = vec![Air::Int {
scope: scope.clone().into(),
value: "1".to_string(),
}];
let air2 = vec![Air::Int {
scope: scope2.clone().into(),
value: "2".to_string(),
}];
let mut stack1 = AirStack {
id_gen: id_gen.clone(),
scope: scope.into(),
air,
};
let stack2 = AirStack {
id_gen,
scope: scope2.into(),
air: air2,
};
stack1.merge_child(stack2);
assert_eq!(stack1.scope, vec![0, 1].into());
assert_eq!(
stack1.air,
vec![
Air::Int {
scope: vec![0, 1].into(),
value: "1".to_string(),
},
Air::Int {
scope: vec![0, 1, 2].into(),
value: "2".to_string(),
},
],
)
}
}