|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: MIT |
| 3 | + * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +use deep_causality_ast::ConstTree; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_get_child() { |
| 10 | + let leaf1 = ConstTree::new(1); |
| 11 | + let leaf2 = ConstTree::new(2); |
| 12 | + let tree = ConstTree::with_children(0, vec![leaf1.clone(), leaf2.clone()]); |
| 13 | + |
| 14 | + assert_eq!(tree.get_child(0), Some(&leaf1)); |
| 15 | + assert_eq!(tree.get_child(1), Some(&leaf2)); |
| 16 | + assert_eq!(tree.get_child(2), None); |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_size() { |
| 21 | + let tree = ConstTree::with_children( |
| 22 | + 1, |
| 23 | + vec![ |
| 24 | + ConstTree::new(2), |
| 25 | + ConstTree::with_children(3, vec![ConstTree::new(4)]), |
| 26 | + ], |
| 27 | + ); |
| 28 | + assert_eq!(tree.size(), 4); |
| 29 | + assert_eq!(ConstTree::new(0).size(), 1); |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn test_depth() { |
| 34 | + let tree = ConstTree::with_children( |
| 35 | + 1, |
| 36 | + vec![ |
| 37 | + ConstTree::new(2), |
| 38 | + ConstTree::with_children(3, vec![ConstTree::new(4)]), |
| 39 | + ], |
| 40 | + ); |
| 41 | + assert_eq!(tree.depth(), 3); |
| 42 | + let leaf = ConstTree::new(0); |
| 43 | + assert_eq!(leaf.depth(), 1); |
| 44 | + let empty_children_tree: ConstTree<i32> = ConstTree::with_children(0, vec![]); |
| 45 | + assert_eq!(empty_children_tree.depth(), 1); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn test_get_id() { |
| 50 | + let tree1 = ConstTree::new(10); |
| 51 | + let tree2 = ConstTree::new(10); // Same value, different allocation |
| 52 | + let tree1_clone = tree1.clone(); |
| 53 | + |
| 54 | + // IDs should be non-zero memory addresses |
| 55 | + assert_ne!(tree1.get_id(), 0); |
| 56 | + assert_ne!(tree2.get_id(), 0); |
| 57 | + |
| 58 | + // A clone should have the same ID as the original |
| 59 | + assert_eq!(tree1.get_id(), tree1_clone.get_id()); |
| 60 | + |
| 61 | + // Independently created trees should have different IDs |
| 62 | + assert_ne!(tree1.get_id(), tree2.get_id()); |
| 63 | + |
| 64 | + // Verify that ptr_eq and get_id are consistent |
| 65 | + assert!(tree1.ptr_eq(&tree1_clone)); |
| 66 | + assert_eq!(tree1.get_id(), tree1_clone.get_id()); |
| 67 | + |
| 68 | + assert!(!tree1.ptr_eq(&tree2)); |
| 69 | + assert_ne!(tree1.get_id(), tree2.get_id()); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_search() { |
| 74 | + let tree = ConstTree::with_children( |
| 75 | + 10, |
| 76 | + vec![ |
| 77 | + ConstTree::new(20), |
| 78 | + ConstTree::with_children(30, vec![ConstTree::new(40)]), |
| 79 | + ], |
| 80 | + ); |
| 81 | + |
| 82 | + // find |
| 83 | + let found = tree.find(|v| *v == 30).unwrap(); |
| 84 | + assert_eq!(*found.value(), 30); |
| 85 | + assert!(!found.is_leaf()); |
| 86 | + |
| 87 | + assert!(tree.find(|v| *v == 99).is_none()); |
| 88 | + |
| 89 | + // find_all |
| 90 | + let all_gt_15: Vec<_> = tree.find_all(|v| *v > 15).map(|n| *n.value()).collect(); |
| 91 | + assert_eq!(all_gt_15, vec![20, 30, 40]); |
| 92 | + |
| 93 | + // contains |
| 94 | + assert!(tree.contains(&20)); |
| 95 | + assert!(!tree.contains(&99)); |
| 96 | +} |
0 commit comments