sizefilter 0.2.0

Human-readable size string parsing, formatting, and filtering with comparison operators (e.g., ">=1GB", "<500KB")
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Core types, constants, and parsing logic.
//!
//! All string constants in errors live in `.rodata` — no heap `String` allocation
//! in error paths. `parse_size` avoids `to_uppercase()` by using byte-level
//! case-insensitive comparison.

use std::error::Error;
use std::fmt;
use std::str::FromStr;

// ── unit constants ───────────────────────────────────────────────────────────

/// Bytes in one kibibyte (2¹⁰).
pub const KB: i64 = 1 << 10;
/// Bytes in one mebibyte (2²⁰).
pub const MB: i64 = 1 << 20;
/// Bytes in one gibibyte (2³⁰).
pub const GB: i64 = 1 << 30;
/// Bytes in one tebibyte (2⁴⁰).
pub const TB: i64 = 1 << 40;
/// Bytes in one pebibyte (2⁵⁰).
pub const PB: i64 = 1 << 50;
/// Bytes in one exbibyte (2⁶⁰).
pub const EB: i64 = 1 << 60;

// ── SizeOp ───────────────────────────────────────────────────────────────────

/// Size comparison operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SizeOp {
    /// Greater than (`>`)
    Gt,
    /// Greater than or equal to (`>=`)
    Ge,
    /// Less than (`<`)
    Lt,
    /// Less than or equal to (`<=`)
    Le,
    /// Equal to (`=`)
    Eq,
}

impl SizeOp {
    /// All variants, in declaration order.
    pub const ALL: [SizeOp; 5] = [SizeOp::Gt, SizeOp::Ge, SizeOp::Lt, SizeOp::Le, SizeOp::Eq];

    /// Apply this operator to two values.
    #[inline]
    #[must_use]
    pub fn applies(self, value: i64, threshold: i64) -> bool {
        match self {
            SizeOp::Gt => value > threshold,
            SizeOp::Ge => value >= threshold,
            SizeOp::Lt => value < threshold,
            SizeOp::Le => value <= threshold,
            SizeOp::Eq => value == threshold,
        }
    }
}

impl fmt::Display for SizeOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            SizeOp::Gt => ">",
            SizeOp::Ge => ">=",
            SizeOp::Lt => "<",
            SizeOp::Le => "<=",
            SizeOp::Eq => "=",
        })
    }
}

// ── SizeFilter ───────────────────────────────────────────────────────────────

/// A size filter with operator (e.g., `>=1GB`, `<500KB`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SizeFilter {
    op: SizeOp,
    bytes: i64,
}

impl SizeFilter {
    /// Create a new filter from an operator and byte threshold.
    #[inline]
    #[must_use]
    pub const fn new(op: SizeOp, bytes: i64) -> Self {
        SizeFilter { op, bytes }
    }

    /// Get the comparison operator.
    #[inline]
    #[must_use]
    pub const fn op(self) -> SizeOp {
        self.op
    }

    /// Get the byte threshold.
    #[inline]
    #[must_use]
    pub const fn bytes(self) -> i64 {
        self.bytes
    }

    /// Filter: `value > threshold`.
    #[inline]
    #[must_use]
    pub const fn gt(bytes: i64) -> Self {
        SizeFilter {
            op: SizeOp::Gt,
            bytes,
        }
    }

    /// Filter: `value >= threshold`.
    #[inline]
    #[must_use]
    pub const fn ge(bytes: i64) -> Self {
        SizeFilter {
            op: SizeOp::Ge,
            bytes,
        }
    }

    /// Filter: `value < threshold`.
    #[inline]
    #[must_use]
    pub const fn lt(bytes: i64) -> Self {
        SizeFilter {
            op: SizeOp::Lt,
            bytes,
        }
    }

    /// Filter: `value <= threshold`.
    #[inline]
    #[must_use]
    pub const fn le(bytes: i64) -> Self {
        SizeFilter {
            op: SizeOp::Le,
            bytes,
        }
    }

    /// Filter: `value == threshold`.
    #[inline]
    #[must_use]
    pub const fn eq(bytes: i64) -> Self {
        SizeFilter {
            op: SizeOp::Eq,
            bytes,
        }
    }

