Addig tests about tripod-id prost feature
This commit is contained in:
parent
4793a96587
commit
26dda29c8d
11 changed files with 202 additions and 42 deletions
|
|
@ -4,7 +4,7 @@ use rand::{distributions::Standard, prelude::Distribution, Rng};
|
|||
|
||||
use crate::{utils::is_delimiter, Error, TripodId, Single};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Double{
|
||||
inner: (Single, Single)
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ impl TripodId for Double{
|
|||
|
||||
#[cfg(test)]
|
||||
fn is_valid(&self) -> bool {
|
||||
self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(self) < Self::SIZE)
|
||||
self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(*self) < Self::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -98,16 +98,16 @@ impl TryFrom<u32> for Double {
|
|||
)})
|
||||
} else {
|
||||
Err(Error::OutsideOfRange{
|
||||
expected: Self::SIZE as usize,
|
||||
found: value as usize
|
||||
expected: Self::SIZE as u64,
|
||||
found: value as u64
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Double> for u32 {
|
||||
fn from(value: &Double) -> Self {
|
||||
u32::from(u16::from(&value.inner.0)) * u32::from(Single::SIZE) + u32::from(u16::from(&value.inner.1))
|
||||
impl From<Double> for u32 {
|
||||
fn from(value: Double) -> Self {
|
||||
u32::from(u16::from(value.inner.0)) * u32::from(Single::SIZE) + u32::from(u16::from(value.inner.1))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ mod tests {
|
|||
let id: Double = rand.r#gen();
|
||||
assert!(id.is_valid());
|
||||
assert_eq!(id,Double::from_str(&id.to_string()).unwrap());
|
||||
assert_eq!(id, Double::try_from(u32::from(&id)).unwrap())
|
||||
assert_eq!(id, Double::try_from(u32::from(id)).unwrap())
|
||||
}
|
||||
#[test]
|
||||
fn random_x10() {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
pub enum Error {
|
||||
#[error("expected under {expected}, found {found}")]
|
||||
OutsideOfRange{
|
||||
expected: usize,
|
||||
found: usize,
|
||||
expected: u64,
|
||||
found: u64,
|
||||
},
|
||||
#[error("Invalid chunk: {0}")]
|
||||
InvalidChunk(String),
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ pub use error::*;
|
|||
|
||||
#[cfg(feature="prost")]
|
||||
pub mod prost;
|
||||
#[cfg(feature="prost")]
|
||||
pub use prost::{ SingleMessage, DoubleMessage, TripleMessage };
|
||||
|
||||
pub trait TripodId {
|
||||
type SizeType;
|
||||
|
|
@ -24,4 +26,31 @@ pub trait TripodId {
|
|||
#[cfg(test)]
|
||||
fn is_valid(&self) -> bool;
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{fmt::Display, fmt::Debug, str::FromStr};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[cfg(feature="prost")]
|
||||
fn assert_prost(id: T) where
|
||||
T: TripodId<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy,
|
||||
I: From<T> {
|
||||
use crate::SingleMessage;
|
||||
let message = SingleMessage::from(*id);
|
||||
assert_eq!(message.is_valid());
|
||||
let result = Single::try_from(message).unwrap();
|
||||
assert_eq!(id, result);
|
||||
}
|
||||
|
||||
fn assert_id<T, I> (id: T) where
|
||||
T: TripodId<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy,
|
||||
I: From<T>
|
||||
{
|
||||
assert!(id.is_valid());
|
||||
let s = id.to_string();
|
||||
assert_eq!(id,T::from_str(&s).unwrap());
|
||||
let i = T::SizeType::from(id);
|
||||
assert_eq!(id, T::try_from(i).unwrap());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
include!(concat!(env!("OUT_DIR"), "/fireturtle.tripod_id.rs"));
|
||||
}
|
||||
pub use crate::fireturtle::tripod_id::*;
|
||||
type SingleMessage = Single;
|
||||
type DoubleMessage = Double;
|
||||
type TripleMessage = Triple;
|
||||
34
tripod-id/src/prost/double.rs
Normal file
34
tripod-id/src/prost/double.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use prost::Name;
|
||||
|
||||
use crate::{prost::Double, Error, TripodId};
|
||||
|
||||
impl Name for Double {
|
||||
const NAME: &'static str = "Double";
|
||||
const PACKAGE: &'static str = super::PACKAGE_NAME;
|
||||
}
|
||||
|
||||
impl Double {
|
||||
#[cfg(test)]
|
||||
pub fn is_valid(&self) -> bool {
|
||||
use crate::TripodId;
|
||||
|
||||
self.id < u32::from(crate::Double::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Double> for Double {
|
||||
fn from(value: crate::Double) -> Self {
|
||||
Self {
|
||||
id: u32::from(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
impl TryFrom<Double> for crate::Double {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Double) -> Result<Self, Self::Error> {
|
||||
Self::try_from(
|
||||
value.id
|
||||
)
|
||||
}
|
||||
}
|
||||
20
tripod-id/src/prost/mod.rs
Normal file
20
tripod-id/src/prost/mod.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
mod generated {
|
||||
include!(concat!(env!("OUT_DIR"), "/fireturtle.tripod_id.rs"));
|
||||
}
|
||||
|
||||
mod single;
|
||||
mod double;
|
||||
mod triple;
|
||||
|
||||
pub use generated::*;
|
||||
const PACKAGE_NAME: &'static str = "fireturtle.tripod_id";
|
||||
pub type SingleMessage = Single;
|
||||
pub type DoubleMessage = Double;
|
||||
pub type TripleMessage = Triple;
|
||||
|
||||
pub trait TrypodIdMessage: From<Self::TrypodId> {
|
||||
type TrypodId: crate::TripodId + TryFrom<Self>;
|
||||
|
||||
#[cfg(test)]
|
||||
fn is_valid(&self) -> bool;
|
||||
}
|
||||
37
tripod-id/src/prost/single.rs
Normal file
37
tripod-id/src/prost/single.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use prost::Name;
|
||||
|
||||
use crate::{prost::Single, Error, TripodId};
|
||||
|
||||
impl Name for Single {
|
||||
const NAME: &'static str = "Single";
|
||||
const PACKAGE: &'static str = super::PACKAGE_NAME;
|
||||
}
|
||||
|
||||
impl Single {
|
||||
#[cfg(test)]
|
||||
pub fn is_valid(&self) -> bool {
|
||||
use crate::TripodId;
|
||||
|
||||
self.id < u32::from(crate::Single::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Single> for Single {
|
||||
fn from(value: crate::Single) -> Self {
|
||||
Self {
|
||||
id: u32::from(u16::from(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl TryFrom<Single> for crate::Single {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Single) -> Result<Self, Self::Error> {
|
||||
Self::try_from(
|
||||
u16::try_from(value.id).or(Err(Error::OutsideOfRange {
|
||||
expected: u64::from(crate::Single::SIZE),
|
||||
found: u64::from(value.id)
|
||||
}))?
|
||||
)
|
||||
}
|
||||
}
|
||||
34
tripod-id/src/prost/triple.rs
Normal file
34
tripod-id/src/prost/triple.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use prost::Name;
|
||||
|
||||
use crate::{prost::Triple, Error, TripodId};
|
||||
|
||||
impl Name for Triple {
|
||||
const NAME: &'static str = "Triple";
|
||||
const PACKAGE: &'static str = super::PACKAGE_NAME;
|
||||
}
|
||||
|
||||
impl Triple {
|
||||
#[cfg(test)]
|
||||
pub fn is_valid(&self) -> bool {
|
||||
use crate::TripodId;
|
||||
|
||||
self.id < crate::Triple::SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Triple> for Triple {
|
||||
fn from(value: crate::Triple) -> Self {
|
||||
Self {
|
||||
id: u64::from(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
impl TryFrom<Triple> for crate::Triple {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Triple) -> Result<Self, Self::Error> {
|
||||
Self::try_from(
|
||||
value.id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ impl FromSql for Single {
|
|||
|
||||
impl ToSql for Single {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||
Ok(u16::from(self).into())
|
||||
Ok(u16::from(*self).into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ impl FromSql for Double {
|
|||
|
||||
impl ToSql for Double {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||
Ok(u32::from(self).into())
|
||||
Ok(u32::from(*self).into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ impl FromSql for Triple {
|
|||
impl ToSql for Triple {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||
Ok(rusqlite::types::ToSqlOutput::Owned(rusqlite::types::Value::Integer(
|
||||
i64::try_from(u64::from(self)).map_err(
|
||||
i64::try_from(u64::from(*self)).map_err(
|
||||
|err| Error::ToSqlConversionFailure(err.into())
|
||||
)?
|
||||
)))
|
||||
|
|
|
|||
|
|
@ -91,8 +91,8 @@ fn str_to_u16(s: &str) -> Result<u16, Error> {
|
|||
fn u16_to_string(int: u16) -> Result<String, Error> {
|
||||
if int >= CUBED_BASE {
|
||||
return Err(Error::OutsideOfRange{
|
||||
expected: CUBED_BASE as usize,
|
||||
found: int as usize
|
||||
expected: CUBED_BASE as u64,
|
||||
found: int as u64
|
||||
})
|
||||
}
|
||||
let first_char = char::from(CHARACTERS[usize::try_from(int / SQUARED_BASE).unwrap()]);
|
||||
|
|
@ -101,7 +101,7 @@ fn u16_to_string(int: u16) -> Result<String, Error> {
|
|||
Ok(format!("{}{}{}", first_char, second_char, third_char))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Single{
|
||||
inner: u16
|
||||
}
|
||||
|
|
@ -171,15 +171,15 @@ impl TryFrom<u16> for Single {
|
|||
Ok(Self{inner: value})
|
||||
} else {
|
||||
Err(Error::OutsideOfRange{
|
||||
expected: Self::SIZE as usize,
|
||||
found: value as usize
|
||||
expected: Self::SIZE as u64,
|
||||
found: value as u64
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Single> for u16 {
|
||||
fn from(value: &Single) -> Self {
|
||||
impl From<Single> for u16 {
|
||||
fn from(value: Single) -> Self {
|
||||
value.inner
|
||||
}
|
||||
}
|
||||
|
|
@ -190,16 +190,29 @@ impl From<&Single> for u16 {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(feature="prost")]
|
||||
fn assert_prost(id: Single) {
|
||||
use crate::SingleMessage;
|
||||
let message = SingleMessage::from(*id);
|
||||
assert_eq!(message.is_valid());
|
||||
let result = Single::try_from(message).unwrap();
|
||||
assert_eq!(id, result);
|
||||
}
|
||||
|
||||
fn assert_id(id: Single) {
|
||||
assert!(id.is_valid());
|
||||
let s = id.to_string();
|
||||
assert_eq!(id,Single::from_str(&s).unwrap());
|
||||
let i = u16::from(id);
|
||||
assert_eq!(id, Single::try_from(i).unwrap());
|
||||
}
|
||||
|
||||
fn assert_random<R>(rand: &mut R)
|
||||
where
|
||||
R: Rng
|
||||
{
|
||||
let chunk: Single = rand.r#gen();
|
||||
assert!(chunk.is_valid());
|
||||
let s = chunk.to_string();
|
||||
assert_eq!(chunk,Single::from_str(&s).unwrap());
|
||||
let i = u16::from(&chunk);
|
||||
assert_eq!(chunk, Single::try_from(i).unwrap());
|
||||
|
||||
}
|
||||
#[test]
|
||||
fn random_x10() {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use rand::{distributions::Standard, prelude::Distribution, Rng};
|
|||
|
||||
use crate::TripodId;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Triple {
|
||||
inner: (Single, Single, Single)
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ impl TripodId for Triple{
|
|||
|
||||
#[cfg(test)]
|
||||
fn is_valid(&self) -> bool {
|
||||
self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(self) < Self::SIZE)
|
||||
self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(*self) < Self::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,18 +106,18 @@ impl TryFrom<u64> for Triple {
|
|||
)})
|
||||
} else {
|
||||
Err(Error::OutsideOfRange{
|
||||
expected: Self::SIZE as usize,
|
||||
found: value as usize
|
||||
expected: Self::SIZE as u64,
|
||||
found: value as u64
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Triple> for u64 {
|
||||
fn from(value: &Triple) -> Self {
|
||||
(u16::from(&value.inner.0) as u64) * (Double::SIZE as u64)
|
||||
+ (u16::from(&value.inner.1) as u64) * (Single::SIZE as u64)
|
||||
+ (u16::from(&value.inner.2) as u64)
|
||||
impl From<Triple> for u64 {
|
||||
fn from(value: Triple) -> Self {
|
||||
(u16::from(value.inner.0) as u64) * (Double::SIZE as u64)
|
||||
+ (u16::from(value.inner.1) as u64) * (Single::SIZE as u64)
|
||||
+ (u16::from(value.inner.2) as u64)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ mod tests {
|
|||
let id: Triple = rand.r#gen();
|
||||
assert!(id.is_valid());
|
||||
assert_eq!(id, Triple::from_str(&id.to_string()).unwrap());
|
||||
assert_eq!(id, Triple::try_from(u64::from(&id)).unwrap());
|
||||
assert_eq!(id, Triple::try_from(u64::from(id)).unwrap());
|
||||
}
|
||||
#[test]
|
||||
fn random_x10() {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue