use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct Rotation {
pub qx: f64,
pub qy: f64,
pub qz: f64,
pub qw: f64
}
impl PartialEq for Rotation {
fn eq(&self, other: &Self) -> bool {
self.qx == other.qx && self.qy == other.qy && self.qz == other.qz && self.qw == other.qw
}
}
impl Rotation {
pub fn new(qx: f64, qy: f64, qz: f64, qw: f64) -> Rotation { Rotation { qx, qy, qz, qw } }
}
#[cfg(test)]
mod tests {
use serde_json::from_str;
use serde_json::to_string;
use super::*;
#[test]
fn test_new() {
let result = Rotation::new(1.5, -2.3, 3.9, 5.5);
assert_eq!(result.qx, 1.5);
assert_eq!(result.qy, -2.3);
assert_eq!(result.qz, 3.9);
assert_eq!(result.qw, 5.5);
}
#[test]
fn test_partialeq_true() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(1.5, -2.3, 3.9, 5.5);
assert_eq!(a.eq(&b), true);
assert_eq!(b.eq(&a), true);
}
#[test]
fn test_partialeq_first_different_false() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(1.4, -2.3, 3.9, 5.5);
assert_eq!(a.eq(&b), false);
assert_eq!(b.eq(&a), false);
}
#[test]
fn test_partialeq_second_different_false() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(1.5, -2.4, 3.9, 5.5);
assert_eq!(a.eq(&b), false);
assert_eq!(b.eq(&a), false);
}
#[test]
fn test_partialeq_third_different_false() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(1.5, -2.3, 3.4, 5.5);
assert_eq!(a.eq(&b), false);
assert_eq!(b.eq(&a), false);
}
#[test]
fn test_partialeq_fourth_different_false() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(1.5, -2.3, 3.4, 5.4);
assert_eq!(a.eq(&b), false);
assert_eq!(b.eq(&a), false);
}
#[test]
fn test_partialeq_all_different_false() {
let a = Rotation::new(1.5, -2.3, 3.9, 5.5);
let b = Rotation::new(5.5, 2.3, 33.9, 50.51);
assert_eq!(a.eq(&b), false);
assert_eq!(b.eq(&a), false);
}
#[test]
fn test_to_json() {
let input = Rotation::new(1.5, -2.3, 3.9, 5.5);
let input_serialized = to_string(&input);
assert_eq!(input_serialized.is_ok(), true);
let input_serialized_string = input_serialized.ok().unwrap();
assert_eq!(input_serialized_string, "{\"qx\":1.5,\"qy\":-2.3,\"qz\":3.9,\"qw\":5.5}");
}
#[test]
fn test_from_json() {
let json = "{\"qx\":1.5,\"qy\":-2.3,\"qz\":3.9,\"qw\":5.5}";
let actual_result = from_str::<Rotation>(json);
assert_eq!(actual_result.is_ok(), true);
let actual = actual_result.ok().unwrap();
let expected = Rotation::new(1.5, -2.3, 3.9, 5.5);
assert_eq!(expected.eq(&actual), true);
}
}