Skip to content

Commit 490b8e5

Browse files
committed
Add failing tests for best_prefix() bit-family unit bug (issue #95)
best_prefix() on Bit-family instances (Bit, Kib, Mib, kb, Mb, etc.) incorrectly returns Byte-family units (MiB, GiB, MB, GB, etc.) because the conversion method name is always built with a hardcoded 'B' suffix. 13 new tests document the expected behaviour across NIST and SI systems. All 13 fail against the current implementation. Existing 30 tests pass.
1 parent c486f91 commit 490b8e5

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

tests/test_best_prefix_NIST.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,58 @@ def test_bitmath_best_prefix_NIST_exbi(self):
142142
"""bitmath.best_prefix return an exbibyte for a huge number of bytes"""
143143
result = bitmath.best_prefix(1152921504606846977)
144144
self.assertIs(type(result), bitmath.EiB)
145+
146+
##################################################################
147+
# Tests for bit-family inputs (issue #95)
148+
#
149+
# best_prefix() on a Bit-family instance should return a Bit-family
150+
# result. Before the fix these all incorrectly return Byte-family
151+
# units (e.g. MiB instead of Mib).
152+
153+
def test_bit_input_returns_bit_family(self):
154+
"""NIST: best_prefix on Bit() returns a Bit-family unit, not a Byte-family unit"""
155+
# The exact value from issue #95
156+
result = bitmath.Bit(30950093.15655963).best_prefix()
157+
self.assertIsInstance(result, bitmath.Bit)
158+
159+
def test_bit_input_returns_mib_type(self):
160+
"""NIST: Bit(30950093) best_prefix is Mib, not MiB (issue #95)"""
161+
result = bitmath.Bit(30950093.15655963).best_prefix()
162+
self.assertIs(type(result), bitmath.Mib)
163+
164+
def test_kib_input_returns_mib_type(self):
165+
"""NIST: Kib(8192).best_prefix() returns Mib, not MiB
166+
167+
Kib(8192) = 8,388,608 bits = 1,048,576 bytes; log(1048576, 1024) = 2 -> Mib.
168+
"""
169+
result = bitmath.Kib(8192).best_prefix()
170+
self.assertIs(type(result), bitmath.Mib)
171+
172+
def test_mib_input_returns_gib_type(self):
173+
"""NIST: Mib(8192).best_prefix() returns Gib, not GiB
174+
175+
Mib(8192) = 8,589,934,592 bits = 1,073,741,824 bytes; log(1073741824, 1024) = 3 -> Gib.
176+
"""
177+
result = bitmath.Mib(8192).best_prefix()
178+
self.assertIs(type(result), bitmath.Gib)
179+
180+
def test_bit_multi_oom_round_up(self):
181+
"""NIST: A very large Kib rounds up into a Pib
182+
183+
Pib(8) = 2^53 bits = 2^50 bytes; log(2^50, 1024) = 5 -> Pib.
184+
"""
185+
large_Kib = bitmath.Kib.from_other(bitmath.Pib(8))
186+
self.assertIs(type(large_Kib.best_prefix()), bitmath.Pib)
187+
188+
def test_bit_multi_oom_round_down(self):
189+
"""NIST: A very small Pib rounds down into a Kib
190+
191+
Kib(8) = 8192 bits = 1024 bytes; log(1024, 1024) = 1 -> Kib.
192+
"""
193+
small_Pib = bitmath.Pib.from_other(bitmath.Kib(8))
194+
self.assertIs(type(small_Pib.best_prefix()), bitmath.Kib)
195+
196+
def test_bit_input_prefer_nist_returns_bit_family(self):
197+
"""NIST: best_prefix(system=NIST) on a Bit() still returns a Bit-family unit"""
198+
result = bitmath.Bit(30950093.15655963).best_prefix(system=bitmath.NIST)
199+
self.assertIs(type(result), bitmath.Mib)

tests/test_best_prefix_SI.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,52 @@ def test_bitmath_best_prefix_SI_yotta(self):
142142
"""bitmath.best_prefix return a yottabyte for a huge number of bytes"""
143143
result = bitmath.best_prefix(1000000000000000000000001, system=bitmath.SI)
144144
self.assertIs(type(result), bitmath.YB)
145+
146+
##################################################################
147+
# Tests for bit-family inputs (issue #95)
148+
#
149+
# best_prefix() on a Bit-family SI instance should return a
150+
# Bit-family SI result. Before the fix these incorrectly return
151+
# Byte-family units (e.g. MB instead of Mb).
152+
153+
def test_bit_input_returns_bit_family_si(self):
154+
"""SI: best_prefix on Bit() returns a Bit-family unit, not a Byte-family unit"""
155+
result = bitmath.Bit.from_other(bitmath.Mb(1)).best_prefix(system=bitmath.SI)
156+
self.assertIsInstance(result, bitmath.Bit)
157+
158+
def test_kb_input_returns_mb_type(self):
159+
"""SI: kb(8000).best_prefix() returns Mb, not MB
160+
161+
kb(8000) = 8,000,000 bits = 1,000,000 bytes; log(1000000, 1000) = 2 -> Mb.
162+
"""
163+
result = bitmath.kb(8000).best_prefix()
164+
self.assertIs(type(result), bitmath.Mb)
165+
166+
def test_mb_input_returns_gb_type(self):
167+
"""SI: Mb(8000).best_prefix() returns Gb, not GB
168+
169+
Mb(8000) = 8,000,000,000 bits = 1,000,000,000 bytes; log(1000000000, 1000) = 3 -> Gb.
170+
"""
171+
result = bitmath.Mb(8000).best_prefix()
172+
self.assertIs(type(result), bitmath.Gb)
173+
174+
def test_bit_multi_oom_round_up_si(self):
175+
"""SI: A very large kb rounds up into a Pb
176+
177+
Pb(8) = 8*10^15 bits = 10^15 bytes; log(10^15, 1000) = 5 -> Pb.
178+
"""
179+
large_kb = bitmath.kb.from_other(bitmath.Pb(8))
180+
self.assertIs(type(large_kb.best_prefix()), bitmath.Pb)
181+
182+
def test_bit_multi_oom_round_down_si(self):
183+
"""SI: A very small Pb rounds down into a kb
184+
185+
kb(8) = 8000 bits = 1000 bytes; log(1000, 1000) = 1 -> kb.
186+
"""
187+
small_Pb = bitmath.Pb.from_other(bitmath.kb(8))
188+
self.assertIs(type(small_Pb.best_prefix()), bitmath.kb)
189+
190+
def test_bit_input_prefer_si_returns_bit_family(self):
191+
"""SI: best_prefix(system=SI) on a kb() still returns a Bit-family unit"""
192+
result = bitmath.kb(8000).best_prefix(system=bitmath.SI)
193+
self.assertIs(type(result), bitmath.Mb)

0 commit comments

Comments
 (0)