Update test about tripod-id

This commit is contained in:
fluo10 2025-09-25 08:04:15 +09:00
parent 26dda29c8d
commit 65d189c990
7 changed files with 214 additions and 113 deletions

View file

@ -10,8 +10,8 @@ pub struct Double{
} }
impl TripodId for Double{ impl TripodId for Double{
type SizeType = u32; type Integer = u32;
const SIZE: Self::SizeType = (Single::SIZE as u32).pow(2); const CAPACITY: Self::Integer = (Single::CAPACITY as u32).pow(2);
/// ``` /// ```
/// use tripod_id::{TripodId, Double}; /// use tripod_id::{TripodId, Double};
/// use std::str::FromStr; /// use std::str::FromStr;
@ -35,8 +35,8 @@ impl TripodId for Double{
}; };
#[cfg(test)] #[cfg(test)]
fn is_valid(&self) -> bool { fn validate_inner(self) -> bool {
self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(*self) < Self::SIZE) self.inner.0.validate_inner() && self.inner.1.validate_inner() && (u32::from(self) < Self::CAPACITY)
} }
} }
@ -90,15 +90,15 @@ impl TryFrom<u32> for Double {
type Error = Error; type Error = Error;
fn try_from(value: u32) -> Result<Self, Self::Error> { fn try_from(value: u32) -> Result<Self, Self::Error> {
if value < Self::SIZE { if value < Self::CAPACITY {
Ok(Self{ Ok(Self{
inner: ( inner: (
Single::try_from(u16::try_from(value/(Single::SIZE as u32)).unwrap())?, Single::try_from(u16::try_from(value/(Single::CAPACITY as u32)).unwrap())?,
Single::try_from(u16::try_from(value % (Single::SIZE as u32)).unwrap())? Single::try_from(u16::try_from(value % (Single::CAPACITY as u32)).unwrap())?
)}) )})
} else { } else {
Err(Error::OutsideOfRange{ Err(Error::OutsideOfRange{
expected: Self::SIZE as u64, expected: Self::CAPACITY as u64,
found: value as u64 found: value as u64
}) })
} }
@ -107,29 +107,61 @@ impl TryFrom<u32> for Double {
impl From<Double> for u32 { impl From<Double> for u32 {
fn from(value: Double) -> Self { fn from(value: Double) -> Self {
u32::from(u16::from(value.inner.0)) * u32::from(Single::SIZE) + u32::from(u16::from(value.inner.1)) u32::from(u16::from(value.inner.0)) * u32::from(Single::CAPACITY) + u32::from(u16::from(value.inner.1))
} }
} }
impl PartialEq<u32> for Double {
fn eq(&self, other: &u32) -> bool {
&u32::from(*self) == other
}
}
impl PartialEq<String> for Double {
fn eq(&self, other: &String) -> bool {
match Self::from_str(other) {
Ok(x) => *self == x,
Err(_) => false
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use crate::tests::{assert_id_eq_int, assert_id_eq_str};
fn assert_random<R>(rand: &mut R) use super::*;
where
R: Rng
{
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())
}
#[test] #[test]
fn random_x10() { fn nil() {
assert!(Double::NIL.validate_all().unwrap());
assert_eq!(Double::NIL, 0);
assert_eq!(Double::NIL, "000000".to_string());
assert_eq!(Double::NIL, "000-000".to_string());
}
#[test]
fn max() {
assert!(Double::MAX.validate_all().unwrap());
assert_eq!(Double::MAX, Double::CAPACITY-1);
assert_eq!(Double::MAX, "zzzzzz".to_string());
assert_eq!(Double::MAX, "ZZZ-ZZZ".to_string());
}
#[test]
#[should_panic]
fn over_sized() {
Double::try_from(Double::CAPACITY).unwrap();
}
#[test]
fn random() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
for _ in 0..10 { for _ in 0..10 {
assert_random(&mut rng); let id: Double = rng.r#gen();
} assert!(id.validate_all().unwrap());
}
} }
} }

View file

@ -8,6 +8,8 @@ mod rusqlite;
#[cfg(feature="serde")] #[cfg(feature="serde")]
mod serde; mod serde;
use std::{fmt::Display, str::FromStr};
pub use single::*; pub use single::*;
pub use double::*; pub use double::*;
pub use triple::*; pub use triple::*;
@ -18,39 +20,63 @@ pub mod prost;
#[cfg(feature="prost")] #[cfg(feature="prost")]
pub use prost::{ SingleMessage, DoubleMessage, TripleMessage }; pub use prost::{ SingleMessage, DoubleMessage, TripleMessage };
pub trait TripodId { pub trait TripodId: Copy + Display + TryFrom<Self::Integer, Error=Error> + FromStr<Err=Error> + PartialEq {
type SizeType; type Integer: From<Self>;
const NIL: Self; const NIL: Self;
const MAX: Self; const MAX: Self;
const SIZE: Self::SizeType; const CAPACITY: Self::Integer;
#[cfg(test)] #[cfg(test)]
fn is_valid(&self) -> bool; fn validate_inner(self) -> bool;
#[cfg(test)]
fn validate_string_convertion(self) -> Result<bool, Error> {
Ok(self == Self::from_str(&self.to_string())?)
}
#[cfg(test)]
fn validate_integer_conversion(self) -> Result<bool, Error> {
Ok(self == Self::try_from(Self::Integer::from(self))?)
}
#[cfg(test)]
fn validate_all(self) -> Result<bool, Error> {
Ok(self.validate_inner()
&& self.validate_string_convertion()?
&& self.validate_integer_conversion()?
)
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fmt::Display, fmt::Debug, str::FromStr}; use std::{fmt::Display, fmt::Debug, str::FromStr};
#[cfg(feature="prost")]
use crate::prost::TrypodIdMessage;
use super::*; use super::*;
#[cfg(feature="prost")] #[cfg(feature="prost")]
fn assert_prost(id: T) where pub fn assert_valid_message<T, M>(id: &T) where
T: TripodId<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy, T: TripodId + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<M, Error = Error> + Copy,
I: From<T> { M: TrypodIdMessage<TrypodId = T> + From<T>
use crate::SingleMessage; {
let message = SingleMessage::from(*id); let message = M::from(*id);
assert_eq!(message.is_valid()); assert!(message.is_valid());
let result = Single::try_from(message).unwrap(); assert_eq!(*id, T::try_from(message).unwrap());
assert_eq!(id, result);
} }
fn assert_id<T, I> (id: T) where pub fn assert_id_eq_int<T, I> (id: T, int: I ) where
T: TripodId<SizeType = I> + Debug + Display + FromStr<Err=Error> + PartialEq + TryFrom<I, Error = Error> + Copy, T: TripodId<Integer = I> + Debug + PartialEq + TryFrom<I, Error = Error> + Copy,
I: From<T> I: From<T> + PartialEq + Debug + Copy
{ {
assert!(id.is_valid()); assert_eq!(id, T::try_from(int).unwrap());
let s = id.to_string(); assert_eq!(I::from(id), int);
assert_eq!(id,T::from_str(&s).unwrap());
let i = T::SizeType::from(id);
assert_eq!(id, T::try_from(i).unwrap());
} }
pub fn assert_id_eq_str<T> (id: T, code: &str ) where
T: TripodId + Debug + Display + FromStr<Err=Error> + PartialEq + Copy,
{
assert_eq!(id, T::from_str(code).unwrap());
}
} }

View file

@ -12,7 +12,7 @@ impl Double {
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
use crate::TripodId; use crate::TripodId;
self.id < u32::from(crate::Double::SIZE) self.id < u32::from(crate::Double::CAPACITY)
} }
} }

