@@ -86,7 +86,14 @@ class TestEncoding(unittest.TestCase):
8686
8787 def setUp (self ):
8888 self .testdata = []
89- headermap = {0 : 'lat' , 1 : 'lng' , 2 : 'length' , 3 : 'code' }
89+ headermap = {
90+ 0 : 'lat' ,
91+ 1 : 'lng' ,
92+ 2 : 'latInt' ,
93+ 3 : 'lngInt' ,
94+ 4 : 'length' ,
95+ 5 : 'code'
96+ }
9097 tests_fn = _TEST_DATA + '/encoding.csv'
9198 with open (tests_fn , mode = 'r' , encoding = 'utf-8' ) as fin :
9299 for line in fin :
@@ -95,20 +102,48 @@ def setUp(self):
95102 td = line .strip ().split (',' )
96103 assert len (td ) == len (
97104 headermap ), 'Wrong format of testing data: {0}' .format (line )
98- # first 3 keys should be numbers
99- for i in range (0 , 3 ):
100- td [i ] = float (td [i ])
105+ # First two columns are floats, next three are integers.
106+ td [0 ] = float (td [0 ])
107+ td [1 ] = float (td [1 ])
108+ td [2 ] = int (td [2 ])
109+ td [3 ] = int (td [3 ])
110+ td [4 ] = int (td [4 ])
101111 self .testdata .append ({
102112 headermap [i ]: v for i , v in enumerate (td )
103113 })
104114
105- def test_encoding (self ):
115+ def test_converting_degrees (self ):
116+ for td in self .testdata :
117+ got = olc .locationToIntegers (td ['lat' ], td ['lng' ])
118+ # Due to floating point precision limitations, we may get values 1 less than expected.
119+ self .assertTrue (
120+ td ['latInt' ] - 1 <= got [0 ] <= td ['latInt' ],
121+ f'Latitude conversion { td ["lat" ]} : want { td ["latInt" ]} got { got [0 ]} '
122+ )
123+ self .assertTrue (
124+ td ['lngInt' ] - 1 <= got [1 ] <= td ['lngInt' ],
125+ f'Longitude conversion { td ["lng" ]} : want { td ["lngInt" ]} got { got [1 ]} '
126+ )
127+
128+ def test_encoding_degrees (self ):
129+ # Allow a small proportion of errors due to floating point.
130+ allowedErrorRate = 0.05
131+ errors = 0
132+ for td in self .testdata :
133+ got = olc .encode (td ['lat' ], td ['lng' ], td ['length' ])
134+ if got != td ['code' ]:
135+ print (
136+ f'olc.encode({ td ["lat" ]} , { td ["lng" ]} , { td ["length" ]} ) want { td ["code" ]} , got { got } '
137+ )
138+ errors += 1
139+ self .assertLessEqual (errors / len (self .testdata ), allowedErrorRate ,
140+ "olc.encode error rate too high" )
141+
142+ def test_encoding_integers (self ):
106143 for td in self .testdata :
107- codelength = len (td ['code' ]) - 1
108- if '0' in td ['code' ]:
109- codelength = td ['code' ].index ('0' )
110- self .assertEqual (td ['code' ],
111- olc .encode (td ['lat' ], td ['lng' ], codelength ))
144+ self .assertEqual (
145+ td ['code' ],
146+ olc .encodeIntegers (td ['latInt' ], td ['lngInt' ], td ['length' ]))
112147
113148
114149class TestDecoding (unittest .TestCase ):
0 commit comments