diff --git a/CHANGELOG.md b/CHANGELOG.md index d33332a..aa9e740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,10 @@ ## Unreleased * [#7] - Add `strtoul` and `strcpy` +* [#8] - Add `abs` [#7]: https://github.com/rust-embedded-community/tinyrlibc/pull/7 +[#8]: https://github.com/rust-embedded-community/tinyrlibc/pull/8 ## v0.2.2 (2022-03-17) diff --git a/README.md b/README.md index b59d57c..e834105 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This crate basically came about so that the [nrfxlib](https://github.com/NordicP ## Implemented so far +* abs * strol * atoi * strcmp diff --git a/src/abs.rs b/src/abs.rs new file mode 100644 index 0000000..c6d257b --- /dev/null +++ b/src/abs.rs @@ -0,0 +1,36 @@ +//! Rust implementation of C library function `abs` +//! +//! Licensed under the Blue Oak Model Licence 1.0.0 + +use crate::CInt; + +/// Calculates the integer absolute value +/// +/// ``` +/// use tinyrlibc::abs; +/// assert_eq!(abs(-2), 2); +/// ``` +#[no_mangle] +pub extern "C" fn abs(i: CInt) -> CInt { + i.abs() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn neg() { + assert_eq!(abs(-2), 2); + } + + #[test] + fn pos() { + assert_eq!(abs(3), 3); + } + + #[test] + fn zero() { + assert_eq!(abs(0), 0); + } +} diff --git a/src/lib.rs b/src/lib.rs index 9cda140..3f0346e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,9 @@ #[allow(unused_imports)] use std as core; +mod abs; +pub use self::abs::abs; + mod strcmp; pub use self::strcmp::strcmp;