    /// Check whether `value` (in bytes) passes this filter.
    #[inline]
    #[must_use]
    pub fn matches(self, value: i64) -> bool {
        self.op.applies(value, self.bytes)
    }
}

impl fmt::Display for SizeFilter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.op, format_size(self.bytes))
    }
}

impl FromStr for SizeFilter {
    type Err = SizeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_size_filter(s)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for SizeFilter {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for SizeFilter {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

// ── Size newtype ─────────────────────────────────────────────────────────────

/// A byte size that can be parsed from / formatted to a human-readable string.
///
/// # Examples
///
/// ```
/// use sizefilter::{Size, GB};
///
/// let s: Size = "1.5GB".parse().unwrap();
/// assert_eq!(s.bytes(), 1_610_612_736);
/// assert_eq!(s.to_string(), "1.5GB");
///
/// // Arithmetic with constants
/// assert_eq!(s, Size::from_bytes(GB + GB / 2));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Size(i64);

impl Size {
    /// Zero bytes.
    pub const ZERO: Size = Size(0);

    /// Create a `Size` from a raw byte count.
    #[inline]
    #[must_use]
    pub const fn from_bytes(bytes: i64) -> Self {
        Size(bytes)
    }

    /// Create a `Size` from a quantity of kilobytes (binary: 1024).
    #[inline]
    #[must_use]
    pub const fn from_kb(kb: i64) -> Self {
        Size(kb * KB)
    }

    /// Create a `Size` from a quantity of megabytes (binary: 1024²).
    #[inline]
    #[must_use]
    pub const fn from_mb(mb: i64) -> Self {
        Size(mb * MB)
    }

    /// Create a `Size` from a quantity of gigabytes (binary: 1024³).
    #[inline]
    #[must_use]
    pub const fn from_gb(gb: i64) -> Self {
        Size(gb * GB)
    }

    /// Create a `Size` from a quantity of terabytes (binary: 1024⁴).
    #[inline]
    #[must_use]
    pub const fn from_tb(tb: i64) -> Self {
        Size(tb * TB)
    }

    /// Return the raw byte count.
    #[inline]
    #[must_use]
    pub const fn bytes(self) -> i64 {
        self.0
    }
}

impl From<i64> for Size {
    #[inline]
    fn from(v: i64) -> Self {
        Size(v)
    }
}

impl From<Size> for i64 {
    #[inline]
    fn from(s: Size) -> Self {
        s.0
    }
}

impl fmt::Display for Size {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&format_size(self.0))
    }
}

impl FromStr for Size {
    type Err = SizeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_size(s).map(Size)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Size {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Size {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

// ── arithmetic ──────────────────────────────────────────────────────────────

impl std::ops::Add for Size {
    type Output = Size;
    #[inline]
    fn add(self, rhs: Size) -> Size {
        Size(self.0 + rhs.0)
    }
}

impl std::ops::Add<i64> for Size {
    type Output = Size;
    #[inline]
    fn add(self, rhs: i64) -> Size {
        Size(self.0 + rhs)
    }
}

impl std::ops::Add<Size> for i64 {
    type Output = Size;
    #[inline]
    fn add(self, rhs: Size) -> Size {
        Size(self + rhs.0)
    }
}

impl std::ops::AddAssign for Size {
    #[inline]
    fn add_assign(&mut self, rhs: Size) {
        self.0 += rhs.0;
    }
}

impl std::ops::AddAssign<i64> for Size {
    #[inline]
    fn add_assign(&mut self, rhs: i64) {
        self.0 += rhs;
    }
}

impl std::ops::Sub for Size {
    type Output = Size;
    #[inline]
    fn sub(self, rhs: Size) -> Size {
        Size(self.0 - rhs.0)
    }
}

impl std::ops::Sub<i64> for Size {
    type Output = Size;
    #[inline]
    fn sub(self, rhs: i64) -> Size {
        Size(self.0 - rhs)
    }
}

impl std::ops::Sub<Size> for i64 {
    type Output = Size;
    #[inline]
    fn sub(self, rhs: Size) -> Size {
        Size(self - rhs.0)
    }
}

impl std::ops::SubAssign for Size {
    #[inline]
    fn sub_assign(&mut self, rhs: Size) {
        self.0 -= rhs.0;
    }
}

impl std::ops::SubAssign<i64> for Size {
    #[inline]
    fn sub_assign(&mut self, rhs: i64) {
        self.0 -= rhs;
    }
}

impl std::ops::Mul<i64> for Size {
    type Output = Size;
    #[inline]
    fn mul(self, rhs: i64) -> Size {
        Size(self.0 * rhs)
    }
}

impl std::ops::Mul<Size> for i64 {
    type Output = Size;
    #[inline]
    fn mul(self, rhs: Size) -> Size {
        Size(self * rhs.0)
    }
}

impl std::ops::MulAssign<i64> for Size {
    #[inline]
    fn mul_assign(&mut self, rhs: i64) {
        self.0 *= rhs;
    }
}

impl std::ops::Div<i64> for Size {
    type Output = Size;
    #[inline]
    fn div(self, rhs: i64) -> Size {
        Size(self.0 / rhs)
    }
}

impl std::ops::DivAssign<i64> for Size {
    #[inline]
    fn div_assign(&mut self, rhs: i64) {
        self.0 /= rhs;
    }
}

impl std::ops::Rem<i64> for Size {
    type Output = Size;
    #[inline]
    fn rem(self, rhs: i64) -> Size {
        Size(self.0 % rhs)
    }
}

impl std::ops::RemAssign<i64> for Size {
    #[inline]
    fn rem_assign(&mut self, rhs: i64) {
        self.0 %= rhs;
    }
}

impl std::ops::Neg for Size {
    type Output = Size;
    #[inline]
    fn neg(self) -> Size {
        Size(-self.0)
    }
}

// ── SizeError ────────────────────────────────────────────────────────────────

/// Errors that can occur during size parsing and formatting.
///
/// All variants carry zero heap-allocated data — error strings are
/// `&'static str` literals in `.rodata`.
///
/// This enum is `#[non_exhaustive]` — new variants may be added in
/// minor releases without breaking changes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SizeError {
    /// No operator found in filter string.
    MissingOperator,
    /// Unable to parse the numeric part of a size string.
    InvalidNumber,
    /// Unknown or unsupported size unit suffix.
    UnknownUnit,
    /// Empty input string.
    EmptyInput,
}

impl fmt::Display for SizeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            SizeError::MissingOperator => {
                "size filter must start with an operator (>=, >, <=, <, =)"
            }
            SizeError::InvalidNumber => "failed to parse number",
            SizeError::UnknownUnit => "unknown size unit",
            SizeError::EmptyInput => "empty input",
        })
    }
}

