60 lines
1.5 KiB
Rust
Raw Normal View History

2024-09-26 15:06:01 -07:00
use crate::types::{Vector2,Vector3,Matrix3x4,Matrix4x2,Matrix3x2,Matrix2x3};
2024-08-28 13:36:17 -07:00
2024-09-05 16:45:44 -07:00
#[test]
fn test_bool(){
assert_eq!(Vector3::new([false,false,false]).any(),false);
assert_eq!(Vector3::new([false,false,true]).any(),true);
assert_eq!(Vector3::new([false,false,true]).all(),false);
assert_eq!(Vector3::new([true,true,true]).all(),true);
}
#[test]
fn test_length_squared(){
assert_eq!(Vector3::new([1,2,3]).length_squared(),14);
}
#[test]
fn test_arithmetic(){
let a=Vector3::new([1,2,3]);
assert_eq!((a+a*2).array,Vector3::new([1*3,2*3,3*3]).array);
}
2024-09-09 17:32:39 -07:00
#[test]
fn matrix_transform_vector(){
2024-09-26 15:06:01 -07:00
let m=Matrix2x3::new([
2024-09-09 17:32:39 -07:00
[1,2,3],
[4,5,6],
2024-09-26 15:06:01 -07:00
]).transpose();
2024-09-09 17:32:39 -07:00
let v=Vector3::new([1,2,3]);
let transformed=m*v;
assert_eq!(transformed.array,Vector2::new([14,32]).array);
}
#[test]
fn matrix_dot(){
2024-09-26 15:06:01 -07:00
// All this code was written row major and I converted the lib to colum major
let rhs=Matrix4x2::new([
2024-09-06 10:36:34 -07:00
[ 1.0, 2.0],
[ 3.0, 4.0],
[ 5.0, 6.0],
[ 7.0, 8.0],
2024-09-26 15:06:01 -07:00
]).transpose(); // | | |
let lhs=Matrix3x4::new([ // | | |
2024-09-06 10:36:34 -07:00
[1.0, 2.0, 3.0, 4.0],// [ 50.0, 60.0],
[5.0, 6.0, 7.0, 8.0],// [114.0,140.0],
[9.0,10.0,11.0,12.0],// [178.0,220.0],
2024-09-26 15:06:01 -07:00
]).transpose();
// Mat3<Vec4>.dot(Mat4<Vec2>) -> Mat3<Vec2>
2024-09-09 17:01:52 -07:00
let m_dot=lhs*rhs;
//In[1]:= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} . {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
//Out[1]= {{50, 60}, {114, 140}, {178, 220}}
assert_eq!(
m_dot.array,
2024-09-26 15:06:01 -07:00
Matrix3x2::new([
[50.0,60.0],
[114.0,140.0],
[178.0,220.0],
2024-09-26 15:06:01 -07:00
]).transpose().array
);
}