What are the rules for a rustdoc test of a method to pass?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


What are the rules for a rustdoc test of a method to pass?



I am trying to write a rustdoc test of a method called insert.
The testing function is called in the last line of the test, and when I comment it out the test passes just fine.


insert



Here is the (unhelpful) error message:


$ cargo test
...
failures:
src/blindhide.rs - blindhide::BlindHide::insert (line 34)



Could it be that there are special rules for a rustdoc test to pass (for example the use of assert! macro to prove that it returns a proper value)?


assert!



I am asking because my method passes integration test with very similar code, so I am pretty confident it's correct



The test:


/// # Examples
/// ```
/// extern crate crypsto;
/// 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 = crypsto::BlindHide::new(2, 12);
/// let new_data = settings.insert(p, &msg);
/// ```
pub fn insert(&self, img: &[u8], msg: &[u8]) -> Result<Vec<u8>, NoFit> {
// Number of characters that can fit
let cap = ( img.len() as f64 * self.lsb as f64 / 8_f64 )
as usize / self.step;

if msg.len() > cap {
return Err(NoFit(String::from("Message doesn't fit")));
}

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;
}
}

Ok(ret)
}





Please review how to create a Minimal, Complete, and Verifiable example and then edit your question to include it. We cannot tell what crates, types, traits, fields, etc. are present in the code. Try to produce something that reproduces your error on the Rust Playground or you can reproduce it in a brand new Cargo project. There are Rust-specific MCVE tips as well.
– Shepmaster
31 mins ago





There are no special rules for test success compared to "standard" Rust tests.
– Shepmaster
30 mins ago





I am aware that my question is incomplete, but it's very hard to provide a complete one here as it would mean copy-pasting most of my library. I will add the test though.
– Στέφανος Μανδαλάς
26 mins ago







Create a minimal reproduction of the problem. That's what all of the links I provided say to do. They do not say to put your entire code in. I'd almost guarantee that you can create something that reproduces the problem with less than 25 lines of code in a completely new Cargo project.
– Shepmaster
20 mins ago







Okay I will try, thanks.
– Στέφανος Μανδαλάς
18 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

kwJmRnl3NC1u BHxrSoDlPdouofpaxJBu0QJKMtD7m,3PMF6hv,5tYlWLb6sRFDddRV6FAKex LuogVJU19m zFt,LU rTPGL CE
o5Kxmx upM1hSzK,MlkPy7iM7 i,d p39yqFbCRYOHm0n

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham