This commit is contained in:
nora 2024-04-04 21:04:38 +02:00
parent a028b8117f
commit 1721d6a45a
3 changed files with 69 additions and 1 deletions

67
src/assert.rs Normal file
View file

@ -0,0 +1,67 @@
use std::fmt::Debug;
pub fn assert<T>(v: T) -> Assert<T> {
Assert { v }
}
pub struct Assert<T> {
v: T,
}
impl Assert<bool> {
#[track_caller]
pub fn is_true(self) {
assert!(self.v);
}
#[track_caller]
pub fn is_false(self) {
assert!(!self.v);
}
}
impl<T: Debug> Assert<T> {
#[track_caller]
pub fn equals<U: Debug>(self, other: U)
where
T: PartialEq<U>,
{
assert_eq!(self.v, other);
}
#[track_caller]
pub fn not_equals<U: Debug>(self, other: U)
where
T: PartialEq<U>,
{
assert_ne!(self.v, other);
}
}
impl<T: AsRef<str>> Assert<T> {
#[track_caller]
pub fn contains(self, other: &str) {
assert!(self.v.as_ref().contains(other), "pattern '{other}' not found in string: {}", self.v.as_ref());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn assert_bool() {
assert(true).is_true();
assert(false).is_false();
}
#[test]
fn assert_equal() {
assert(1).equals(1);
assert(2).not_equals(1);
}
#[test]
fn assert_str() {
assert("uwu owo").contains("uwu");
assert("uwu owo".to_owned()).contains("uwu");
}
}

View file

@ -71,7 +71,7 @@ mod tests {
for count in [1, 10, 100, 1000, 10_000, 100_000] { for count in [1, 10, 100, 1000, 10_000, 100_000] {
test_many::<M, _>(count, RandomState::new()); test_many::<M, _>(count, RandomState::new());
} }
test_many::<M, _>(5000, BuildHasherDefault::<CollidingHasher>::default()); test_many::<M, _>(1000, BuildHasherDefault::<CollidingHasher>::default());
} }
fn test_many<M: HashMapFamily, H: BuildHasher>(count: usize, h: H) { fn test_many<M: HashMapFamily, H: BuildHasher>(count: usize, h: H) {

View file

@ -10,6 +10,7 @@ pub mod sendsync;
pub mod thin_u128; pub mod thin_u128;
pub mod unroll_int; pub mod unroll_int;
pub mod unsized_clone; pub mod unsized_clone;
pub mod assert;
pub mod safe_extern { pub mod safe_extern {
pub use pm::safe_extern; pub use pm::safe_extern;