Skip to content

Commit 3930ea9

Browse files
authored
Merge pull request #3 from robamu/feature_overhead_calculator
add two new functions
2 parents 8a421d5 + 4c4c113 commit 3930ea9

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

python3/cobs/cobs/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@
2626
DecodeError.__module__ = 'cobs.cobs'
2727

2828
from .._version import *
29-

python3/cobs/cobs/_cobs_py.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,17 @@ def decode(in_bytes):
8888
else:
8989
break
9090
return bytes(out_bytes)
91+
92+
93+
def encoding_overhead(source_len):
94+
"""Calculates the maximum overhead when encoding a message with the given length.
95+
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
96+
if source_len == 0:
97+
return 1
98+
return (source_len + 253) // 254
99+
100+
101+
def max_encoded_length(source_len):
102+
"""Calculates how maximum possible size of an encoded message given the length of the
103+
source message."""
104+
return source_len + encoding_overhead(source_len)

python3/cobs/cobs/test.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from .. import cobs as cobs
1414
#from ..cobs import _cobs_py as cobs
1515

16-
1716
def infinite_non_zero_generator():
1817
while True:
1918
for i in range(1,50):
@@ -191,6 +190,25 @@ def test_array_of_half_words(self):
191190
cobs.decode(array_encoded_string)
192191

193192

193+
class UtilTests(unittest.TestCase):
194+
195+
def test_encoded_len_calc(self):
196+
self.assertEqual(cobs.encoding_overhead(5), 1)
197+
self.assertEqual(cobs.max_encoded_length(5), 6)
198+
199+
def test_encoded_len_calc_empty_packet(self):
200+
self.assertEqual(cobs.encoding_overhead(0), 1)
201+
self.assertEqual(cobs.max_encoded_length(0), 1)
202+
203+
def test_encoded_len_calc_still_one_byte_overhead(self):
204+
self.assertEqual(cobs.encoding_overhead(254), 1)
205+
self.assertEqual(cobs.max_encoded_length(254), 255)
206+
207+
def test_encoded_len_calc_two_byte_overhead(self):
208+
self.assertEqual(cobs.encoding_overhead(255), 2)
209+
self.assertEqual(cobs.max_encoded_length(255), 257)
210+
211+
194212
def runtests():
195213
unittest.main()
196214

python3/cobs/cobsr/_cobsr_py.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,17 @@ def decode(in_bytes):
9696
else:
9797
break
9898
return bytes(out_bytes)
99+
100+
101+
def encoding_overhead(source_len):
102+
"""Calculates the maximum overhead when encoding a message with the given length.
103+
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
104+
if source_len == 0:
105+
return 1
106+
return (source_len + 253) // 254
107+
108+
109+
def max_encoded_length(source_len):
110+
"""Calculates how maximum possible size of an encoded message given the length of the
111+
source message."""
112+
return source_len + encoding_overhead(source_len)

python3/cobs/cobsr/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,25 @@ def test_array_of_half_words(self):
223223
cobsr.decode(array_encoded_string)
224224

225225

226+
class UtilTests(unittest.TestCase):
227+
228+
def test_encoded_len_calc(self):
229+
self.assertEqual(cobsr.encoding_overhead(5), 1)
230+
self.assertEqual(cobsr.max_encoded_length(5), 6)
231+
232+
def test_encoded_len_calc_empty_packet(self):
233+
self.assertEqual(cobsr.encoding_overhead(0), 1)
234+
self.assertEqual(cobsr.max_encoded_length(0), 1)
235+
236+
def test_encoded_len_calc_still_one_byte_overhead(self):
237+
self.assertEqual(cobsr.encoding_overhead(254), 1)
238+
self.assertEqual(cobsr.max_encoded_length(254), 255)
239+
240+
def test_encoded_len_calc_two_byte_overhead(self):
241+
self.assertEqual(cobsr.encoding_overhead(255), 2)
242+
self.assertEqual(cobsr.max_encoded_length(255), 257)
243+
244+
226245
def runtests():
227246
unittest.main()
228247

0 commit comments

Comments
 (0)