impl Error for SizeError {}

/// `Result` type alias for `sizefilter` operations.
pub type SizeResult<T> = Result<T, SizeError>;

// ── parsing ──────────────────────────────────────────────────────────────────

/// Parse a size filter string like `">=1GB"`, `"<500KB"`, `"=0"`.
///
/// Operator is required — returns error if missing.
///
/// # Errors
///
/// Returns [`SizeError::MissingOperator`] if no operator is found,
/// or [`SizeError`] variants from size parsing.
pub fn parse_size_filter(s: &str) -> SizeResult<SizeFilter> {
    let s = s.trim();
    let prefixes: &[(&str, SizeOp)] = &[
        (">=", SizeOp::Ge),
        ("<=", SizeOp::Le),
        (">", SizeOp::Gt),
        ("<", SizeOp::Lt),
        ("=", SizeOp::Eq),
    ];
    let (op, rest) = prefixes
        .iter()
        .find_map(|&(prefix, op)| s.strip_prefix(prefix).map(|r| (op, r)))
        .ok_or(SizeError::MissingOperator)?;
    let bytes = parse_size(rest)?;
    Ok(SizeFilter { op, bytes })
}

/// Parse human-readable size string to bytes.
///
/// Supports: `"1GB"`, `"500KB"`, `"1024"`, `"1B"`, `"1K"`, `"1M"`, `"1G"`, `"1T"`, `"1P"`, `"1E"`.
/// Uses binary units (1KB = 1024 bytes).
///
/// No heap allocation during parsing — unit comparison is done via
/// byte-level `eq_ignore_ascii_case`.
///
/// # Errors
///
/// Returns [`SizeError::InvalidNumber`] if the numeric part cannot be parsed,
/// or [`SizeError::UnknownUnit`] if the unit suffix is not recognized.
pub fn parse_size(size_str: &str) -> SizeResult<i64> {
    let size_str = size_str.trim();
    if size_str.is_empty() {
        return Err(SizeError::EmptyInput);
    }

    // Find split between number and alphabetic unit.  Negative sign "-" at
    // position 0 is part of the number, so skip it when searching.
    let search_start = usize::from(size_str.starts_with('-'));
    let alpha_pos = size_str[search_start..].find(|c: char| c.is_ascii_alphabetic());

    let (num_part, unit) = match alpha_pos {
        Some(pos) => size_str.split_at(search_start + pos),
        None => (size_str, ""),
    };

    let num_part = num_part.trim();

    // Reject purely alphabetic inputs (e.g., "abc") as invalid numbers
    if num_part.is_empty() {
        return Err(SizeError::InvalidNumber);
    }

    let multiplier = unit_multiplier(unit.trim()).ok_or(SizeError::UnknownUnit)?;

    // Parse using integer arithmetic to avoid floating-point precision issues.
    // Handle optional decimal point.
    let (is_negative, num_str) = if let Some(rest) = num_part.strip_prefix('-') {
        (true, rest)
    } else {
        (false, num_part)
    };

    let (int_part, frac_part, frac_digits) = if let Some(dot_pos) = num_str.find('.') {
        let int_str = &num_str[..dot_pos];
        let frac_str = &num_str[dot_pos + 1..];
        let int_val: i64 = if int_str.is_empty() {
            0
        } else {
            int_str.parse().map_err(|_| SizeError::InvalidNumber)?
        };
        let frac_val: i64 = frac_str.parse().map_err(|_| SizeError::InvalidNumber)?;
        let frac_digits = u32::try_from(frac_str.len()).map_err(|_| SizeError::InvalidNumber)?;
        (int_val, frac_val, frac_digits)
    } else {
        let int_val: i64 = num_str.parse().map_err(|_| SizeError::InvalidNumber)?;
        (int_val, 0, 0)
    };

    // Compute: (int_part * 10^frac_digits + frac_part) * multiplier / 10^frac_digits
    // Use i128 to avoid overflow during intermediate calculations.
    let scale = 10i64.pow(frac_digits);
    let numerator = i128::from(int_part) * i128::from(scale) + i128::from(frac_part);
    let result = numerator * i128::from(multiplier) / i128::from(scale);

    let result = i64::try_from(result).map_err(|_| SizeError::InvalidNumber)?;
    Ok(if is_negative { -result } else { result })
}

