• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2012 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: Patrick Mezard
16 //
17 // Basic test cases for MappingFileProvider.
18 
19 #include "phonenumbers/geocoding/area_code_map.h"
20 
21 #include <cstddef>
22 #include <vector>
23 
24 #include <gtest/gtest.h>  // NOLINT(build/include_order)
25 
26 #include "phonenumbers/geocoding/geocoding_data.h"
27 #include "phonenumbers/phonenumber.pb.h"
28 
29 namespace i18n {
30 namespace phonenumbers {
31 
32 namespace {
33 
MakeCodeMap(const PrefixDescriptions * descriptions,scoped_ptr<AreaCodeMap> * code_map)34 void MakeCodeMap(const PrefixDescriptions* descriptions,
35                  scoped_ptr<AreaCodeMap>* code_map) {
36   scoped_ptr<AreaCodeMap> cm(new AreaCodeMap());
37   cm->ReadAreaCodeMap(descriptions);
38   code_map->swap(cm);
39 }
40 
41 const int32 prefix_1_us_prefixes[] = {
42   1212,
43   1480,
44   1650,
45   1907,
46   1201664,
47   1480893,
48   1501372,
49   1626308,
50   1650345,
51   1867993,
52   1972480,
53 };
54 
55 const char* prefix_1_us_descriptions[] = {
56   "New York",
57   "Arizona",
58   "California",
59   "Alaska",
60   "Westwood, NJ",
61   "Phoenix, AZ",
62   "Little Rock, AR",
63   "Alhambra, CA",
64   "San Mateo, CA",
65   "Dawson, YT",
66   "Richardson, TX",
67 };
68 
69 const int32 prefix_1_us_lengths[] = {
70   4, 7,
71 };
72 
73 const PrefixDescriptions prefix_1_us = {
74   prefix_1_us_prefixes,
75   sizeof(prefix_1_us_prefixes) / sizeof(*prefix_1_us_prefixes),
76   prefix_1_us_descriptions,
77   prefix_1_us_lengths,
78   sizeof(prefix_1_us_lengths) / sizeof(*prefix_1_us_lengths),
79 };
80 
81 const int32 prefix_39_it_prefixes[] = {
82   3902,
83   3906,
84   39010,
85   390131,
86   390321,
87   390975,
88 };
89 
90 const char* prefix_39_it_descriptions[] = {
91   "Milan",
92   "Rome",
93   "Genoa",
94   "Alessandria",
95   "Novara",
96   "Potenza",
97 };
98 
99 const int32 prefix_39_it_lengths[] = {
100   4, 5, 6,
101 };
102 
103 const PrefixDescriptions prefix_39_it = {
104   prefix_39_it_prefixes,
105   sizeof(prefix_39_it_prefixes) / sizeof(*prefix_39_it_prefixes),
106   prefix_39_it_descriptions,
107   prefix_39_it_lengths,
108   sizeof(prefix_39_it_lengths) / sizeof(*prefix_1_us_lengths),
109 };
110 
MakeCodeMapUS(scoped_ptr<AreaCodeMap> * code_map)111 void MakeCodeMapUS(scoped_ptr<AreaCodeMap>* code_map) {
112   MakeCodeMap(&prefix_1_us, code_map);
113 }
114 
MakeCodeMapIT(scoped_ptr<AreaCodeMap> * code_map)115 void MakeCodeMapIT(scoped_ptr<AreaCodeMap>* code_map) {
116   MakeCodeMap(&prefix_39_it, code_map);
117 }
118 
MakePhoneNumber(int32 country_code,uint64 national_number)119 PhoneNumber MakePhoneNumber(int32 country_code, uint64 national_number) {
120   PhoneNumber number;
121   number.set_country_code(country_code);
122   number.set_national_number(national_number);
123   return number;
124 }
125 
126 }  // namespace
127 
128 class AreaCodeMapTest : public testing::Test {
129  protected:
SetUp()130   virtual void SetUp() {
131     MakeCodeMapUS(&map_US_);
132     MakeCodeMapIT(&map_IT_);
133   }
134 
135   scoped_ptr<AreaCodeMap> map_US_;
136   scoped_ptr<AreaCodeMap> map_IT_;
137 };
138 
TEST_F(AreaCodeMapTest,TestLookupInvalidNumberUS)139 TEST_F(AreaCodeMapTest, TestLookupInvalidNumberUS) {
140   EXPECT_STREQ("New York", map_US_->Lookup(MakePhoneNumber(1, 2121234567L)));
141 }
142 
TEST_F(AreaCodeMapTest,TestLookupNumberNJ)143 TEST_F(AreaCodeMapTest, TestLookupNumberNJ) {
144   EXPECT_STREQ("Westwood, NJ",
145                map_US_->Lookup(MakePhoneNumber(1, 2016641234L)));
146 }
147 
TEST_F(AreaCodeMapTest,TestLookupNumberNY)148 TEST_F(AreaCodeMapTest, TestLookupNumberNY) {
149   EXPECT_STREQ("New York", map_US_->Lookup(MakePhoneNumber(1, 2126641234L)));
150 }
151 
TEST_F(AreaCodeMapTest,TestLookupNumberCA1)152 TEST_F(AreaCodeMapTest, TestLookupNumberCA1) {
153   EXPECT_STREQ("San Mateo, CA",
154                map_US_->Lookup(MakePhoneNumber(1, 6503451234LL)));
155 }
156 
TEST_F(AreaCodeMapTest,TestLookupNumberCA2)157 TEST_F(AreaCodeMapTest, TestLookupNumberCA2) {
158   EXPECT_STREQ("California", map_US_->Lookup(MakePhoneNumber(1, 6502531234LL)));
159 }
160 
TEST_F(AreaCodeMapTest,TestLookupNumberTX)161 TEST_F(AreaCodeMapTest, TestLookupNumberTX) {
162   EXPECT_STREQ("Richardson, TX",
163             map_US_->Lookup(MakePhoneNumber(1, 9724801234LL)));
164 }
165 
TEST_F(AreaCodeMapTest,TestLookupNumberNotFoundTX)166 TEST_F(AreaCodeMapTest, TestLookupNumberNotFoundTX) {
167   EXPECT_STREQ(NULL, map_US_->Lookup(MakePhoneNumber(1, 9724811234LL)));
168 }
169 
TEST_F(AreaCodeMapTest,TestLookupNumberCH)170 TEST_F(AreaCodeMapTest, TestLookupNumberCH) {
171   EXPECT_STREQ(NULL, map_US_->Lookup(MakePhoneNumber(41, 446681300L)));
172 }
173 
TEST_F(AreaCodeMapTest,TestLookupNumberIT)174 TEST_F(AreaCodeMapTest, TestLookupNumberIT) {
175   PhoneNumber number = MakePhoneNumber(39, 212345678L);
176   number.set_italian_leading_zero(true);
177   EXPECT_STREQ("Milan", map_IT_->Lookup(number));
178 
179   number.set_national_number(612345678L);
180   EXPECT_STREQ("Rome", map_IT_->Lookup(number));
181 
182   number.set_national_number(3211234L);
183   EXPECT_STREQ("Novara", map_IT_->Lookup(number));
184 
185   // A mobile number
186   number.set_national_number(321123456L);
187   number.set_italian_leading_zero(false);
188   EXPECT_STREQ(NULL, map_IT_->Lookup(number));
189 
190   // An invalid number (too short)
191   number.set_national_number(321123L);
192   number.set_italian_leading_zero(true);
193   EXPECT_STREQ("Novara", map_IT_->Lookup(number));
194 }
195 
196 }  // namespace phonenumbers
197 }  // namespace i18n
198