Skip to content

Commit 1087e42

Browse files
committed
test(deep_causality_tensor): Improved test coverage and test organization.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent 33b14b3 commit 1087e42

File tree

10 files changed

+113
-80
lines changed

10 files changed

+113
-80
lines changed

deep_causality_ast/tests/const_tree/const_tree_tests.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,20 @@ fn test_default() {
259259
}
260260

261261
#[test]
262-
fn test_from() {
262+
fn test_from_value() {
263263
let tree = ConstTree::from(42);
264264
assert_eq!(*tree.value(), 42);
265265
assert!(tree.is_leaf());
266266
}
267267

268+
#[test]
269+
fn test_from_ref_value() {
270+
let value = 100;
271+
let tree: ConstTree<i32> = ConstTree::from(&value);
272+
assert_eq!(*tree.value(), 100);
273+
assert!(tree.is_leaf());
274+
}
275+
268276
#[test]
269277
fn test_consuming_iterator() {
270278
let tree = ConstTree::with_children(1, vec![ConstTree::new(2), ConstTree::new(3)]);

deep_causality_tensor/src/types/causal_tensor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod op_tensor_shape;
2222
/// Defines operations between two tensors (e.g., `tensor_a + tensor_b`).
2323
mod op_tensor_tensor;
2424
/// Defines tensor view operations (e.g., slicing).
25-
mod op_view;
25+
mod op_tensor_view;
2626

2727
/// `CausalTensor` is a low-dimensional (up to ~5-25 dimensions recommended) tensor
2828
/// backed by a single, contiguous `Vec<T>`. It uses a stride-based memory layout

deep_causality_tensor/tests/types/causal_tensor/causal_tensor_inspectors_tests.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use deep_causality_tensor::CausalTensor;
7+
8+
#[test]
9+
fn test_from_t_for_causal_tensor() {
10+
let data = 42;
11+
let tensor = CausalTensor::from(data);
12+
assert_eq!(tensor.data(), &[42]);
13+
assert!(tensor.shape().is_empty());
14+
}
15+
16+
#[test]
17+
fn test_from_ref_t_for_causal_tensor() {
18+
let data = 42;
19+
let tensor: CausalTensor<i32> = CausalTensor::from(&data);
20+
assert_eq!(tensor.data(), &[42]);
21+
assert!(tensor.shape().is_empty());
22+
}
23+
24+
#[test]
25+
fn test_from_ref_causal_tensor_for_causal_tensor() {
26+
let original_tensor = CausalTensor::new(vec![1, 2, 3], vec![3]).unwrap();
27+
let cloned_tensor = CausalTensor::from(&original_tensor);
28+
assert_eq!(cloned_tensor.data(), &[1, 2, 3]);
29+
assert_eq!(cloned_tensor.shape(), &[3]);
30+
assert_eq!(original_tensor, cloned_tensor);
31+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,68 @@ fn test_get_mut_from_3d() {
7777
*tensor.get_mut(&[1, 1, 1]).unwrap() = 100;
7878
assert_eq!(tensor.get(&[1, 1, 1]), Some(&100));
7979
}
80+
81+
#[test]
82+
fn test_inspectors_on_standard_tensor() {
83+
let data = vec![1, 2, 3, 4, 5, 6];
84+
let shape = vec![2, 3];
85+
let tensor = CausalTensor::new(data.clone(), shape.clone()).unwrap();
86+
87+
assert!(!tensor.is_empty());
88+
assert_eq!(tensor.shape(), shape.as_slice());
89+
assert_eq!(tensor.num_dim(), 2);
90+
assert_eq!(tensor.len(), 6);
91+
assert_eq!(tensor.as_slice(), data.as_slice());
92+
}
93+
94+
#[test]
95+
fn test_inspectors_on_scalar_tensor() {
96+
let data = vec![42];
97+
let shape = vec![];
98+
let tensor = CausalTensor::new(data.clone(), shape.clone()).unwrap();
99+
100+
assert!(!tensor.is_empty());
101+
assert_eq!(tensor.shape(), shape.as_slice());
102+
assert_eq!(tensor.num_dim(), 0);
103+
assert_eq!(tensor.len(), 1);
104+
assert_eq!(tensor.as_slice(), data.as_slice());
105+
}
106+
107+
#[test]
108+
fn test_inspectors_on_empty_tensor() {
109+
let data: Vec<i32> = vec![];
110+
let shape = vec![0];
111+
let tensor = CausalTensor::new(data.clone(), shape.clone()).unwrap();
112+
113+
assert!(tensor.is_empty());
114+
assert_eq!(tensor.shape(), shape.as_slice());
115+
assert_eq!(tensor.num_dim(), 1);
116+
assert_eq!(tensor.len(), 0);
117+
assert_eq!(tensor.as_slice(), data.as_slice());
118+
}
119+
120+
#[test]
121+
fn test_inspectors_on_empty_tensor_multi_dim() {
122+
let data: Vec<i32> = vec![];
123+
let shape = vec![2, 0, 3];
124+
let tensor = CausalTensor::new(data.clone(), shape.clone()).unwrap();
125+
126+
assert!(tensor.is_empty());
127+
assert_eq!(tensor.shape(), shape.as_slice());
128+
assert_eq!(tensor.num_dim(), 3);
129+
assert_eq!(tensor.len(), 0);
130+
assert_eq!(tensor.as_slice(), data.as_slice());
131+
}
132+
133+
#[test]
134+
fn test_inspectors_on_vector() {
135+
let data = vec![10, 20, 30];
136+
let shape = vec![3];
137+
let tensor = CausalTensor::new(data.clone(), shape.clone()).unwrap();
138+
139+
assert!(!tensor.is_empty());
140+
assert_eq!(tensor.shape(), shape.as_slice());
141+
assert_eq!(tensor.num_dim(), 1);
142+
assert_eq!(tensor.len(), 3);
143+
assert_eq!(tensor.as_slice(), data.as_slice());
144+
}

deep_causality_tensor/tests/types/causal_tensor/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
*/
55

66
#[cfg(test)]
7-
mod causal_tensor_constructor_tests;
7+
mod constructor_tests;
88
#[cfg(test)]
9-
mod causal_tensor_display_tests;
9+
mod display_tests;
10+
mod from_tests;
1011
#[cfg(test)]
11-
mod causal_tensor_getters_tests;
12-
#[cfg(test)]
13-
mod causal_tensor_inspectors_tests;
14-
#[cfg(test)]
15-
mod causal_tensor_view_tests;
12+
mod getters_tests;
1613
#[cfg(test)]
1714
mod op_scalar_tensor_tests;
15+
#[cfg(test)]
1816
mod op_tensor_ein_sum_ast_tests;
1917
mod op_tensor_ein_sum_tests;
2018
#[cfg(test)]
@@ -27,3 +25,5 @@ mod op_tensor_scalar_tests;
2725
mod op_tensor_shape_tests;
2826
#[cfg(test)]
2927
mod op_tensor_tensor_tests;
28+
#[cfg(test)]
29+
mod op_tensor_view_tests;

0 commit comments

Comments
 (0)