1
+ use std:: fmt;
2
+ use std:: ops:: Add ;
3
+
1
4
/// This enum represents the sum of a sequence of numbers that may be integers or floating point.
2
5
/// Integer is the default. When a floating point number is added to the sum, the type is converted
3
6
/// to Float.
4
- #[ derive( Debug , PartialEq ) ]
7
+ #[ derive( Debug , PartialEq , Clone , Copy ) ]
5
8
pub enum Sum {
6
9
Integer ( i128 ) ,
7
10
Float ( f64 ) ,
8
11
}
9
12
10
- impl Sum {
11
- fn new ( ) -> Self {
12
- Self :: Integer ( 0 )
13
- }
13
+ impl Add for Sum {
14
+ type Output = Self ;
14
15
15
- /// Adds `n` to the sum. The type (Integer or Float) of the Sum is unchanged.
16
- fn add_integer ( & mut self , n : i128 ) {
17
- * self = match * self {
18
- Sum :: Integer ( i) => Sum :: Integer ( i + n) ,
19
- Sum :: Float ( f) => Sum :: Float ( f + n as f64 ) ,
20
- } ;
16
+ /// Adds two Sums. If either is a Float, the result will be a Float.
17
+ fn add ( self , other : Self ) -> Self {
18
+ match ( self , other) {
19
+ ( Sum :: Integer ( a) , Sum :: Integer ( b) ) => Sum :: Integer ( a + b) ,
20
+ ( Sum :: Float ( a) , Sum :: Float ( b) ) => Sum :: Float ( a + b) ,
21
+ ( Sum :: Integer ( a) , Sum :: Float ( b) ) => Sum :: Float ( a as f64 + b) ,
22
+ ( Sum :: Float ( a) , Sum :: Integer ( b) ) => Sum :: Float ( a + b as f64 ) ,
23
+ }
21
24
}
25
+ }
22
26
23
- /// Adds `n` to the sum. If the type is Intger, it's changed to Float after this operation.
24
- fn add_float ( & mut self , n : f64 ) {
25
- * self = match * self {
26
- Sum :: Integer ( i ) => Sum :: Float ( i as f64 + n ) ,
27
- Sum :: Float ( f ) => Sum :: Float ( f + n ) ,
28
- } ;
27
+ impl fmt :: Display for Sum {
28
+ fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
29
+ match self {
30
+ Sum :: Integer ( n ) => write ! ( f , "{n}" ) ,
31
+ Sum :: Float ( n ) => write ! ( f , "{n}" ) ,
32
+ }
29
33
}
30
34
}
31
35
32
- impl Default for Sum {
33
- fn default ( ) -> Self {
34
- Self :: new ( )
36
+ impl fmt:: UpperHex for Sum {
37
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
38
+ match self {
39
+ Sum :: Integer ( n) => fmt:: UpperHex :: fmt ( n, f) ,
40
+ Sum :: Float ( n) => fmt:: Display :: fmt ( n, f) ,
41
+ }
35
42
}
36
43
}
37
44
@@ -40,30 +47,24 @@ mod tests {
40
47
use super :: * ;
41
48
42
49
#[ test]
43
- fn default_works ( ) {
44
- assert_eq ! ( Sum :: new( ) , Default :: default ( ) ) ;
45
- }
46
-
47
- #[ test]
48
- fn integer_sum_works ( ) {
49
- let mut sum = Sum :: Integer ( 0 ) ;
50
- assert_eq ! ( sum, Sum :: Integer ( 0 ) ) ;
51
- sum. add_integer ( 2 ) ;
52
- assert_eq ! ( sum, Sum :: Integer ( 2 ) ) ;
50
+ fn sum_integer_works ( ) {
51
+ let a = Sum :: Integer ( 1 ) ;
52
+ let b = Sum :: Integer ( 2 ) ;
53
+ assert_eq ! ( a + b, Sum :: Integer ( 3 ) ) ;
53
54
}
54
55
55
56
#[ test]
56
- fn float_sum_works ( ) {
57
- let mut sum = Sum :: Float ( 1.2 ) ;
58
- assert_eq ! ( sum, Sum :: Float ( 1.2 ) ) ;
59
- sum. add_float ( 0.7 ) ;
60
- assert_eq ! ( sum, Sum :: Float ( 1.9 ) ) ;
57
+ fn sum_float_works ( ) {
58
+ let a = Sum :: Float ( 0.2 ) ;
59
+ let b = Sum :: Float ( 0.8 ) ;
60
+ assert_eq ! ( a + b, Sum :: Float ( 1.0 ) ) ;
61
61
}
62
62
63
63
#[ test]
64
- fn mixed_sum_works ( ) {
65
- let mut sum = Sum :: Integer ( 1 ) ;
66
- sum. add_float ( 0.2 ) ;
67
- assert_eq ! ( sum, Sum :: Float ( 1.2 ) ) ;
64
+ fn sum_mixed_works ( ) {
65
+ let a = Sum :: Integer ( 1 ) ;
66
+ let b = Sum :: Float ( 0.2 ) ;
67
+ assert_eq ! ( a + b, Sum :: Float ( 1.2 ) ) ;
68
+ assert_eq ! ( b + a, Sum :: Float ( 1.2 ) ) ;
68
69
}
69
70
}
0 commit comments