use rand::random; use gemma::chip8::stack::Chip8Stack; #[test] #[should_panic] fn stack_overflow_test() { let mut x = Chip8Stack::new(); for i in 0..17 { x.push(&i); } } #[test] #[should_panic] fn stack_underflow_test() { let mut x = Chip8Stack::new(); x.pop(); } #[test] fn stack_lots_of_subs() { let mut x = Chip8Stack::new(); let stack_contents = [ 0x123, 0x321, 0xabc, 0xdef, 0xbad, 0xbef, 0xfed, 0xcab, 0xbed, 0xcad, 0xfeb, 0xcab, 0xfff, 0x000, 0x001, ]; for i in stack_contents { x.push(&i); } assert_eq!(x.depth(), 15); // up to 50 random loops let num_loops: u8 = random::() % 50; for i in 0..num_loops { let start_count = x.depth(); let num_pop = random::() % x.depth() as u8; for current_pop in 0..num_pop { x.pop(); } let post_pop_count = x.depth(); assert_eq!(post_pop_count as u8, start_count as u8 - num_pop); for current_push in 0..num_pop { x.push(&stack_contents[(current_push + post_pop_count as u8) as usize]); } assert_eq!(x.depth(), 15); } }