mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:19:59 +00:00
Merge pull request #6 from Magnus167/move_tests
Refactor test organization for matrices and frames
This commit is contained in:
commit
3335fc170a
@ -2,7 +2,6 @@ use crate::matrix::*;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::{Index, IndexMut, Not};
|
||||
|
||||
|
||||
/// A data frame – a Matrix with string‑identified columns (column‑major).
|
||||
///
|
||||
/// Restricts the element type T to anything that is at least Clone –
|
||||
@ -310,3 +309,443 @@ impl Not for Frame<bool> {
|
||||
Frame::new(!self.matrix, self.column_names)
|
||||
}
|
||||
}
|
||||
|
||||
// Unit Tests
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Frame, Matrix};
|
||||
|
||||
// Helper function to create a standard test frame
|
||||
fn create_test_frame_i32() -> Frame<i32> {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![1, 2, 3], // Col "A"
|
||||
vec![4, 5, 6], // Col "B"
|
||||
vec![7, 8, 9], // Col "C"
|
||||
]);
|
||||
Frame::new(matrix, vec!["A", "B", "C"])
|
||||
}
|
||||
|
||||
fn create_test_frame_bool() -> Frame<bool> {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![true, false], // Col "P"
|
||||
vec![false, true], // Col "Q"
|
||||
]);
|
||||
Frame::new(matrix, vec!["P", "Q"])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_frame_success() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
let frame = Frame::new(matrix.clone(), vec!["col1", "col2"]);
|
||||
|
||||
assert_eq!(frame.column_names, vec!["col1", "col2"]);
|
||||
assert_eq!(frame.matrix(), &matrix);
|
||||
assert_eq!(frame.lookup.get("col1"), Some(&0));
|
||||
assert_eq!(frame.lookup.get("col2"), Some(&1));
|
||||
assert_eq!(frame.lookup.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column name count mismatch")]
|
||||
fn test_new_frame_panic_name_count_mismatch() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
Frame::new(matrix, vec!["col1"]); // Only one name for two columns
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: col1")]
|
||||
fn test_new_frame_panic_duplicate_names() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
Frame::new(matrix, vec!["col1", "col1"]); // Duplicate name
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_accessors() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
|
||||
// matrix()
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
assert_eq!(frame.matrix().cols(), 3);
|
||||
|
||||
// column()
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
assert_eq!(frame.column("C"), &[7, 8, 9]);
|
||||
|
||||
// column_mut()
|
||||
frame.column_mut("B")[1] = 50;
|
||||
assert_eq!(frame.column("B"), &[4, 50, 6]);
|
||||
|
||||
// column_index()
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
assert_eq!(frame.column_index("Z"), None);
|
||||
|
||||
// matrix_mut() - check by modifying through matrix_mut
|
||||
*frame.matrix_mut().get_mut(0, 0) = 100; // Modify element at (0, 0) which is A[0]
|
||||
assert_eq!(frame.column("A"), &[100, 2, 3]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_column_panic_unknown_label() {
|
||||
let frame = create_test_frame_i32();
|
||||
frame.column("Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_column_mut_panic_unknown_label() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.column_mut("Z");
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_swap_columns() {
|
||||
// let mut frame = create_test_frame_i32();
|
||||
// let initial_a_data = frame.column("A").to_vec();
|
||||
// let initial_c_data = frame.column("C").to_vec();
|
||||
|
||||
// frame.swap_columns("A", "C");
|
||||
|
||||
// // Check names order
|
||||
// assert_eq!(frame.column_names, vec!["C", "B", "A"]);
|
||||
|
||||
// // Check lookup map
|
||||
// assert_eq!(frame.column_index("A"), Some(2));
|
||||
// assert_eq!(frame.column_index("B"), Some(1));
|
||||
// assert_eq!(frame.column_index("C"), Some(0));
|
||||
|
||||
// // Check data using new names (should be swapped)
|
||||
// assert_eq!(frame.column("C"), initial_a_data); // "C" now has A's old data
|
||||
// assert_eq!(frame.column("A"), initial_c_data); // "A" now has C's old data
|
||||
// assert_eq!(frame.column("B"), &[4, 5, 6]); // "B" should be unchanged
|
||||
|
||||
// // Test swapping with self
|
||||
// let state_before_self_swap = frame.clone();
|
||||
// frame.swap_columns("B", "B");
|
||||
// assert_eq!(frame, state_before_self_swap);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_swap_columns_panic_unknown_a() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.swap_columns("Z", "B");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_swap_columns_panic_unknown_b() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.swap_columns("A", "Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let original_b_data = frame.column("B").to_vec();
|
||||
|
||||
frame.rename("B", "Beta");
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "Beta", "C"]);
|
||||
|
||||
// Check lookup
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("Beta"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
assert_eq!(frame.column_index("B"), None); // Old name gone
|
||||
|
||||
// Check data accessible via new name
|
||||
assert_eq!(frame.column("Beta"), original_b_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_rename_panic_unknown_old() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.rename("Z", "Omega");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: C")]
|
||||
fn test_rename_panic_duplicate_new() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.rename("A", "C"); // "C" already exists
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let new_col_data = vec![10, 11, 12];
|
||||
|
||||
frame.add_column("D", new_col_data.clone());
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "B", "C", "D"]);
|
||||
|
||||
// Check lookup
|
||||
assert_eq!(frame.column_index("D"), Some(3));
|
||||
|
||||
// Check matrix dimensions
|
||||
assert_eq!(frame.matrix().cols(), 4);
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
|
||||
// Check data of new column
|
||||
assert_eq!(frame.column("D"), new_col_data);
|
||||
// Check old columns are still there
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: B")]
|
||||
fn test_add_column_panic_duplicate_name() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.add_column("B", vec![0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column length mismatch")]
|
||||
fn test_add_column_panic_length_mismatch() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
// Matrix::add_column panics if lengths mismatch
|
||||
frame.add_column("D", vec![10, 11]); // Only 2 elements, expected 3
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let original_b_data = frame.column("B").to_vec();
|
||||
let original_c_data = frame.column("C").to_vec(); // Need to check data shift
|
||||
|
||||
let deleted_data = frame.delete_column("B");
|
||||
|
||||
// Check returned data
|
||||
assert_eq!(deleted_data, original_b_data);
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "C"]);
|
||||
|
||||
// Check lookup (rebuilt)
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("C"), Some(1));
|
||||
assert_eq!(frame.column_index("B"), None);
|
||||
|
||||
// Check matrix dimensions
|
||||
assert_eq!(frame.matrix().cols(), 2);
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
|
||||
// Check remaining data
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
assert_eq!(frame.column("C"), original_c_data); // "C" should now be at index 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_delete_column_panic_unknown() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.delete_column("Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sort_columns() {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![7, 8, 9], // Col "C"
|
||||
vec![1, 2, 3], // Col "A"
|
||||
vec![4, 5, 6], // Col "B"
|
||||
]);
|
||||
let mut frame = Frame::new(matrix, vec!["C", "A", "B"]);
|
||||
|
||||
let orig_a = frame.column("A").to_vec();
|
||||
let orig_b = frame.column("B").to_vec();
|
||||
let orig_c = frame.column("C").to_vec();
|
||||
|
||||
frame.sort_columns();
|
||||
|
||||
// Check names order
|
||||
assert_eq!(frame.column_names, vec!["A", "B", "C"]);
|
||||
|
||||
// Check lookup map
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("B"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
|
||||
// Check data integrity (data moved with the names)
|
||||
assert_eq!(frame.column("A"), orig_a);
|
||||
assert_eq!(frame.column("B"), orig_b);
|
||||
assert_eq!(frame.column("C"), orig_c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sort_columns_single_column() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2, 3]]);
|
||||
let mut frame = Frame::new(matrix.clone(), vec!["Solo"]);
|
||||
let expected = frame.clone();
|
||||
frame.sort_columns();
|
||||
assert_eq!(frame, expected); // Should be unchanged
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
let frame = create_test_frame_i32();
|
||||
assert_eq!(frame["A"].to_vec(), vec![1, 2, 3]);
|
||||
assert_eq!(frame["C"].to_vec(), vec![7, 8, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_index_panic_unknown() {
|
||||
let frame = create_test_frame_i32();
|
||||
let _ = frame["Z"];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_mut() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame["B"][0] = 42;
|
||||
frame["C"][2] = 99;
|
||||
|
||||
assert_eq!(frame["B"].to_vec(), &[42, 5, 6]);
|
||||
assert_eq!(frame["C"].to_vec(), &[7, 8, 99]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_index_mut_panic_unknown() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let _ = &mut frame["Z"];
|
||||
}
|
||||
|
||||
// --- Test Ops ---
|
||||
#[test]
|
||||
fn test_elementwise_ops_numeric() {
|
||||
let frame1 = create_test_frame_i32(); // A=[1,2,3], B=[4,5,6], C=[7,8,9]
|
||||
let matrix2 = Matrix::from_cols(vec![
|
||||
vec![10, 10, 10], // Col "A"
|
||||
vec![2, 2, 2], // Col "B"
|
||||
vec![1, 1, 1], // Col "C"
|
||||
]);
|
||||
let frame2 = Frame::new(matrix2, vec!["A", "B", "C"]); // Must have same names and dims
|
||||
|
||||
// Add
|
||||
let frame_add = &frame1 + &frame2;
|
||||
assert_eq!(frame_add.column_names, frame1.column_names);
|
||||
assert_eq!(frame_add["A"].to_vec(), &[11, 12, 13]);
|
||||
assert_eq!(frame_add["B"].to_vec(), &[6, 7, 8]);
|
||||
assert_eq!(frame_add["C"].to_vec(), &[8, 9, 10]);
|
||||
|
||||
// Sub
|
||||
let frame_sub = &frame1 - &frame2;
|
||||
assert_eq!(frame_sub.column_names, frame1.column_names);
|
||||
assert_eq!(frame_sub["A"].to_vec(), &[-9, -8, -7]);
|
||||
assert_eq!(frame_sub["B"].to_vec(), &[2, 3, 4]);
|
||||
assert_eq!(frame_sub["C"].to_vec(), &[6, 7, 8]);
|
||||
|
||||
// Mul
|
||||
let frame_mul = &frame1 * &frame2;
|
||||
assert_eq!(frame_mul.column_names, frame1.column_names);
|
||||
assert_eq!(frame_mul["A"].to_vec(), &[10, 20, 30]);
|
||||
assert_eq!(frame_mul["B"].to_vec(), &[8, 10, 12]);
|
||||
assert_eq!(frame_mul["C"].to_vec(), &[7, 8, 9]);
|
||||
|
||||
// Div
|
||||
let frame_div = &frame1 / &frame2; // Integer division
|
||||
assert_eq!(frame_div.column_names, frame1.column_names);
|
||||
assert_eq!(frame_div["A"].to_vec(), &[0, 0, 0]); // 1/10, 2/10, 3/10
|
||||
assert_eq!(frame_div["B"].to_vec(), &[2, 2, 3]); // 4/2, 5/2, 6/2
|
||||
assert_eq!(frame_div["C"].to_vec(), &[7, 8, 9]); // 7/1, 8/1, 9/1
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic] // Exact message depends on Matrix op panic message ("row count mismatch" or "col count mismatch")
|
||||
fn test_elementwise_op_panic_dimension_mismatch() {
|
||||
let frame1 = create_test_frame_i32(); // 3x3
|
||||
let matrix2 = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]); // 2x2
|
||||
let frame2 = Frame::new(matrix2, vec!["X", "Y"]);
|
||||
let _ = &frame1 + &frame2; // Should panic due to dimension mismatch
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_ops_bool() {
|
||||
let frame1 = create_test_frame_bool(); // P=[T, F], Q=[F, T]
|
||||
let matrix2 = Matrix::from_cols(vec![
|
||||
vec![true, true], // P
|
||||
vec![false, false], // Q
|
||||
]);
|
||||
let frame2 = Frame::new(matrix2, vec!["P", "Q"]);
|
||||
|
||||
// BitAnd
|
||||
let frame_and = &frame1 & &frame2;
|
||||
assert_eq!(frame_and.column_names, frame1.column_names);
|
||||
assert_eq!(frame_and["P"].to_vec(), &[true, false]); // T&T=T, F&T=F
|
||||
assert_eq!(frame_and["Q"].to_vec(), &[false, false]); // F&F=F, T&F=F
|
||||
|
||||
// BitOr
|
||||
let frame_or = &frame1 | &frame2;
|
||||
assert_eq!(frame_or.column_names, frame1.column_names);
|
||||
assert_eq!(frame_or["P"].to_vec(), &[true, true]); // T|T=T, F|T=T
|
||||
assert_eq!(frame_or["Q"].to_vec(), &[false, true]); // F|F=F, T|F=T
|
||||
|
||||
// BitXor
|
||||
let frame_xor = &frame1 ^ &frame2;
|
||||
assert_eq!(frame_xor.column_names, frame1.column_names);
|
||||
assert_eq!(frame_xor["P"].to_vec(), &[false, true]); // T^T=F, F^T=T
|
||||
assert_eq!(frame_xor["Q"].to_vec(), &[false, true]); // F^F=F, T^F=T
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_op_bool() {
|
||||
let frame = create_test_frame_bool(); // P=[T, F], Q=[F, T]
|
||||
let frame_not = !frame; // Note: consumes the original frame
|
||||
|
||||
assert_eq!(frame_not.column_names, vec!["P", "Q"]);
|
||||
assert_eq!(frame_not["P"].to_vec(), &[false, true]);
|
||||
assert_eq!(frame_not["Q"].to_vec(), &[true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_columns() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let initial_a_data = frame.column("A").to_vec(); // [1, 2, 3]
|
||||
let initial_c_data = frame.column("C").to_vec(); // [7, 8, 9]
|
||||
|
||||
frame.swap_columns("A", "C");
|
||||
|
||||
// Check names order
|
||||
assert_eq!(frame.column_names, vec!["C", "B", "A"]);
|
||||
|
||||
// Check lookup map
|
||||
assert_eq!(frame.column_index("A"), Some(2));
|
||||
assert_eq!(frame.column_index("B"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(0));
|
||||
|
||||
// Check data using new names (should be swapped)
|
||||
|
||||
// Accessing by name "C" (now at index 0) should retrieve the data
|
||||
// that was swapped INTO index 0, which was the *original C data*.
|
||||
assert_eq!(
|
||||
frame.column("C"),
|
||||
initial_c_data.as_slice(),
|
||||
"Data for name 'C' should be original C data"
|
||||
);
|
||||
|
||||
// Accessing by name "A" (now at index 2) should retrieve the data
|
||||
// that was swapped INTO index 2, which was the *original A data*.
|
||||
assert_eq!(
|
||||
frame.column("A"),
|
||||
initial_a_data.as_slice(),
|
||||
"Data for name 'A' should be original A data"
|
||||
);
|
||||
|
||||
// Column "B" should remain unchanged in data and position.
|
||||
assert_eq!(
|
||||
frame.column("B"),
|
||||
&[4, 5, 6],
|
||||
"Column 'B' should be unchanged"
|
||||
);
|
||||
|
||||
// Test swapping with self
|
||||
let state_before_self_swap = frame.clone();
|
||||
frame.swap_columns("B", "B");
|
||||
assert_eq!(frame, state_before_self_swap);
|
||||
}
|
||||
}
|
||||
|
@ -74,3 +74,174 @@ impl BoolOps for BoolMatrix {
|
||||
}
|
||||
}
|
||||
// use macros to generate the implementations for BitAnd, BitOr, BitXor, and Not
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Helper function to create a BoolMatrix for BoolOps testing
|
||||
fn create_bool_test_matrix() -> BoolMatrix {
|
||||
// 3x3 matrix (column-major)
|
||||
// T F T
|
||||
// F T F
|
||||
// T F F
|
||||
let data = vec![true, false, true, false, true, false, true, false, false];
|
||||
BoolMatrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// --- Tests for BoolOps (BoolMatrix) ---
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: T | F | T = T
|
||||
// Col 1: F | T | F = T
|
||||
// Col 2: T | F | F = T
|
||||
let expected = vec![true, true, true];
|
||||
assert_eq!(matrix.any_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: T | F | T = T
|
||||
// Row 1: F | T | F = T
|
||||
// Row 2: T | F | F = T
|
||||
let expected = vec![true, true, true];
|
||||
assert_eq!(matrix.any_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: T & F & T = F
|
||||
// Col 1: F & T & F = F
|
||||
// Col 2: T & F & F = F
|
||||
let expected = vec![false, false, false];
|
||||
assert_eq!(matrix.all_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: T & F & T = F
|
||||
// Row 1: F & T & F = F
|
||||
// Row 2: T & F & F = F
|
||||
let expected = vec![false, false, false];
|
||||
assert_eq!(matrix.all_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: count true in [T, F, T] = 2
|
||||
// Col 1: count true in [F, T, F] = 1
|
||||
// Col 2: count true in [T, F, F] = 1
|
||||
let expected = vec![2, 1, 1];
|
||||
assert_eq!(matrix.count_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: count true in [T, F, T] = 2
|
||||
// Row 1: count true in [F, T, F] = 1
|
||||
// Row 2: count true in [T, F, F] = 1
|
||||
let expected = vec![2, 1, 1];
|
||||
assert_eq!(matrix.count_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Has true values
|
||||
assert!(matrix.any());
|
||||
|
||||
let matrix_all_false = BoolMatrix::from_vec(vec![false; 9], 3, 3);
|
||||
assert!(!matrix_all_false.any());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Has false values
|
||||
assert!(!matrix.all());
|
||||
|
||||
let matrix_all_true = BoolMatrix::from_vec(vec![true; 9], 3, 3);
|
||||
assert!(matrix_all_true.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Data: [T, F, T, F, T, F, T, F, F]
|
||||
// Count of true values: 4
|
||||
assert_eq!(matrix.count(), 4);
|
||||
|
||||
let matrix_all_false = BoolMatrix::from_vec(vec![false; 5], 5, 1); // 5x1
|
||||
assert_eq!(matrix_all_false.count(), 0);
|
||||
|
||||
let matrix_all_true = BoolMatrix::from_vec(vec![true; 4], 2, 2); // 2x2
|
||||
assert_eq!(matrix_all_true.count(), 4);
|
||||
}
|
||||
|
||||
// --- Edge Cases for BoolOps ---
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_1x1() {
|
||||
let matrix_t = BoolMatrix::from_vec(vec![true], 1, 1);
|
||||
assert_eq!(matrix_t.any_vertical(), vec![true]);
|
||||
assert_eq!(matrix_t.any_horizontal(), vec![true]);
|
||||
assert_eq!(matrix_t.all_vertical(), vec![true]);
|
||||
assert_eq!(matrix_t.all_horizontal(), vec![true]);
|
||||
assert_eq!(matrix_t.count_vertical(), vec![1]);
|
||||
assert_eq!(matrix_t.count_horizontal(), vec![1]);
|
||||
assert!(matrix_t.any());
|
||||
assert!(matrix_t.all());
|
||||
assert_eq!(matrix_t.count(), 1);
|
||||
|
||||
let matrix_f = BoolMatrix::from_vec(vec![false], 1, 1);
|
||||
assert_eq!(matrix_f.any_vertical(), vec![false]);
|
||||
assert_eq!(matrix_f.any_horizontal(), vec![false]);
|
||||
assert_eq!(matrix_f.all_vertical(), vec![false]);
|
||||
assert_eq!(matrix_f.all_horizontal(), vec![false]);
|
||||
assert_eq!(matrix_f.count_vertical(), vec![0]);
|
||||
assert_eq!(matrix_f.count_horizontal(), vec![0]);
|
||||
assert!(!matrix_f.any());
|
||||
assert!(!matrix_f.all());
|
||||
assert_eq!(matrix_f.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_1xn_matrix() {
|
||||
let matrix = BoolMatrix::from_vec(vec![true, false, false, true], 1, 4); // 1 row, 4 cols
|
||||
// Data: [T, F, F, T]
|
||||
|
||||
assert_eq!(matrix.any_vertical(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.all_vertical(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.count_vertical(), vec![1, 0, 0, 1]);
|
||||
|
||||
assert_eq!(matrix.any_horizontal(), vec![true]); // T | F | F | T = T
|
||||
assert_eq!(matrix.all_horizontal(), vec![false]); // T & F & F & T = F
|
||||
assert_eq!(matrix.count_horizontal(), vec![2]); // count true in [T, F, F, T] = 2
|
||||
|
||||
assert!(matrix.any());
|
||||
assert!(!matrix.all());
|
||||
assert_eq!(matrix.count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_nx1_matrix() {
|
||||
let matrix = BoolMatrix::from_vec(vec![true, false, false, true], 4, 1); // 4 rows, 1 col
|
||||
// Data: [T, F, F, T]
|
||||
|
||||
assert_eq!(matrix.any_vertical(), vec![true]); // T|F|F|T = T
|
||||
assert_eq!(matrix.all_vertical(), vec![false]); // T&F&F&T = F
|
||||
assert_eq!(matrix.count_vertical(), vec![2]); // count true in [T, F, F, T] = 2
|
||||
|
||||
assert_eq!(matrix.any_horizontal(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.all_horizontal(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.count_horizontal(), vec![1, 0, 0, 1]);
|
||||
|
||||
assert!(matrix.any());
|
||||
assert!(!matrix.all());
|
||||
assert_eq!(matrix.count(), 2);
|
||||
}
|
||||
}
|
||||
|
@ -255,3 +255,964 @@ pub enum Axis {
|
||||
/// Operate row‑wise (horizontal).
|
||||
Row,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{BoolMatrix, FloatMatrix, Matrix, StringMatrix};
|
||||
|
||||
// Helper function to create a basic Matrix for testing
|
||||
fn create_test_matrix() -> Matrix<i32> {
|
||||
// Column-major data:
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
Matrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// Another helper for a different size
|
||||
fn create_test_matrix_2x4() -> Matrix<i32> {
|
||||
// Column-major data:
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
|
||||
Matrix::from_vec(data, 2, 4)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_vec_basic() {
|
||||
let data = vec![1, 2, 3, 4, 5, 6]; // 2 rows, 3 cols (column-major)
|
||||
let matrix = Matrix::from_vec(data, 2, 3);
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6]);
|
||||
|
||||
// Check some elements
|
||||
assert_eq!(matrix[(0, 0)], 1); // First row, first col
|
||||
assert_eq!(matrix[(1, 0)], 2); // Second row, first col
|
||||
assert_eq!(matrix[(0, 1)], 3); // First row, second col
|
||||
assert_eq!(matrix[(1, 2)], 6); // Second row, third col
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "data length mismatch")]
|
||||
fn test_from_vec_wrong_length() {
|
||||
let data = vec![1, 2, 3, 4, 5]; // Should be 6 for 2x3
|
||||
Matrix::from_vec(data, 2, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one row")]
|
||||
fn test_from_vec_zero_rows() {
|
||||
let data = vec![1, 2, 3];
|
||||
Matrix::from_vec(data, 0, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one column")]
|
||||
fn test_from_vec_zero_cols() {
|
||||
let data = vec![1, 2, 3];
|
||||
Matrix::from_vec(data, 3, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cols_basic() {
|
||||
// Representing:
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
let cols_data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
||||
let matrix = Matrix::from_cols(cols_data);
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
// Internal data should be column-major
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
// Check some elements
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(2, 0)], 3);
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cols_1x1() {
|
||||
let cols_data = vec![vec![42]];
|
||||
let matrix = Matrix::from_cols(cols_data);
|
||||
assert_eq!(matrix.rows(), 1);
|
||||
assert_eq!(matrix.cols(), 1);
|
||||
assert_eq!(matrix.data(), &[42]);
|
||||
assert_eq!(matrix[(0, 0)], 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one column")]
|
||||
fn test_from_cols_empty_cols() {
|
||||
let empty_cols: Vec<Vec<i32>> = vec![];
|
||||
Matrix::from_cols(empty_cols);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one row")]
|
||||
fn test_from_cols_empty_rows() {
|
||||
let empty_row: Vec<Vec<String>> = vec![vec![], vec![]];
|
||||
Matrix::from_cols(empty_row);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col 1 has len 2, expected 3")]
|
||||
fn test_from_cols_mismatched_lengths() {
|
||||
let cols_data = vec![vec![1, 2, 3], vec![4, 5], vec![6, 7, 8]];
|
||||
Matrix::from_cols(cols_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getters() {
|
||||
let matrix = create_test_matrix();
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_and_get() {
|
||||
let matrix = create_test_matrix();
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(2, 2)], 9);
|
||||
|
||||
assert_eq!(*matrix.get(0, 0), 1);
|
||||
assert_eq!(*matrix.get(1, 1), 5);
|
||||
assert_eq!(*matrix.get(2, 2), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_out_of_bounds_row() {
|
||||
let matrix = create_test_matrix(); // 3x3
|
||||
let _ = matrix[(3, 0)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_out_of_bounds_col() {
|
||||
let matrix = create_test_matrix(); // 3x3
|
||||
let _ = matrix[(0, 3)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_mut_and_get_mut() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
|
||||
matrix[(0, 0)] = 10;
|
||||
matrix[(1, 1)] = 20;
|
||||
matrix[(2, 2)] = 30;
|
||||
|
||||
assert_eq!(matrix[(0, 0)], 10);
|
||||
assert_eq!(matrix[(1, 1)], 20);
|
||||
assert_eq!(matrix[(2, 2)], 30);
|
||||
|
||||
*matrix.get_mut(0, 1) = 15;
|
||||
*matrix.get_mut(2, 1) = 25;
|
||||
|
||||
assert_eq!(matrix[(0, 1)], 15);
|
||||
assert_eq!(matrix[(2, 1)], 25);
|
||||
|
||||
// Check underlying data consistency (column-major)
|
||||
// Should be:
|
||||
// 10 15 7
|
||||
// 2 20 8
|
||||
// 3 25 30
|
||||
assert_eq!(matrix.data(), &[10, 2, 3, 15, 20, 25, 7, 8, 30]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_mut_out_of_bounds_row() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix[(3, 0)] = 99;
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_mut_out_of_bounds_col() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix[(0, 3)] = 99;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_column() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
assert_eq!(matrix.column(0), &[1, 2]);
|
||||
assert_eq!(matrix.column(1), &[3, 4]);
|
||||
assert_eq!(matrix.column(2), &[5, 6]);
|
||||
assert_eq!(matrix.column(3), &[7, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index")]
|
||||
fn test_column_out_of_bounds() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.column(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_column_mut() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let col1_mut = matrix.column_mut(1);
|
||||
col1_mut[0] = 30;
|
||||
col1_mut[1] = 40;
|
||||
|
||||
let col3_mut = matrix.column_mut(3);
|
||||
col3_mut[0] = 70;
|
||||
|
||||
// Check changes via indexing
|
||||
assert_eq!(matrix[(0, 1)], 30);
|
||||
assert_eq!(matrix[(1, 1)], 40);
|
||||
assert_eq!(matrix[(0, 3)], 70);
|
||||
assert_eq!(matrix[(1, 3)], 8); // Unchanged
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Should be:
|
||||
// 1 30 5 70
|
||||
// 2 40 6 8
|
||||
assert_eq!(matrix.data(), &[1, 2, 30, 40, 5, 6, 70, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index")]
|
||||
fn test_column_mut_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.column_mut(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_columns() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let cols: Vec<&[i32]> = matrix.iter_columns().collect();
|
||||
assert_eq!(cols.len(), 4);
|
||||
assert_eq!(cols[0], &[1, 2]);
|
||||
assert_eq!(cols[1], &[3, 4]);
|
||||
assert_eq!(cols[2], &[5, 6]);
|
||||
assert_eq!(cols[3], &[7, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_rows() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let rows: Vec<Vec<i32>> = matrix
|
||||
.iter_rows()
|
||||
.map(|row| row.iter().cloned().collect())
|
||||
.collect();
|
||||
assert_eq!(rows.len(), 2);
|
||||
assert_eq!(rows[0], vec![1, 3, 5, 7]);
|
||||
assert_eq!(rows[1], vec![2, 4, 6, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matrix_row_get_and_iter() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let row0 = matrix.iter_rows().next().unwrap();
|
||||
assert_eq!(*row0.get(0), 1);
|
||||
assert_eq!(*row0.get(1), 3);
|
||||
assert_eq!(*row0.get(3), 7);
|
||||
let row0_vec: Vec<i32> = row0.iter().cloned().collect();
|
||||
assert_eq!(row0_vec, vec![1, 3, 5, 7]);
|
||||
|
||||
let row1 = matrix.iter_rows().nth(1).unwrap();
|
||||
assert_eq!(*row1.get(0), 2);
|
||||
assert_eq!(*row1.get(2), 6);
|
||||
let row1_vec: Vec<i32> = row1.iter().cloned().collect();
|
||||
assert_eq!(row1_vec, vec![2, 4, 6, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_columns() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
|
||||
matrix.swap_columns(0, 2); // Swap first and last
|
||||
|
||||
// Should be:
|
||||
// 7 4 1
|
||||
// 8 5 2
|
||||
// 9 6 3
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 7);
|
||||
assert_eq!(matrix[(1, 0)], 8);
|
||||
assert_eq!(matrix[(2, 0)], 9);
|
||||
assert_eq!(matrix[(0, 1)], 4); // Middle col unchanged
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(2, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 1);
|
||||
assert_eq!(matrix[(1, 2)], 2);
|
||||
assert_eq!(matrix[(2, 2)], 3);
|
||||
|
||||
// Swap the same column (should do nothing)
|
||||
let original_data = matrix.data().to_vec();
|
||||
matrix.swap_columns(1, 1);
|
||||
assert_eq!(matrix.data(), &original_data); // Data should be identical
|
||||
|
||||
// Check underlying data (column-major) after swap(0, 2)
|
||||
assert_eq!(matrix.data(), &[7, 8, 9, 4, 5, 6, 1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_swap_columns_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix.swap_columns(0, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_column() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
matrix.delete_column(1); // Delete the second column (index 1)
|
||||
|
||||
// Should be:
|
||||
// 1 5 7
|
||||
// 2 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 2);
|
||||
assert_eq!(matrix[(0, 1)], 5);
|
||||
assert_eq!(matrix[(1, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
assert_eq!(matrix[(1, 2)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
assert_eq!(matrix.data(), &[1, 2, 5, 6, 7, 8]);
|
||||
|
||||
// Delete the first column
|
||||
matrix.delete_column(0);
|
||||
// Should be:
|
||||
// 5 7
|
||||
// 6 8
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 2);
|
||||
assert_eq!(matrix.data(), &[5, 6, 7, 8]);
|
||||
|
||||
// Delete the last column
|
||||
matrix.delete_column(1);
|
||||
// Should be:
|
||||
// 5
|
||||
// 6
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 1);
|
||||
assert_eq!(matrix.data(), &[5, 6]);
|
||||
|
||||
// Delete the only column
|
||||
matrix.delete_column(0);
|
||||
// Should be empty
|
||||
assert_eq!(matrix.rows(), 2); // Rows stay the same
|
||||
assert_eq!(matrix.cols(), 0); // Cols becomes 0
|
||||
assert_eq!(matrix.data(), &[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_delete_column_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.delete_column(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_row() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
|
||||
matrix.delete_row(1); // Delete the second row (index 1)
|
||||
|
||||
// Should be:
|
||||
// 1 4 7
|
||||
// 3 6 9
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 3);
|
||||
assert_eq!(matrix[(0, 1)], 4);
|
||||
assert_eq!(matrix[(1, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
assert_eq!(matrix[(1, 2)], 9);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
// Delete row 1: [1, 3, 4, 6, 7, 9]
|
||||
assert_eq!(matrix.data(), &[1, 3, 4, 6, 7, 9]);
|
||||
|
||||
// Delete the first row
|
||||
matrix.delete_row(0);
|
||||
// Should be:
|
||||
// 3 6 9
|
||||
assert_eq!(matrix.rows(), 1);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[3, 6, 9]);
|
||||
|
||||
// Delete the last (and only) row
|
||||
matrix.delete_row(0);
|
||||
// Should be empty
|
||||
assert_eq!(matrix.rows(), 0); // Rows becomes 0
|
||||
assert_eq!(matrix.cols(), 3); // Cols stay the same
|
||||
assert_eq!(matrix.data(), &[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row index out of bounds")]
|
||||
fn test_delete_row_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix.delete_row(3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_column() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let new_col = vec![9, 10];
|
||||
matrix.add_column(2, new_col); // Add at index 2
|
||||
|
||||
// Should be:
|
||||
// 1 3 9 5 7
|
||||
// 2 4 10 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 5);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 2);
|
||||
assert_eq!(matrix[(0, 1)], 3);
|
||||
assert_eq!(matrix[(1, 1)], 4);
|
||||
assert_eq!(matrix[(0, 2)], 9);
|
||||
assert_eq!(matrix[(1, 2)], 10);
|
||||
assert_eq!(matrix[(0, 3)], 5); // Shifted
|
||||
assert_eq!(matrix[(1, 3)], 6);
|
||||
assert_eq!(matrix[(0, 4)], 7); // Shifted
|
||||
assert_eq!(matrix[(1, 4)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
// Add [9, 10] at index 2: [1, 2, 3, 4, 9, 10, 5, 6, 7, 8]
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);
|
||||
|
||||
// Add a column at the beginning
|
||||
let new_col_start = vec![11, 12];
|
||||
matrix.add_column(0, new_col_start);
|
||||
// Should be:
|
||||
// 11 1 3 9 5 7
|
||||
// 12 2 4 10 6 8
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 6);
|
||||
assert_eq!(matrix[(0, 0)], 11);
|
||||
assert_eq!(matrix[(1, 0)], 12);
|
||||
assert_eq!(matrix.data(), &[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);
|
||||
|
||||
// Add a column at the end
|
||||
let new_col_end = vec![13, 14];
|
||||
matrix.add_column(6, new_col_end);
|
||||
// Should be:
|
||||
// 11 1 3 9 5 7 13
|
||||
// 12 2 4 10 6 8 14
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 7);
|
||||
assert_eq!(matrix[(0, 6)], 13);
|
||||
assert_eq!(matrix[(1, 6)], 14);
|
||||
assert_eq!(
|
||||
matrix.data(),
|
||||
&[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8, 13, 14]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_add_column_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
let new_col = vec![9, 10];
|
||||
matrix.add_column(5, new_col); // Index 5 is out of bounds for 4 columns
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column length mismatch")]
|
||||
fn test_add_column_length_mismatch() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4 (2 rows)
|
||||
let new_col = vec![9, 10, 11]; // Wrong length
|
||||
matrix.add_column(0, new_col);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_row() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let new_row = vec![9, 10, 11, 12];
|
||||
matrix.add_row(1, new_row); // Add at index 1
|
||||
|
||||
// Should be:
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(0, 1)], 3);
|
||||
assert_eq!(matrix[(0, 2)], 5);
|
||||
assert_eq!(matrix[(0, 3)], 7);
|
||||
assert_eq!(matrix[(1, 0)], 9);
|
||||
assert_eq!(matrix[(1, 1)], 10);
|
||||
assert_eq!(matrix[(1, 2)], 11);
|
||||
assert_eq!(matrix[(1, 3)], 12);
|
||||
assert_eq!(matrix[(2, 0)], 2);
|
||||
assert_eq!(matrix[(2, 1)], 4);
|
||||
assert_eq!(matrix[(2, 2)], 6);
|
||||
assert_eq!(matrix[(2, 3)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8] (rows 0, 1)
|
||||
// Add [9, 10, 11, 12] at index 1 (new row will be index 1, original row 1 becomes index 2)
|
||||
// Col 0: [1, 9, 2]
|
||||
// Col 1: [3, 10, 4]
|
||||
// Col 2: [5, 11, 6]
|
||||
// Col 3: [7, 12, 8]
|
||||
// Data: [1, 9, 2, 3, 10, 4, 5, 11, 6, 7, 12, 8]
|
||||
assert_eq!(matrix.data(), &[1, 9, 2, 3, 10, 4, 5, 11, 6, 7, 12, 8]);
|
||||
|
||||
// Add a row at the beginning
|
||||
let new_row_start = vec![13, 14, 15, 16];
|
||||
matrix.add_row(0, new_row_start);
|
||||
// Should be:
|
||||
// 13 14 15 16
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
assert_eq!(matrix.rows(), 4);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
assert_eq!(matrix[(0, 0)], 13);
|
||||
assert_eq!(matrix[(0, 3)], 16);
|
||||
// Check some existing elements to ensure they shifted correctly
|
||||
assert_eq!(matrix[(1, 0)], 1);
|
||||
assert_eq!(matrix[(2, 1)], 10);
|
||||
assert_eq!(matrix[(3, 3)], 8);
|
||||
|
||||
// Add a row at the end
|
||||
let new_row_end = vec![17, 18, 19, 20];
|
||||
matrix.add_row(4, new_row_end);
|
||||
// Should be:
|
||||
// 13 14 15 16
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
// 17 18 19 20
|
||||
assert_eq!(matrix.rows(), 5);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
assert_eq!(matrix[(4, 0)], 17);
|
||||
assert_eq!(matrix[(4, 3)], 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row index out of bounds")]
|
||||
fn test_add_row_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
let new_row = vec![9, 10, 11, 12];
|
||||
matrix.add_row(3, new_row); // Index 3 is out of bounds for 2 rows
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row length mismatch")]
|
||||
fn test_add_row_length_mismatch() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4 (4 cols)
|
||||
let new_row = vec![9, 10, 11]; // Wrong length
|
||||
matrix.add_row(0, new_row);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_add() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![9, 8, 7, 6, 5, 4, 3, 2, 1], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 + &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1+9 4+6 7+3 => 10 10 10
|
||||
// 2+8 5+5 8+2 => 10 10 10
|
||||
// 3+7 6+4 9+1 => 10 10 10
|
||||
// Column-major data: [10, 10, 10, 10, 10, 10, 10, 10, 10]
|
||||
assert_eq!(result.data(), &[10, 10, 10, 10, 10, 10, 10, 10, 10]);
|
||||
assert_eq!(result[(0, 0)], 10);
|
||||
assert_eq!(result[(1, 1)], 10);
|
||||
assert_eq!(result[(2, 2)], 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_sub() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 - &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1-1 4-2 7-3 => 0 2 4
|
||||
// 2-1 5-2 8-3 => 1 3 5
|
||||
// 3-1 6-2 9-3 => 2 4 6
|
||||
// Column-major data: [0, 1, 2, 2, 3, 4, 4, 5, 6]
|
||||
assert_eq!(result.data(), &[0, 1, 2, 2, 3, 4, 4, 5, 6]);
|
||||
assert_eq!(result[(0, 0)], 0);
|
||||
assert_eq!(result[(1, 1)], 3);
|
||||
assert_eq!(result[(2, 2)], 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_mul() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 2, 3, 1, 2, 3, 1, 2, 3], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 * &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1*1 4*1 7*1 => 1 4 7
|
||||
// 2*2 5*2 8*2 => 4 10 16
|
||||
// 3*3 6*3 9*3 => 9 18 27
|
||||
// Column-major data: [1, 4, 9, 4, 10, 18, 7, 16, 27]
|
||||
assert_eq!(result.data(), &[1, 4, 9, 4, 10, 18, 7, 16, 27]);
|
||||
assert_eq!(result[(0, 0)], 1);
|
||||
assert_eq!(result[(1, 1)], 10);
|
||||
assert_eq!(result[(2, 2)], 27);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_div() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 7, 8, 9], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 / &matrix2; // Integer division
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1/1 4/2 7/7 => 1 2 1
|
||||
// 2/1 5/2 8/8 => 2 2 1 (integer division)
|
||||
// 3/1 6/2 9/9 => 3 3 1
|
||||
// Column-major data: [1, 2, 3, 2, 2, 3, 1, 1, 1]
|
||||
assert_eq!(result.data(), &[1, 2, 3, 2, 2, 3, 1, 1, 1]);
|
||||
assert_eq!(result[(0, 0)], 1);
|
||||
assert_eq!(result[(1, 1)], 2);
|
||||
assert_eq!(result[(2, 2)], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row count mismatch")]
|
||||
fn test_elementwise_op_row_mismatch() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = create_test_matrix_2x4(); // 2x4
|
||||
let _ = &matrix1 + &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row count mismatch")]
|
||||
fn test_elementwise_op_col_mismatch() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = create_test_matrix_2x4(); // 2x4
|
||||
let _ = &matrix1 * &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_and() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T & T = T
|
||||
// F & T = F
|
||||
// T & F = F
|
||||
// F & F = F
|
||||
// T & T = T
|
||||
// F & T = F
|
||||
// Data: [T, F, F, F, T, F]
|
||||
let expected_data = vec![true, false, false, false, true, false];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 & &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_or() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T | T = T
|
||||
// F | T = T
|
||||
// T | F = T
|
||||
// F | F = F
|
||||
// T | T = T
|
||||
// F | T = T
|
||||
// Data: [T, T, T, F, T, T]
|
||||
let expected_data = vec![true, true, true, false, true, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 | &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_xor() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T ^ T = F
|
||||
// F ^ T = T
|
||||
// T ^ F = T
|
||||
// F ^ F = F
|
||||
// T ^ T = F
|
||||
// F ^ T = T
|
||||
// Data: [F, T, T, F, F, T]
|
||||
let expected_data = vec![false, true, true, false, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 ^ &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_not() {
|
||||
let data = vec![true, false, true, false, true, false]; // 2x3
|
||||
let matrix = BoolMatrix::from_vec(data, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// !T = F
|
||||
// !F = T
|
||||
// !T = F
|
||||
// !F = T
|
||||
// !T = F
|
||||
// !F = T
|
||||
// Data: [F, T, F, T, F, T]
|
||||
let expected_data = vec![false, true, false, true, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = !matrix; // Not consumes the matrix
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col count mismatch")]
|
||||
fn test_bitwise_op_row_mismatch() {
|
||||
let data1 = vec![true, false, true, false]; // 2x2
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
let _ = &matrix1 & &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col count mismatch")]
|
||||
fn test_bitwise_op_col_mismatch() {
|
||||
let data1 = vec![true, false, true, false]; // 2x2
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
let _ = &matrix1 | &matrix2; // Should panic
|
||||
}
|
||||
|
||||
// Test with String type (requires Clone, PartialEq)
|
||||
#[test]
|
||||
fn test_string_matrix() {
|
||||
let data = vec![
|
||||
"a".to_string(),
|
||||
"b".to_string(),
|
||||
"c".to_string(),
|
||||
"d".to_string(),
|
||||
];
|
||||
let matrix = StringMatrix::from_vec(data.clone(), 2, 2); // 2x2
|
||||
|
||||
assert_eq!(matrix[(0, 0)], "a".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "b".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "c".to_string());
|
||||
assert_eq!(matrix[(1, 1)], "d".to_string());
|
||||
|
||||
// Test modification
|
||||
let mut matrix = matrix;
|
||||
matrix[(0, 0)] = "hello".to_string();
|
||||
assert_eq!(matrix[(0, 0)], "hello".to_string());
|
||||
|
||||
// Test add_column (requires Clone)
|
||||
let new_col = vec!["e".to_string(), "f".to_string()];
|
||||
matrix.add_column(1, new_col); // Add at index 1
|
||||
// Should be:
|
||||
// hello c d
|
||||
// b e f
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], "hello".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "b".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "e".to_string()); // New col
|
||||
assert_eq!(matrix[(1, 1)], "f".to_string()); // New col
|
||||
assert_eq!(matrix[(0, 2)], "c".to_string()); // Shifted
|
||||
assert_eq!(matrix[(1, 2)], "d".to_string()); // Shifted
|
||||
|
||||
// Test add_row (requires Clone)
|
||||
let new_row = vec!["g".to_string(), "h".to_string(), "i".to_string()];
|
||||
matrix.add_row(0, new_row); // Add at index 0
|
||||
// Should be:
|
||||
// g h i
|
||||
// hello e c
|
||||
// b f d
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], "g".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "h".to_string());
|
||||
assert_eq!(matrix[(0, 2)], "i".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "hello".to_string()); // Shifted
|
||||
assert_eq!(matrix[(2, 2)], "d".to_string()); // Shifted
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_matrix_ops() {
|
||||
let data1 = vec![1.0, 2.0, 3.0, 4.0]; // 2x2
|
||||
let data2 = vec![0.5, 1.5, 2.5, 3.5]; // 2x2
|
||||
let matrix1 = FloatMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = FloatMatrix::from_vec(data2, 2, 2);
|
||||
|
||||
let sum = &matrix1 + &matrix2;
|
||||
let diff = &matrix1 - &matrix2;
|
||||
let prod = &matrix1 * &matrix2;
|
||||
let div = &matrix1 / &matrix2;
|
||||
|
||||
// Check sums (col-major): [1.5, 3.5, 5.5, 7.5]
|
||||
assert_eq!(sum.data(), &[1.5, 3.5, 5.5, 7.5]);
|
||||
|
||||
// Check diffs (col-major): [0.5, 0.5, 0.5, 0.5]
|
||||
assert_eq!(diff.data(), &[0.5, 0.5, 0.5, 0.5]);
|
||||
|
||||
// Check prods (col-major): [0.5, 3.0, 7.5, 14.0]
|
||||
assert_eq!(prod.data(), &[0.5, 3.0, 7.5, 14.0]);
|
||||
|
||||
// Check divs (col-major): [2.0, 1.333..., 1.2, 1.14...]
|
||||
// Using element access for more specific checks on floating point results
|
||||
assert_eq!(div.rows(), 2);
|
||||
assert_eq!(div.cols(), 2);
|
||||
assert!((div[(0, 0)] - 1.0 / 0.5).abs() < 1e-9); // 2.0
|
||||
assert!((div[(1, 0)] - 2.0 / 1.5).abs() < 1e-9); // 1.333...
|
||||
assert!((div[(0, 1)] - 3.0 / 2.5).abs() < 1e-9); // 1.2
|
||||
assert!((div[(1, 1)] - 4.0 / 3.5).abs() < 1e-9); // 1.14...
|
||||
}
|
||||
|
||||
|
||||
fn create_test_matrix_i32() -> Matrix<i32> {
|
||||
Matrix::from_cols(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matrix_swap_columns_directly() {
|
||||
let mut matrix = create_test_matrix_i32();
|
||||
|
||||
// Store the initial state of the columns we intend to swap AND one that shouldn't change
|
||||
let initial_col0_data = matrix.column(0).to_vec(); // Should be [1, 2, 3]
|
||||
let initial_col1_data = matrix.column(1).to_vec(); // Should be [4, 5, 6]
|
||||
let initial_col2_data = matrix.column(2).to_vec(); // Should be [7, 8, 9]
|
||||
|
||||
// Perform the swap directly on the matrix
|
||||
matrix.swap_columns(0, 2); // Swap column 0 and column 2
|
||||
|
||||
// --- Assertions ---
|
||||
|
||||
// 1. Verify the dimensions are unchanged
|
||||
assert_eq!(matrix.rows(), 3, "Matrix rows should remain unchanged");
|
||||
assert_eq!(matrix.cols(), 3, "Matrix cols should remain unchanged");
|
||||
|
||||
// 2. Verify the column that was NOT swapped is unchanged
|
||||
assert_eq!(
|
||||
matrix.column(1),
|
||||
initial_col1_data.as_slice(), // Comparing slice to slice
|
||||
"Column 1 data should be unchanged"
|
||||
);
|
||||
|
||||
// 3. Verify the data swap occurred correctly using the COLUMN ACCESSOR
|
||||
// The data originally at index 0 should now be at index 2
|
||||
assert_eq!(
|
||||
matrix.column(2),
|
||||
initial_col0_data.as_slice(),
|
||||
"Column 2 should now contain the original data from column 0"
|
||||
);
|
||||
// The data originally at index 2 should now be at index 0
|
||||
assert_eq!(
|
||||
matrix.column(0),
|
||||
initial_col2_data.as_slice(),
|
||||
"Column 0 should now contain the original data from column 2"
|
||||
);
|
||||
|
||||
// 4. (Optional but useful) Verify the underlying raw data vector
|
||||
// Original data: [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
// Expected data after swapping col 0 and col 2: [7, 8, 9, 4, 5, 6, 1, 2, 3]
|
||||
assert_eq!(
|
||||
matrix.data(),
|
||||
&[7, 8, 9, 4, 5, 6, 1, 2, 3],
|
||||
"Underlying data vector is incorrect after swap"
|
||||
);
|
||||
|
||||
// 5. Test swapping with self (should be a no-op)
|
||||
let state_before_self_swap = matrix.clone();
|
||||
matrix.swap_columns(1, 1);
|
||||
assert_eq!(
|
||||
matrix, state_before_self_swap,
|
||||
"Swapping a column with itself should not change the matrix"
|
||||
);
|
||||
|
||||
// 6. Test swapping adjacent columns
|
||||
let mut matrix2 = create_test_matrix_i32();
|
||||
let initial_col0_data_m2 = matrix2.column(0).to_vec();
|
||||
let initial_col1_data_m2 = matrix2.column(1).to_vec();
|
||||
matrix2.swap_columns(0, 1);
|
||||
assert_eq!(matrix2.column(0), initial_col1_data_m2.as_slice());
|
||||
assert_eq!(matrix2.column(1), initial_col0_data_m2.as_slice());
|
||||
assert_eq!(matrix2.data(), &[4, 5, 6, 1, 2, 3, 7, 8, 9]);
|
||||
}
|
||||
// Axis enum doesn't have logic, no tests needed directly, but its presence is verified by compilation.
|
||||
}
|
||||
|
@ -141,3 +141,224 @@ impl SeriesOps for FloatMatrix {
|
||||
BoolMatrix::from_vec(data, self.rows(), self.cols())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Helper function to create a FloatMatrix for SeriesOps testing
|
||||
fn create_float_test_matrix() -> FloatMatrix {
|
||||
// 3x3 matrix (column-major) with some NaNs
|
||||
// 1.0 4.0 7.0
|
||||
// 2.0 NaN 8.0
|
||||
// 3.0 6.0 NaN
|
||||
let data = vec![1.0, 2.0, 3.0, 4.0, f64::NAN, 6.0, 7.0, 8.0, f64::NAN];
|
||||
FloatMatrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// --- Tests for SeriesOps (FloatMatrix) ---
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_sum_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 1.0 + 2.0 + 3.0 = 6.0
|
||||
// Col 1: 4.0 + NaN + 6.0 = 10.0 (NaN ignored)
|
||||
// Col 2: 7.0 + 8.0 + NaN = 15.0 (NaN ignored)
|
||||
let expected = vec![6.0, 10.0, 15.0];
|
||||
assert_eq!(matrix.sum_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_sum_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 1.0 + 4.0 + 7.0 = 12.0
|
||||
// Row 1: 2.0 + NaN + 8.0 = 10.0 (NaN ignored)
|
||||
// Row 2: 3.0 + 6.0 + NaN = 9.0 (NaN ignored)
|
||||
let expected = vec![12.0, 10.0, 9.0];
|
||||
assert_eq!(matrix.sum_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_prod_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 1.0 * 2.0 * 3.0 = 6.0
|
||||
// Col 1: 4.0 * NaN * 6.0 = 24.0 (NaN ignored, starts with 1.0)
|
||||
// Col 2: 7.0 * 8.0 * NaN = 56.0 (NaN ignored, starts with 1.0)
|
||||
let expected = vec![6.0, 24.0, 56.0];
|
||||
assert_eq!(matrix.prod_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_prod_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 1.0 * 4.0 * 7.0 = 28.0
|
||||
// Row 1: 2.0 * NaN * 8.0 = 16.0 (NaN ignored, starts with 1.0)
|
||||
// Row 2: 3.0 * 6.0 * NaN = 18.0 (NaN ignored, starts with 1.0)
|
||||
let expected = vec![28.0, 16.0, 18.0];
|
||||
assert_eq!(matrix.prod_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_cumsum_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: [1.0, 1.0+2.0=3.0, 3.0+3.0=6.0]
|
||||
// Col 1: [4.0, 4.0+NaN=4.0, 4.0+6.0=10.0] (NaN ignored, cumulative sum doesn't reset)
|
||||
// Col 2: [7.0, 7.0+8.0=15.0, 15.0+NaN=15.0]
|
||||
// Expected data (column-major): [1.0, 3.0, 6.0, 4.0, 4.0, 10.0, 7.0, 15.0, 15.0]
|
||||
let expected_data = vec![1.0, 3.0, 6.0, 4.0, 4.0, 10.0, 7.0, 15.0, 15.0];
|
||||
let expected_matrix = FloatMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.cumsum_vertical(), expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_cumsum_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: [1.0, 1.0+4.0=5.0, 5.0+7.0=12.0]
|
||||
// Row 1: [2.0, 2.0+NaN=2.0, 2.0+8.0=10.0] (NaN ignored, cumulative sum doesn't reset)
|
||||
// Row 2: [3.0, 3.0+6.0=9.0, 9.0+NaN=9.0]
|
||||
// Expected data (column-major construction from row results):
|
||||
// Col 0: (R0,C0)=1.0, (R1,C0)=2.0, (R2,C0)=3.0 => [1.0, 2.0, 3.0]
|
||||
// Col 1: (R0,C1)=5.0, (R1,C1)=2.0, (R2,C1)=9.0 => [5.0, 2.0, 9.0]
|
||||
// Col 2: (R0,C2)=12.0, (R1,C2)=10.0, (R2,C2)=9.0 => [12.0, 10.0, 9.0]
|
||||
// Combined data: [1.0, 2.0, 3.0, 5.0, 2.0, 9.0, 12.0, 10.0, 9.0]
|
||||
let expected_data = vec![1.0, 2.0, 3.0, 5.0, 2.0, 9.0, 12.0, 10.0, 9.0];
|
||||
let expected_matrix = FloatMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.cumsum_horizontal(), expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_count_nan_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 0 NaNs
|
||||
// Col 1: 1 NaN
|
||||
// Col 2: 1 NaN
|
||||
let expected = vec![0, 1, 1];
|
||||
assert_eq!(matrix.count_nan_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_count_nan_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 0 NaNs
|
||||
// Row 1: 1 NaN
|
||||
// Row 2: 1 NaN
|
||||
let expected = vec![0, 1, 1];
|
||||
assert_eq!(matrix.count_nan_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_is_nan() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Original data (col-major): [1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, NaN]
|
||||
// is_nan() applied: [F, F, F, F, T, F, F, F, T]
|
||||
let expected_data = vec![false, false, false, false, true, false, false, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.is_nan(), expected_matrix);
|
||||
}
|
||||
|
||||
// --- Edge Cases for SeriesOps ---
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_1x1() {
|
||||
let matrix = FloatMatrix::from_vec(vec![42.0], 1, 1);
|
||||
assert_eq!(matrix.sum_vertical(), vec![42.0]);
|
||||
assert_eq!(matrix.sum_horizontal(), vec![42.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![42.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![42.0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[42.0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[42.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![0]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![0]);
|
||||
assert_eq!(matrix.is_nan().data(), &[false]);
|
||||
|
||||
let matrix_nan = FloatMatrix::from_vec(vec![f64::NAN], 1, 1);
|
||||
assert_eq!(matrix_nan.sum_vertical(), vec![0.0]); // sum of empty set is 0
|
||||
assert_eq!(matrix_nan.sum_horizontal(), vec![0.0]);
|
||||
assert_eq!(matrix_nan.prod_vertical(), vec![1.0]); // product of empty set is 1
|
||||
assert_eq!(matrix_nan.prod_horizontal(), vec![1.0]);
|
||||
assert_eq!(matrix_nan.cumsum_vertical().data(), &[0.0]); // cumsum starts at 0, nan ignored
|
||||
assert_eq!(matrix_nan.cumsum_horizontal().data(), &[0.0]);
|
||||
assert_eq!(matrix_nan.count_nan_vertical(), vec![1]);
|
||||
assert_eq!(matrix_nan.count_nan_horizontal(), vec![1]);
|
||||
assert_eq!(matrix_nan.is_nan().data(), &[true]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_1xn_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![1.0, f64::NAN, 3.0, 4.0], 1, 4); // 1 row, 4 cols
|
||||
// Data: [1.0, NaN, 3.0, 4.0]
|
||||
|
||||
// Vertical (sums/prods/counts per column - each col is just one element)
|
||||
assert_eq!(matrix.sum_vertical(), vec![1.0, 0.0, 3.0, 4.0]); // NaN sum is 0
|
||||
assert_eq!(matrix.prod_vertical(), vec![1.0, 1.0, 3.0, 4.0]); // NaN prod is 1
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![0, 1, 0, 0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[1.0, 0.0, 3.0, 4.0]); // Cumsum on single element column
|
||||
|
||||
// Horizontal (sums/prods/counts for the single row)
|
||||
// Row 0: 1.0 + NaN + 3.0 + 4.0 = 8.0
|
||||
// Row 0: 1.0 * NaN * 3.0 * 4.0 = 12.0
|
||||
// Row 0: 1 NaN
|
||||
assert_eq!(matrix.sum_horizontal(), vec![8.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![12.0]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![1]);
|
||||
|
||||
// Cumsum Horizontal
|
||||
// Row 0: [1.0, 1.0+NaN=1.0, 1.0+3.0=4.0, 4.0+4.0=8.0]
|
||||
// Data (col-major): [1.0, 1.0, 4.0, 8.0] (since it's 1 row, data is the same as the row result)
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[1.0, 1.0, 4.0, 8.0]);
|
||||
|
||||
// is_nan
|
||||
// Data: [1.0, NaN, 3.0, 4.0]
|
||||
// Expected: [F, T, F, F]
|
||||
assert_eq!(matrix.is_nan().data(), &[false, true, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_nx1_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![1.0, 2.0, f64::NAN, 4.0], 4, 1); // 4 rows, 1 col
|
||||
// Data: [1.0, 2.0, NaN, 4.0]
|
||||
|
||||
// Vertical (sums/prods/counts for the single column)
|
||||
// Col 0: 1.0 + 2.0 + NaN + 4.0 = 7.0
|
||||
// Col 0: 1.0 * 2.0 * NaN * 4.0 = 8.0
|
||||
// Col 0: 1 NaN
|
||||
assert_eq!(matrix.sum_vertical(), vec![7.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![8.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![1]);
|
||||
|
||||
// Cumsum Vertical
|
||||
// Col 0: [1.0, 1.0+2.0=3.0, 3.0+NaN=3.0, 3.0+4.0=7.0]
|
||||
// Data (col-major): [1.0, 3.0, 3.0, 7.0] (since it's 1 col, data is the same as the col result)
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[1.0, 3.0, 3.0, 7.0]);
|
||||
|
||||
// Horizontal (sums/prods/counts per row - each row is just one element)
|
||||
assert_eq!(matrix.sum_horizontal(), vec![1.0, 2.0, 0.0, 4.0]); // NaN sum is 0
|
||||
assert_eq!(matrix.prod_horizontal(), vec![1.0, 2.0, 1.0, 4.0]); // NaN prod is 1
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![0, 0, 1, 0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[1.0, 2.0, 0.0, 4.0]); // Cumsum on single element row
|
||||
|
||||
// is_nan
|
||||
// Data: [1.0, 2.0, NaN, 4.0]
|
||||
// Expected: [F, F, T, F]
|
||||
assert_eq!(matrix.is_nan().data(), &[false, false, true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_all_nan_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![f64::NAN, f64::NAN, f64::NAN, f64::NAN], 2, 2);
|
||||
// NaN NaN
|
||||
// NaN NaN
|
||||
// Data: [NaN, NaN, NaN, NaN]
|
||||
|
||||
assert_eq!(matrix.sum_vertical(), vec![0.0, 0.0]);
|
||||
assert_eq!(matrix.sum_horizontal(), vec![0.0, 0.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![1.0, 1.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![1.0, 1.0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[0.0, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[0.0, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![2, 2]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![2, 2]);
|
||||
assert_eq!(matrix.is_nan().data(), &[true, true, true, true]);
|
||||
}
|
||||
}
|
@ -1,405 +0,0 @@
|
||||
// --- PASTE THE FRAME STRUCT AND IMPL HERE ---
|
||||
// #[derive(Debug, Clone, PartialEq, Eq)]
|
||||
// pub struct Frame<T: Clone> { ... }
|
||||
// impl<T: Clone> Frame<T> { ... }
|
||||
// impl<T: Clone> Index<&str> for Frame<T> { ... }
|
||||
// impl<T: Clone> IndexMut<&str> for Frame<T> { ... }
|
||||
// macro_rules! impl_elementwise_frame_op { ... }
|
||||
// impl_elementwise_frame_op!(Add, add, +);
|
||||
// ... etc ...
|
||||
// impl Not for Frame<bool> { ... }
|
||||
// --- END OF FRAME CODE ---
|
||||
|
||||
// Unit Tests
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustframe::frame::*;
|
||||
use rustframe::matrix::*;
|
||||
|
||||
// Helper function to create a standard test frame
|
||||
fn create_test_frame_i32() -> Frame<i32> {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![1, 2, 3], // Col "A"
|
||||
vec![4, 5, 6], // Col "B"
|
||||
vec![7, 8, 9], // Col "C"
|
||||
]);
|
||||
Frame::new(matrix, vec!["A", "B", "C"])
|
||||
}
|
||||
|
||||
fn create_test_frame_bool() -> Frame<bool> {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![true, false], // Col "P"
|
||||
vec![false, true], // Col "Q"
|
||||
]);
|
||||
Frame::new(matrix, vec!["P", "Q"])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_frame_success() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
let frame = Frame::new(matrix.clone(), vec!["col1", "col2"]);
|
||||
|
||||
assert_eq!(frame.column_names, vec!["col1", "col2"]);
|
||||
assert_eq!(frame.matrix(), &matrix);
|
||||
assert_eq!(frame.lookup.get("col1"), Some(&0));
|
||||
assert_eq!(frame.lookup.get("col2"), Some(&1));
|
||||
assert_eq!(frame.lookup.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column name count mismatch")]
|
||||
fn test_new_frame_panic_name_count_mismatch() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
Frame::new(matrix, vec!["col1"]); // Only one name for two columns
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: col1")]
|
||||
fn test_new_frame_panic_duplicate_names() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
Frame::new(matrix, vec!["col1", "col1"]); // Duplicate name
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_accessors() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
|
||||
// matrix()
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
assert_eq!(frame.matrix().cols(), 3);
|
||||
|
||||
// column()
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
assert_eq!(frame.column("C"), &[7, 8, 9]);
|
||||
|
||||
// column_mut()
|
||||
frame.column_mut("B")[1] = 50;
|
||||
assert_eq!(frame.column("B"), &[4, 50, 6]);
|
||||
|
||||
// column_index()
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
assert_eq!(frame.column_index("Z"), None);
|
||||
|
||||
// matrix_mut() - check by modifying through matrix_mut
|
||||
*frame.matrix_mut().get_mut(0, 0) = 100; // Modify element at (0, 0) which is A[0]
|
||||
assert_eq!(frame.column("A"), &[100, 2, 3]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_column_panic_unknown_label() {
|
||||
let frame = create_test_frame_i32();
|
||||
frame.column("Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_column_mut_panic_unknown_label() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.column_mut("Z");
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_swap_columns() {
|
||||
// let mut frame = create_test_frame_i32();
|
||||
// let initial_a_data = frame.column("A").to_vec();
|
||||
// let initial_c_data = frame.column("C").to_vec();
|
||||
|
||||
// frame.swap_columns("A", "C");
|
||||
|
||||
// // Check names order
|
||||
// assert_eq!(frame.column_names, vec!["C", "B", "A"]);
|
||||
|
||||
// // Check lookup map
|
||||
// assert_eq!(frame.column_index("A"), Some(2));
|
||||
// assert_eq!(frame.column_index("B"), Some(1));
|
||||
// assert_eq!(frame.column_index("C"), Some(0));
|
||||
|
||||
// // Check data using new names (should be swapped)
|
||||
// assert_eq!(frame.column("C"), initial_a_data); // "C" now has A's old data
|
||||
// assert_eq!(frame.column("A"), initial_c_data); // "A" now has C's old data
|
||||
// assert_eq!(frame.column("B"), &[4, 5, 6]); // "B" should be unchanged
|
||||
|
||||
// // Test swapping with self
|
||||
// let state_before_self_swap = frame.clone();
|
||||
// frame.swap_columns("B", "B");
|
||||
// assert_eq!(frame, state_before_self_swap);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_swap_columns_panic_unknown_a() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.swap_columns("Z", "B");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_swap_columns_panic_unknown_b() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.swap_columns("A", "Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let original_b_data = frame.column("B").to_vec();
|
||||
|
||||
frame.rename("B", "Beta");
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "Beta", "C"]);
|
||||
|
||||
// Check lookup
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("Beta"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
assert_eq!(frame.column_index("B"), None); // Old name gone
|
||||
|
||||
// Check data accessible via new name
|
||||
assert_eq!(frame.column("Beta"), original_b_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_rename_panic_unknown_old() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.rename("Z", "Omega");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: C")]
|
||||
fn test_rename_panic_duplicate_new() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.rename("A", "C"); // "C" already exists
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let new_col_data = vec![10, 11, 12];
|
||||
|
||||
frame.add_column("D", new_col_data.clone());
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "B", "C", "D"]);
|
||||
|
||||
// Check lookup
|
||||
assert_eq!(frame.column_index("D"), Some(3));
|
||||
|
||||
// Check matrix dimensions
|
||||
assert_eq!(frame.matrix().cols(), 4);
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
|
||||
// Check data of new column
|
||||
assert_eq!(frame.column("D"), new_col_data);
|
||||
// Check old columns are still there
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "duplicate column label: B")]
|
||||
fn test_add_column_panic_duplicate_name() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.add_column("B", vec![0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column length mismatch")]
|
||||
fn test_add_column_panic_length_mismatch() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
// Matrix::add_column panics if lengths mismatch
|
||||
frame.add_column("D", vec![10, 11]); // Only 2 elements, expected 3
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_column() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let original_b_data = frame.column("B").to_vec();
|
||||
let original_c_data = frame.column("C").to_vec(); // Need to check data shift
|
||||
|
||||
let deleted_data = frame.delete_column("B");
|
||||
|
||||
// Check returned data
|
||||
assert_eq!(deleted_data, original_b_data);
|
||||
|
||||
// Check names
|
||||
assert_eq!(frame.column_names, vec!["A", "C"]);
|
||||
|
||||
// Check lookup (rebuilt)
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("C"), Some(1));
|
||||
assert_eq!(frame.column_index("B"), None);
|
||||
|
||||
// Check matrix dimensions
|
||||
assert_eq!(frame.matrix().cols(), 2);
|
||||
assert_eq!(frame.matrix().rows(), 3);
|
||||
|
||||
// Check remaining data
|
||||
assert_eq!(frame.column("A"), &[1, 2, 3]);
|
||||
assert_eq!(frame.column("C"), original_c_data); // "C" should now be at index 1
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_delete_column_panic_unknown() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame.delete_column("Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sort_columns() {
|
||||
let matrix = Matrix::from_cols(vec![
|
||||
vec![7, 8, 9], // Col "C"
|
||||
vec![1, 2, 3], // Col "A"
|
||||
vec![4, 5, 6], // Col "B"
|
||||
]);
|
||||
let mut frame = Frame::new(matrix, vec!["C", "A", "B"]);
|
||||
|
||||
let orig_a = frame.column("A").to_vec();
|
||||
let orig_b = frame.column("B").to_vec();
|
||||
let orig_c = frame.column("C").to_vec();
|
||||
|
||||
frame.sort_columns();
|
||||
|
||||
// Check names order
|
||||
assert_eq!(frame.column_names, vec!["A", "B", "C"]);
|
||||
|
||||
// Check lookup map
|
||||
assert_eq!(frame.column_index("A"), Some(0));
|
||||
assert_eq!(frame.column_index("B"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(2));
|
||||
|
||||
// Check data integrity (data moved with the names)
|
||||
assert_eq!(frame.column("A"), orig_a);
|
||||
assert_eq!(frame.column("B"), orig_b);
|
||||
assert_eq!(frame.column("C"), orig_c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sort_columns_single_column() {
|
||||
let matrix = Matrix::from_cols(vec![vec![1, 2, 3]]);
|
||||
let mut frame = Frame::new(matrix.clone(), vec!["Solo"]);
|
||||
let expected = frame.clone();
|
||||
frame.sort_columns();
|
||||
assert_eq!(frame, expected); // Should be unchanged
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
let frame = create_test_frame_i32();
|
||||
assert_eq!(frame["A"].to_vec(), vec![1, 2, 3]);
|
||||
assert_eq!(frame["C"].to_vec(), vec![7, 8, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_index_panic_unknown() {
|
||||
let frame = create_test_frame_i32();
|
||||
let _ = frame["Z"];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_mut() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
frame["B"][0] = 42;
|
||||
frame["C"][2] = 99;
|
||||
|
||||
assert_eq!(frame["B"].to_vec(), &[42, 5, 6]);
|
||||
assert_eq!(frame["C"].to_vec(), &[7, 8, 99]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown column label: Z")]
|
||||
fn test_index_mut_panic_unknown() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let _ = &mut frame["Z"];
|
||||
}
|
||||
|
||||
// --- Test Ops ---
|
||||
#[test]
|
||||
fn test_elementwise_ops_numeric() {
|
||||
let frame1 = create_test_frame_i32(); // A=[1,2,3], B=[4,5,6], C=[7,8,9]
|
||||
let matrix2 = Matrix::from_cols(vec![
|
||||
vec![10, 10, 10], // Col "A"
|
||||
vec![2, 2, 2], // Col "B"
|
||||
vec![1, 1, 1], // Col "C"
|
||||
]);
|
||||
let frame2 = Frame::new(matrix2, vec!["A", "B", "C"]); // Must have same names and dims
|
||||
|
||||
// Add
|
||||
let frame_add = &frame1 + &frame2;
|
||||
assert_eq!(frame_add.column_names, frame1.column_names);
|
||||
assert_eq!(frame_add["A"].to_vec(), &[11, 12, 13]);
|
||||
assert_eq!(frame_add["B"].to_vec(), &[6, 7, 8]);
|
||||
assert_eq!(frame_add["C"].to_vec(), &[8, 9, 10]);
|
||||
|
||||
// Sub
|
||||
let frame_sub = &frame1 - &frame2;
|
||||
assert_eq!(frame_sub.column_names, frame1.column_names);
|
||||
assert_eq!(frame_sub["A"].to_vec(), &[-9, -8, -7]);
|
||||
assert_eq!(frame_sub["B"].to_vec(), &[2, 3, 4]);
|
||||
assert_eq!(frame_sub["C"].to_vec(), &[6, 7, 8]);
|
||||
|
||||
// Mul
|
||||
let frame_mul = &frame1 * &frame2;
|
||||
assert_eq!(frame_mul.column_names, frame1.column_names);
|
||||
assert_eq!(frame_mul["A"].to_vec(), &[10, 20, 30]);
|
||||
assert_eq!(frame_mul["B"].to_vec(), &[8, 10, 12]);
|
||||
assert_eq!(frame_mul["C"].to_vec(), &[7, 8, 9]);
|
||||
|
||||
// Div
|
||||
let frame_div = &frame1 / &frame2; // Integer division
|
||||
assert_eq!(frame_div.column_names, frame1.column_names);
|
||||
assert_eq!(frame_div["A"].to_vec(), &[0, 0, 0]); // 1/10, 2/10, 3/10
|
||||
assert_eq!(frame_div["B"].to_vec(), &[2, 2, 3]); // 4/2, 5/2, 6/2
|
||||
assert_eq!(frame_div["C"].to_vec(), &[7, 8, 9]); // 7/1, 8/1, 9/1
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic] // Exact message depends on Matrix op panic message ("row count mismatch" or "col count mismatch")
|
||||
fn test_elementwise_op_panic_dimension_mismatch() {
|
||||
let frame1 = create_test_frame_i32(); // 3x3
|
||||
let matrix2 = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]); // 2x2
|
||||
let frame2 = Frame::new(matrix2, vec!["X", "Y"]);
|
||||
let _ = &frame1 + &frame2; // Should panic due to dimension mismatch
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_ops_bool() {
|
||||
let frame1 = create_test_frame_bool(); // P=[T, F], Q=[F, T]
|
||||
let matrix2 = Matrix::from_cols(vec![
|
||||
vec![true, true], // P
|
||||
vec![false, false], // Q
|
||||
]);
|
||||
let frame2 = Frame::new(matrix2, vec!["P", "Q"]);
|
||||
|
||||
// BitAnd
|
||||
let frame_and = &frame1 & &frame2;
|
||||
assert_eq!(frame_and.column_names, frame1.column_names);
|
||||
assert_eq!(frame_and["P"].to_vec(), &[true, false]); // T&T=T, F&T=F
|
||||
assert_eq!(frame_and["Q"].to_vec(), &[false, false]); // F&F=F, T&F=F
|
||||
|
||||
// BitOr
|
||||
let frame_or = &frame1 | &frame2;
|
||||
assert_eq!(frame_or.column_names, frame1.column_names);
|
||||
assert_eq!(frame_or["P"].to_vec(), &[true, true]); // T|T=T, F|T=T
|
||||
assert_eq!(frame_or["Q"].to_vec(), &[false, true]); // F|F=F, T|F=T
|
||||
|
||||
// BitXor
|
||||
let frame_xor = &frame1 ^ &frame2;
|
||||
assert_eq!(frame_xor.column_names, frame1.column_names);
|
||||
assert_eq!(frame_xor["P"].to_vec(), &[false, true]); // T^T=F, F^T=T
|
||||
assert_eq!(frame_xor["Q"].to_vec(), &[false, true]); // F^F=F, T^F=T
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_op_bool() {
|
||||
let frame = create_test_frame_bool(); // P=[T, F], Q=[F, T]
|
||||
let frame_not = !frame; // Note: consumes the original frame
|
||||
|
||||
assert_eq!(frame_not.column_names, vec!["P", "Q"]);
|
||||
assert_eq!(frame_not["P"].to_vec(), &[false, true]);
|
||||
assert_eq!(frame_not["Q"].to_vec(), &[true, false]);
|
||||
}
|
||||
}
|
@ -1,385 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustframe::matrix::*;
|
||||
|
||||
// Helper function to create a FloatMatrix for SeriesOps testing
|
||||
fn create_float_test_matrix() -> FloatMatrix {
|
||||
// 3x3 matrix (column-major) with some NaNs
|
||||
// 1.0 4.0 7.0
|
||||
// 2.0 NaN 8.0
|
||||
// 3.0 6.0 NaN
|
||||
let data = vec![1.0, 2.0, 3.0, 4.0, f64::NAN, 6.0, 7.0, 8.0, f64::NAN];
|
||||
FloatMatrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// Helper function to create a BoolMatrix for BoolOps testing
|
||||
fn create_bool_test_matrix() -> BoolMatrix {
|
||||
// 3x3 matrix (column-major)
|
||||
// T F T
|
||||
// F T F
|
||||
// T F F
|
||||
let data = vec![true, false, true, false, true, false, true, false, false];
|
||||
BoolMatrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// --- Tests for SeriesOps (FloatMatrix) ---
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_sum_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 1.0 + 2.0 + 3.0 = 6.0
|
||||
// Col 1: 4.0 + NaN + 6.0 = 10.0 (NaN ignored)
|
||||
// Col 2: 7.0 + 8.0 + NaN = 15.0 (NaN ignored)
|
||||
let expected = vec![6.0, 10.0, 15.0];
|
||||
assert_eq!(matrix.sum_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_sum_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 1.0 + 4.0 + 7.0 = 12.0
|
||||
// Row 1: 2.0 + NaN + 8.0 = 10.0 (NaN ignored)
|
||||
// Row 2: 3.0 + 6.0 + NaN = 9.0 (NaN ignored)
|
||||
let expected = vec![12.0, 10.0, 9.0];
|
||||
assert_eq!(matrix.sum_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_prod_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 1.0 * 2.0 * 3.0 = 6.0
|
||||
// Col 1: 4.0 * NaN * 6.0 = 24.0 (NaN ignored, starts with 1.0)
|
||||
// Col 2: 7.0 * 8.0 * NaN = 56.0 (NaN ignored, starts with 1.0)
|
||||
let expected = vec![6.0, 24.0, 56.0];
|
||||
assert_eq!(matrix.prod_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_prod_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 1.0 * 4.0 * 7.0 = 28.0
|
||||
// Row 1: 2.0 * NaN * 8.0 = 16.0 (NaN ignored, starts with 1.0)
|
||||
// Row 2: 3.0 * 6.0 * NaN = 18.0 (NaN ignored, starts with 1.0)
|
||||
let expected = vec![28.0, 16.0, 18.0];
|
||||
assert_eq!(matrix.prod_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_cumsum_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: [1.0, 1.0+2.0=3.0, 3.0+3.0=6.0]
|
||||
// Col 1: [4.0, 4.0+NaN=4.0, 4.0+6.0=10.0] (NaN ignored, cumulative sum doesn't reset)
|
||||
// Col 2: [7.0, 7.0+8.0=15.0, 15.0+NaN=15.0]
|
||||
// Expected data (column-major): [1.0, 3.0, 6.0, 4.0, 4.0, 10.0, 7.0, 15.0, 15.0]
|
||||
let expected_data = vec![1.0, 3.0, 6.0, 4.0, 4.0, 10.0, 7.0, 15.0, 15.0];
|
||||
let expected_matrix = FloatMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.cumsum_vertical(), expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_cumsum_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: [1.0, 1.0+4.0=5.0, 5.0+7.0=12.0]
|
||||
// Row 1: [2.0, 2.0+NaN=2.0, 2.0+8.0=10.0] (NaN ignored, cumulative sum doesn't reset)
|
||||
// Row 2: [3.0, 3.0+6.0=9.0, 9.0+NaN=9.0]
|
||||
// Expected data (column-major construction from row results):
|
||||
// Col 0: (R0,C0)=1.0, (R1,C0)=2.0, (R2,C0)=3.0 => [1.0, 2.0, 3.0]
|
||||
// Col 1: (R0,C1)=5.0, (R1,C1)=2.0, (R2,C1)=9.0 => [5.0, 2.0, 9.0]
|
||||
// Col 2: (R0,C2)=12.0, (R1,C2)=10.0, (R2,C2)=9.0 => [12.0, 10.0, 9.0]
|
||||
// Combined data: [1.0, 2.0, 3.0, 5.0, 2.0, 9.0, 12.0, 10.0, 9.0]
|
||||
let expected_data = vec![1.0, 2.0, 3.0, 5.0, 2.0, 9.0, 12.0, 10.0, 9.0];
|
||||
let expected_matrix = FloatMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.cumsum_horizontal(), expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_count_nan_vertical() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Col 0: 0 NaNs
|
||||
// Col 1: 1 NaN
|
||||
// Col 2: 1 NaN
|
||||
let expected = vec![0, 1, 1];
|
||||
assert_eq!(matrix.count_nan_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_count_nan_horizontal() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Row 0: 0 NaNs
|
||||
// Row 1: 1 NaN
|
||||
// Row 2: 1 NaN
|
||||
let expected = vec![0, 1, 1];
|
||||
assert_eq!(matrix.count_nan_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_is_nan() {
|
||||
let matrix = create_float_test_matrix();
|
||||
// Original data (col-major): [1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, NaN]
|
||||
// is_nan() applied: [F, F, F, F, T, F, F, F, T]
|
||||
let expected_data = vec![false, false, false, false, true, false, false, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 3, 3);
|
||||
assert_eq!(matrix.is_nan(), expected_matrix);
|
||||
}
|
||||
|
||||
// --- Edge Cases for SeriesOps ---
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_1x1() {
|
||||
let matrix = FloatMatrix::from_vec(vec![42.0], 1, 1);
|
||||
assert_eq!(matrix.sum_vertical(), vec![42.0]);
|
||||
assert_eq!(matrix.sum_horizontal(), vec![42.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![42.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![42.0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[42.0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[42.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![0]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![0]);
|
||||
assert_eq!(matrix.is_nan().data(), &[false]);
|
||||
|
||||
let matrix_nan = FloatMatrix::from_vec(vec![f64::NAN], 1, 1);
|
||||
assert_eq!(matrix_nan.sum_vertical(), vec![0.0]); // sum of empty set is 0
|
||||
assert_eq!(matrix_nan.sum_horizontal(), vec![0.0]);
|
||||
assert_eq!(matrix_nan.prod_vertical(), vec![1.0]); // product of empty set is 1
|
||||
assert_eq!(matrix_nan.prod_horizontal(), vec![1.0]);
|
||||
assert_eq!(matrix_nan.cumsum_vertical().data(), &[0.0]); // cumsum starts at 0, nan ignored
|
||||
assert_eq!(matrix_nan.cumsum_horizontal().data(), &[0.0]);
|
||||
assert_eq!(matrix_nan.count_nan_vertical(), vec![1]);
|
||||
assert_eq!(matrix_nan.count_nan_horizontal(), vec![1]);
|
||||
assert_eq!(matrix_nan.is_nan().data(), &[true]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_1xn_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![1.0, f64::NAN, 3.0, 4.0], 1, 4); // 1 row, 4 cols
|
||||
// Data: [1.0, NaN, 3.0, 4.0]
|
||||
|
||||
// Vertical (sums/prods/counts per column - each col is just one element)
|
||||
assert_eq!(matrix.sum_vertical(), vec![1.0, 0.0, 3.0, 4.0]); // NaN sum is 0
|
||||
assert_eq!(matrix.prod_vertical(), vec![1.0, 1.0, 3.0, 4.0]); // NaN prod is 1
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![0, 1, 0, 0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[1.0, 0.0, 3.0, 4.0]); // Cumsum on single element column
|
||||
|
||||
// Horizontal (sums/prods/counts for the single row)
|
||||
// Row 0: 1.0 + NaN + 3.0 + 4.0 = 8.0
|
||||
// Row 0: 1.0 * NaN * 3.0 * 4.0 = 12.0
|
||||
// Row 0: 1 NaN
|
||||
assert_eq!(matrix.sum_horizontal(), vec![8.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![12.0]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![1]);
|
||||
|
||||
// Cumsum Horizontal
|
||||
// Row 0: [1.0, 1.0+NaN=1.0, 1.0+3.0=4.0, 4.0+4.0=8.0]
|
||||
// Data (col-major): [1.0, 1.0, 4.0, 8.0] (since it's 1 row, data is the same as the row result)
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[1.0, 1.0, 4.0, 8.0]);
|
||||
|
||||
// is_nan
|
||||
// Data: [1.0, NaN, 3.0, 4.0]
|
||||
// Expected: [F, T, F, F]
|
||||
assert_eq!(matrix.is_nan().data(), &[false, true, false, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_nx1_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![1.0, 2.0, f64::NAN, 4.0], 4, 1); // 4 rows, 1 col
|
||||
// Data: [1.0, 2.0, NaN, 4.0]
|
||||
|
||||
// Vertical (sums/prods/counts for the single column)
|
||||
// Col 0: 1.0 + 2.0 + NaN + 4.0 = 7.0
|
||||
// Col 0: 1.0 * 2.0 * NaN * 4.0 = 8.0
|
||||
// Col 0: 1 NaN
|
||||
assert_eq!(matrix.sum_vertical(), vec![7.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![8.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![1]);
|
||||
|
||||
// Cumsum Vertical
|
||||
// Col 0: [1.0, 1.0+2.0=3.0, 3.0+NaN=3.0, 3.0+4.0=7.0]
|
||||
// Data (col-major): [1.0, 3.0, 3.0, 7.0] (since it's 1 col, data is the same as the col result)
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[1.0, 3.0, 3.0, 7.0]);
|
||||
|
||||
// Horizontal (sums/prods/counts per row - each row is just one element)
|
||||
assert_eq!(matrix.sum_horizontal(), vec![1.0, 2.0, 0.0, 4.0]); // NaN sum is 0
|
||||
assert_eq!(matrix.prod_horizontal(), vec![1.0, 2.0, 1.0, 4.0]); // NaN prod is 1
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![0, 0, 1, 0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[1.0, 2.0, 0.0, 4.0]); // Cumsum on single element row
|
||||
|
||||
// is_nan
|
||||
// Data: [1.0, 2.0, NaN, 4.0]
|
||||
// Expected: [F, F, T, F]
|
||||
assert_eq!(matrix.is_nan().data(), &[false, false, true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_ops_all_nan_matrix() {
|
||||
let matrix = FloatMatrix::from_vec(vec![f64::NAN, f64::NAN, f64::NAN, f64::NAN], 2, 2);
|
||||
// NaN NaN
|
||||
// NaN NaN
|
||||
// Data: [NaN, NaN, NaN, NaN]
|
||||
|
||||
assert_eq!(matrix.sum_vertical(), vec![0.0, 0.0]);
|
||||
assert_eq!(matrix.sum_horizontal(), vec![0.0, 0.0]);
|
||||
assert_eq!(matrix.prod_vertical(), vec![1.0, 1.0]);
|
||||
assert_eq!(matrix.prod_horizontal(), vec![1.0, 1.0]);
|
||||
assert_eq!(matrix.cumsum_vertical().data(), &[0.0, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(matrix.cumsum_horizontal().data(), &[0.0, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(matrix.count_nan_vertical(), vec![2, 2]);
|
||||
assert_eq!(matrix.count_nan_horizontal(), vec![2, 2]);
|
||||
assert_eq!(matrix.is_nan().data(), &[true, true, true, true]);
|
||||
}
|
||||
|
||||
// --- Tests for BoolOps (BoolMatrix) ---
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: T | F | T = T
|
||||
// Col 1: F | T | F = T
|
||||
// Col 2: T | F | F = T
|
||||
let expected = vec![true, true, true];
|
||||
assert_eq!(matrix.any_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: T | F | T = T
|
||||
// Row 1: F | T | F = T
|
||||
// Row 2: T | F | F = T
|
||||
let expected = vec![true, true, true];
|
||||
assert_eq!(matrix.any_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: T & F & T = F
|
||||
// Col 1: F & T & F = F
|
||||
// Col 2: T & F & F = F
|
||||
let expected = vec![false, false, false];
|
||||
assert_eq!(matrix.all_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: T & F & T = F
|
||||
// Row 1: F & T & F = F
|
||||
// Row 2: T & F & F = F
|
||||
let expected = vec![false, false, false];
|
||||
assert_eq!(matrix.all_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_vertical() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Col 0: count true in [T, F, T] = 2
|
||||
// Col 1: count true in [F, T, F] = 1
|
||||
// Col 2: count true in [T, F, F] = 1
|
||||
let expected = vec![2, 1, 1];
|
||||
assert_eq!(matrix.count_vertical(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_horizontal() {
|
||||
let matrix = create_bool_test_matrix();
|
||||
// Row 0: count true in [T, F, T] = 2
|
||||
// Row 1: count true in [F, T, F] = 1
|
||||
// Row 2: count true in [T, F, F] = 1
|
||||
let expected = vec![2, 1, 1];
|
||||
assert_eq!(matrix.count_horizontal(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_any_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Has true values
|
||||
assert!(matrix.any());
|
||||
|
||||
let matrix_all_false = BoolMatrix::from_vec(vec![false; 9], 3, 3);
|
||||
assert!(!matrix_all_false.any());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_all_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Has false values
|
||||
assert!(!matrix.all());
|
||||
|
||||
let matrix_all_true = BoolMatrix::from_vec(vec![true; 9], 3, 3);
|
||||
assert!(matrix_all_true.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_count_overall() {
|
||||
let matrix = create_bool_test_matrix(); // Data: [T, F, T, F, T, F, T, F, F]
|
||||
// Count of true values: 4
|
||||
assert_eq!(matrix.count(), 4);
|
||||
|
||||
let matrix_all_false = BoolMatrix::from_vec(vec![false; 5], 5, 1); // 5x1
|
||||
assert_eq!(matrix_all_false.count(), 0);
|
||||
|
||||
let matrix_all_true = BoolMatrix::from_vec(vec![true; 4], 2, 2); // 2x2
|
||||
assert_eq!(matrix_all_true.count(), 4);
|
||||
}
|
||||
|
||||
// --- Edge Cases for BoolOps ---
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_1x1() {
|
||||
let matrix_t = BoolMatrix::from_vec(vec![true], 1, 1);
|
||||
assert_eq!(matrix_t.any_vertical(), vec![true]);
|
||||
assert_eq!(matrix_t.any_horizontal(), vec![true]);
|
||||
assert_eq!(matrix_t.all_vertical(), vec![true]);
|
||||
assert_eq!(matrix_t.all_horizontal(), vec![true]);
|
||||
assert_eq!(matrix_t.count_vertical(), vec![1]);
|
||||
assert_eq!(matrix_t.count_horizontal(), vec![1]);
|
||||
assert!(matrix_t.any());
|
||||
assert!(matrix_t.all());
|
||||
assert_eq!(matrix_t.count(), 1);
|
||||
|
||||
let matrix_f = BoolMatrix::from_vec(vec![false], 1, 1);
|
||||
assert_eq!(matrix_f.any_vertical(), vec![false]);
|
||||
assert_eq!(matrix_f.any_horizontal(), vec![false]);
|
||||
assert_eq!(matrix_f.all_vertical(), vec![false]);
|
||||
assert_eq!(matrix_f.all_horizontal(), vec![false]);
|
||||
assert_eq!(matrix_f.count_vertical(), vec![0]);
|
||||
assert_eq!(matrix_f.count_horizontal(), vec![0]);
|
||||
assert!(!matrix_f.any());
|
||||
assert!(!matrix_f.all());
|
||||
assert_eq!(matrix_f.count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_1xn_matrix() {
|
||||
let matrix = BoolMatrix::from_vec(vec![true, false, false, true], 1, 4); // 1 row, 4 cols
|
||||
// Data: [T, F, F, T]
|
||||
|
||||
assert_eq!(matrix.any_vertical(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.all_vertical(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.count_vertical(), vec![1, 0, 0, 1]);
|
||||
|
||||
assert_eq!(matrix.any_horizontal(), vec![true]); // T | F | F | T = T
|
||||
assert_eq!(matrix.all_horizontal(), vec![false]); // T & F & F & T = F
|
||||
assert_eq!(matrix.count_horizontal(), vec![2]); // count true in [T, F, F, T] = 2
|
||||
|
||||
assert!(matrix.any());
|
||||
assert!(!matrix.all());
|
||||
assert_eq!(matrix.count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops_nx1_matrix() {
|
||||
let matrix = BoolMatrix::from_vec(vec![true, false, false, true], 4, 1); // 4 rows, 1 col
|
||||
// Data: [T, F, F, T]
|
||||
|
||||
assert_eq!(matrix.any_vertical(), vec![true]); // T|F|F|T = T
|
||||
assert_eq!(matrix.all_vertical(), vec![false]); // T&F&F&T = F
|
||||
assert_eq!(matrix.count_vertical(), vec![2]); // count true in [T, F, F, T] = 2
|
||||
|
||||
assert_eq!(matrix.any_horizontal(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.all_horizontal(), vec![true, false, false, true]);
|
||||
assert_eq!(matrix.count_horizontal(), vec![1, 0, 0, 1]);
|
||||
|
||||
assert!(matrix.any());
|
||||
assert!(!matrix.all());
|
||||
assert_eq!(matrix.count(), 2);
|
||||
}
|
||||
}
|
@ -1,890 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustframe::matrix::{BoolMatrix, FloatMatrix, Matrix, StringMatrix};
|
||||
|
||||
// Helper function to create a basic Matrix for testing
|
||||
fn create_test_matrix() -> Matrix<i32> {
|
||||
// Column-major data:
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
Matrix::from_vec(data, 3, 3)
|
||||
}
|
||||
|
||||
// Another helper for a different size
|
||||
fn create_test_matrix_2x4() -> Matrix<i32> {
|
||||
// Column-major data:
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
|
||||
Matrix::from_vec(data, 2, 4)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_vec_basic() {
|
||||
let data = vec![1, 2, 3, 4, 5, 6]; // 2 rows, 3 cols (column-major)
|
||||
let matrix = Matrix::from_vec(data, 2, 3);
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6]);
|
||||
|
||||
// Check some elements
|
||||
assert_eq!(matrix[(0, 0)], 1); // First row, first col
|
||||
assert_eq!(matrix[(1, 0)], 2); // Second row, first col
|
||||
assert_eq!(matrix[(0, 1)], 3); // First row, second col
|
||||
assert_eq!(matrix[(1, 2)], 6); // Second row, third col
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "data length mismatch")]
|
||||
fn test_from_vec_wrong_length() {
|
||||
let data = vec![1, 2, 3, 4, 5]; // Should be 6 for 2x3
|
||||
Matrix::from_vec(data, 2, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one row")]
|
||||
fn test_from_vec_zero_rows() {
|
||||
let data = vec![1, 2, 3];
|
||||
Matrix::from_vec(data, 0, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one column")]
|
||||
fn test_from_vec_zero_cols() {
|
||||
let data = vec![1, 2, 3];
|
||||
Matrix::from_vec(data, 3, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cols_basic() {
|
||||
// Representing:
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
let cols_data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
||||
let matrix = Matrix::from_cols(cols_data);
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
// Internal data should be column-major
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
// Check some elements
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(2, 0)], 3);
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cols_1x1() {
|
||||
let cols_data = vec![vec![42]];
|
||||
let matrix = Matrix::from_cols(cols_data);
|
||||
assert_eq!(matrix.rows(), 1);
|
||||
assert_eq!(matrix.cols(), 1);
|
||||
assert_eq!(matrix.data(), &[42]);
|
||||
assert_eq!(matrix[(0, 0)], 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one column")]
|
||||
fn test_from_cols_empty_cols() {
|
||||
let empty_cols: Vec<Vec<i32>> = vec![];
|
||||
Matrix::from_cols(empty_cols);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "need at least one row")]
|
||||
fn test_from_cols_empty_rows() {
|
||||
let empty_row: Vec<Vec<String>> = vec![vec![], vec![]];
|
||||
Matrix::from_cols(empty_row);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col 1 has len 2, expected 3")]
|
||||
fn test_from_cols_mismatched_lengths() {
|
||||
let cols_data = vec![vec![1, 2, 3], vec![4, 5], vec![6, 7, 8]];
|
||||
Matrix::from_cols(cols_data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_getters() {
|
||||
let matrix = create_test_matrix();
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_and_get() {
|
||||
let matrix = create_test_matrix();
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(2, 2)], 9);
|
||||
|
||||
assert_eq!(*matrix.get(0, 0), 1);
|
||||
assert_eq!(*matrix.get(1, 1), 5);
|
||||
assert_eq!(*matrix.get(2, 2), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_out_of_bounds_row() {
|
||||
let matrix = create_test_matrix(); // 3x3
|
||||
let _ = matrix[(3, 0)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_out_of_bounds_col() {
|
||||
let matrix = create_test_matrix(); // 3x3
|
||||
let _ = matrix[(0, 3)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index_mut_and_get_mut() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
|
||||
matrix[(0, 0)] = 10;
|
||||
matrix[(1, 1)] = 20;
|
||||
matrix[(2, 2)] = 30;
|
||||
|
||||
assert_eq!(matrix[(0, 0)], 10);
|
||||
assert_eq!(matrix[(1, 1)], 20);
|
||||
assert_eq!(matrix[(2, 2)], 30);
|
||||
|
||||
*matrix.get_mut(0, 1) = 15;
|
||||
*matrix.get_mut(2, 1) = 25;
|
||||
|
||||
assert_eq!(matrix[(0, 1)], 15);
|
||||
assert_eq!(matrix[(2, 1)], 25);
|
||||
|
||||
// Check underlying data consistency (column-major)
|
||||
// Should be:
|
||||
// 10 15 7
|
||||
// 2 20 8
|
||||
// 3 25 30
|
||||
assert_eq!(matrix.data(), &[10, 2, 3, 15, 20, 25, 7, 8, 30]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_mut_out_of_bounds_row() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix[(3, 0)] = 99;
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "index out of bounds")]
|
||||
fn test_index_mut_out_of_bounds_col() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix[(0, 3)] = 99;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_column() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
assert_eq!(matrix.column(0), &[1, 2]);
|
||||
assert_eq!(matrix.column(1), &[3, 4]);
|
||||
assert_eq!(matrix.column(2), &[5, 6]);
|
||||
assert_eq!(matrix.column(3), &[7, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index")]
|
||||
fn test_column_out_of_bounds() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.column(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_column_mut() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let col1_mut = matrix.column_mut(1);
|
||||
col1_mut[0] = 30;
|
||||
col1_mut[1] = 40;
|
||||
|
||||
let col3_mut = matrix.column_mut(3);
|
||||
col3_mut[0] = 70;
|
||||
|
||||
// Check changes via indexing
|
||||
assert_eq!(matrix[(0, 1)], 30);
|
||||
assert_eq!(matrix[(1, 1)], 40);
|
||||
assert_eq!(matrix[(0, 3)], 70);
|
||||
assert_eq!(matrix[(1, 3)], 8); // Unchanged
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Should be:
|
||||
// 1 30 5 70
|
||||
// 2 40 6 8
|
||||
assert_eq!(matrix.data(), &[1, 2, 30, 40, 5, 6, 70, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index")]
|
||||
fn test_column_mut_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.column_mut(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_columns() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let cols: Vec<&[i32]> = matrix.iter_columns().collect();
|
||||
assert_eq!(cols.len(), 4);
|
||||
assert_eq!(cols[0], &[1, 2]);
|
||||
assert_eq!(cols[1], &[3, 4]);
|
||||
assert_eq!(cols[2], &[5, 6]);
|
||||
assert_eq!(cols[3], &[7, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_rows() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let rows: Vec<Vec<i32>> = matrix
|
||||
.iter_rows()
|
||||
.map(|row| row.iter().cloned().collect())
|
||||
.collect();
|
||||
assert_eq!(rows.len(), 2);
|
||||
assert_eq!(rows[0], vec![1, 3, 5, 7]);
|
||||
assert_eq!(rows[1], vec![2, 4, 6, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matrix_row_get_and_iter() {
|
||||
let matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let row0 = matrix.iter_rows().next().unwrap();
|
||||
assert_eq!(*row0.get(0), 1);
|
||||
assert_eq!(*row0.get(1), 3);
|
||||
assert_eq!(*row0.get(3), 7);
|
||||
let row0_vec: Vec<i32> = row0.iter().cloned().collect();
|
||||
assert_eq!(row0_vec, vec![1, 3, 5, 7]);
|
||||
|
||||
let row1 = matrix.iter_rows().nth(1).unwrap();
|
||||
assert_eq!(*row1.get(0), 2);
|
||||
assert_eq!(*row1.get(2), 6);
|
||||
let row1_vec: Vec<i32> = row1.iter().cloned().collect();
|
||||
assert_eq!(row1_vec, vec![2, 4, 6, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_columns() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
|
||||
matrix.swap_columns(0, 2); // Swap first and last
|
||||
|
||||
// Should be:
|
||||
// 7 4 1
|
||||
// 8 5 2
|
||||
// 9 6 3
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 7);
|
||||
assert_eq!(matrix[(1, 0)], 8);
|
||||
assert_eq!(matrix[(2, 0)], 9);
|
||||
assert_eq!(matrix[(0, 1)], 4); // Middle col unchanged
|
||||
assert_eq!(matrix[(1, 1)], 5);
|
||||
assert_eq!(matrix[(2, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 1);
|
||||
assert_eq!(matrix[(1, 2)], 2);
|
||||
assert_eq!(matrix[(2, 2)], 3);
|
||||
|
||||
// Swap the same column (should do nothing)
|
||||
let original_data = matrix.data().to_vec();
|
||||
matrix.swap_columns(1, 1);
|
||||
assert_eq!(matrix.data(), &original_data); // Data should be identical
|
||||
|
||||
// Check underlying data (column-major) after swap(0, 2)
|
||||
assert_eq!(matrix.data(), &[7, 8, 9, 4, 5, 6, 1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_swap_columns_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix.swap_columns(0, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_column() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
matrix.delete_column(1); // Delete the second column (index 1)
|
||||
|
||||
// Should be:
|
||||
// 1 5 7
|
||||
// 2 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 2);
|
||||
assert_eq!(matrix[(0, 1)], 5);
|
||||
assert_eq!(matrix[(1, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
assert_eq!(matrix[(1, 2)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
assert_eq!(matrix.data(), &[1, 2, 5, 6, 7, 8]);
|
||||
|
||||
// Delete the first column
|
||||
matrix.delete_column(0);
|
||||
// Should be:
|
||||
// 5 7
|
||||
// 6 8
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 2);
|
||||
assert_eq!(matrix.data(), &[5, 6, 7, 8]);
|
||||
|
||||
// Delete the last column
|
||||
matrix.delete_column(1);
|
||||
// Should be:
|
||||
// 5
|
||||
// 6
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 1);
|
||||
assert_eq!(matrix.data(), &[5, 6]);
|
||||
|
||||
// Delete the only column
|
||||
matrix.delete_column(0);
|
||||
// Should be empty
|
||||
assert_eq!(matrix.rows(), 2); // Rows stay the same
|
||||
assert_eq!(matrix.cols(), 0); // Cols becomes 0
|
||||
assert_eq!(matrix.data(), &[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_delete_column_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
matrix.delete_column(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_row() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
// 1 4 7
|
||||
// 2 5 8
|
||||
// 3 6 9
|
||||
|
||||
matrix.delete_row(1); // Delete the second row (index 1)
|
||||
|
||||
// Should be:
|
||||
// 1 4 7
|
||||
// 3 6 9
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 3);
|
||||
assert_eq!(matrix[(0, 1)], 4);
|
||||
assert_eq!(matrix[(1, 1)], 6);
|
||||
assert_eq!(matrix[(0, 2)], 7);
|
||||
assert_eq!(matrix[(1, 2)], 9);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
// Delete row 1: [1, 3, 4, 6, 7, 9]
|
||||
assert_eq!(matrix.data(), &[1, 3, 4, 6, 7, 9]);
|
||||
|
||||
// Delete the first row
|
||||
matrix.delete_row(0);
|
||||
// Should be:
|
||||
// 3 6 9
|
||||
assert_eq!(matrix.rows(), 1);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix.data(), &[3, 6, 9]);
|
||||
|
||||
// Delete the last (and only) row
|
||||
matrix.delete_row(0);
|
||||
// Should be empty
|
||||
assert_eq!(matrix.rows(), 0); // Rows becomes 0
|
||||
assert_eq!(matrix.cols(), 3); // Cols stay the same
|
||||
assert_eq!(matrix.data(), &[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row index out of bounds")]
|
||||
fn test_delete_row_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix(); // 3x3
|
||||
matrix.delete_row(3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_column() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let new_col = vec![9, 10];
|
||||
matrix.add_column(2, new_col); // Add at index 2
|
||||
|
||||
// Should be:
|
||||
// 1 3 9 5 7
|
||||
// 2 4 10 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 5);
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(1, 0)], 2);
|
||||
assert_eq!(matrix[(0, 1)], 3);
|
||||
assert_eq!(matrix[(1, 1)], 4);
|
||||
assert_eq!(matrix[(0, 2)], 9);
|
||||
assert_eq!(matrix[(1, 2)], 10);
|
||||
assert_eq!(matrix[(0, 3)], 5); // Shifted
|
||||
assert_eq!(matrix[(1, 3)], 6);
|
||||
assert_eq!(matrix[(0, 4)], 7); // Shifted
|
||||
assert_eq!(matrix[(1, 4)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
// Add [9, 10] at index 2: [1, 2, 3, 4, 9, 10, 5, 6, 7, 8]
|
||||
assert_eq!(matrix.data(), &[1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);
|
||||
|
||||
// Add a column at the beginning
|
||||
let new_col_start = vec![11, 12];
|
||||
matrix.add_column(0, new_col_start);
|
||||
// Should be:
|
||||
// 11 1 3 9 5 7
|
||||
// 12 2 4 10 6 8
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 6);
|
||||
assert_eq!(matrix[(0, 0)], 11);
|
||||
assert_eq!(matrix[(1, 0)], 12);
|
||||
assert_eq!(matrix.data(), &[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8]);
|
||||
|
||||
// Add a column at the end
|
||||
let new_col_end = vec![13, 14];
|
||||
matrix.add_column(6, new_col_end);
|
||||
// Should be:
|
||||
// 11 1 3 9 5 7 13
|
||||
// 12 2 4 10 6 8 14
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 7);
|
||||
assert_eq!(matrix[(0, 6)], 13);
|
||||
assert_eq!(matrix[(1, 6)], 14);
|
||||
assert_eq!(
|
||||
matrix.data(),
|
||||
&[11, 12, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8, 13, 14]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column index out of bounds")]
|
||||
fn test_add_column_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
let new_col = vec![9, 10];
|
||||
matrix.add_column(5, new_col); // Index 5 is out of bounds for 4 columns
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "column length mismatch")]
|
||||
fn test_add_column_length_mismatch() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4 (2 rows)
|
||||
let new_col = vec![9, 10, 11]; // Wrong length
|
||||
matrix.add_column(0, new_col);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_row() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
// 1 3 5 7
|
||||
// 2 4 6 8
|
||||
|
||||
let new_row = vec![9, 10, 11, 12];
|
||||
matrix.add_row(1, new_row); // Add at index 1
|
||||
|
||||
// Should be:
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
|
||||
assert_eq!(matrix[(0, 0)], 1);
|
||||
assert_eq!(matrix[(0, 1)], 3);
|
||||
assert_eq!(matrix[(0, 2)], 5);
|
||||
assert_eq!(matrix[(0, 3)], 7);
|
||||
assert_eq!(matrix[(1, 0)], 9);
|
||||
assert_eq!(matrix[(1, 1)], 10);
|
||||
assert_eq!(matrix[(1, 2)], 11);
|
||||
assert_eq!(matrix[(1, 3)], 12);
|
||||
assert_eq!(matrix[(2, 0)], 2);
|
||||
assert_eq!(matrix[(2, 1)], 4);
|
||||
assert_eq!(matrix[(2, 2)], 6);
|
||||
assert_eq!(matrix[(2, 3)], 8);
|
||||
|
||||
// Check underlying data (column-major)
|
||||
// Original: [1, 2, 3, 4, 5, 6, 7, 8] (rows 0, 1)
|
||||
// Add [9, 10, 11, 12] at index 1 (new row will be index 1, original row 1 becomes index 2)
|
||||
// Col 0: [1, 9, 2]
|
||||
// Col 1: [3, 10, 4]
|
||||
// Col 2: [5, 11, 6]
|
||||
// Col 3: [7, 12, 8]
|
||||
// Data: [1, 9, 2, 3, 10, 4, 5, 11, 6, 7, 12, 8]
|
||||
assert_eq!(matrix.data(), &[1, 9, 2, 3, 10, 4, 5, 11, 6, 7, 12, 8]);
|
||||
|
||||
// Add a row at the beginning
|
||||
let new_row_start = vec![13, 14, 15, 16];
|
||||
matrix.add_row(0, new_row_start);
|
||||
// Should be:
|
||||
// 13 14 15 16
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
assert_eq!(matrix.rows(), 4);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
assert_eq!(matrix[(0, 0)], 13);
|
||||
assert_eq!(matrix[(0, 3)], 16);
|
||||
// Check some existing elements to ensure they shifted correctly
|
||||
assert_eq!(matrix[(1, 0)], 1);
|
||||
assert_eq!(matrix[(2, 1)], 10);
|
||||
assert_eq!(matrix[(3, 3)], 8);
|
||||
|
||||
// Add a row at the end
|
||||
let new_row_end = vec![17, 18, 19, 20];
|
||||
matrix.add_row(4, new_row_end);
|
||||
// Should be:
|
||||
// 13 14 15 16
|
||||
// 1 3 5 7
|
||||
// 9 10 11 12
|
||||
// 2 4 6 8
|
||||
// 17 18 19 20
|
||||
assert_eq!(matrix.rows(), 5);
|
||||
assert_eq!(matrix.cols(), 4);
|
||||
assert_eq!(matrix[(4, 0)], 17);
|
||||
assert_eq!(matrix[(4, 3)], 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row index out of bounds")]
|
||||
fn test_add_row_out_of_bounds() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4
|
||||
let new_row = vec![9, 10, 11, 12];
|
||||
matrix.add_row(3, new_row); // Index 3 is out of bounds for 2 rows
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row length mismatch")]
|
||||
fn test_add_row_length_mismatch() {
|
||||
let mut matrix = create_test_matrix_2x4(); // 2x4 (4 cols)
|
||||
let new_row = vec![9, 10, 11]; // Wrong length
|
||||
matrix.add_row(0, new_row);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_add() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![9, 8, 7, 6, 5, 4, 3, 2, 1], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 + &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1+9 4+6 7+3 => 10 10 10
|
||||
// 2+8 5+5 8+2 => 10 10 10
|
||||
// 3+7 6+4 9+1 => 10 10 10
|
||||
// Column-major data: [10, 10, 10, 10, 10, 10, 10, 10, 10]
|
||||
assert_eq!(result.data(), &[10, 10, 10, 10, 10, 10, 10, 10, 10]);
|
||||
assert_eq!(result[(0, 0)], 10);
|
||||
assert_eq!(result[(1, 1)], 10);
|
||||
assert_eq!(result[(2, 2)], 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_sub() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 3, 3, 3], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 - &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1-1 4-2 7-3 => 0 2 4
|
||||
// 2-1 5-2 8-3 => 1 3 5
|
||||
// 3-1 6-2 9-3 => 2 4 6
|
||||
// Column-major data: [0, 1, 2, 2, 3, 4, 4, 5, 6]
|
||||
assert_eq!(result.data(), &[0, 1, 2, 2, 3, 4, 4, 5, 6]);
|
||||
assert_eq!(result[(0, 0)], 0);
|
||||
assert_eq!(result[(1, 1)], 3);
|
||||
assert_eq!(result[(2, 2)], 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_mul() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 2, 3, 1, 2, 3, 1, 2, 3], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 * &matrix2;
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1*1 4*1 7*1 => 1 4 7
|
||||
// 2*2 5*2 8*2 => 4 10 16
|
||||
// 3*3 6*3 9*3 => 9 18 27
|
||||
// Column-major data: [1, 4, 9, 4, 10, 18, 7, 16, 27]
|
||||
assert_eq!(result.data(), &[1, 4, 9, 4, 10, 18, 7, 16, 27]);
|
||||
assert_eq!(result[(0, 0)], 1);
|
||||
assert_eq!(result[(1, 1)], 10);
|
||||
assert_eq!(result[(2, 2)], 27);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elementwise_div() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = Matrix::from_vec(vec![1, 1, 1, 2, 2, 2, 7, 8, 9], 3, 3); // 3x3
|
||||
|
||||
let result = &matrix1 / &matrix2; // Integer division
|
||||
|
||||
assert_eq!(result.rows(), 3);
|
||||
assert_eq!(result.cols(), 3);
|
||||
|
||||
// Expected:
|
||||
// 1/1 4/2 7/7 => 1 2 1
|
||||
// 2/1 5/2 8/8 => 2 2 1 (integer division)
|
||||
// 3/1 6/2 9/9 => 3 3 1
|
||||
// Column-major data: [1, 2, 3, 2, 2, 3, 1, 1, 1]
|
||||
assert_eq!(result.data(), &[1, 2, 3, 2, 2, 3, 1, 1, 1]);
|
||||
assert_eq!(result[(0, 0)], 1);
|
||||
assert_eq!(result[(1, 1)], 2);
|
||||
assert_eq!(result[(2, 2)], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row count mismatch")]
|
||||
fn test_elementwise_op_row_mismatch() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = create_test_matrix_2x4(); // 2x4
|
||||
let _ = &matrix1 + &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "row count mismatch")]
|
||||
fn test_elementwise_op_col_mismatch() {
|
||||
let matrix1 = create_test_matrix(); // 3x3
|
||||
let matrix2 = create_test_matrix_2x4(); // 2x4
|
||||
let _ = &matrix1 * &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_and() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T & T = T
|
||||
// F & T = F
|
||||
// T & F = F
|
||||
// F & F = F
|
||||
// T & T = T
|
||||
// F & T = F
|
||||
// Data: [T, F, F, F, T, F]
|
||||
let expected_data = vec![true, false, false, false, true, false];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 & &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_or() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T | T = T
|
||||
// F | T = T
|
||||
// T | F = T
|
||||
// F | F = F
|
||||
// T | T = T
|
||||
// F | T = T
|
||||
// Data: [T, T, T, F, T, T]
|
||||
let expected_data = vec![true, true, true, false, true, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 | &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_xor() {
|
||||
let data1 = vec![true, false, true, false, true, false]; // 2x3
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 3);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// T ^ T = F
|
||||
// F ^ T = T
|
||||
// T ^ F = T
|
||||
// F ^ F = F
|
||||
// T ^ T = F
|
||||
// F ^ T = T
|
||||
// Data: [F, T, T, F, F, T]
|
||||
let expected_data = vec![false, true, true, false, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = &matrix1 ^ &matrix2;
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_not() {
|
||||
let data = vec![true, false, true, false, true, false]; // 2x3
|
||||
let matrix = BoolMatrix::from_vec(data, 2, 3);
|
||||
|
||||
// Expected column-major results:
|
||||
// !T = F
|
||||
// !F = T
|
||||
// !T = F
|
||||
// !F = T
|
||||
// !T = F
|
||||
// !F = T
|
||||
// Data: [F, T, F, T, F, T]
|
||||
let expected_data = vec![false, true, false, true, false, true];
|
||||
let expected_matrix = BoolMatrix::from_vec(expected_data, 2, 3);
|
||||
|
||||
let result = !matrix; // Not consumes the matrix
|
||||
assert_eq!(result, expected_matrix);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col count mismatch")]
|
||||
fn test_bitwise_op_row_mismatch() {
|
||||
let data1 = vec![true, false, true, false]; // 2x2
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
let _ = &matrix1 & &matrix2; // Should panic
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "col count mismatch")]
|
||||
fn test_bitwise_op_col_mismatch() {
|
||||
let data1 = vec![true, false, true, false]; // 2x2
|
||||
let data2 = vec![true, true, false, false, true, true]; // 2x3
|
||||
let matrix1 = BoolMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = BoolMatrix::from_vec(data2, 2, 3);
|
||||
let _ = &matrix1 | &matrix2; // Should panic
|
||||
}
|
||||
|
||||
// Test with String type (requires Clone, PartialEq)
|
||||
#[test]
|
||||
fn test_string_matrix() {
|
||||
let data = vec![
|
||||
"a".to_string(),
|
||||
"b".to_string(),
|
||||
"c".to_string(),
|
||||
"d".to_string(),
|
||||
];
|
||||
let matrix = StringMatrix::from_vec(data.clone(), 2, 2); // 2x2
|
||||
|
||||
assert_eq!(matrix[(0, 0)], "a".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "b".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "c".to_string());
|
||||
assert_eq!(matrix[(1, 1)], "d".to_string());
|
||||
|
||||
// Test modification
|
||||
let mut matrix = matrix;
|
||||
matrix[(0, 0)] = "hello".to_string();
|
||||
assert_eq!(matrix[(0, 0)], "hello".to_string());
|
||||
|
||||
// Test add_column (requires Clone)
|
||||
let new_col = vec!["e".to_string(), "f".to_string()];
|
||||
matrix.add_column(1, new_col); // Add at index 1
|
||||
// Should be:
|
||||
// hello c d
|
||||
// b e f
|
||||
assert_eq!(matrix.rows(), 2);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], "hello".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "b".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "e".to_string()); // New col
|
||||
assert_eq!(matrix[(1, 1)], "f".to_string()); // New col
|
||||
assert_eq!(matrix[(0, 2)], "c".to_string()); // Shifted
|
||||
assert_eq!(matrix[(1, 2)], "d".to_string()); // Shifted
|
||||
|
||||
// Test add_row (requires Clone)
|
||||
let new_row = vec!["g".to_string(), "h".to_string(), "i".to_string()];
|
||||
matrix.add_row(0, new_row); // Add at index 0
|
||||
// Should be:
|
||||
// g h i
|
||||
// hello e c
|
||||
// b f d
|
||||
assert_eq!(matrix.rows(), 3);
|
||||
assert_eq!(matrix.cols(), 3);
|
||||
assert_eq!(matrix[(0, 0)], "g".to_string());
|
||||
assert_eq!(matrix[(0, 1)], "h".to_string());
|
||||
assert_eq!(matrix[(0, 2)], "i".to_string());
|
||||
assert_eq!(matrix[(1, 0)], "hello".to_string()); // Shifted
|
||||
assert_eq!(matrix[(2, 2)], "d".to_string()); // Shifted
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float_matrix_ops() {
|
||||
let data1 = vec![1.0, 2.0, 3.0, 4.0]; // 2x2
|
||||
let data2 = vec![0.5, 1.5, 2.5, 3.5]; // 2x2
|
||||
let matrix1 = FloatMatrix::from_vec(data1, 2, 2);
|
||||
let matrix2 = FloatMatrix::from_vec(data2, 2, 2);
|
||||
|
||||
let sum = &matrix1 + &matrix2;
|
||||
let diff = &matrix1 - &matrix2;
|
||||
let prod = &matrix1 * &matrix2;
|
||||
let div = &matrix1 / &matrix2;
|
||||
|
||||
// Check sums (col-major): [1.5, 3.5, 5.5, 7.5]
|
||||
assert_eq!(sum.data(), &[1.5, 3.5, 5.5, 7.5]);
|
||||
|
||||
// Check diffs (col-major): [0.5, 0.5, 0.5, 0.5]
|
||||
assert_eq!(diff.data(), &[0.5, 0.5, 0.5, 0.5]);
|
||||
|
||||
// Check prods (col-major): [0.5, 3.0, 7.5, 14.0]
|
||||
assert_eq!(prod.data(), &[0.5, 3.0, 7.5, 14.0]);
|
||||
|
||||
// Check divs (col-major): [2.0, 1.333..., 1.2, 1.14...]
|
||||
// Using element access for more specific checks on floating point results
|
||||
assert_eq!(div.rows(), 2);
|
||||
assert_eq!(div.cols(), 2);
|
||||
assert!((div[(0, 0)] - 1.0 / 0.5).abs() < 1e-9); // 2.0
|
||||
assert!((div[(1, 0)] - 2.0 / 1.5).abs() < 1e-9); // 1.333...
|
||||
assert!((div[(0, 1)] - 3.0 / 2.5).abs() < 1e-9); // 1.2
|
||||
assert!((div[(1, 1)] - 4.0 / 3.5).abs() < 1e-9); // 1.14...
|
||||
}
|
||||
|
||||
// Axis enum doesn't have logic, no tests needed directly, but its presence is verified by compilation.
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rustframe::frame::*;
|
||||
use rustframe::matrix::*;
|
||||
// Or explicitly: use crate::matrix::Matrix;
|
||||
|
||||
// --- Include your other tests here ---
|
||||
|
||||
/// Creates a standard 3x3 matrix used in several tests.
|
||||
/// Column 0: [1, 2, 3]
|
||||
/// Column 1: [4, 5, 6]
|
||||
/// Column 2: [7, 8, 9]
|
||||
fn create_test_matrix_i32() -> Matrix<i32> {
|
||||
Matrix::from_cols(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]])
|
||||
}
|
||||
|
||||
// --- The new test ---
|
||||
#[test]
|
||||
fn test_matrix_swap_columns_directly() {
|
||||
let mut matrix = create_test_matrix_i32();
|
||||
|
||||
// Store the initial state of the columns we intend to swap AND one that shouldn't change
|
||||
let initial_col0_data = matrix.column(0).to_vec(); // Should be [1, 2, 3]
|
||||
let initial_col1_data = matrix.column(1).to_vec(); // Should be [4, 5, 6]
|
||||
let initial_col2_data = matrix.column(2).to_vec(); // Should be [7, 8, 9]
|
||||
|
||||
// Perform the swap directly on the matrix
|
||||
matrix.swap_columns(0, 2); // Swap column 0 and column 2
|
||||
|
||||
// --- Assertions ---
|
||||
|
||||
// 1. Verify the dimensions are unchanged
|
||||
assert_eq!(matrix.rows(), 3, "Matrix rows should remain unchanged");
|
||||
assert_eq!(matrix.cols(), 3, "Matrix cols should remain unchanged");
|
||||
|
||||
// 2. Verify the column that was NOT swapped is unchanged
|
||||
assert_eq!(
|
||||
matrix.column(1),
|
||||
initial_col1_data.as_slice(), // Comparing slice to slice
|
||||
"Column 1 data should be unchanged"
|
||||
);
|
||||
|
||||
// 3. Verify the data swap occurred correctly using the COLUMN ACCESSOR
|
||||
// The data originally at index 0 should now be at index 2
|
||||
assert_eq!(
|
||||
matrix.column(2),
|
||||
initial_col0_data.as_slice(),
|
||||
"Column 2 should now contain the original data from column 0"
|
||||
);
|
||||
// The data originally at index 2 should now be at index 0
|
||||
assert_eq!(
|
||||
matrix.column(0),
|
||||
initial_col2_data.as_slice(),
|
||||
"Column 0 should now contain the original data from column 2"
|
||||
);
|
||||
|
||||
// 4. (Optional but useful) Verify the underlying raw data vector
|
||||
// Original data: [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
// Expected data after swapping col 0 and col 2: [7, 8, 9, 4, 5, 6, 1, 2, 3]
|
||||
assert_eq!(
|
||||
matrix.data(),
|
||||
&[7, 8, 9, 4, 5, 6, 1, 2, 3],
|
||||
"Underlying data vector is incorrect after swap"
|
||||
);
|
||||
|
||||
// 5. Test swapping with self (should be a no-op)
|
||||
let state_before_self_swap = matrix.clone();
|
||||
matrix.swap_columns(1, 1);
|
||||
assert_eq!(
|
||||
matrix, state_before_self_swap,
|
||||
"Swapping a column with itself should not change the matrix"
|
||||
);
|
||||
|
||||
// 6. Test swapping adjacent columns
|
||||
let mut matrix2 = create_test_matrix_i32();
|
||||
let initial_col0_data_m2 = matrix2.column(0).to_vec();
|
||||
let initial_col1_data_m2 = matrix2.column(1).to_vec();
|
||||
matrix2.swap_columns(0, 1);
|
||||
assert_eq!(matrix2.column(0), initial_col1_data_m2.as_slice());
|
||||
assert_eq!(matrix2.column(1), initial_col0_data_m2.as_slice());
|
||||
assert_eq!(matrix2.data(), &[4, 5, 6, 1, 2, 3, 7, 8, 9]);
|
||||
}
|
||||
|
||||
// --- Include your failing Frame test_swap_columns here as well ---
|
||||
#[test]
|
||||
fn test_swap_columns() {
|
||||
let mut frame = create_test_frame_i32();
|
||||
let initial_a_data = frame.column("A").to_vec(); // [1, 2, 3]
|
||||
let initial_c_data = frame.column("C").to_vec(); // [7, 8, 9]
|
||||
|
||||
frame.swap_columns("A", "C");
|
||||
|
||||
// Check names order
|
||||
assert_eq!(frame.column_names, vec!["C", "B", "A"]);
|
||||
|
||||
// Check lookup map
|
||||
assert_eq!(frame.column_index("A"), Some(2));
|
||||
assert_eq!(frame.column_index("B"), Some(1));
|
||||
assert_eq!(frame.column_index("C"), Some(0));
|
||||
|
||||
// Check data using new names (should be swapped)
|
||||
|
||||
// Accessing by name "C" (now at index 0) should retrieve the data
|
||||
// that was swapped INTO index 0, which was the *original C data*.
|
||||
assert_eq!(
|
||||
frame.column("C"),
|
||||
initial_c_data.as_slice(),
|
||||
"Data for name 'C' should be original C data"
|
||||
);
|
||||
|
||||
// Accessing by name "A" (now at index 2) should retrieve the data
|
||||
// that was swapped INTO index 2, which was the *original A data*.
|
||||
assert_eq!(
|
||||
frame.column("A"),
|
||||
initial_a_data.as_slice(),
|
||||
"Data for name 'A' should be original A data"
|
||||
);
|
||||
|
||||
// Column "B" should remain unchanged in data and position.
|
||||
assert_eq!(
|
||||
frame.column("B"),
|
||||
&[4, 5, 6],
|
||||
"Column 'B' should be unchanged"
|
||||
);
|
||||
|
||||
// Test swapping with self
|
||||
let state_before_self_swap = frame.clone();
|
||||
frame.swap_columns("B", "B");
|
||||
assert_eq!(frame, state_before_self_swap);
|
||||
}
|
||||
|
||||
fn create_test_frame_i32() -> Frame<i32> {
|
||||
// Ensure this uses the same logic/data as create_test_matrix_i32
|
||||
let matrix = create_test_matrix_i32();
|
||||
Frame::new(matrix, vec!["A", "B", "C"])
|
||||
}
|
||||
} // end mod tests
|
Loading…
x
Reference in New Issue
Block a user