Skip to content

Commit

Permalink
Merge branch 'master' into add_upload_coverage_report
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 authored Nov 10, 2023
2 parents 88b5ee9 + 3cc18de commit 072c55f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
11 changes: 6 additions & 5 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
* Compression
* [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/compression/run_length_encoding.rs)
* Conversions
* [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
* [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
* [Binary to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
* [Binary to Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
* [Decimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
* [Hexadecimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
* [Octal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
* [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
* Data Structures
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod binary_to_hexadecimal;
mod decimal_to_binary;
mod hexadecimal_to_binary;
mod octal_to_binary;
mod octal_to_decimal;
pub use self::binary_to_decimal::binary_to_decimal;
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
pub use self::decimal_to_binary::decimal_to_binary;
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
pub use self::octal_to_binary::octal_to_binary;
pub use self::octal_to_decimal::octal_to_decimal;
60 changes: 60 additions & 0 deletions src/conversions/octal_to_decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Author: cyrixninja
// Octal to Decimal Converter: Converts Octal to Decimal
// Wikipedia References:
// 1. https://en.wikipedia.org/wiki/Octal
// 2. https://en.wikipedia.org/wiki/Decimal

pub fn octal_to_decimal(octal_str: &str) -> Result<u64, &'static str> {
let octal_str = octal_str.trim();

if octal_str.is_empty() {
return Err("Empty");
}

if !octal_str.chars().all(|c| ('0'..='7').contains(&c)) {
return Err("Non-octal Value");
}

// Convert octal to decimal and directly return the Result
u64::from_str_radix(octal_str, 8).map_err(|_| "Conversion error")
}

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

#[test]
fn test_empty_string() {
let input = "";
let expected = Err("Empty");
assert_eq!(octal_to_decimal(input), expected);
}

#[test]
fn test_invalid_octal() {
let input = "89";
let expected = Err("Non-octal Value");
assert_eq!(octal_to_decimal(input), expected);
}

#[test]
fn test_valid_octal() {
let input = "123";
let expected = Ok(83);
assert_eq!(octal_to_decimal(input), expected);
}

#[test]
fn test_valid_octal2() {
let input = "1234";
let expected = Ok(668);
assert_eq!(octal_to_decimal(input), expected);
}

#[test]
fn test_valid_octal3() {
let input = "12345";
let expected = Ok(5349);
assert_eq!(octal_to_decimal(input), expected);
}
}

0 comments on commit 072c55f

Please sign in to comment.