From 85b6f96520250c0c472ae459fb691d29220a10b5 Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Sat, 20 Jun 2026 16:06:31 -0300 Subject: [PATCH 1/2] fix: correct unit index calculation and improve speed monitor formula --- lib/utils/bytes.dart | 4 ++-- lib/utils/speed_monitor.dart | 6 +++--- test/utils/bytes_test.dart | 5 +++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/utils/bytes.dart b/lib/utils/bytes.dart index f1cf4e3..9c2f3c1 100644 --- a/lib/utils/bytes.dart +++ b/lib/utils/bytes.dart @@ -43,7 +43,7 @@ class Bytes { return n / pow(dimension.value, index); } - int get unitIndex => min(index, type.units.length); + int get unitIndex => index.clamp(0, type.units.length); bool get _isInvalid => n.isNaN || n.isInfinite || n.isZero || n.isNegative; @@ -165,7 +165,7 @@ class _Bits extends Bytes { UnitType get type => .bit; @override - int get unitIndex => min(index, type.units.length); + int get unitIndex => index.clamp(0, type.units.length); @override String toString([String separator = ""]) { diff --git a/lib/utils/speed_monitor.dart b/lib/utils/speed_monitor.dart index be57e49..cc552ba 100644 --- a/lib/utils/speed_monitor.dart +++ b/lib/utils/speed_monitor.dart @@ -43,8 +43,8 @@ class SpeedMonitor implements Disposable { if (first == last) return _zero; - return last.units - - first.units / last.time - - first.time / _windowDuration.inMicroseconds; + return (last.units - first.units) / + ((last.time - first.time) / + _windowDuration.inMicroseconds); } } diff --git a/test/utils/bytes_test.dart b/test/utils/bytes_test.dart index 49a1bff..2365bd5 100644 --- a/test/utils/bytes_test.dart +++ b/test/utils/bytes_test.dart @@ -34,5 +34,10 @@ void main() { const Bytes bytes = .bits(1024 * 1024 * 10 * 8); expect(bytes.toString(), "80.0Mb"); }); + + test("${4.996949731576378e-7 * 8}", () { + const Bytes bytes = .bits(4.996949731576378e-7 * 8); + expect(bytes.toString(), "0.0b"); + }); }); } From 96ff6ffb4533afbbaa2cd528a4dbc25a0cc4c4dc Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Sat, 20 Jun 2026 16:15:49 -0300 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20ajustar=20c=C3=A1lculo=20de=20v?= =?UTF-8?q?elocidade=20no=20SpeedMonitor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/speed_monitor.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utils/speed_monitor.dart b/lib/utils/speed_monitor.dart index cc552ba..0c32f2d 100644 --- a/lib/utils/speed_monitor.dart +++ b/lib/utils/speed_monitor.dart @@ -44,7 +44,6 @@ class SpeedMonitor implements Disposable { if (first == last) return _zero; return (last.units - first.units) / - ((last.time - first.time) / - _windowDuration.inMicroseconds); + ((last.time - first.time) / _windowDuration.inMicroseconds); } }