1
- /*
2
- ## Scaler
3
- ### Instructions
4
- Create the following functions, that receives two parameters:
5
- - sum, that returns the sum between two values from 0 to 255
6
- - diff, that returns the difference between two values from -32768 to 32767
7
- - pro, that returns the product of the multiplication between two values from -128 to 127
8
- - quo, that returns the quotient of the division between two values
9
- - rem, that returns the remainder of the division between two values
10
- ### Notions
11
- - https://doc.rust-lang.org/book/ch03-02-data-types.html
12
- */
13
-
14
- // fn main() {
15
- // // sum
16
- // println!("sum : {}", sum(234, 2));
17
- // println!("sum : {}", sum(1, 255)); // 'ERROR: attempt to add with overflow'
18
- // // diff
19
- // println!("diff : {}", diff(234, 2));
20
- // println!("diff : {}", diff(-32768, 32766)); // 'ERROR: attempt to subtract with overflow'
21
- // // product
22
- // println!("pro : {}", pro(23, 2));
23
- // println!("pro : {}", pro(-128, 2)); // 'ERROR: attempt to multiply with overflow'
24
- // // quotient
25
- // println!("quo : {}", quo(22.0, 2.0));
26
- // println!("quo : {}", quo(-128.23, 2.0));
27
- // // remainder
28
- // println!("rem : {}", rem(22.0, 2.0));
29
- // println!("rem : {}", rem(-128.23, 2.0));
30
- // }
31
-
32
1
pub fn sum ( a : u8 , b : u8 ) -> u8 {
33
2
a + b
34
3
}
@@ -44,34 +13,3 @@ pub fn quo(a: f32, b: f32) -> f32 {
44
13
pub fn rem ( a : f32 , b : f32 ) -> f32 {
45
14
a % b
46
15
}
47
-
48
- // tests
49
- #[ cfg( test) ]
50
- mod tests {
51
- use super :: * ;
52
-
53
- #[ test]
54
- #[ should_panic]
55
- fn test_panic_sum ( ) {
56
- sum ( 25 , 255 ) ;
57
- }
58
- #[ test]
59
- #[ should_panic]
60
- fn test_panic_diff ( ) {
61
- diff ( -32768 , 32766 ) ;
62
- }
63
- #[ test]
64
- #[ should_panic]
65
- fn test_panic_pro ( ) {
66
- pro ( -128 , 2 ) ;
67
- }
68
-
69
- #[ test]
70
- fn pass ( ) {
71
- assert_eq ! ( sum( 1 , 2 ) , 3 ) ;
72
- assert_eq ! ( diff( 1 , 2 ) , -1 ) ;
73
- assert_eq ! ( pro( 1 , 2 ) , 2 ) ;
74
- assert_eq ! ( quo( 1.0 , 2.0 ) , 0.5 ) ;
75
- assert_eq ! ( rem( 1.0 , 2.0 ) , 1.0 ) ;
76
- }
77
- }
0 commit comments