Skip to content

Commit

Permalink
fixed cache bugs; rust working; rust missing tests for reader and wri…
Browse files Browse the repository at this point in the history
…ter; spec is adjusted to accurately explain header
  • Loading branch information
Mr Martian committed Jul 29, 2024
1 parent f6c25ee commit 4377154
Show file tree
Hide file tree
Showing 14 changed files with 957 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
export CARGO_INCREMENTAL=0
export RUSTFLAGS='-Cinstrument-coverage'
export LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw'
cargo test
cargo test --lib
grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "/*/.cargo/*" --ignore "/*/target/*" -o ./coverage/rust-lcov.info
shell: bash

Expand Down
160 changes: 160 additions & 0 deletions rust/bit_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,163 @@ impl BitCast for usize {
value as usize
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_bitcast_u64() {
// from
assert_eq!(u64::from_u64(0), 0);
assert_eq!(u64::from_u64(1), 1);
assert_eq!(u64::from_u64(0xffffffff), 0xffffffff);

// to
assert_eq!(u64::to_u64(&0), 0);
assert_eq!(u64::to_u64(&1), 1);
assert_eq!(u64::to_u64(&0xffffffff), 0xffffffff);
}

#[test]
fn test_bitcast_i64() {
// from
assert_eq!(i64::from_u64(0), 0);
assert_eq!(i64::from_u64(1), 1);
assert_eq!(i64::from_u64(18446744073709551615), -1);

// to
assert_eq!(i64::to_u64(&0), 0);
assert_eq!(i64::to_u64(&1), 1);
assert_eq!(i64::to_u64(&-1), 18446744073709551615);
}

#[test]
fn test_bitcast_f64() {
// from
assert_eq!(f64::from_u64(0), 0.0);
assert_eq!(f64::from_u64(4607182418800017408), 1.0);
assert_eq!(f64::from_u64(13830554455654793216), -1.0);

// to
assert_eq!(f64::to_u64(&0.0), 0);
assert_eq!(f64::to_u64(&1.0), 4607182418800017408);
assert_eq!(f64::to_u64(&-1.0), 13830554455654793216);
}

#[test]
fn test_bitcast_u32() {
// from
assert_eq!(u32::from_u64(0), 0);
assert_eq!(u32::from_u64(1), 1);
assert_eq!(u32::from_u64(0xffffffff), 0xffffffff);

// to
assert_eq!(u32::to_u64(&0), 0);
assert_eq!(u32::to_u64(&1), 1);
assert_eq!(u32::to_u64(&0xffffffff), 0xffffffff);
}

#[test]
fn test_bitcast_i32() {
// from
assert_eq!(i32::from_u64(0), 0);
assert_eq!(i32::from_u64(1), 1);
assert_eq!(i32::from_u64(4294967295), -1);

// to
assert_eq!(i32::to_u64(&0), 0);
assert_eq!(i32::to_u64(&1), 1);
assert_eq!(i32::to_u64(&-1), 4294967295);
}

#[test]
fn test_bitcast_f32() {
// from
assert_eq!(f32::from_u64(0), 0.0);
assert_eq!(f32::from_u64(1065353216), 1.0);
assert_eq!(f32::from_u64(3212836864), -1.0);

// to
assert_eq!(f32::to_u64(&0.0), 0);
assert_eq!(f32::to_u64(&1.0), 1065353216);
assert_eq!(f32::to_u64(&-1.0), 3212836864);
}

#[test]
fn test_bitcast_u16() {
// from
assert_eq!(u16::from_u64(0), 0);
assert_eq!(u16::from_u64(1), 1);
assert_eq!(u16::from_u64(0xffff), 0xffff);

// to
assert_eq!(u16::to_u64(&0), 0);
assert_eq!(u16::to_u64(&1), 1);
assert_eq!(u16::to_u64(&0xffff), 0xffff);
}

#[test]
fn test_bitcast_i16() {
// from
assert_eq!(i16::from_u64(0), 0);
assert_eq!(i16::from_u64(1), 1);
assert_eq!(i16::from_u64(65535), -1);

// to
assert_eq!(i16::to_u64(&0), 0);
assert_eq!(i16::to_u64(&1), 1);
assert_eq!(i16::to_u64(&-1), 65535);
}

#[test]
fn test_bitcast_u8() {
// from
assert_eq!(u8::from_u64(0), 0);
assert_eq!(u8::from_u64(1), 1);
assert_eq!(u8::from_u64(255), 255);

// to
assert_eq!(u8::to_u64(&0), 0);
assert_eq!(u8::to_u64(&1), 1);
assert_eq!(u8::to_u64(&255), 255);
}

#[test]
fn test_bitcast_i8() {
// from
assert_eq!(i8::from_u64(0), 0);
assert_eq!(i8::from_u64(1), 1);
assert_eq!(i8::from_u64(255), -1);

// to
assert_eq!(i8::to_u64(&0), 0);
assert_eq!(i8::to_u64(&1), 1);
assert_eq!(i8::to_u64(&-1), 255);
}

#[test]
fn test_bitcast_bool() {
// from
assert!(!bool::from_u64(0));
assert!(bool::from_u64(1));
assert!(bool::from_u64(2));

// to
assert_eq!(bool::to_u64(&false), 0);
assert_eq!(bool::to_u64(&true), 1);
}

#[test]
fn test_bitcast_usize() {
// from
assert_eq!(usize::from_u64(0), 0);
assert_eq!(usize::from_u64(1), 1);
assert_eq!(usize::from_u64(4294967295), 4294967295);

// to
assert_eq!(usize::to_u64(&0), 0);
assert_eq!(usize::to_u64(&1), 1);
assert_eq!(usize::to_u64(&4294967295), 4294967295);
}
}
Loading

0 comments on commit 4377154

Please sign in to comment.