Skip to content

Commit

Permalink
fixed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Jan 1, 2024
1 parent 195d734 commit 9989e0f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 30 deletions.
18 changes: 0 additions & 18 deletions src/structure/prims.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
// pub trait FrcStructure
// where
// Self: Sized,
// {
// const SCHEMA: &'static str;
// const TYPE: &'static str;
// const SIZE: usize;
// const DESCRIPTION: FrcStructDesc = FrcStructDesc {
// schema: Self::SCHEMA,
// type_str: Self::TYPE,
// size: Self::SIZE,
// };

// fn pack(&self, buffer: &mut impl BufMut);

// fn unpack(buffer: &mut impl Buf) -> Self;
// }

macro_rules! empty_schema_supplier {
() => {
|| String::with_capacity(0)
Expand Down
7 changes: 4 additions & 3 deletions src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod test {
let start = uptime().as_micros();
thread::sleep(Duration::from_millis(100));
let end = uptime().as_micros();
assert!(end - start >= 100_000);
assert!(end.saturating_sub(start) >= 100_000);
}

fn test_pause() {
Expand All @@ -151,11 +151,12 @@ mod test {
let start = uptime().as_micros();
thread::sleep(Duration::from_millis(1000));
let end = uptime().as_micros();
assert!(end + 5 - start < 100);
// assert!(end + 5 - start < 100);
assert!(end.saturating_sub(start) < 100);
try_pause(false).expect("Pause Error");
thread::sleep(Duration::from_millis(1000));
let end = uptime().as_micros();
assert!(end - start >= 1_000_000);
assert!(end.saturating_sub(start) >= 1_000_000);
}

/// Tests all of the time functions in one thread to make it sequential
Expand Down
26 changes: 21 additions & 5 deletions src/units/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use paste;
/// This macro is used to define a new unit of measurement.
///
/// # Example
/// ```no_run
/// ```
/// use frclib_core::unit;
///
/// unit!(DegreeFloat: float);
Expand Down Expand Up @@ -122,13 +122,17 @@ macro_rules! unit {
/// This macro is used to define a conversion between two units of the same dimension.
///
/// # Example
/// ```no_run
/// ```
/// use frclib_core::{unit_conversion, unit};
///
/// unit!(Degree: float);
/// unit!(Radian: float);
///
/// unit_conversion!(Degree(float) <-> Radian(float) ~ degree_to_radian);
///
/// fn degree_to_radian(degree: f64) -> f64 {
/// degree.to_radians()
/// }
/// ````
#[macro_export]
macro_rules! unit_conversion {
Expand Down Expand Up @@ -167,8 +171,8 @@ macro_rules! unit_conversion {
/// and allows for functions to be generic over all units of a family.
///
/// # Example
/// ```no_run
/// use frclib_core::{unit_family, unit};
/// ```
/// use frclib_core::{unit_family, unit, unit_conversion};
///
/// unit!(Degree: float);
/// unit!(Radian: float);
Expand All @@ -179,6 +183,18 @@ macro_rules! unit_conversion {
/// unit_conversion!(Radian(float) <-> Rotation(float) ~ radian_to_rotation);
///
/// unit_family!(Angle(Radian): Degree, Rotation);
///
/// fn degree_to_radian(degree: f64) -> f64 {
/// degree.to_radians()
/// }
///
/// fn degree_to_rotation(degree: f64) -> f64 {
/// degree / 360.0
/// }
///
/// fn radian_to_rotation(radian: f64) -> f64 {
/// degree_to_rotation(radian.to_degrees())
/// }
/// ````
#[macro_export]
macro_rules! unit_family {
Expand Down Expand Up @@ -222,7 +238,7 @@ macro_rules! unit_family {
/// A macro for defining a unit dimension analysis.
///
/// # Example
/// ```no_run
/// ```
/// use frclib_core::{unit_dim_analysis, unit};
///
/// unit!(Degree: float);
Expand Down
2 changes: 1 addition & 1 deletion src/units/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ pub mod torque;
#[doc(hidden)]
mod test;
#[doc(hidden)]
mod macros;
pub mod macros;
48 changes: 46 additions & 2 deletions src/value/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use crate::value::FrcValue;
use crate::value::{FrcValue, IntoFrcValue};

#[test]
fn test_value_serde() {
Expand All @@ -20,8 +20,52 @@ fn test_value_serde() {
assert_eq!(test_inst, test_de);
}

// warns if enum size changes
const _: fn() = || {
let _ = core::mem::transmute::<FrcValue, [u8; 24]>;
};

#[allow(trivial_casts, clippy::missing_assert_message)]
#[test]
fn test_into_frc_value() {
fn assert_frc_value(v: impl IntoFrcValue, cmp: FrcValue) {{
match (v.into_frc_value(), cmp) {
(FrcValue::Boolean(a), FrcValue::Boolean(b)) => assert_eq!(a, b),
(FrcValue::Float(a), FrcValue::Float(b)) => assert!((a-b).abs() < f32::EPSILON),
(FrcValue::Double(a), FrcValue::Double(b)) => assert!((a-b).abs() < f64::EPSILON),
(FrcValue::Int(a), FrcValue::Int(b)) => assert_eq!(a, b),
(FrcValue::Void, FrcValue::Void) => (),
(FrcValue::String(a), FrcValue::String(b)) => assert_eq!(a, b),
(FrcValue::BooleanArray(a), FrcValue::BooleanArray(b)) => assert_eq!(a, b),
(FrcValue::FloatArray(a), FrcValue::FloatArray(b)) => assert_eq!(a, b),
(FrcValue::DoubleArray(a), FrcValue::DoubleArray(b)) => assert_eq!(a, b),
(FrcValue::StringArray(a), FrcValue::StringArray(b)) => assert_eq!(a, b),
(any_v, any_cmp) => panic!("assert_frc_value failed: {any_v:?} != {any_cmp:?}")
}
}}
assert_frc_value(1.0f64, FrcValue::Double(1.0));
assert_frc_value(1.0f32, FrcValue::Float(1.0));
assert_frc_value(1i64, FrcValue::Int(1));
assert_frc_value(1i32, FrcValue::Int(1));
assert_frc_value(1i16, FrcValue::Int(1));
assert_frc_value(1i8, FrcValue::Int(1));
assert_frc_value(1u32, FrcValue::Int(1));
assert_frc_value(1u16, FrcValue::Int(1));
assert_frc_value(1u8, FrcValue::Int(1));
assert_frc_value("1", FrcValue::String(Box::from("1")));
assert_frc_value("1".to_string(), FrcValue::String(Box::from("1")));
assert_frc_value(Box::from("1") as Box<str>, FrcValue::String(Box::from("1")));
assert_frc_value(true, FrcValue::Boolean(true));
assert_frc_value([true, false], FrcValue::BooleanArray(Box::from([true, false])));
assert_frc_value(
Box::from(["1", "2"]) as Box<[&str]>,
FrcValue::StringArray(
Box::from([Box::from("1"), Box::from("2")])
));
assert_frc_value(
vec!["1", "2"],
FrcValue::StringArray(
Box::from([Box::from("1"), Box::from("2")])
));
assert_frc_value(None::<i64>, FrcValue::Void);
assert_frc_value(Some(1), FrcValue::Int(1));
}
17 changes: 17 additions & 0 deletions src/value/trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ impl From<FrcTimestampedValue> for FrcValue {
v.value
}
}
impl<T: Into<Self>> From<Option<T>> for FrcValue {
fn from(v: Option<T>) -> Self {
match v {
Some(v) => v.into(),
None => Self::Void,
}
}
}
impl<const N: usize, T> From<[T; N]> for FrcValue
where
Box<[T]>: Into<Self> //i love rust
{
fn from(v: [T; N]) -> Self {
let boxed: Box<[T]> = Box::from(v);
boxed.into()
}
}

impl TryFrom<FrcValue> for f64 {
type Error = FrcValueCastError;
Expand Down
2 changes: 1 addition & 1 deletion src/value/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ impl<T: Into<FrcValue> + Send + Sync> IntoFrcValue for T {
fn into_frc_value(self) -> FrcValue {
self.into()
}
}
}

0 comments on commit 9989e0f

Please sign in to comment.