Unit testing checks individual program units in isolation against expected behavior. In Rust, Cargo provides direct support for running these tests alongside the code they verify. section 5.5
A practical testing workflow includes:
- Place tests in a test module marked with #[cfg(test)]. section 5.5
- Import the surrounding implementation with use super::*. section 5.5
- Mark each test with #[test] and compare actual values with assert_eq!. section 5.5
- Run cargo test from the crate’s root directory and inspect passed or failed results. section 5.5
Testing Q7 conversions
The Q7 implementation tests conversion from f32, conversion to f32, and out-of-bounds inputs. The out_of_bounds test verifies that converting 10. and 1. produces the same value, while -10. and -1. also match. section 5.5
$ cargo test Compiling ch5-q v0.1.0 (file:///path/to/ch5/ch5-q) Finished dev [unoptimized + debuginfo] target(s) in 2.86 s Running target\debug\deps\ch5_q-013c963f84b21f92 running 3 tests test tests::f32_to_q7 ... ok test tests::out_of_bounds ... ok test tests::q7_to_f32 ... ok test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests ch5-q running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
The test output shows three named tests passing, with no failures or ignored tests. section 5.5




















































