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