TupleVariation: round deltas before encoding (#861)

* TupleVariationTest.test_compileDeltaValues(): also test floats

* TupleVariation: round deltas before encoding

Python 3 was raising 'struct.error: required argument is not an integer' and Python 2 was truncating when deltas are floats
This commit is contained in:
Denis Moyogo Jacquerye 2017-02-24 16:58:41 +00:00 committed by Behdad Esfahbod
parent 55a5ace5d8
commit 891405fd68
2 changed files with 7 additions and 2 deletions

View File

@ -366,7 +366,7 @@ class TupleVariation(object):
assert runLength >= 1 and runLength <= 64
stream.write(bytechr(runLength - 1))
for i in range(offset, pos):
stream.write(struct.pack('b', deltas[i]))
stream.write(struct.pack('b', round(deltas[i])))
return pos
@staticmethod
@ -400,7 +400,7 @@ class TupleVariation(object):
assert runLength >= 1 and runLength <= 64
stream.write(bytechr(DELTAS_ARE_WORDS | (runLength - 1)))
for i in range(offset, pos):
stream.write(struct.pack('>h', deltas[i]))
stream.write(struct.pack('>h', round(deltas[i])))
return pos
@staticmethod

View File

@ -512,6 +512,11 @@ class TupleVariationTest(unittest.TestCase):
# words, zeroes
self.assertEqual("40 66 66 80", compileDeltaValues([0x6666, 0]))
self.assertEqual("40 66 66 81", compileDeltaValues([0x6666, 0, 0]))
# bytes or words from floats
self.assertEqual("00 01", compileDeltaValues([1.1]))
self.assertEqual("00 02", compileDeltaValues([1.9]))
self.assertEqual("40 66 66", compileDeltaValues([0x6666 + 0.1]))
self.assertEqual("40 66 66", compileDeltaValues([0x6665 + 0.9]))
def test_decompileDeltas(self):
decompileDeltas = TupleVariation.decompileDeltas_