Fixed NounList construction in cold.rs Noun serialization tests

This commit is contained in:
Chris Allen 2024-10-30 14:31:34 -05:00
parent 8ece38e8af
commit c4b9bc1f3f

View File

@ -1176,7 +1176,9 @@ impl Nounable for Cold {
} }
#[cfg(test)] #[cfg(test)]
mod test { pub(crate) mod test {
use std::iter::FromIterator;
use super::*; use super::*;
use crate::{ use crate::{
hamt::Hamt, hamt::Hamt,
@ -1184,8 +1186,9 @@ mod test {
noun::{Cell, Noun, D}, noun::{Cell, Noun, D},
}; };
fn make_test_stack() -> NockStack { /// Default stack size for tests where you aren't intending to run out of space
let size = 1 << 27; pub(crate) const DEFAULT_STACK_SIZE: usize = 1 << 27;
pub(crate) fn make_test_stack(size: usize) -> NockStack {
let top_slots = 100; let top_slots = 100;
let stack = NockStack::new(size, top_slots); let stack = NockStack::new(size, top_slots);
stack stack
@ -1221,7 +1224,7 @@ mod test {
#[test] #[test]
fn cold_bidirectional_conversion() { fn cold_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let cold = make_cold_state(&mut stack); let cold = make_cold_state(&mut stack);
let cold_noun = cold.into_noun(&mut stack); let cold_noun = cold.into_noun(&mut stack);
let new_cold = let new_cold =
@ -1311,7 +1314,7 @@ mod test {
#[test] #[test]
fn hamt_bidirectional_conversion() { fn hamt_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let items = vec![(D(0), D(1)), (D(2), D(3))]; let items = vec![(D(0), D(1)), (D(2), D(3))];
let hamt = super::hamt_from_vec(&mut stack, items); let hamt = super::hamt_from_vec(&mut stack, items);
let noun = hamt.into_noun(&mut stack); let noun = hamt.into_noun(&mut stack);
@ -1364,7 +1367,7 @@ mod test {
#[test] #[test]
fn batteries_list_bidirectional_conversion() { fn batteries_list_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let batteries_list2 = make_batteries_list(&mut stack, &[1, 2]); let batteries_list2 = make_batteries_list(&mut stack, &[1, 2]);
let batteries_list_noun = batteries_list2.into_noun(&mut stack); let batteries_list_noun = batteries_list2.into_noun(&mut stack);
let new_batteries_list2 = BatteriesList::from_noun(&mut stack, &batteries_list_noun) let new_batteries_list2 = BatteriesList::from_noun(&mut stack, &batteries_list_noun)
@ -1405,7 +1408,7 @@ mod test {
#[test] #[test]
fn batteries_bidirectional_conversion() { fn batteries_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let batteries2 = make_batteries(&mut stack); let batteries2 = make_batteries(&mut stack);
let batteries_noun = batteries2.into_noun(&mut stack); let batteries_noun = batteries2.into_noun(&mut stack);
let new_batteries = Batteries::from_noun(&mut stack, &batteries_noun) let new_batteries = Batteries::from_noun(&mut stack, &batteries_noun)
@ -1438,7 +1441,7 @@ mod test {
#[test] #[test]
fn tuple_bidirectional_conversion() { fn tuple_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let tup = (D(1), D(2), D(3)); let tup = (D(1), D(2), D(3));
let noun = tup.into_noun(&mut stack); let noun = tup.into_noun(&mut stack);
let new_tup: (Noun, Noun, Noun) = let new_tup: (Noun, Noun, Noun) =
@ -1461,14 +1464,15 @@ mod test {
); );
} }
fn make_noun_list(stack: &mut NockStack, v: &[u64]) -> NounList { pub(crate) fn make_noun_list(stack: &mut NockStack, v: &[u64]) -> NounList {
let mut noun_list = NOUN_LIST_NIL; let mut noun_list = NOUN_LIST_NIL;
for &item in v.iter().rev() { // let mut prev = noun_list;
for &item in v.iter() {
let noun_list_mem: *mut NounListMem = unsafe { stack.alloc_struct(1) }; let noun_list_mem: *mut NounListMem = unsafe { stack.alloc_struct(1) };
unsafe { unsafe {
noun_list_mem.write(NounListMem { noun_list_mem.write(NounListMem {
element: D(item), element: D(item),
next: NOUN_LIST_NIL, next: noun_list,
}); });
} }
noun_list = NounList(noun_list_mem); noun_list = NounList(noun_list_mem);
@ -1478,16 +1482,21 @@ mod test {
#[test] #[test]
fn noun_list_bidirectional_conversion() { fn noun_list_bidirectional_conversion() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let items = vec![D(1), D(2)]; const ITEM_COUNT: u64 = 2;
let noun_list = make_noun_list(&mut stack, &[1, 2]); let vec = Vec::from_iter(1..=ITEM_COUNT);
let items = vec.iter().map(|&x| D(x)).collect::<Vec<Noun>>();
let slice = vec.as_slice();
let noun_list = make_noun_list(&mut stack, slice);
let noun = noun_list.into_noun(&mut stack); let noun = noun_list.into_noun(&mut stack);
let new_noun_list: NounList = let new_noun_list: NounList =
<NounList as Nounable>::from_noun::<NockStack>(&mut stack, &noun).unwrap(); <NounList as Nounable>::from_noun::<NockStack>(&mut stack, &noun).unwrap();
let mut item_count = 0;
for (a, b) in new_noun_list.zip(items.iter()) { for (a, b) in new_noun_list.zip(items.iter()) {
let a_ptr = a; let a_ptr = a;
let b_ptr = &mut b.clone() as *mut Noun; let b_ptr = &mut b.clone() as *mut Noun;
let a_val = unsafe { *a_ptr }; let a_val = unsafe { *a_ptr };
item_count += 1;
assert!( assert!(
unsafe { unifying_equality(&mut stack, a_ptr, b_ptr) }, unsafe { unifying_equality(&mut stack, a_ptr, b_ptr) },
"Items don't match: {:?} {:?}", "Items don't match: {:?} {:?}",
@ -1495,11 +1504,12 @@ mod test {
b b
); );
} }
assert_eq!(item_count, ITEM_COUNT as usize);
} }
#[test] #[test]
fn how_to_noun() { fn how_to_noun() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let tup: &[Noun] = &[D(0), D(1)]; let tup: &[Noun] = &[D(0), D(1)];
let cell = Cell::new_tuple(&mut stack, tup); let cell = Cell::new_tuple(&mut stack, tup);
let noun: Noun = cell.as_noun(); let noun: Noun = cell.as_noun();
@ -1511,7 +1521,7 @@ mod test {
#[test] #[test]
fn how_to_noun_but_listy() { fn how_to_noun_but_listy() {
let mut stack = make_test_stack(); let mut stack = make_test_stack(DEFAULT_STACK_SIZE);
let tup: &[Noun] = &[D(0), D(1)]; let tup: &[Noun] = &[D(0), D(1)];
let cell = Cell::new_tuple(&mut stack, tup); let cell = Cell::new_tuple(&mut stack, tup);
let noun: Noun = cell.as_noun(); let noun: Noun = cell.as_noun();