/// Map a unit string to its byte multiplier, or `None` if unknown.
///
/// Comparison is ASCII case-insensitive — no allocation.
fn unit_multiplier(unit: &str) -> Option<i64> {
    // Use eq_ignore_ascii_case to avoid heap allocation from to_ascii_uppercase()
    if unit.is_empty() || unit.eq_ignore_ascii_case("b") {
        Some(1)
    } else if unit.eq_ignore_ascii_case("k") || unit.eq_ignore_ascii_case("kb") {
        Some(KB)
    } else if unit.eq_ignore_ascii_case("m") || unit.eq_ignore_ascii_case("mb") {
        Some(MB)
    } else if unit.eq_ignore_ascii_case("g") || unit.eq_ignore_ascii_case("gb") {
        Some(GB)
    } else if unit.eq_ignore_ascii_case("t") || unit.eq_ignore_ascii_case("tb") {
        Some(TB)
    } else if unit.eq_ignore_ascii_case("p") || unit.eq_ignore_ascii_case("pb") {
        Some(PB)
    } else if unit.eq_ignore_ascii_case("e") || unit.eq_ignore_ascii_case("eb") {
        Some(EB)
    } else {
        None
    }
}

// ── formatting ───────────────────────────────────────────────────────────────

/// Format size (in bytes) to human-readable string.
///
/// Uses binary units: `B`, `KB`, `MB`, `GB`, `TB`, `PB`, `EB`.
/// The returned `String` is the output — unavoidable allocation.
#[must_use]
pub fn format_size(size: i64) -> String {
    const UNITS: &[(u64, &str)] = &[
        (1u64 << 60, "EB"),
        (1u64 << 50, "PB"),
        (1u64 << 40, "TB"),
        (1u64 << 30, "GB"),
        (1u64 << 20, "MB"),
        (1u64 << 10, "KB"),
    ];

    let abs = size.unsigned_abs();
    let prefix = if size < 0 { "-" } else { "" };

    UNITS
        .iter()
        .find(|&&(threshold, _)| abs >= threshold)
        .map_or_else(
            || format!("{prefix}{abs}B"),
            // Precision loss acceptable: only 1 decimal place displayed
            #[allow(clippy::cast_precision_loss)]
            |&(threshold, unit)| format!("{prefix}{:.1}{unit}", abs as f64 / threshold as f64),
        )
}

