-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathoctal_to_decimal.rs
60 lines (50 loc) · 1.45 KB
/
octal_to_decimal.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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);
}
}