Writing and testing a crate – logic gate simulator
Armed with all of this knowledge, let's start things off with our logic gate simulation crate. We'll create a new project by running cargo new logic_gates --lib
. Starting with primitive gates implemented as functions such as and
, xor
, and so on, we will write unit tests for these gates. Following that, we'll write integration tests by implementing a half adder that uses our primitive gates. During this process, we'll also get to write documentation for our crate.
First off, we'll start with some unit tests. Here's the initial crate code in its entirety:
//! This is a logic gates simulation crate built to demonstrate writing unit tests and integration tests // logic_gates/src/lib.rs pub fn and(a: u8, b: u8) -> u8 { unimplemented!() } pub fn xor(a: u8, b: u8) -> u8 { unimplemented!() } #[cfg(test)] mod tests { use crate::{xor, and}; #[test] fn test_and() { assert_eq!(1, and(1, 1)); assert_eq!(0...