1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3from fontTools.misc.fixedTools import fixedToFloat, floatToFixed 4import unittest 5 6 7class FixedToolsTest(unittest.TestCase): 8 9 def test_roundtrip(self): 10 for bits in range(0, 15): 11 for value in range(-(2**(bits+1)), 2**(bits+1)): 12 self.assertEqual(value, floatToFixed(fixedToFloat(value, bits), bits)) 13 14 def test_fixedToFloat_precision14(self): 15 self.assertEqual(0.8, fixedToFloat(13107, 14)) 16 self.assertEqual(0.0, fixedToFloat(0, 14)) 17 self.assertEqual(1.0, fixedToFloat(16384, 14)) 18 self.assertEqual(-1.0, fixedToFloat(-16384, 14)) 19 self.assertEqual(0.99994, fixedToFloat(16383, 14)) 20 self.assertEqual(-0.99994, fixedToFloat(-16383, 14)) 21 22 def test_fixedToFloat_precision6(self): 23 self.assertAlmostEqual(-9.98, fixedToFloat(-639, 6)) 24 self.assertAlmostEqual(-10.0, fixedToFloat(-640, 6)) 25 self.assertAlmostEqual(9.98, fixedToFloat(639, 6)) 26 self.assertAlmostEqual(10.0, fixedToFloat(640, 6)) 27 28 def test_floatToFixed_precision14(self): 29 self.assertEqual(13107, floatToFixed(0.8, 14)) 30 self.assertEqual(16384, floatToFixed(1.0, 14)) 31 self.assertEqual(16384, floatToFixed(1, 14)) 32 self.assertEqual(-16384, floatToFixed(-1.0, 14)) 33 self.assertEqual(-16384, floatToFixed(-1, 14)) 34 self.assertEqual(0, floatToFixed(0, 14)) 35 36 def test_fixedToFloat_return_float(self): 37 value = fixedToFloat(16384, 14) 38 self.assertIsInstance(value, float) 39 40 41if __name__ == "__main__": 42 import sys 43 sys.exit(unittest.main()) 44