• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gluesql / gluesql / 19007449208

02 Nov 2025 04:42AM UTC coverage: 97.993% (-0.02%) from 98.015%
19007449208

push

github

web-flow
Merge pull request #1833 from moreal/clippy-manual-let-else

Resolves more clippy rules

clippy::manual_let_else
clippy::manual_string_new
clippy::manual_assert
clippy::from_iter_instead_of_collect
clippy::implicit_clone
clippy::inefficient_to_string
clippy::map_unwrap_or
clippy::needless_continue

279 of 304 new or added lines in 59 files covered. (91.78%)

2 existing lines in 2 files now uncovered.

39502 of 40311 relevant lines covered (97.99%)

73228.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.92
/core/src/data/value/binary_op/f64.rs
1
use {
2
    super::TryBinaryOperator,
3
    crate::{
4
        data::{NumericBinaryOperator, ValueError},
5
        prelude::Value,
6
        result::Result,
7
    },
8
    Value::*,
9
    rust_decimal::prelude::Decimal,
10
    std::cmp::Ordering,
11
};
12

13
impl PartialEq<Value> for f64 {
14
    fn eq(&self, other: &Value) -> bool {
48✔
15
        let lhs = *self;
48✔
16

17
        match *other {
48✔
18
            I8(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
19
            I16(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
20
            I32(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
21
            I64(rhs) => (lhs - (rhs as f64)).abs() < f64::EPSILON,
2✔
22
            I128(rhs) => (lhs - (rhs as f64)).abs() < f64::EPSILON,
1✔
23
            U8(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
24
            U16(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
25
            U32(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
1✔
26
            U64(rhs) => (lhs - (rhs as f64)).abs() < f64::EPSILON,
1✔
27
            U128(rhs) => (lhs - (rhs as f64)).abs() < f64::EPSILON,
1✔
28
            F32(rhs) => (lhs - f64::from(rhs)).abs() < f64::EPSILON,
8✔
29
            F64(rhs) => (lhs - rhs).abs() < f64::EPSILON,
27✔
30
            Decimal(rhs) => Decimal::from_f64_retain(lhs).is_some_and(|x| rhs == x),
1✔
31
            _ => false,
1✔
32
        }
33
    }
48✔
34
}
35

36
impl PartialOrd<Value> for f64 {
37
    fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
15✔
38
        match *other {
15✔
39
            I8(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
40
            I16(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
41
            I32(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
42
            I64(rhs) => self.partial_cmp(&(rhs as f64)),
1✔
43
            I128(rhs) => self.partial_cmp(&(rhs as f64)),
1✔
44
            U8(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
45
            U16(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
46
            U32(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
47
            U64(rhs) => self.partial_cmp(&(rhs as f64)),
1✔
48
            U128(rhs) => self.partial_cmp(&(rhs as f64)),
1✔
49
            F32(rhs) => self.partial_cmp(&f64::from(rhs)),
1✔
50
            F64(rhs) => self.partial_cmp(&rhs),
2✔
51
            Decimal(rhs) => Decimal::from_f64_retain(*self).and_then(|x| x.partial_cmp(&rhs)),
1✔
52
            _ => None,
1✔
53
        }
54
    }
15✔
55
}
56

57
impl TryBinaryOperator for f64 {
58
    type Rhs = Value;
59

60
    fn try_add(&self, rhs: &Self::Rhs) -> Result<Value> {
1,239✔
61
        let lhs = *self;
1,239✔
62

63
        match *rhs {
1,239✔
64
            I8(rhs) => Ok(F64(lhs + f64::from(rhs))),
2✔
65
            I16(rhs) => Ok(F64(lhs + f64::from(rhs))),
1✔
66
            I32(rhs) => Ok(F64(lhs + f64::from(rhs))),
2✔
67
            I64(rhs) => Ok(F64(lhs + rhs as f64)),
1,220✔
68
            I128(rhs) => Ok(F64(lhs + rhs as f64)),
1✔
69
            U8(rhs) => Ok(F64(lhs + f64::from(rhs))),
2✔
70
            U16(rhs) => Ok(F64(lhs + f64::from(rhs))),
1✔
71
            U32(rhs) => Ok(F64(lhs + f64::from(rhs))),
1✔
72
            U64(rhs) => Ok(F64(lhs + rhs as f64)),
1✔
73
            U128(rhs) => Ok(F64(lhs + rhs as f64)),
1✔
74
            F32(rhs) => Ok(F64(lhs + f64::from(rhs))),
2✔
75
            F64(rhs) => Ok(F64(lhs + rhs)),
2✔
76
            Decimal(rhs) => Decimal::from_f64_retain(lhs).map_or_else(
1✔
NEW
77
                || Err(ValueError::FloatToDecimalConversionFailure(lhs).into()),
×
78
                |x| Ok(Decimal(x + rhs)),
1✔
79
            ),
80
            Null => Ok(Null),
1✔
81
            _ => Err(ValueError::NonNumericMathOperation {
1✔
82
                lhs: F64(lhs),
1✔
83
                operator: NumericBinaryOperator::Add,
1✔
84
                rhs: rhs.clone(),
1✔
85
            }
1✔
86
            .into()),
1✔
87
        }
88
    }
1,239✔
89

90
    fn try_subtract(&self, rhs: &Self::Rhs) -> Result<Value> {
10,982✔
91
        let lhs = *self;
10,982✔
92

93
        match *rhs {
10,982✔
94
            I8(rhs) => Ok(F64(lhs - f64::from(rhs))),
2✔
95
            I16(rhs) => Ok(F64(lhs - f64::from(rhs))),
1✔
96
            I32(rhs) => Ok(F64(lhs - f64::from(rhs))),
1✔
97
            I64(rhs) => Ok(F64(lhs - rhs as f64)),
10,964✔
98
            I128(rhs) => Ok(F64(lhs - rhs as f64)),
1✔
99
            U8(rhs) => Ok(F64(lhs - f64::from(rhs))),
2✔
100
            U16(rhs) => Ok(F64(lhs - f64::from(rhs))),
1✔
101
            U32(rhs) => Ok(F64(lhs - f64::from(rhs))),
1✔
102
            U64(rhs) => Ok(F64(lhs - rhs as f64)),
1✔
103
            U128(rhs) => Ok(F64(lhs - rhs as f64)),
1✔
104
            F32(rhs) => Ok(F64(lhs - f64::from(rhs))),
2✔
105
            F64(rhs) => Ok(F64(lhs - rhs)),
2✔
106
            Decimal(rhs) => Decimal::from_f64_retain(lhs).map_or_else(
1✔
NEW
107
                || Err(ValueError::FloatToDecimalConversionFailure(lhs).into()),
×
108
                |x| Ok(Decimal(x - rhs)),
1✔
109
            ),
110
            Null => Ok(Null),
1✔
111
            _ => Err(ValueError::NonNumericMathOperation {
1✔
112
                lhs: F64(lhs),
1✔
113
                operator: NumericBinaryOperator::Subtract,
1✔
114
                rhs: rhs.clone(),
1✔
115
            }
1✔
116
            .into()),
1✔
117
        }
118
    }
10,982✔
119

120
    fn try_multiply(&self, rhs: &Self::Rhs) -> Result<Value> {
6,113✔
121
        let lhs = *self;
6,113✔
122

123
        match *rhs {
6,113✔
124
            I8(rhs) => Ok(F64(lhs * f64::from(rhs))),
2✔
125
            I16(rhs) => Ok(F64(lhs * f64::from(rhs))),
1✔
126
            I32(rhs) => Ok(F64(lhs * f64::from(rhs))),
2✔
127
            I64(rhs) => Ok(F64(lhs * rhs as f64)),
6,092✔
128
            I128(rhs) => Ok(F64(lhs * rhs as f64)),
2✔
129
            U8(rhs) => Ok(F64(lhs * f64::from(rhs))),
2✔
130
            U16(rhs) => Ok(F64(lhs * f64::from(rhs))),
1✔
131
            U32(rhs) => Ok(F64(lhs * f64::from(rhs))),
1✔
132
            U64(rhs) => Ok(F64(lhs * rhs as f64)),
1✔
133
            U128(rhs) => Ok(F64(lhs * rhs as f64)),
1✔
134
            F32(rhs) => Ok(F64(lhs * f64::from(rhs))),
2✔
135
            F64(rhs) => Ok(F64(lhs * rhs)),
2✔
136
            Interval(rhs) => Ok(Interval(lhs * rhs)),
1✔
137
            Decimal(rhs) => Decimal::from_f64_retain(lhs).map_or_else(
1✔
NEW
138
                || Err(ValueError::FloatToDecimalConversionFailure(lhs).into()),
×
139
                |x| Ok(Decimal(x * rhs)),
1✔
140
            ),
141
            Null => Ok(Null),
1✔
142
            _ => Err(ValueError::NonNumericMathOperation {
1✔
143
                lhs: F64(lhs),
1✔
144
                operator: NumericBinaryOperator::Multiply,
1✔
145
                rhs: rhs.clone(),
1✔
146
            }
1✔
147
            .into()),
1✔
148
        }
149
    }
6,113✔
150

151
    fn try_divide(&self, rhs: &Self::Rhs) -> Result<Value> {
18,294✔
152
        let lhs = *self;
18,294✔
153

154
        match *rhs {
18,294✔
155
            I8(rhs) => Ok(F64(lhs / f64::from(rhs))),
2✔
156
            I16(rhs) => Ok(F64(lhs / f64::from(rhs))),
2✔
157
            I32(rhs) => Ok(F64(lhs / f64::from(rhs))),
2✔
158
            I64(rhs) => Ok(F64(lhs / rhs as f64)),
18,272✔
159
            I128(rhs) => Ok(F64(lhs / rhs as f64)),
2✔
160
            U8(rhs) => Ok(F64(lhs / f64::from(rhs))),
2✔
161
            U16(rhs) => Ok(F64(lhs / f64::from(rhs))),
1✔
162
            U32(rhs) => Ok(F64(lhs / f64::from(rhs))),
1✔
163
            U64(rhs) => Ok(F64(lhs / rhs as f64)),
1✔
164
            U128(rhs) => Ok(F64(lhs / rhs as f64)),
1✔
165
            F32(rhs) => Ok(F64(lhs / f64::from(rhs))),
4✔
166
            F64(rhs) => Ok(F64(lhs / rhs)),
1✔
167
            Decimal(rhs) => Decimal::from_f64_retain(lhs).map_or_else(
1✔
NEW
168
                || Err(ValueError::FloatToDecimalConversionFailure(lhs).into()),
×
169
                |x| Ok(Decimal(x * rhs)),
1✔
170
            ),
171
            Null => Ok(Null),
1✔
172
            _ => Err(ValueError::NonNumericMathOperation {
1✔
173
                lhs: F64(lhs),
1✔
174
                operator: NumericBinaryOperator::Divide,
1✔
175
                rhs: rhs.clone(),
1✔
176
            }
1✔
177
            .into()),
1✔
178
        }
179
    }
18,294✔
180

181
    fn try_modulo(&self, rhs: &Self::Rhs) -> Result<Value> {
3,673✔
182
        let lhs = *self;
3,673✔
183

184
        match *rhs {
3,673✔
185
            I8(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
186
            I16(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
187
            I32(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
188
            I64(rhs) => Ok(F64(lhs % rhs as f64)),
2✔
189
            I128(rhs) => Ok(F64(lhs % rhs as f64)),
1✔
190
            U8(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
191
            U16(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
192
            U32(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
193
            U64(rhs) => Ok(F64(lhs % rhs as f64)),
1✔
194
            U128(rhs) => Ok(F64(lhs % rhs as f64)),
1✔
195
            F32(rhs) => Ok(F64(lhs % f64::from(rhs))),
1✔
196
            F64(rhs) => Ok(F64(lhs % rhs)),
3,656✔
197
            Decimal(rhs) => match Decimal::from_f64_retain(lhs) {
3✔
198
                Some(x) => x.checked_rem(rhs).map_or_else(
2✔
199
                    || {
1✔
200
                        Err(ValueError::BinaryOperationOverflow {
1✔
201
                            lhs: F64(lhs),
1✔
202
                            operator: NumericBinaryOperator::Modulo,
1✔
203
                            rhs: Decimal(rhs),
1✔
204
                        }
1✔
205
                        .into())
1✔
206
                    },
1✔
207
                    |y| Ok(Decimal(y)),
1✔
208
                ),
209
                _ => Err(ValueError::FloatToDecimalConversionFailure(lhs).into()),
1✔
210
            },
211
            Null => Ok(Null),
1✔
212
            _ => Err(ValueError::NonNumericMathOperation {
1✔
213
                lhs: F64(lhs),
1✔
214
                operator: NumericBinaryOperator::Modulo,
1✔
215
                rhs: rhs.clone(),
1✔
216
            }
1✔
217
            .into()),
1✔
218
        }
219
    }
3,673✔
220
}
221

222
#[cfg(test)]
223
mod tests {
224
    use {
225
        super::{TryBinaryOperator, Value::*},
226
        crate::data::{NumericBinaryOperator, ValueError},
227
        rust_decimal::prelude::Decimal,
228
        std::cmp::Ordering,
229
    };
230

231
    #[test]
232
    fn eq() {
1✔
233
        let base = 1.0_f64;
1✔
234

235
        assert_eq!(base, I8(1));
1✔
236
        assert_eq!(base, I16(1));
1✔
237
        assert_eq!(base, I32(1));
1✔
238
        assert_eq!(base, I64(1));
1✔
239
        assert_eq!(base, I128(1));
1✔
240
        assert_eq!(base, U8(1));
1✔
241
        assert_eq!(base, U16(1));
1✔
242
        assert_eq!(base, U32(1));
1✔
243
        assert_eq!(base, U64(1));
1✔
244
        assert_eq!(base, U128(1));
1✔
245
        assert_eq!(base, F32(1.0_f32));
1✔
246
        assert_eq!(base, F64(1.0));
1✔
247
        assert_eq!(base, Decimal(Decimal::from(1)));
1✔
248

249
        assert_ne!(base, Bool(true));
1✔
250
    }
1✔
251

252
    #[test]
253
    fn partial_cmp() {
1✔
254
        let base = 1.0_f64;
1✔
255

256
        assert_eq!(base.partial_cmp(&I8(1)), Some(Ordering::Equal));
1✔
257
        assert_eq!(base.partial_cmp(&I16(1)), Some(Ordering::Equal));
1✔
258
        assert_eq!(base.partial_cmp(&I32(1)), Some(Ordering::Equal));
1✔
259
        assert_eq!(base.partial_cmp(&I64(1)), Some(Ordering::Equal));
1✔
260
        assert_eq!(base.partial_cmp(&I128(1)), Some(Ordering::Equal));
1✔
261
        assert_eq!(base.partial_cmp(&U8(1)), Some(Ordering::Equal));
1✔
262
        assert_eq!(base.partial_cmp(&U16(1)), Some(Ordering::Equal));
1✔
263
        assert_eq!(base.partial_cmp(&U32(1)), Some(Ordering::Equal));
1✔
264
        assert_eq!(base.partial_cmp(&U64(1)), Some(Ordering::Equal));
1✔
265
        assert_eq!(base.partial_cmp(&U128(1)), Some(Ordering::Equal));
1✔
266
        assert_eq!(base.partial_cmp(&F32(1.0_f32)), Some(Ordering::Equal));
1✔
267
        assert_eq!(base.partial_cmp(&F64(1.0)), Some(Ordering::Equal));
1✔
268
        assert_eq!(
1✔
269
            base.partial_cmp(&Decimal(Decimal::ONE)),
1✔
270
            Some(Ordering::Equal)
271
        );
272

273
        assert_eq!(base.partial_cmp(&Bool(true)), None);
1✔
274
    }
1✔
275

276
    #[test]
277
    fn try_add() {
1✔
278
        let base = 1.0_f64;
1✔
279

280
        assert!(matches!(base.try_add(&I8(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
281
        assert!(matches!(base.try_add(&I16(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
282
        assert!(matches!(base.try_add(&I32(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
283
        assert!(matches!(base.try_add(&I64(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
284
        assert!(matches!(base.try_add(&I128(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
285
        assert!(matches!(base.try_add(&U8(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
286
        assert!(matches!(base.try_add(&U16(1)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
287
        assert!(matches!(base.try_add(&U32(1)),Ok(F64(x)) if (x-2.0).abs() < f64::EPSILON));
1✔
288
        assert!(matches!(base.try_add(&U64(1)),Ok(F64(x)) if (x-2.0).abs() < f64::EPSILON));
1✔
289
        assert!(matches!(base.try_add(&U128(1)),Ok(F64(x)) if (x-2.0).abs()<f64::EPSILON));
1✔
290
        assert!(
1✔
291
            matches!(base.try_add(&F32(1.0_f32)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON )
1✔
292
        );
293
        assert!(matches!(base.try_add(&F64(1.0)), Ok(F64(x)) if (x - 2.0).abs() < f64::EPSILON ));
1✔
294
        assert!(
1✔
295
            matches!(base.try_add(&Decimal(Decimal::ONE)), Ok(Decimal(x)) if x == Decimal::TWO)
1✔
296
        );
297

298
        assert_eq!(
1✔
299
            base.try_add(&Bool(true)),
1✔
300
            Err(ValueError::NonNumericMathOperation {
1✔
301
                lhs: F64(1.0),
1✔
302
                operator: NumericBinaryOperator::Add,
1✔
303
                rhs: Bool(true)
1✔
304
            }
1✔
305
            .into())
1✔
306
        );
307
    }
1✔
308

309
    #[test]
310
    fn try_subtract() {
1✔
311
        let base = 1.0_f64;
1✔
312

313
        assert!(matches!(base.try_subtract(&I8(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
314
        assert!(
1✔
315
            matches!(base.try_subtract(&I16(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
316
        );
317
        assert!(
1✔
318
            matches!(base.try_subtract(&I32(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
319
        );
320
        assert!(
1✔
321
            matches!(base.try_subtract(&I64(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
322
        );
323
        assert!(
1✔
324
            matches!(base.try_subtract(&I128(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
325
        );
326
        assert!(matches!(base.try_subtract(&U8(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
327
        assert!(
1✔
328
            matches!(base.try_subtract(&U16(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
329
        );
330
        assert!(
1✔
331
            matches!(base.try_subtract(&U32(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
332
        );
333

334
        assert!(
1✔
335
            matches!(base.try_subtract(&U64(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
336
        );
337
        assert!(
1✔
338
            matches!(base.try_subtract(&U128(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
339
        );
340

341
        assert!(
1✔
342
            matches!(base.try_subtract(&F32(1.0_f32)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
343
        );
344
        assert!(
1✔
345
            matches!(base.try_subtract(&F64(1.0)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
346
        );
347
        assert!(
1✔
348
            matches!(base.try_subtract(&Decimal(Decimal::ONE)), Ok(Decimal(x)) if x == Decimal::ZERO)
1✔
349
        );
350

351
        assert_eq!(
1✔
352
            base.try_subtract(&Bool(true)),
1✔
353
            Err(ValueError::NonNumericMathOperation {
1✔
354
                lhs: F64(1.0),
1✔
355
                operator: NumericBinaryOperator::Subtract,
1✔
356
                rhs: Bool(true)
1✔
357
            }
1✔
358
            .into())
1✔
359
        );
360
    }
1✔
361

362
    #[test]
363
    fn try_multiply() {
1✔
364
        let base = 1.0_f64;
1✔
365

366
        assert!(matches!(base.try_multiply(&I8(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
367
        assert!(
1✔
368
            matches!(base.try_multiply(&I16(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
369
        );
370
        assert!(
1✔
371
            matches!(base.try_multiply(&I32(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
372
        );
373
        assert!(
1✔
374
            matches!(base.try_multiply(&I64(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
375
        );
376
        assert!(
1✔
377
            matches!(base.try_multiply(&I128(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
378
        );
379
        assert!(matches!(base.try_multiply(&U8(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
380
        assert!(
1✔
381
            matches!(base.try_multiply(&U16(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
382
        );
383
        assert!(
1✔
384
            matches!(base.try_multiply(&U32(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
385
        );
386
        assert!(
1✔
387
            matches!(base.try_multiply(&U64(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
388
        );
389
        assert!(
1✔
390
            matches!(base.try_multiply(&U128(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
391
        );
392
        assert!(
1✔
393
            matches!(base.try_multiply(&F32(1.0_f32)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
394
        );
395
        assert!(
1✔
396
            matches!(base.try_multiply(&F64(1.0)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
397
        );
398
        assert!(
1✔
399
            matches!(base.try_multiply(&Decimal(Decimal::ONE)), Ok(Decimal(x)) if x == Decimal::ONE)
1✔
400
        );
401

402
        assert_eq!(
1✔
403
            base.try_multiply(&Bool(true)),
1✔
404
            Err(ValueError::NonNumericMathOperation {
1✔
405
                lhs: F64(1.0),
1✔
406
                operator: NumericBinaryOperator::Multiply,
1✔
407
                rhs: Bool(true)
1✔
408
            }
1✔
409
            .into())
1✔
410
        );
411
    }
1✔
412

413
    #[test]
414
    fn try_divide() {
1✔
415
        let base = 1.0_f64;
1✔
416

417
        assert!(matches!(base.try_divide(&I8(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
418
        assert!(matches!(base.try_divide(&I16(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
419
        assert!(matches!(base.try_divide(&I32(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
420
        assert!(matches!(base.try_divide(&I64(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
421
        assert!(matches!(base.try_divide(&I128(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
422
        assert!(matches!(base.try_divide(&U8(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
423
        assert!(matches!(base.try_divide(&U16(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
424
        assert!(matches!(base.try_divide(&U32(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
425
        assert!(matches!(base.try_divide(&U64(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
426
        assert!(matches!(base.try_divide(&U128(1)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON ));
1✔
427

428
        assert!(
1✔
429
            matches!(base.try_divide(&F32(1.0_f32)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
430
        );
431
        assert!(
1✔
432
            matches!(base.try_divide(&F64(1.0)), Ok(F64(x)) if (x - 1.0).abs() < f64::EPSILON )
1✔
433
        );
434
        assert!(
1✔
435
            matches!(base.try_divide(&Decimal(Decimal::ONE)), Ok(Decimal(x)) if x == Decimal::ONE)
1✔
436
        );
437

438
        assert_eq!(
1✔
439
            base.try_divide(&Bool(true)),
1✔
440
            Err(ValueError::NonNumericMathOperation {
1✔
441
                lhs: F64(1.0),
1✔
442
                operator: NumericBinaryOperator::Divide,
1✔
443
                rhs: Bool(true)
1✔
444
            }
1✔
445
            .into())
1✔
446
        );
447
    }
1✔
448

449
    #[test]
450
    fn try_modulo() {
1✔
451
        let base = 1.0_f64;
1✔
452

453
        assert!(matches!(base.try_modulo(&I8(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
454
        assert!(matches!(base.try_modulo(&I16(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
455
        assert!(matches!(base.try_modulo(&I32(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
456
        assert!(matches!(base.try_modulo(&I64(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
457
        assert!(matches!(base.try_modulo(&I128(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
458
        assert!(matches!(base.try_modulo(&U8(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
459
        assert!(matches!(base.try_modulo(&U16(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
460
        assert!(matches!(base.try_modulo(&U32(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
461
        assert!(matches!(base.try_modulo(&U64(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
462
        assert!(matches!(base.try_modulo(&U128(1)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON ));
1✔
463

464
        assert!(
1✔
465
            matches!(base.try_modulo(&F32(1.0_f32)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
466
        );
467
        assert!(
1✔
468
            matches!(base.try_modulo(&F64(1.0)), Ok(F64(x)) if (x - 0.0).abs() < f64::EPSILON )
1✔
469
        );
470
        assert!(
1✔
471
            matches!(base.try_modulo(&Decimal(Decimal::ONE)), Ok(Decimal(x)) if x == Decimal::ZERO)
1✔
472
        );
473
        assert_eq!(
1✔
474
            f64::MAX.try_modulo(&Decimal(Decimal::TWO)),
1✔
475
            Err(ValueError::FloatToDecimalConversionFailure(f64::MAX).into())
1✔
476
        );
477
        assert_eq!(
1✔
478
            base.try_modulo(&Decimal(Decimal::ZERO)),
1✔
479
            Err(ValueError::BinaryOperationOverflow {
1✔
480
                lhs: F64(base),
1✔
481
                rhs: Decimal(Decimal::ZERO),
1✔
482
                operator: NumericBinaryOperator::Modulo,
1✔
483
            }
1✔
484
            .into())
1✔
485
        );
486

487
        assert_eq!(
1✔
488
            base.try_modulo(&Bool(true)),
1✔
489
            Err(ValueError::NonNumericMathOperation {
1✔
490
                lhs: F64(1.0),
1✔
491
                operator: NumericBinaryOperator::Modulo,
1✔
492
                rhs: Bool(true)
1✔
493
            }
1✔
494
            .into())
1✔
495
        );
496
    }
1✔
497
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc