-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathhexadecimal_to_decimal.rs
61 lines (53 loc) · 1.84 KB
/
hexadecimal_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
61
pub fn hexadecimal_to_decimal(hexadecimal_str: &str) -> Result<u64, &'static str> {
if hexadecimal_str.is_empty() {
return Err("Empty input");
}
for hexadecimal_str in hexadecimal_str.chars() {
if !hexadecimal_str.is_ascii_hexdigit() {
return Err("Input was not a hexadecimal number");
}
}
match u64::from_str_radix(hexadecimal_str, 16) {
Ok(decimal) => Ok(decimal),
Err(_e) => Err("Failed to convert octal to hexadecimal"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hexadecimal_to_decimal_empty() {
assert_eq!(hexadecimal_to_decimal(""), Err("Empty input"));
}
#[test]
fn test_hexadecimal_to_decimal_invalid() {
assert_eq!(
hexadecimal_to_decimal("xyz"),
Err("Input was not a hexadecimal number")
);
assert_eq!(
hexadecimal_to_decimal("0xabc"),
Err("Input was not a hexadecimal number")
);
}
#[test]
fn test_hexadecimal_to_decimal_valid1() {
assert_eq!(hexadecimal_to_decimal("45"), Ok(69));
assert_eq!(hexadecimal_to_decimal("2b3"), Ok(691));
assert_eq!(hexadecimal_to_decimal("4d2"), Ok(1234));
assert_eq!(hexadecimal_to_decimal("1267a"), Ok(75386));
}
#[test]
fn test_hexadecimal_to_decimal_valid2() {
assert_eq!(hexadecimal_to_decimal("1a"), Ok(26));
assert_eq!(hexadecimal_to_decimal("ff"), Ok(255));
assert_eq!(hexadecimal_to_decimal("a1b"), Ok(2587));
assert_eq!(hexadecimal_to_decimal("7fffffff"), Ok(2147483647));
}
#[test]
fn test_hexadecimal_to_decimal_valid3() {
assert_eq!(hexadecimal_to_decimal("0"), Ok(0));
assert_eq!(hexadecimal_to_decimal("7f"), Ok(127));
assert_eq!(hexadecimal_to_decimal("80000000"), Ok(2147483648));
}
}