Skip to content

Commit

Permalink
build: fix nostd tests (m4b#89)
Browse files Browse the repository at this point in the history
* Add nostd tests
* Fix `cargo test --no-default-features` to pass by making std-only tests/docs conditional (added to CI)
* There are a few `FIXME` in the code where things seemed suspicious
  • Loading branch information
nyurik authored and Frostie314159 committed Nov 20, 2023
1 parent 15982d5 commit 56c98b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,23 @@ mod tests {
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {num:?}");
println!("num: {:?}", &num);
assert_eq!(130u64, num.into());
assert_eq!(num.size(), 2);

let buf = [0x00, 0x01];
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {num:?}");
println!("num: {:?}", &num);
assert_eq!(0u64, num.into());
assert_eq!(num.size(), 1);

let buf = [0x21];
let bytes = &buf[..];
let num = bytes.pread::<Uleb128>(0).unwrap();
#[cfg(feature = "std")]
println!("num: {num:?}");
println!("num: {:?}", &num);
assert_eq!(0x21u64, num.into());
assert_eq!(num.size(), 1);
}
Expand Down
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,22 @@ mod tests {
let b = &bytes[..];
let s: &str = b.pread(0).unwrap();
#[cfg(feature = "std")]
println!("str: {s}");
println!("str: {}", s);
assert_eq!(s.len(), bytes[..].len() - 1);
let bytes: &[u8] = b"hello, world!\0some_other_things";
let hello_world: &str = bytes.pread_with(0, StrCtx::Delimiter(NULL)).unwrap();
#[cfg(feature = "std")]
println!("{hello_world:?}");
println!("{:?}", &hello_world);
assert_eq!(hello_world.len(), 13);
let hello: &str = bytes.pread_with(0, StrCtx::Delimiter(SPACE)).unwrap();
#[cfg(feature = "std")]
println!("{hello:?}");
println!("{:?}", &hello);
assert_eq!(hello.len(), 6);
// this could result in underflow so we just try it
let _error = bytes.pread_with::<&str>(6, StrCtx::Delimiter(SPACE));
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("{error:?}");
println!("{:?}", &error);
assert!(error.is_ok());
}

Expand All @@ -383,14 +383,16 @@ mod tests {
use super::Pread;
let bytes: &[u8] = b"";
let hello_world = bytes.pread_with::<&str>(0, StrCtx::Delimiter(NULL));
#[cfg(feature = "std")]
println!("1 {:?}", &hello_world);
assert!(hello_world.is_err());
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("2 {error:?}");
println!("2 {:?}", &error);
assert!(error.is_err());
let bytes: &[u8] = b"\0";
let null = bytes.pread::<&str>(0).unwrap();
#[cfg(feature = "std")]
println!("3 {:?}", &null);
assert!(null.is_empty());
}
Expand Down Expand Up @@ -619,9 +621,10 @@ mod tests {
let astring: [u8; 3] = [0x45, 0x2a, 0x44];
let string = astring.gread_with::<&str>(offset, StrCtx::Length(2));
match &string {
&Ok(_) => {}
Err(err) => {
println!("{}", &err);
Ok(_) => {}
Err(_err) => {
#[cfg(feature = "std")]
println!("{_err}");
panic!();
}
}
Expand Down

0 comments on commit 56c98b9

Please sign in to comment.