Start adding some tests
Improve the fixed_version function Better binary search parameter return values
This commit is contained in:
		
							
								
								
									
										7
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								setup.py
									
									
									
									
									
								
							| @@ -1,4 +1,4 @@ | |||||||
| from setuptools import setup | from setuptools import setup, find_packages | ||||||
| from os import path | from os import path | ||||||
| from zttf import __version__ | from zttf import __version__ | ||||||
|  |  | ||||||
| @@ -32,5 +32,6 @@ setup( | |||||||
|         'Programming Language :: Python :: 3.4', |         'Programming Language :: Python :: 3.4', | ||||||
|     ], |     ], | ||||||
|     keywords='fonts truetype ttf', |     keywords='fonts truetype ttf', | ||||||
|     packages=['zttf'] |     packages=find_packages(exclude=['tests']), | ||||||
| ) |     test_suite='tests' | ||||||
|  | ) | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | __author__ = 'david' | ||||||
							
								
								
									
										23
									
								
								tests/utils_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tests/utils_test.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | import unittest | ||||||
|  |  | ||||||
|  | from zttf.utils import fixed_version, binary_search_parameters | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TestUtils(unittest.TestCase): | ||||||
|  |     def test_fixed_version(self): | ||||||
|  |         cases = [ | ||||||
|  |             (0x00005000, 0.5), | ||||||
|  |             (0x00010000, 1.0), | ||||||
|  |             (0x00035000, 3.5), | ||||||
|  |             (0x00105000, 10.5) | ||||||
|  |         ] | ||||||
|  |         for case in cases: | ||||||
|  |             self.assertEqual(fixed_version(case[0]), case[1]) | ||||||
|  |  | ||||||
|  |     def test_binary_parameters(self): | ||||||
|  |         cases = { | ||||||
|  |             39: (5, 64, 14), | ||||||
|  |             10: (3, 16, 4) | ||||||
|  |         } | ||||||
|  |         for n, result in cases.items(): | ||||||
|  |             self.assertEqual(binary_search_parameters(n), result) | ||||||
| @@ -91,26 +91,22 @@ def fixed_version(num): | |||||||
|     :param num: fixed 16:16 floating point number as a 32-bit unsigned integer |     :param num: fixed 16:16 floating point number as a 32-bit unsigned integer | ||||||
|     :return: version number (float) |     :return: version number (float) | ||||||
|     """ |     """ | ||||||
|     if num == 0x00005000: |     return float("{:04x}.{:04x}".format(num >> 16, num & 0x0000ffff)) | ||||||
|         return 0.5 |  | ||||||
|     elif num == 0x00010000: |  | ||||||
|         return 1.0 |  | ||||||
|     elif num == 0x00020000: |  | ||||||
|         return 2.0 |  | ||||||
|     elif num == 0x00025000: |  | ||||||
|         return 2.5 |  | ||||||
|     elif num == 0x00030000: |  | ||||||
|         return 3.0 |  | ||||||
|     return num |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def binary_search_parameters(length): | def binary_search_parameters(length): | ||||||
|     search_range = 1 |     """ The TTF specification has several places that require binary search | ||||||
|     entry_selector = 0 |         parameters. For an example look at the CMAP Format 4 table. | ||||||
|  |     :param length: The range over which the search will be performed. | ||||||
|  |     :return: The 3 parameters required. | ||||||
|  |     """ | ||||||
|  |     search_range = 2 | ||||||
|  |     entry_selector = 1 | ||||||
|     while search_range * 2 <= length: |     while search_range * 2 <= length: | ||||||
|         search_range *= 2 |         search_range *= 2 | ||||||
|         entry_selector += 1 |         entry_selector += 1 | ||||||
|     return entry_selector, search_range, length - search_range |     search_range *= 2 | ||||||
|  |     return entry_selector, search_range, 2 * length - search_range | ||||||
|  |  | ||||||
|  |  | ||||||
| class Range: | class Range: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user