我正在尝试编写一个名为insert
. 测试函数在测试的最后一行被调用,当我把它注释掉时,测试就通过了。
错误信息:
$ cargo test
Compiling reproduce v0.1.0
(file:///home/user/reproduce)
Finished dev [unoptimized + debuginfo] target(s) in 2.03s
Running target/debug/deps/reproduce-17ad4bb50aa9c47e
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Doc-tests reproduce
running 1 test
test src/lib.rs - MyStruct::method (line 19) ... FAILED
failures:
failures:
src/lib.rs - MyStruct::method (line 19)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--doc'
代码:
impl MyStruct {
pub fn new(lsb: u8, step: usize) -> MyStruct {
let mask: u8 = match lsb {
1 => 0b0000_0001,
2 => 0b0000_0011,
_ => 0b0000_1111
};
MyStruct { lsb, mask, step }
}
/// # Examples
/// ```
/// extern crate reproduce;
/// extern crate rgb;
///
/// use std::slice;
/// use rgb::RGB;
///
/// let msg = "This is a secret message".as_bytes();
/// let img = vec![RGB {r: 0, g: 0, b: 0}; 800*500];
///
/// // Create a reference to the bytes of img
/// let p: *const Vec<RGB<u8>> = &img;
/// let p: *const u8 = p as *const u8;
/// let p: &[u8] = unsafe {
/// slice::from_raw_parts(p, 3*800*500)
/// };
///
/// let settings = reproduce::MyStruct::new(2, 12);
/// let new_data = settings.method(p, &msg);
/// ```
pub fn method(&self, img: &[u8], msg: &[u8]) -> Vec<u8> {
let mut ret = img.to_vec();
let mut n = 0;
for ch in msg.iter() {
for i in 1..=8/self.lsb {
let shift = (self.lsb*i) as u32;
ret[n] = (ret[n] & !self.mask) |
(ch & self.mask.rotate_right(shift)).rotate_left(shift);
n += self.step;
}
}
ret
}
}
是否有特殊规则可以通过 rustdoc 测试(例如使用assert!
宏来证明它返回正确的值)?
我问是因为我的方法通过了非常相似的代码的集成测试,所以我很有信心它是正确的