add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / test / org / apache / lucene / spatial / geohash / TestGeoHashUtils.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.spatial.geohash;
19
20 import org.apache.lucene.util.LuceneTestCase;
21 import org.junit.Test;
22
23 /**
24  * Tests for {@link GeoHashUtils}
25  */
26 public class TestGeoHashUtils extends LuceneTestCase {
27   
28   /**
29    * Pass condition: lat=42.6, lng=-5.6 should be encoded as "ezs42e44yx96",
30    * lat=57.64911 lng=10.40744 should be encoded as "u4pruydqqvj8"
31    */
32   @Test
33   public void testEncode() {
34     String hash = GeoHashUtils.encode(42.6, -5.6);
35     assertEquals("ezs42e44yx96", hash);
36     
37     hash = GeoHashUtils.encode(57.64911, 10.40744);
38     assertEquals("u4pruydqqvj8", hash);
39   }
40   
41   /**
42    * Pass condition: lat=52.3738007, lng=4.8909347 should be encoded and then
43    * decoded within 0.00001 of the original value
44    */
45   @Test
46   public void testDecodePreciseLongitudeLatitude() {
47     String hash = GeoHashUtils.encode(52.3738007, 4.8909347);
48     
49     double[] latitudeLongitude = GeoHashUtils.decode(hash);
50     
51     assertEquals(52.3738007, latitudeLongitude[0], 0.00001D);
52     assertEquals(4.8909347, latitudeLongitude[1], 0.00001D);
53   }
54   
55   /**
56    * Pass condition: lat=84.6, lng=10.5 should be encoded and then decoded
57    * within 0.00001 of the original value
58    */
59   @Test
60   public void testDecodeImpreciseLongitudeLatitude() {
61     String hash = GeoHashUtils.encode(84.6, 10.5);
62     
63     double[] latitudeLongitude = GeoHashUtils.decode(hash);
64     
65     assertEquals(84.6, latitudeLongitude[0], 0.00001D);
66     assertEquals(10.5, latitudeLongitude[1], 0.00001D);
67   }
68   
69   /*
70    * see https://issues.apache.org/jira/browse/LUCENE-1815 for details
71    */
72   @Test
73   public void testDecodeEncode() {
74     String geoHash = "u173zq37x014";
75     assertEquals(geoHash, GeoHashUtils.encode(52.3738007, 4.8909347));
76     double[] decode = GeoHashUtils.decode(geoHash);
77     assertEquals(52.37380061d, decode[0], 0.000001d);
78     assertEquals(4.8909343d, decode[1], 0.000001d);
79     
80     assertEquals(geoHash, GeoHashUtils.encode(decode[0], decode[1]));
81     
82     geoHash = "u173";
83     decode = GeoHashUtils.decode("u173");
84     geoHash = GeoHashUtils.encode(decode[0], decode[1]);
85     assertEquals(decode[0], GeoHashUtils.decode(geoHash)[0], 0.000001d);
86     assertEquals(decode[1], GeoHashUtils.decode(geoHash)[1], 0.000001d);
87   }
88 }