// ── tests ───────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_parse_pb() {
        assert_eq!(parse_size("1PB").unwrap(), PB);
        assert_eq!(parse_size("1P").unwrap(), PB);
        assert_eq!(parse_size("1pb").unwrap(), PB);
        assert_eq!(parse_size("2PB").unwrap(), 2 * PB);
    }

    #[test]
    fn test_parse_eb() {
        assert_eq!(parse_size("1EB").unwrap(), EB);
        assert_eq!(parse_size("1E").unwrap(), EB);
        assert_eq!(parse_size("1eb").unwrap(), EB);
        assert_eq!(parse_size("2EB").unwrap(), 2 * EB);
    }

    #[test]
    fn test_decimal_precision() {
        // Test that integer arithmetic avoids floating-point precision issues
        // 1.5GB should be exactly 1.5 * 1073741824 = 1610612736
        assert_eq!(parse_size("1.5GB").unwrap(), 1_610_612_736);
        assert_eq!(parse_size("0.5KB").unwrap(), 512);
        assert_eq!(parse_size("2.25MB").unwrap(), 2_359_296); // 2.25 * 1048576
        assert_eq!(parse_size("0.125GB").unwrap(), 134_217_728); // 0.125 * 1073741824
    }

    #[test]
    fn test_negative_decimal() {
        assert_eq!(parse_size("-1.5GB").unwrap(), -1_610_612_736);
        assert_eq!(parse_size("-0.5KB").unwrap(), -512);
    }

    #[test]
    fn test_large_decimal() {
        // Test with large values that would overflow f64 precision
        assert_eq!(parse_size("1.1PB").unwrap(), (1.1 * PB as f64) as i64);
    }

    #[test]
    fn test_float_precision_loss() {
        // This test demonstrates the precision characteristics of our
        // integer-based parser vs f64 arithmetic.

        // Case 1: Basic decimal parsing — both give same result
        // 0.3GB = 0.3 * 1073741824 = 322122547.2 -> 322122547
        let f64_result = (0.3 * GB as f64) as i64;
        let int_result = parse_size("0.3GB").unwrap();
        assert_eq!(f64_result, int_result); // both 322122547

        // Case 2: The classic f64 failure — 0.1 + 0.2 != 0.3
        assert_ne!(0.1_f64 + 0.2_f64, 0.3_f64); // false!

        // Case 3: Integer truncation is deterministic
        // 0.1GB = GB / 10 = 107374182 (truncated, 4 bytes less than exact)
        let one_tenth = parse_size("0.1GB").unwrap();
        assert_eq!(one_tenth, 107_374_182);
        // Exact would be 107374182.4, but we truncate consistently

        // Case 4: Deterministic behavior — same input always gives same output
        assert_eq!(parse_size("0.1GB").unwrap(), parse_size("0.1GB").unwrap());
        assert_eq!(parse_size("0.3GB").unwrap(), parse_size("0.3GB").unwrap());

        // Case 5: Our parser handles various precisions correctly
        assert_eq!(parse_size("0.001GB").unwrap(), 1_073_741); // truncated
        assert_eq!(parse_size("0.0001GB").unwrap(), 107_374); // truncated
        assert_eq!(parse_size("0.5GB").unwrap(), GB / 2); // exact!
    }
}