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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Addition operator trait implementation
//!

use super::*;
use stdlib::borrow::ToOwned;


impl Add<BigDecimal> for BigDecimal {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimal) -> BigDecimal {
        arithmetic::addition::add_bigdecimals(self, rhs)
    }
}

impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for BigDecimal {
    type Output = BigDecimal;

    fn add(mut self, rhs: T) -> BigDecimal {
        self.add_assign(rhs);
        self
    }
}

impl Add<BigInt> for BigDecimal {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigInt) -> BigDecimal {
        self + BigDecimal::from(rhs)
    }
}



impl Add<BigDecimal> for &'_ BigDecimal {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimal) -> BigDecimal {
        rhs + self
    }
}

impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for &'_ BigDecimal {
    type Output = BigDecimal;
    fn add(self, rhs: T) -> BigDecimal {
        arithmetic::addition::add_bigdecimal_refs(self, rhs)
    }
}

impl Add<BigInt> for &'_ BigDecimal {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigInt) -> BigDecimal {
        self.to_ref() + rhs
    }
}


impl Add<BigDecimal> for BigDecimalRef<'_> {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimal) -> BigDecimal {
        rhs + self
    }
}

impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for BigDecimalRef<'_> {
    type Output = BigDecimal;
    fn add(self, rhs: T) -> BigDecimal {
        arithmetic::addition::add_bigdecimal_refs(self, rhs)
    }
}

impl Add<BigInt> for BigDecimalRef<'_> {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigInt) -> BigDecimal {
        self + BigDecimal::from(rhs)
    }
}


impl Add<BigDecimal> for BigInt {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimal) -> BigDecimal {
        BigDecimal::from(self) + rhs
    }
}

impl<'a> Add<&'a BigDecimal> for BigInt {
    type Output = BigDecimal;

    fn add(self, rhs: &BigDecimal) -> BigDecimal {
        BigDecimal::from(self) + rhs
    }
}

impl<'a> Add<BigDecimalRef<'a>> for BigInt {
    type Output = BigDecimal;

    fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal {
        BigDecimal::from(self) + rhs
    }
}


impl Add<BigDecimal> for &BigInt {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimal) -> BigDecimal {
        rhs + self
    }
}

impl<'a> Add<&'a BigDecimal> for &BigInt {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: &BigDecimal) -> BigDecimal {
        rhs + self
    }
}

impl<'a> Add<BigDecimalRef<'a>> for &BigInt {
    type Output = BigDecimal;

    #[inline]
    fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal {
        rhs + self
    }
}


impl AddAssign<BigDecimal> for BigDecimal {
    fn add_assign(&mut self, rhs: BigDecimal) {
        arithmetic::addition::addassign_bigdecimals(self, rhs)
    }
}

impl<'a, N: Into<BigDecimalRef<'a>>> AddAssign<N> for BigDecimal {
    #[inline]
    fn add_assign(&mut self, rhs: N) {
        arithmetic::addition::addassign_bigdecimal_ref(self, rhs)
    }
}

impl AddAssign<BigInt> for BigDecimal {
    #[inline]
    fn add_assign(&mut self, rhs: BigInt) {
        self.add_assign(BigDecimal::from(rhs));
    }
}


#[cfg(test)]
mod test {
    use super::*;
    use paste::paste;

