diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-20 11:36:03 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-20 11:36:03 +0300 |
| commit | 4898351a371ae8f5bffae715fc1f1c2e7a0cac7d (patch) | |
| tree | 27f742ebbfa63df843a566c05440dfa7a53f8d08 /libs/adder | |
| parent | 256ad69edea8bb5eeff38e3f5fa1dcf6e990daf7 (diff) | |
| download | rust-book-4898351a371ae8f5bffae715fc1f1c2e7a0cac7d.tar.gz rust-book-4898351a371ae8f5bffae715fc1f1c2e7a0cac7d.tar.bz2 rust-book-4898351a371ae8f5bffae715fc1f1c2e7a0cac7d.zip | |
add: adder, poem
Diffstat (limited to 'libs/adder')
| -rw-r--r-- | libs/adder/Cargo.toml | 6 | ||||
| -rw-r--r-- | libs/adder/src/lib.rs | 64 | ||||
| -rw-r--r-- | libs/adder/tests/common/mod.rs | 2 | ||||
| -rw-r--r-- | libs/adder/tests/integration_test.rs | 11 |
4 files changed, 83 insertions, 0 deletions
diff --git a/libs/adder/Cargo.toml b/libs/adder/Cargo.toml new file mode 100644 index 0000000..9a5826a --- /dev/null +++ b/libs/adder/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "adder" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/libs/adder/src/lib.rs b/libs/adder/src/lib.rs new file mode 100644 index 0000000..62f625c --- /dev/null +++ b/libs/adder/src/lib.rs @@ -0,0 +1,64 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn can_hold(&self, other: &Rectangle) -> bool { + self.width > other.width && self.height > other.height + } +} + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +pub fn add_two(a: u64) -> u64 { + a + 2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn exploration() { + let result = add(2, 2); + assert_eq!(result, 4); + } + + #[test] + fn larger_can_hold_smaller() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(larger.can_hold(&smaller)); + } + + #[test] + fn smaller_cannot_hold_larger() { + let larger = Rectangle { + width: 8, + height: 7, + }; + let smaller = Rectangle { + width: 5, + height: 1, + }; + + assert!(!smaller.can_hold(&larger)); + } + + #[test] + fn it_adds_two() { + let result = add_two(2); + assert_eq!(result, 4) + } +} diff --git a/libs/adder/tests/common/mod.rs b/libs/adder/tests/common/mod.rs new file mode 100644 index 0000000..71a3f5a --- /dev/null +++ b/libs/adder/tests/common/mod.rs @@ -0,0 +1,2 @@ +pub fn setup() { +} diff --git a/libs/adder/tests/integration_test.rs b/libs/adder/tests/integration_test.rs new file mode 100644 index 0000000..d18b5b6 --- /dev/null +++ b/libs/adder/tests/integration_test.rs @@ -0,0 +1,11 @@ +use adder::add_two; + +mod common; + +#[test] +fn it_adds_two() { + common::setup(); + + let result = add_two(2); + assert_eq!(result, 4); +} |