View file

@ -12,7 +12,7 @@ impl Single {
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
use crate::TripodId; use crate::TripodId;
self.id < u32::from(crate::Single::SIZE) self.id < u32::from(crate::Single::CAPACITY)
} }
} }
@ -29,7 +29,7 @@ impl TryFrom<Single> for crate::Single {
fn try_from(value: Single) -> Result<Self, Self::Error> { fn try_from(value: Single) -> Result<Self, Self::Error> {
Self::try_from( Self::try_from(
u16::try_from(value.id).or(Err(Error::OutsideOfRange { u16::try_from(value.id).or(Err(Error::OutsideOfRange {
expected: u64::from(crate::Single::SIZE), expected: u64::from(crate::Single::CAPACITY),
found: u64::from(value.id) found: u64::from(value.id)
}))? }))?
) )

View file

@ -12,7 +12,7 @@ impl Triple {
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
use crate::TripodId; use crate::TripodId;
self.id < crate::Triple::SIZE self.id < crate::Triple::CAPACITY
} }
} }

View file

@ -108,16 +108,10 @@ pub struct Single{
impl TripodId for Single { impl TripodId for Single {
type SizeType = u16; type Integer = u16;
const SIZE: Self::SizeType = CUBED_BASE; const CAPACITY: Self::Integer = CUBED_BASE;
/// ```
/// use tripod_id::{Single, TripodId};
/// use std::str::FromStr;
///
/// assert_eq!(Single::NIL, Single::from_str("000").unwrap());
/// assert_eq!(Single::NIL, Single::try_from(0).unwrap());
/// ```
const NIL: Single = Single{ const NIL: Single = Single{
inner: 0 inner: 0
}; };
@ -130,12 +124,12 @@ impl TripodId for Single {
/// assert_eq!(Single::MAX, Single::try_from(35936).unwrap()); /// assert_eq!(Single::MAX, Single::try_from(35936).unwrap());
/// ``` /// ```
const MAX: Single = Single{ const MAX: Single = Single{
inner: Self::SIZE-1 inner: Self::CAPACITY-1
}; };
#[cfg(test)] #[cfg(test)]
fn is_valid(&self) -> bool { fn validate_inner(self) -> bool {
self.inner < Self::SIZE self.inner < Self::CAPACITY
} }
} }
@ -158,7 +152,7 @@ impl FromStr for Single {
impl Distribution<Single> for Standard { impl Distribution<Single> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Single { fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Single {
Single { Single {
inner: rng.gen_range(0..Single::SIZE) inner: rng.gen_range(0..Single::CAPACITY)
} }
} }
} }
@ -167,11 +161,11 @@ impl TryFrom<u16> for Single {
type Error = Error; type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> { fn try_from(value: u16) -> Result<Self, Self::Error> {
if value < Self::SIZE { if value < Self::CAPACITY {
Ok(Self{inner: value}) Ok(Self{inner: value})
} else { } else {
Err(Error::OutsideOfRange{ Err(Error::OutsideOfRange{
expected: Self::SIZE as u64, expected: Self::CAPACITY as u64,
found: value as u64 found: value as u64
}) })
} }
@ -184,42 +178,57 @@ impl From<Single> for u16 {
} }
} }
impl PartialEq<u16> for Single {
fn eq(&self, other: &u16) -> bool {
&u16::from(*self) == other
}
}
impl PartialEq<String> for Single {
fn eq(&self, other: &String) -> bool {
match Self::from_str(other) {
Ok(x) => *self == x,
Err(_) => false
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::{assert_id_eq_int, assert_id_eq_str};
use super::*; 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();
}
#[test] #[test]
fn random_x10() { fn nil() {
assert!(Single::NIL.validate_all().unwrap());
assert_eq!(Single::NIL, 0);
assert_eq!(Single::NIL, "000".to_string());
}
#[test]
fn max() {
assert!(Single::MAX.validate_all().unwrap());
assert_eq!(Single::MAX, Single::CAPACITY-1);
assert_eq!(Single::MAX, "zzz".to_string());
assert_eq!(Single::MAX, "ZZZ".to_string());
}
#[test]
#[should_panic]
fn over_sized() {
Single::try_from(Single::CAPACITY).unwrap();
}
#[test]
fn random() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
for _ in 0..10 { for _ in 0..10 {
assert_random(&mut rng); let single: Single = rng.r#gen();
assert!(single.validate_all().unwrap());
}
} }
}
} }

View file

@ -12,8 +12,8 @@ pub struct Triple {
} }
impl TripodId for Triple{ impl TripodId for Triple{
type SizeType = u64; type Integer = u64;
const SIZE: Self::SizeType = (Single::SIZE as u64).pow(3); const CAPACITY: Self::Integer = (Single::CAPACITY as u64).pow(3);
/// ``` /// ```
/// use tripod_id::{TripodId, Triple}; /// use tripod_id::{TripodId, Triple};
/// use std::str::FromStr; /// use std::str::FromStr;
@ -37,8 +37,11 @@ impl TripodId for Triple{
}; };
#[cfg(test)] #[cfg(test)]
fn is_valid(&self) -> bool { fn validate_inner(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.validate_inner()
&& self.inner.1.validate_inner()
&& self.inner.2.validate_inner()
&& (u64::from(self) < Self::CAPACITY)
} }
} }
@ -97,16 +100,16 @@ impl TryFrom<u64> for Triple {
type Error = Error; type Error = Error;
fn try_from(value: u64) -> Result<Self, Self::Error> { fn try_from(value: u64) -> Result<Self, Self::Error> {
if value < Self::SIZE { if value < Self::CAPACITY {
Ok(Self{ Ok(Self{
inner: ( inner: (
Single::try_from(u16::try_from(value / (Double::SIZE as u64)).unwrap())?, Single::try_from(u16::try_from(value / (Double::CAPACITY as u64)).unwrap())?,
Single::try_from(u16::try_from((value % (Double::SIZE as u64)) /(Single::SIZE as u64)).unwrap())?, Single::try_from(u16::try_from((value % (Double::CAPACITY as u64)) /(Single::CAPACITY as u64)).unwrap())?,
Single::try_from(u16::try_from(value % (Single::SIZE as u64)).unwrap())? Single::try_from(u16::try_from(value % (Single::CAPACITY as u64)).unwrap())?
)}) )})
} else { } else {
Err(Error::OutsideOfRange{ Err(Error::OutsideOfRange{
expected: Self::SIZE as u64, expected: Self::CAPACITY as u64,
found: value as u64 found: value as u64
}) })
} }
@ -115,31 +118,62 @@ impl TryFrom<u64> for Triple {
impl From<Triple> for u64 { impl From<Triple> for u64 {
fn from(value: Triple) -> Self { fn from(value: Triple) -> Self {
(u16::from(value.inner.0) as u64) * (Double::SIZE as u64) (u16::from(value.inner.0) as u64) * (Double::CAPACITY as u64)
+ (u16::from(value.inner.1) as u64) * (Single::SIZE as u64) + (u16::from(value.inner.1) as u64) * (Single::CAPACITY as u64)
+ (u16::from(value.inner.2) as u64) + (u16::from(value.inner.2) as u64)
} }
} }
impl PartialEq<u64> for Triple {
fn eq(&self, other: &u64) -> bool {
&u64::from(*self) == other
}
}
impl PartialEq<String> for Triple {
fn eq(&self, other: &String) -> bool {
match Self::from_str(other) {
Ok(x) => *self == x,
Err(_) => false
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use crate::tests::{assert_id_eq_int, assert_id_eq_str};
fn assert_random<R>(rand: &mut R) use super::*;
where
R: Rng
{
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());
}
#[test] #[test]
fn random_x10() { fn nil() {
assert!(Triple::NIL.validate_all().unwrap());
assert_eq!(Triple::NIL, 0);
assert_eq!(Triple::NIL, "000000000".to_string());
assert_eq!(Triple::NIL, "000-000-000".to_string());
}
#[test]
fn max() {
assert!(Triple::MAX.validate_all().unwrap());
assert_eq!(Triple::MAX, Triple::CAPACITY-1);
assert_eq!(Triple::MAX, "zzzzzzzzz".to_string());
assert_eq!(Triple::MAX, "ZZZ-ZZZ-ZZZ".to_string());
}
#[test]
#[should_panic]
fn over_sized() {
Triple::try_from(Triple::CAPACITY).unwrap();
}
#[test]
fn random() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
for _ in 0..10 { for _ in 0..10 {
assert_random(&mut rng); let id: Triple = rng.r#gen();
} assert!(id.validate_all().unwrap());
}
} }
} }