From bd00c29ad4a5aab2389a6ef6cc8ab71daf4b7733 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 25 Feb 2025 12:01:01 -0500 Subject: [PATCH] add depth_of test --- src/logic/future_moves.rs | 72 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 1fdcbda..6da7acf 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -158,13 +158,13 @@ impl FutureMoves { /// Given an index from `self.arena`, what depth is it at? 0-indexed fn depth_of(&self, node_idx: usize) -> usize { let mut depth = 0; - let mut current = self.arena[node_idx].parent; + let mut current = Some(node_idx); while let Some(parent_idx) = current { depth += 1; current = self.arena[parent_idx].parent; } - depth + depth - 1 } /// Compute `Move.value`, propegating upwards from the furthest out Moves @@ -491,4 +491,72 @@ mod tests { "extend_layer didn't grow arena after refocus" ); } + + #[test] + fn depth_of_test() { + let mut futm = FutureMoves::new(Piece::Black, 0, 0); + + futm.arena.push(Move { + i: 0, + j: 0, + board: Board::new(), + winner: Winner::None, + parent: None, + children: vec![1], + value: 0, + self_value: 0, + color: Piece::Black, + lazy_children: false, + }); + + futm.update_root_idx_raw(0); + + // child 1 + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + futm.arena[1].parent = Some(0); + futm.arena[1].children = vec![3]; + + // dummy + futm.arena.push(Move::new( + 1234, + 1234, + Board::new(), + Piece::White, + false, + Piece::Black, + None, + )); + + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + + futm.arena[3].parent = Some(1); + + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + + assert_eq!(futm.depth_of(3), 2); + } }