    macro_rules! impl_case {
        ( $name:ident: $a:literal + $b:literal => $c:literal ) => {
            #[test]
            fn $name() {
                let a: BigDecimal = $a.parse().unwrap();
                let b: BigDecimal = $b.parse().unwrap();
                let c: BigDecimal = $c.parse().unwrap();

                assert_eq!(c, a.clone() + b.clone());
                assert_eq!(c, a.clone() + b.to_ref());
                assert_eq!(c, a.clone() + &b);

                assert_eq!(c, &a + b.clone());
                assert_eq!(c, &a + b.to_ref());
                assert_eq!(c, &a + &b);

                assert_eq!(c, a.to_ref() + b.clone());
                assert_eq!(c, a.to_ref() + b.to_ref());
                assert_eq!(c, a.to_ref() + &b);

                // Reversed

                assert_eq!(c, b.clone() + a.clone());
                assert_eq!(c, b.clone() + a.to_ref());
                assert_eq!(c, b.clone() + &a);

                assert_eq!(c, &b + a.clone());
                assert_eq!(c, &b + a.to_ref());
                assert_eq!(c, &b + &a);

                assert_eq!(c, b.to_ref() + a.clone());
                assert_eq!(c, b.to_ref() + a.to_ref());
                assert_eq!(c, b.to_ref() + &a);

                let mut n = a.clone();
                n += b.clone();
                assert_eq!(c, n);

                let mut n = a.clone();
                n += &b;
                assert_eq!(c, n);

                let mut n = a.clone();
                n += b.to_ref();
                assert_eq!(c, n);

                let mut n = b.clone();
                n += a.clone();
                assert_eq!(c, n);

                let mut n = b.clone();
                n += &a;
                assert_eq!(c, n);

                let mut n = b.clone();
                n += a.to_ref();
                assert_eq!(c, n);
            }
        };
        ( $name:ident: $a:literal + (int) $b:literal => $c:literal ) => {
            #[test]
            fn $name() {
                let a: BigDecimal = $a.parse().unwrap();
                let b: BigInt = $b.parse().unwrap();
                let c: BigDecimal = $c.parse().unwrap();

                assert_eq!(c, a.clone() + b.clone());
                assert_eq!(c, a.clone() + &b);
                assert_eq!(c, &a + &b);
                assert_eq!(c, &a + b.clone());
                assert_eq!(c, a.to_ref() + &b);

                assert_eq!(c, b.clone() + a.clone());
                assert_eq!(c, b.clone() + a.to_ref());
                assert_eq!(c, b.clone() + &a);

                assert_eq!(c, &b + a.clone());
                assert_eq!(c, &b + a.to_ref());
                assert_eq!(c, &b + &a);

                let mut n = a.clone();
                n += b.clone();
                assert_eq!(c, n);

                let mut n = a.clone();
                n += &b;
                assert_eq!(c, n);
            }
        };
    }

    impl_case!(case_1234en2_1234en3: "12.34" + "1.234" => "13.574");
    impl_case!(case_1234en2_n1234en3: "12.34" + "-1.234" => "11.106");
    impl_case!(case_1234en2_n1234en2: "12.34" + "-12.34" => "0");
    impl_case!(case_1234e6_1234en6: "1234e6" + "1234e-6" => "1234000000.001234");
    impl_case!(case_1234en6_1234e6: "1234e6" + "1234e-6" => "1234000000.001234");
    impl_case!(case_18446744073709551616_1: "18446744073709551616.0" + "1" => "18446744073709551617");
    impl_case!(case_184467440737e3380_0: "184467440737e3380" + "0" => "184467440737e3380");
    impl_case!(case_0_776en1: "0" + "77.6" => "77.6");

    impl_case!(case_80802295e5_int0: "80802295e5" + (int)"0" => "80802295e5");
    impl_case!(case_239200en4_intneg101: "23.9200" + (int)"-101" => "-77.0800");
    impl_case!(case_46636423395767125en15_int0: "46.636423395767125" + (int)"123" => "169.636423395767125");


    #[cfg(property_tests)]
    mod prop {
        use super::*;
        use proptest::*;
        use num_traits::FromPrimitive;

        proptest! {
            #[test]
            fn add_refs_and_owners(f: f32, g: f32) {
                // ignore non-normal numbers
                prop_assume!(f.is_normal());
                prop_assume!(g.is_normal());

                let a = BigDecimal::from_f32(f).unwrap();
                let b = BigDecimal::from_f32(g).unwrap();
                let own_plus_ref = a.clone() + &b;
                let ref_plus_own = &a + b.clone();

                let mut c = a.clone();
                c += &b;

                let mut d = a.clone();
                d += b;

                prop_assert_eq!(&own_plus_ref, &ref_plus_own);
                prop_assert_eq!(&c, &ref_plus_own);
                prop_assert_eq!(&d, &ref_plus_own);
            }

            #[test]
            fn addition_is_communative(f: f32, g: f32) {
                // ignore non-normal numbers
                prop_assume!(f.is_normal());
                prop_assume!(g.is_normal());

                let a = BigDecimal::from_f32(f).unwrap();
                let b = BigDecimal::from_f32(g).unwrap();
                let a_plus_b = &a + &b;
                let b_plus_a = &b + &a;

                prop_assert_eq!(a_plus_b, b_plus_a)
            }

            #[test]
            fn addition_is_associative(f: f32, g: f32, h: f32) {
                // ignore non-normal numbers
                prop_assume!(f.is_normal());
                prop_assume!(g.is_normal());
                prop_assume!(h.is_normal());

                let a = BigDecimal::from_f32(f).unwrap();
                let b = BigDecimal::from_f32(g).unwrap();
                let c = BigDecimal::from_f32(h).unwrap();

                let ab = &a + &b;
                let ab_c = ab + &c;

                let bc = &b + &c;
                let a_bc = a + bc;

                prop_assert_eq!(ab_c, a_bc)
            }
        }
    }
}