calculator_danielgorgonha/
lib.rs

1pub mod calc1;
2pub mod calc2;
3pub mod calc3;
4
5/// Simple calculator library for u32 operations.
6/// 
7/// This library provides basic arithmetic operations with overflow protection.
8/// All operations return safe values even in edge cases like overflow or division by zero.
9
10#[cfg(test)]
11mod tests {
12  use super::calc1::{add, sub};
13  use super::calc2::{multiply, rate};
14  use super::calc3::{power, logarithm};
15
16  #[test]
17  fn test_add() {
18    assert_eq!(add(10, 20), 30);
19    assert_eq!(add(u32::MAX, 1), u32::MAX); // Overflow protection
20    assert_eq!(add(0, 0), 0);
21  }
22
23  #[test]
24  fn test_sub() {
25    assert_eq!(sub(20, 10), 10);
26    assert_eq!(sub(10, 20), 0); // Protection against underflow
27    assert_eq!(sub(0, 0), 0);
28  }
29
30  #[test]
31  fn test_multiply() {
32    assert_eq!(multiply(10, 20), 200);
33    assert_eq!(multiply(0, 100), 0);
34    assert_eq!(multiply(u32::MAX, 2), u32::MAX); // Overflow protection
35  }
36
37  #[test]
38  fn test_rate() {
39    assert_eq!(rate(20, 10), 2);
40    assert_eq!(rate(10, 20), 0); // Integer division
41    assert_eq!(rate(10, 0), 0); // Division by zero protection
42    assert_eq!(rate(0, 10), 0);
43  }
44
45  #[test]
46  fn test_power() {
47    assert_eq!(power(2, 3), 8);
48    assert_eq!(power(5, 0), 1);
49    assert_eq!(power(10, 2), 100);
50    assert_eq!(power(2, 10), 1024);
51    assert_eq!(power(0, 5), 0);
52    assert_eq!(power(1, 100), 1);
53    assert_eq!(power(u32::MAX, 1), u32::MAX);
54    assert_eq!(power(2, 31), 2147483648); // 2^31 = 2147483648
55  }
56
57  #[test]
58  fn test_logarithm() {
59    assert_eq!(logarithm(8, 2), 3);
60    assert_eq!(logarithm(100, 10), 2);
61    assert_eq!(logarithm(16, 2), 4);
62    assert_eq!(logarithm(1, 10), 0);
63    assert_eq!(logarithm(0, 10), 0); // Edge case
64    assert_eq!(logarithm(10, 0), 0); // Edge case
65    assert_eq!(logarithm(10, 1), 0); // Edge case
66    assert_eq!(logarithm(5, 10), 0); // number < base
67  }
68}