File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626DecodeError .__module__ = 'cobs.cobs'
2727
2828from .._version import *
29-
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1313from .. import cobs as cobs
1414#from ..cobs import _cobs_py as cobs
1515
16-
1716def 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+
194212def runtests ():
195213 unittest .main ()
196214
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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+
226245def runtests ():
227246 unittest .main ()
228247
You can’t perform that action at this time.
0 commit comments