Move fixedTools tests to unittests
Lifted from https://github.com/behdad/fonttools/pull/287 Adds new roundtrip tests, but they are disabled.
This commit is contained in:
parent
c0eb70308b
commit
2019cfa180
@ -14,31 +14,7 @@ def fixedToFloat(value, precisionBits):
|
|||||||
that has the shortest decimal reprentation. Eg. to convert a
|
that has the shortest decimal reprentation. Eg. to convert a
|
||||||
fixed number in a 2.14 format, use precisionBits=14. This is
|
fixed number in a 2.14 format, use precisionBits=14. This is
|
||||||
pretty slow compared to a simple division. Use sporadically.
|
pretty slow compared to a simple division. Use sporadically.
|
||||||
|
|
||||||
>>> "%g" % fixedToFloat(13107, 14)
|
|
||||||
'0.8'
|
|
||||||
>>> "%g" % fixedToFloat(0, 14)
|
|
||||||
'0'
|
|
||||||
>>> "%g" % fixedToFloat(0x4000, 14)
|
|
||||||
'1'
|
|
||||||
>>> "%g" % fixedToFloat(-16384, 14)
|
|
||||||
'-1'
|
|
||||||
>>> "%g" % fixedToFloat(-16383, 14)
|
|
||||||
'-0.99994'
|
|
||||||
>>> "%g" % fixedToFloat(16384, 14)
|
|
||||||
'1'
|
|
||||||
>>> "%g" % fixedToFloat(16383, 14)
|
|
||||||
'0.99994'
|
|
||||||
>>> "%g" % fixedToFloat(-639, 6)
|
|
||||||
'-9.99'
|
|
||||||
>>> "%g" % fixedToFloat(-640, 6)
|
|
||||||
'-10'
|
|
||||||
>>> "%g" % fixedToFloat(639, 6)
|
|
||||||
'9.99'
|
|
||||||
>>> "%g" % fixedToFloat(640, 6)
|
|
||||||
'10'
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not value: return 0.0
|
if not value: return 0.0
|
||||||
|
|
||||||
scale = 1 << precisionBits
|
scale = 1 << precisionBits
|
||||||
@ -66,25 +42,5 @@ def fixedToFloat(value, precisionBits):
|
|||||||
def floatToFixed(value, precisionBits):
|
def floatToFixed(value, precisionBits):
|
||||||
"""Converts a float to a fixed-point number given the number of
|
"""Converts a float to a fixed-point number given the number of
|
||||||
precisionBits. Ie. int(round(value * (1<<precisionBits))).
|
precisionBits. Ie. int(round(value * (1<<precisionBits))).
|
||||||
|
|
||||||
>>> floatToFixed(0.8, 14)
|
|
||||||
13107
|
|
||||||
>>> floatToFixed(1.0, 14)
|
|
||||||
16384
|
|
||||||
>>> floatToFixed(1, 14)
|
|
||||||
16384
|
|
||||||
>>> floatToFixed(-1.0, 14)
|
|
||||||
-16384
|
|
||||||
>>> floatToFixed(-1, 14)
|
|
||||||
-16384
|
|
||||||
>>> floatToFixed(0, 14)
|
|
||||||
0
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return int(round(value * (1<<precisionBits)))
|
return int(round(value * (1<<precisionBits)))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import sys
|
|
||||||
import doctest
|
|
||||||
sys.exit(doctest.testmod().failed)
|
|
||||||
|
36
Lib/fontTools/misc/fixedTools_test.py
Normal file
36
Lib/fontTools/misc/fixedTools_test.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
from __future__ import print_function, division, absolute_import
|
||||||
|
from fontTools.misc.py23 import *
|
||||||
|
from fontTools.misc.fixedTools import fixedToFloat, floatToFixed
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class FixedToolsTest(unittest.TestCase):
|
||||||
|
#def test_roundtrip(self):
|
||||||
|
# for value in range(-32768, 32768):
|
||||||
|
# self.assertEqual(value, floatToFixed(fixedToFloat(value, 14), 14))
|
||||||
|
|
||||||
|
def test_fixedToFloat_precision14(self):
|
||||||
|
self.assertEqual(0.8, fixedToFloat(13107, 14))
|
||||||
|
self.assertEqual(0.0, fixedToFloat(0, 14))
|
||||||
|
self.assertEqual(1.0, fixedToFloat(16384, 14))
|
||||||
|
self.assertEqual(-1.0, fixedToFloat(-16384, 14))
|
||||||
|
self.assertEqual(0.99994, fixedToFloat(16383, 14))
|
||||||
|
self.assertEqual(-0.99994, fixedToFloat(-16383, 14))
|
||||||
|
|
||||||
|
def test_fixedToFloat_precision6(self):
|
||||||
|
self.assertAlmostEqual(-9.99, fixedToFloat(-639, 6))
|
||||||
|
self.assertAlmostEqual(-10.0, fixedToFloat(-640, 6))
|
||||||
|
self.assertAlmostEqual(9.99, fixedToFloat(639, 6))
|
||||||
|
self.assertAlmostEqual(10.0, fixedToFloat(640, 6))
|
||||||
|
|
||||||
|
def test_floatToFixed_precision14(self):
|
||||||
|
self.assertEqual(13107, floatToFixed(0.8, 14))
|
||||||
|
self.assertEqual(16384, floatToFixed(1.0, 14))
|
||||||
|
self.assertEqual(16384, floatToFixed(1, 14))
|
||||||
|
self.assertEqual(-16384, floatToFixed(-1.0, 14))
|
||||||
|
self.assertEqual(-16384, floatToFixed(-1, 14))
|
||||||
|
self.assertEqual(0, floatToFixed(0, 14))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user