@@ -67,21 +67,92 @@ testSuite({
6767 asyncTestCase . waitForAsync ( 'Waiting for xhr to respond' ) ;
6868 xhrIo_ . send ( DECODING_TEST_FILE , 'GET' ) ;
6969 } ,
70- testEncode : function ( ) {
70+ testEncodeDegrees : function ( ) {
7171 const xhrIo_ = new XhrIo ( ) ;
7272 xhrIo_ . listenOnce ( EventType . COMPLETE , ( ) => {
73+ // Allow a 5% error rate encoding from degree coordinates (because of floating
74+ // point precision).
75+ const allowedErrorRate = 0.05 ;
76+ var errors = 0 ;
7377 const lines = xhrIo_ . getResponseText ( ) . match ( / ^ [ ^ # ] .+ / gm) ;
7478 for ( var i = 0 ; i < lines . length ; i ++ ) {
7579 const fields = lines [ i ] . split ( ',' ) ;
76- const lat = parseFloat ( fields [ 0 ] ) ;
77- const lng = parseFloat ( fields [ 1 ] ) ;
78- const length = parseInt ( fields [ 2 ] , 10 ) ;
79- const code = fields [ 3 ] ;
80+ const latDegrees = parseFloat ( fields [ 0 ] ) ;
81+ const lngDegrees = parseFloat ( fields [ 1 ] ) ;
82+ const length = parseInt ( fields [ 4 ] , 10 ) ;
83+ const code = fields [ 5 ] ;
8084
81- const gotCode = OpenLocationCode . encode ( lat , lng , length ) ;
85+ const got = OpenLocationCode . encode ( latDegrees , lngDegrees , length ) ;
8286 // Did we get the same code?
83- assertEquals ( 'testEncode ' + 1 , code , gotCode ) ;
87+ if ( code != got ) {
88+ console . warn (
89+ 'ENCODING DIFFERENCE: Expected code ' + code + ', got ' + got
90+ ) ;
91+ errors ++ ;
92+ }
93+ asyncTestCase . continueTesting ( ) ;
94+ }
95+ console . info ( 'testEncodeDegrees error rate is ' + ( errors / lines . length ) ) ;
96+ assertTrue (
97+ 'testEncodeDegrees: too many errors ' + errors / lines . length ,
98+ ( errors / lines . length ) < allowedErrorRate
99+ ) ;
100+ } ) ;
101+ asyncTestCase . waitForAsync ( 'Waiting for xhr to respond' ) ;
102+ xhrIo_ . send ( ENCODING_TEST_FILE , 'GET' ) ;
103+ } ,
104+ testLocationToIntegers : function ( ) {
105+ const xhrIo_ = new XhrIo ( ) ;
106+ xhrIo_ . listenOnce ( EventType . COMPLETE , ( ) => {
107+ const lines = xhrIo_ . getResponseText ( ) . match ( / ^ [ ^ # ] .+ / gm) ;
108+ for ( var i = 0 ; i < lines . length ; i ++ ) {
109+ const fields = lines [ i ] . split ( ',' ) ;
110+ const latDegrees = parseFloat ( fields [ 0 ] ) ;
111+ const lngDegrees = parseFloat ( fields [ 1 ] ) ;
112+ const latIntegers = parseInt ( fields [ 2 ] , 10 ) ;
113+ const lngIntegers = parseInt ( fields [ 3 ] , 10 ) ;
84114
115+ const got = OpenLocationCode . _locationToIntegers (
116+ latDegrees ,
117+ lngDegrees
118+ ) ;
119+ // Due to floating point precision limitations, we may get values 1 less
120+ // than expected.
121+ assertTrue (
122+ 'testLocationToIntegers: expected latitude ' + latIntegers + ', got ' + got [ 0 ] ,
123+ got [ 0 ] == latIntegers || got [ 0 ] == latIntegers - 1
124+ ) ;
125+ assertTrue (
126+ 'testLocationToIntegers: expected longitude ' + lngIntegers + ', got ' + got [ 1 ] ,
127+ got [ 1 ] == lngIntegers || got [ 1 ] == lngIntegers - 1
128+ ) ;
129+ asyncTestCase . continueTesting ( ) ;
130+ }
131+ } ) ;
132+ asyncTestCase . waitForAsync ( 'Waiting for xhr to respond' ) ;
133+ xhrIo_ . send ( ENCODING_TEST_FILE , 'GET' ) ;
134+ } ,
135+ testEncodeIntegers : function ( ) {
136+ const xhrIo_ = new XhrIo ( ) ;
137+ xhrIo_ . listenOnce ( EventType . COMPLETE , ( ) => {
138+ const lines = xhrIo_ . getResponseText ( ) . match ( / ^ [ ^ # ] .+ / gm) ;
139+ for ( var i = 0 ; i < lines . length ; i ++ ) {
140+ const fields = lines [ i ] . split ( ',' ) ;
141+ const latIntegers = parseInt ( fields [ 2 ] , 10 ) ;
142+ const lngIntegers = parseInt ( fields [ 3 ] , 10 ) ;
143+ const length = parseInt ( fields [ 4 ] , 10 ) ;
144+ const code = fields [ 5 ] ;
145+
146+ const got = OpenLocationCode . _encodeIntegers (
147+ latIntegers ,
148+ lngIntegers ,
149+ length
150+ ) ;
151+ // Did we get the same code?
152+ assertEquals (
153+ 'testEncodeIntegers: expected code ' + code + ', got ' + got ,
154+ code , got
155+ ) ;
85156 asyncTestCase . continueTesting ( ) ;
86157 }
87158 } ) ;
0 commit comments