Skip to content

Commit c0c17d5

Browse files
committed
test(deep_causality_ast): Improved test organization.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent 1087e42 commit c0c17d5

File tree

15 files changed

+468
-387
lines changed

15 files changed

+468
-387
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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_modification_methods() {
10+
let original = ConstTree::with_children(10, vec![ConstTree::new(11)]);
11+
12+
// with_value (this is actually in mod.rs, but tested here for convenience with other modification methods)
13+
let modified_value = original.with_value(20);
14+
assert_eq!(*original.value(), 10);
15+
assert_eq!(*modified_value.value(), 20);
16+
assert_eq!(original.children(), modified_value.children()); // Children are shared
17+
18+
// add_child
19+
let added_child = original.add_child(ConstTree::new(12));
20+
assert_eq!(original.children().len(), 1);
21+
assert_eq!(added_child.children().len(), 2);
22+
assert_eq!(*added_child.children()[1].value(), 12);
23+
24+
// replace_children
25+
let replaced_children = original.replace_children(vec![ConstTree::new(100)]);
26+
assert_eq!(original.children().len(), 1);
27+
assert_eq!(replaced_children.children().len(), 1);
28+
assert_ne!(original.children(), replaced_children.children());
29+
assert_eq!(*replaced_children.children()[0].value(), 100);
30+
31+
// update_child
32+
let updated_child = original.update_child(0, ConstTree::new(111)).unwrap();
33+
assert_eq!(*original.children()[0].value(), 11);
34+
assert_eq!(*updated_child.children()[0].value(), 111);
35+
assert!(original.update_child(1, ConstTree::new(999)).is_none());
36+
37+
// remove_child
38+
let removed_child = original.remove_child(0).unwrap();
39+
assert_eq!(original.children().len(), 1);
40+
assert!(removed_child.is_leaf());
41+
assert!(original.remove_child(1).is_none());
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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_clone_is_cheap() {
10+
let original = ConstTree::new(10);
11+
let cloned = original.clone();
12+
assert!(original.ptr_eq(&cloned));
13+
assert_eq!(*original.value(), *cloned.value());
14+
}

0 commit comments

Comments
 (0)