| 1 |
use core::hint::black_box; |
| 2 |
use equator::assert; |
| 3 |
use std::collections::HashMap; |
| 4 |
|
| 5 |
#[test] |
| 6 |
#[should_panic] |
| 7 |
pub fn test() { |
| 8 |
black_box(test_asm_1_med as fn(_, _)); |
| 9 |
black_box(test_asm_2_smol as fn(_, _, _, _)); |
| 10 |
black_box(test_asm_2_med as fn(_, _, _, _)); |
| 11 |
black_box(test_asm_2_big as fn(_, _, _, _)); |
| 12 |
|
| 13 |
black_box(test_std_asm_1_med as fn(_, _)); |
| 14 |
black_box(test_std_asm_2_smol as fn(_, _, _, _)); |
| 15 |
black_box(test_std_asm_2_med as fn(_, _, _, _)); |
| 16 |
black_box(test_std_asm_2_big as fn(_, _, _, _)); |
| 17 |
|
| 18 |
black_box(test_assert2_asm_1_med as fn(_, _)); |
| 19 |
black_box(test_assert2_asm_2_smol as fn(_, _, _, _)); |
| 20 |
black_box(test_assert2_asm_2_med as fn(_, _, _, _)); |
| 21 |
black_box(test_assert2_asm_2_big as fn(_, _, _, _)); |
| 22 |
|
| 23 |
let x = 3; |
| 24 |
let y = 2; |
| 25 |
let z = true; |
| 26 |
|
| 27 |
assert!(true == false); |
| 28 |
assert!(all(true == false, x < y, any(!z, z))); |
| 29 |
assert!(all(true == false, x + 1 < y, any(!z, z))); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
#[should_panic] |
| 34 |
pub fn test_different_types() { |
| 35 |
assert!(*[0, 1, 2usize].as_slice() == [0, 1usize]); |
| 36 |
} |
| 37 |
|
| 38 |
#[derive(Copy, Clone, Debug)] |
| 39 |
struct ApproxEq { |
| 40 |
symbol: &'static str, |
| 41 |
tol: f64, |
| 42 |
} |
| 43 |
|
| 44 |
#[derive(Copy, Clone, Debug)] |
| 45 |
enum ApproxEqError { |
| 46 |
Absolute { distance: f64 }, |
| 47 |
} |
| 48 |
|
| 49 |
impl equator::CmpError<ApproxEq, f64, f64> for ApproxEq { |
| 50 |
type Error = ApproxEqError; |
| 51 |
} |
| 52 |
impl equator::Cmp<f64, f64> for ApproxEq { |
| 53 |
fn test(&self, lhs: &f64, rhs: &f64) -> Result<(), Self::Error> { |
| 54 |
let distance = (lhs - rhs).abs(); |
| 55 |
let result = if distance <= self.tol { |
| 56 |
Ok(()) |
| 57 |
} else { |
| 58 |
Err(ApproxEqError::Absolute { distance }) |
| 59 |
}; |
| 60 |
|
| 61 |
result |
| 62 |
} |
| 63 |
} |
| 64 |
impl equator::CmpDisplay<ApproxEq, f64, f64> for ApproxEqError { |
| 65 |
fn fmt( |
| 66 |
&self, |
| 67 |
cmp: &ApproxEq, |
| 68 |
lhs: &f64, |
| 69 |
lhs_source: &str, |
| 70 |
_: &dyn core::fmt::Debug, |
| 71 |
rhs: &f64, |
| 72 |
rhs_source: &str, |
| 73 |
_: &dyn core::fmt::Debug, |
| 74 |
f: &mut core::fmt::Formatter, |
| 75 |
) -> core::fmt::Result { |
| 76 |
let ApproxEq { symbol, tol } = *cmp; |
| 77 |
let ApproxEqError::Absolute { distance } = self; |
| 78 |
|
| 79 |
writeln!( |
| 80 |
f, |
| 81 |
"Assertion failed: {lhs_source} {symbol} {rhs_source}, with absolute tolerance {tol:.1e}" |
| 82 |
)?; |
| 83 |
writeln!(f, "- {lhs_source} = {lhs:#?}")?; |
| 84 |
writeln!(f, "- {rhs_source} = {rhs:#?}")?; |
| 85 |
write!(f, "- distance = {distance:#?}") |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
pub fn test_move() { |
| 91 |
let ref mut m = HashMap::<usize, Vec<()>>::new(); |
| 92 |
let x = vec![]; |
| 93 |
assert!(*x == []); |
| 94 |
assert!(*x == [], "oops {x:?}"); |
| 95 |
assert!(m.insert(0, x).is_none()); |
| 96 |
} |
| 97 |
|
| 98 |
#[inline(never)] |
| 99 |
pub fn test_asm_1_med(a: usize, b: usize) { |
| 100 |
assert!(a == b); |
| 101 |
} |
| 102 |
|
| 103 |
#[inline(never)] |
| 104 |
pub fn test_asm_2_smol(a: u8, b: u8, c: u8, d: u8) { |
| 105 |
assert!([a, c] == [b, d]); |
| 106 |
} |
| 107 |
|
| 108 |
#[inline(never)] |
| 109 |
pub fn test_asm_2_med(a: usize, b: usize, c: usize, d: usize) { |
| 110 |
assert!(all(a == b, c == d)); |
| 111 |
} |
| 112 |
|
| 113 |
#[inline(never)] |
| 114 |
pub fn test_asm_2_big(a: usize, b: usize, c: usize, d: usize) { |
| 115 |
assert!([a, c] == [b, d]); |
| 116 |
} |
| 117 |
|
| 118 |
#[inline(never)] |
| 119 |
pub fn test_std_asm_2_smol(a: u8, b: u8, c: u8, d: u8) { |
| 120 |
std::assert_eq!([a, c], [b, d]); |
| 121 |
} |
| 122 |
|
| 123 |
#[inline(never)] |
| 124 |
pub fn test_std_asm_2_med(a: usize, b: usize, c: usize, d: usize) { |
| 125 |
std::assert_eq!(a, b); |
| 126 |
std::assert_eq!(c, d); |
| 127 |
} |
| 128 |
|
| 129 |
#[inline(never)] |
| 130 |
pub fn test_std_asm_2_big(a: usize, b: usize, c: usize, d: usize) { |
| 131 |
std::assert_eq!([a, c], [b, d]); |
| 132 |
} |
| 133 |
|
| 134 |
#[inline(never)] |
| 135 |
pub fn test_assert2_asm_2_smol(a: u8, b: u8, c: u8, d: u8) { |
| 136 |
assert2::assert!([a, c] == [b, d]); |
| 137 |
} |
| 138 |
|
| 139 |
#[inline(never)] |
| 140 |
pub fn test_assert2_asm_2_med(a: usize, b: usize, c: usize, d: usize) { |
| 141 |
assert2::assert!(a == b); |
| 142 |
assert2::assert!(c == d); |
| 143 |
} |
| 144 |
|
| 145 |
#[inline(never)] |
| 146 |
pub fn test_assert2_asm_2_big(a: usize, b: usize, c: usize, d: usize) { |
| 147 |
assert2::assert!([a, c] == [b, d]); |
| 148 |
} |
| 149 |
|
| 150 |
#[inline(never)] |
| 151 |
pub fn test_std_asm_1_med(a: usize, b: usize) { |
| 152 |
std::assert_eq!(a, b); |
| 153 |
} |
| 154 |
|
| 155 |
#[inline(never)] |
| 156 |
pub fn test_assert2_asm_1_med(a: usize, b: usize) { |
| 157 |
assert2::assert!(a == b); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
#[should_panic] |
| 162 |
pub fn test_big_fail() { |
| 163 |
let x = [core::ptr::null::<()>(); 2]; |
| 164 |
assert!(x != x); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
pub fn test_big() { |
| 169 |
let x = [core::ptr::null::<()>(); 2]; |
| 170 |
assert!(x == x); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
#[should_panic] |
| 175 |
pub fn test_custom_fail() { |
| 176 |
let approx_eq = ApproxEq { |
| 177 |
tol: 0.01, |
| 178 |
symbol: "~", |
| 179 |
}; |
| 180 |
|
| 181 |
let x = 0.1; |
| 182 |
assert!(all(x ~ 0.2, x ~ 0.1, x ~ 0.3)); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
pub fn test_custom() { |
| 187 |
let approx_eq = ApproxEq { |
| 188 |
tol: 0.01, |
| 189 |
symbol: "~", |
| 190 |
}; |
| 191 |
|
| 192 |
assert!(0.1 :approx_eq: 0.10001); |
| 193 |
assert!(0.1 ~ 0.10001); |
| 194 |
} |