Skip to content

Commit 67a5248

Browse files
committed
Make failing best_ratio() validation tests pass
1 parent 70f9589 commit 67a5248

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

basest/core/best_ratio.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def _encoding_ratio(base_from, base_to, chunk_sizes):
1717
"""
1818
best_ratio = (1.0, INF)
1919
for s in chunk_sizes:
20+
# validate each chunk size here
21+
if not isinstance(s, int):
22+
raise TypeError('chunk sizes must be list of ints')
2023
match = ceil(log(base_from ** s, base_to))
2124
ratio = (float(s), match)
2225
if (ratio[0] / ratio[1]) > (best_ratio[0] / best_ratio[1]):
@@ -30,9 +33,16 @@ def best_ratio(input_base, output_bases, chunk_sizes):
3033
sizes, find the most efficient encoding ratio.
3134
Returns the chosen output base, and the chosen encoding ratio.
3235
"""
36+
# validate input base type
37+
if not isinstance(input_base, int):
38+
raise TypeError('input base must be of int type')
39+
3340
encoder = 0
3441
best_ratio = (1.0, INF)
3542
for base_to in output_bases:
43+
# validate each output base here
44+
if not isinstance(base_to, int):
45+
raise TypeError('output bases must be list of ints')
3646
ratio = _encoding_ratio(input_base, base_to, chunk_sizes)
3747
if (
3848
(float(ratio[0]) / float(ratio[1])) >

tests/core/test_best_ratio.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ def test_best_ratio(self, input_base, output_bases, chunk_sizes, expected):
3434
best_ratio(input_base, output_bases, chunk_sizes), expected
3535
)
3636

37-
@data(str, bool, float, bytes)
37+
@data(str, float, bytes)
3838
def test_invalid_inputs(self, data_type):
3939
"""
4040
Any non-integer types (or lists of non-integers) passed to the function
4141
should raise TypeError.
4242
"""
4343
with self.assertRaises(TypeError):
44-
best_ratio(data_type(), [data_type()], [data_type()])
44+
best_ratio(data_type(), [2], [2])
45+
46+
with self.assertRaises(TypeError):
47+
best_ratio(2, [data_type()], [2])
48+
49+
with self.assertRaises(TypeError):
50+
best_ratio(2, [0, 1], [data_type()])

0 commit comments

Comments
 (0)