1 // Copyright (C) 2012 The Libphonenumber Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Author: Patrick Mezard
17 // Basic test cases for MappingFileProvider.
19 #include "phonenumbers/geocoding/area_code_map.h"
24 #include <gtest/gtest.h> // NOLINT(build/include_order)
26 #include "phonenumbers/phonenumber.pb.h"
29 namespace phonenumbers {
37 void MakeCodeMap(const map<int, string>& m, scoped_ptr<AreaCodeMap>* code_map) {
38 scoped_ptr<AreaCodeMap> cm(new AreaCodeMap());
39 cm->ReadAreaCodeMap(m);
43 void MakeCodeMapUS(scoped_ptr<AreaCodeMap>* code_map) {
47 m[1650] = "California";
49 m[1201664] = "Westwood, NJ";
50 m[1480893] = "Phoenix, AZ";
51 m[1501372] = "Little Rock, AR";
52 m[1626308] = "Alhambra, CA";
53 m[1650345] = "San Mateo, CA";
54 m[1867993] = "Dawson, YT";
55 m[1972480] = "Richardson, TX";
56 MakeCodeMap(m, code_map);
59 void MakeCodeMapIT(scoped_ptr<AreaCodeMap>* code_map) {
64 m[390131] = "Alessandria";
66 m[390975] = "Potenza";
67 MakeCodeMap(m, code_map);
70 PhoneNumber MakePhoneNumber(int32 country_code, uint64 national_number) {
72 number.set_country_code(country_code);
73 number.set_national_number(national_number);
79 class AreaCodeMapTest : public testing::Test {
81 virtual void SetUp() {
82 MakeCodeMapUS(&map_US_);
83 MakeCodeMapIT(&map_IT_);
86 scoped_ptr<AreaCodeMap> map_US_;
87 scoped_ptr<AreaCodeMap> map_IT_;
90 TEST_F(AreaCodeMapTest, TestLookupInvalidNumberUS) {
91 EXPECT_EQ("New York", *map_US_->Lookup(MakePhoneNumber(1, 2121234567L)));
94 TEST_F(AreaCodeMapTest, TestLookupNumberNJ) {
95 EXPECT_EQ("Westwood, NJ", *map_US_->Lookup(MakePhoneNumber(1, 2016641234L)));
98 TEST_F(AreaCodeMapTest, TestLookupNumberNY) {
99 EXPECT_EQ("New York", *map_US_->Lookup(MakePhoneNumber(1, 2126641234L)));
102 TEST_F(AreaCodeMapTest, TestLookupNumberCA1) {
103 EXPECT_EQ("San Mateo, CA", *map_US_->Lookup(MakePhoneNumber(1, 6503451234L)));
106 TEST_F(AreaCodeMapTest, TestLookupNumberCA2) {
107 EXPECT_EQ("California", *map_US_->Lookup(MakePhoneNumber(1, 6502531234L)));
110 TEST_F(AreaCodeMapTest, TestLookupNumberTX) {
111 EXPECT_EQ("Richardson, TX",
112 *map_US_->Lookup(MakePhoneNumber(1, 9724801234L)));
115 TEST_F(AreaCodeMapTest, TestLookupNumberNotFoundTX) {
116 EXPECT_EQ(NULL, map_US_->Lookup(MakePhoneNumber(1, 9724811234L)));
119 TEST_F(AreaCodeMapTest, TestLookupNumberCH) {
120 EXPECT_EQ(NULL, map_US_->Lookup(MakePhoneNumber(41, 446681300L)));
123 TEST_F(AreaCodeMapTest, TestLookupNumberIT) {
124 PhoneNumber number = MakePhoneNumber(39, 212345678L);
125 number.set_italian_leading_zero(true);
126 EXPECT_EQ("Milan", *map_IT_->Lookup(number));
128 number.set_national_number(612345678L);
129 EXPECT_EQ("Rome", *map_IT_->Lookup(number));
131 number.set_national_number(3211234L);
132 EXPECT_EQ("Novara", *map_IT_->Lookup(number));
135 number.set_national_number(321123456L);
136 number.set_italian_leading_zero(false);
137 EXPECT_EQ(NULL, map_IT_->Lookup(number));
139 // An invalid number (too short)
140 number.set_national_number(321123L);
141 number.set_italian_leading_zero(true);
142 EXPECT_EQ("Novara", *map_IT_->Lookup(number));
145 } // namespace phonenumbers