• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Libphonenumber Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.i18n.phonenumbers;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import com.google.i18n.phonenumbers.CombineGeoData.Range;
24 
25 import org.junit.Test;
26 
27 import java.util.List;
28 import java.util.SortedMap;
29 import java.util.TreeMap;
30 
31 /**
32  * Unit tests for CombineGeoData class.
33  *
34  * @author Philippe Liard
35  */
36 public class CombineGeoDataTest {
37   @Test
createSortedPrefixArray()38   public void createSortedPrefixArray() {
39     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
40     phonePrefixMap.put("122", null);
41     phonePrefixMap.put("42", null);
42     phonePrefixMap.put("4012", null);
43     phonePrefixMap.put("1000", null);
44 
45     String[] sortedPrefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
46     assertEquals("1000", sortedPrefixes[0]);
47     assertEquals("122", sortedPrefixes[1]);
48     assertEquals("4012", sortedPrefixes[2]);
49     assertEquals("42", sortedPrefixes[3]);
50   }
51 
52   @Test
findRangeEndFromStart()53   public void findRangeEndFromStart() {
54     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
55     phonePrefixMap.put("33130", "Paris");
56     phonePrefixMap.put("33139", "Paris");
57     phonePrefixMap.put("334", "Marseille");
58 
59     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
60     int rangeEnd = CombineGeoData.findRangeEnd(prefixes, phonePrefixMap, 0);
61     assertEquals(1, rangeEnd);
62   }
63 
64   @Test
findRangeEndFromMiddle()65   public void findRangeEndFromMiddle() {
66     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
67     phonePrefixMap.put("33130", "Paris");
68     phonePrefixMap.put("33139", "Paris");
69     phonePrefixMap.put("3341", "Marseille");
70     phonePrefixMap.put("3342", "Marseille");
71 
72     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
73     int rangeEnd = CombineGeoData.findRangeEnd(prefixes, phonePrefixMap, 2);
74     assertEquals(3, rangeEnd);
75   }
76 
77   @Test
findRangeEndWithSameLocationButDifferentPrefix()78   public void findRangeEndWithSameLocationButDifferentPrefix() {
79     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
80     phonePrefixMap.put("33130", "Paris");
81     phonePrefixMap.put("3314", "Paris");
82     phonePrefixMap.put("3341", "Marseille");
83     phonePrefixMap.put("3342", "Marseille");
84 
85     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
86     int rangeEnd = CombineGeoData.findRangeEnd(prefixes, phonePrefixMap, 0);
87     assertEquals(0, rangeEnd);
88   }
89 
90   @Test
createRanges()91   public void createRanges() {
92     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
93     phonePrefixMap.put("33120", "Paris");
94     phonePrefixMap.put("33130", "Paris");
95     phonePrefixMap.put("33139", "Paris");
96     phonePrefixMap.put("3341", "Marseille");
97     phonePrefixMap.put("3342", "Marseille");
98 
99     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
100     List<Range> ranges = CombineGeoData.createRanges(prefixes, phonePrefixMap);
101     assertEquals(3, ranges.size());
102     assertEquals(0, ranges.get(0).start);
103     assertEquals(0, ranges.get(0).end);
104     assertEquals(1, ranges.get(1).start);
105     assertEquals(2, ranges.get(1).end);
106     assertEquals(3, ranges.get(2).start);
107     assertEquals(4, ranges.get(2).end);
108   }
109 
110   @Test
findConflict()111   public void findConflict() {
112     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
113     phonePrefixMap.put("33130", "Saint Germain en Laye");
114     phonePrefixMap.put("33132", "Paris");
115     phonePrefixMap.put("33139", "Paris");
116 
117     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
118     assertTrue(CombineGeoData.findConflict(prefixes, 3313, 0, 0));
119   }
120 
121   @Test
conflictBefore()122   public void conflictBefore() {
123     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
124     phonePrefixMap.put("33130", "Saint Germain en Laye");
125     phonePrefixMap.put("33132", "Paris");
126     phonePrefixMap.put("33139", "Paris");
127     phonePrefixMap.put("3341", "Marseille");
128     phonePrefixMap.put("3342", "Marseille");
129 
130     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
131     List<Range> ranges = CombineGeoData.createRanges(prefixes, phonePrefixMap);
132     assertTrue(CombineGeoData.hasConflict(ranges, 1, prefixes, 3313));
133   }
134 
135   @Test
conflictAfter()136   public void conflictAfter() {
137     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
138     phonePrefixMap.put("33122", "Poissy");
139     phonePrefixMap.put("33132", "Paris");
140     phonePrefixMap.put("33138", "Paris");
141     phonePrefixMap.put("33139", "Saint Germain en Laye");
142     phonePrefixMap.put("3341", "Marseille");
143     phonePrefixMap.put("3342", "Marseille");
144 
145     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
146     List<Range> ranges = CombineGeoData.createRanges(prefixes, phonePrefixMap);
147     assertEquals(4, ranges.size());
148     assertTrue(CombineGeoData.hasConflict(ranges, 1, prefixes, 3313));
149   }
150 
151   @Test
noConflict()152   public void noConflict() {
153     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
154     phonePrefixMap.put("33122", "Poissy");
155     phonePrefixMap.put("33132", "Paris");
156     phonePrefixMap.put("33138", "Paris");
157     phonePrefixMap.put("33149", "Saint Germain en Laye");
158     phonePrefixMap.put("3341", "Marseille");
159     phonePrefixMap.put("3342", "Marseille");
160 
161     String[] prefixes = CombineGeoData.createSortedPrefixArray(phonePrefixMap);
162     List<Range> ranges = CombineGeoData.createRanges(prefixes, phonePrefixMap);
163     assertEquals(4, ranges.size());
164     assertFalse(CombineGeoData.hasConflict(ranges, 1, prefixes, 3313));
165   }
166 
167   @Test
combineRemovesLastDigit()168   public void combineRemovesLastDigit() {
169     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
170     phonePrefixMap.put("33122", "Poissy");
171     phonePrefixMap.put("33132", "Paris");
172     phonePrefixMap.put("33149", "Saint Germain en Laye");
173     phonePrefixMap.put("3342", "Marseille");
174 
175     phonePrefixMap = CombineGeoData.combine(phonePrefixMap);
176     assertEquals(4, phonePrefixMap.size());
177     assertEquals("Poissy", phonePrefixMap.get("3312"));
178     assertEquals("Paris", phonePrefixMap.get("3313"));
179     assertEquals("Saint Germain en Laye", phonePrefixMap.get("3314"));
180     assertEquals("Marseille", phonePrefixMap.get("334"));
181   }
182 
183   @Test
combineMergesSamePrefixAndLocation()184   public void combineMergesSamePrefixAndLocation() {
185     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
186     phonePrefixMap.put("33132", "Paris");
187     phonePrefixMap.put("33133", "Paris");
188     phonePrefixMap.put("33134", "Paris");
189 
190     phonePrefixMap = CombineGeoData.combine(phonePrefixMap);
191     assertEquals(1, phonePrefixMap.size());
192     assertEquals("Paris", phonePrefixMap.get("3313"));
193   }
194 
195   @Test
combineWithNoPossibleCombination()196   public void combineWithNoPossibleCombination() {
197     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
198     phonePrefixMap.put("3312", "Poissy");
199     phonePrefixMap.put("3313", "Paris");
200     phonePrefixMap.put("3314", "Saint Germain en Laye");
201 
202     phonePrefixMap = CombineGeoData.combine(phonePrefixMap);
203     assertEquals(3, phonePrefixMap.size());
204     assertEquals("Poissy", phonePrefixMap.get("3312"));
205     assertEquals("Paris", phonePrefixMap.get("3313"));
206     assertEquals("Saint Germain en Laye", phonePrefixMap.get("3314"));
207   }
208 
209   @Test
combineMultipleTimes()210   public void combineMultipleTimes() {
211     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
212     phonePrefixMap.put("33132", "Paris");
213     phonePrefixMap.put("33133", "Paris");
214     phonePrefixMap.put("33134", "Paris");
215 
216     phonePrefixMap = CombineGeoData.combineMultipleTimes(phonePrefixMap);
217     assertEquals(1, phonePrefixMap.size());
218     assertEquals("Paris", phonePrefixMap.get("3"));
219   }
220 
221   @Test
combineMultipleTimesWithPrefixesWithDifferentLengths()222   public void combineMultipleTimesWithPrefixesWithDifferentLengths() {
223     SortedMap<String, String> phonePrefixMap = new TreeMap<String, String>();
224     phonePrefixMap.put("332", "Paris");
225     phonePrefixMap.put("33133", "Paris");
226     phonePrefixMap.put("41", "Marseille");
227 
228     phonePrefixMap = CombineGeoData.combineMultipleTimes(phonePrefixMap);
229     assertEquals(2, phonePrefixMap.size());
230     assertEquals("Paris", phonePrefixMap.get("3"));
231     assertEquals("Marseille", phonePrefixMap.get("4"));
232   }
233 }
234