• 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 #include <cmath>
18 #include <set>
19 #include <string>
20 
21 #include <gtest/gtest.h>  // NOLINT(build/include_order)
22 
23 #include "phonenumbers/base/basictypes.h"
24 #include "phonenumbers/geocoding/geocoding_data.h"
25 #include "phonenumbers/geocoding/geocoding_test_data.h"
26 
27 namespace i18n {
28 namespace phonenumbers {
29 
30 using std::set;
31 using std::string;
32 
33 namespace {
34 
35 typedef const CountryLanguages* (*country_languages_getter)(int index);
36 typedef const PrefixDescriptions* (*prefix_descriptions_getter)(int index);
37 
TestCountryLanguages(const CountryLanguages * languages)38 void TestCountryLanguages(const CountryLanguages* languages) {
39   EXPECT_GT(languages->available_languages_size, 0);
40   for (int i = 0; i < languages->available_languages_size; ++i) {
41     string language(languages->available_languages[i]);
42     EXPECT_GT(language.size(), 0);
43     if (i > 0) {
44       EXPECT_LT(string(languages->available_languages[i - 1]),
45                 language);
46     }
47   }
48 }
49 
TestCountryCallingCodeLanguages(const int * country_calling_codes,int country_calling_codes_size,country_languages_getter get_country_languages)50 void TestCountryCallingCodeLanguages(
51     const int* country_calling_codes, int country_calling_codes_size,
52     country_languages_getter get_country_languages) {
53   EXPECT_GT(country_calling_codes_size, 0);
54   for (int i = 0; i < country_calling_codes_size; ++i) {
55     int code = country_calling_codes[i];
56     EXPECT_GT(code, 0);
57     if (i > 0) {
58       EXPECT_LT(country_calling_codes[i-1], code);
59     }
60     TestCountryLanguages(get_country_languages(i));
61   }
62 }
63 
TestPrefixDescriptions(const PrefixDescriptions * descriptions)64 void TestPrefixDescriptions(const PrefixDescriptions* descriptions) {
65   EXPECT_GT(descriptions->prefixes_size, 0);
66   set<int> possible_lengths;
67   for (int i = 0; i < descriptions->prefixes_size; ++i) {
68     int prefix = descriptions->prefixes[i];
69     EXPECT_GT(prefix, 0);
70     if (i > 0) {
71       EXPECT_LT(descriptions->prefixes[i - 1], prefix);
72     }
73     possible_lengths.insert(log10(prefix) + 1);
74   }
75 
76   EXPECT_GT(descriptions->possible_lengths_size, 0);
77   for (int i = 0; i < descriptions->possible_lengths_size; ++i) {
78     int possible_length = descriptions->possible_lengths[i];
79     EXPECT_GT(possible_length, 0);
80     if (i > 0) {
81       EXPECT_LT(descriptions->possible_lengths[i - 1], possible_length);
82     }
83     EXPECT_TRUE(
84         possible_lengths.find(possible_length) != possible_lengths.end());
85   }
86 }
87 
TestAllPrefixDescriptions(const char ** prefix_language_code_pairs,int prefix_language_code_pairs_size,prefix_descriptions_getter get_prefix_descriptions)88 void TestAllPrefixDescriptions(
89     const char** prefix_language_code_pairs,
90     int prefix_language_code_pairs_size,
91     prefix_descriptions_getter get_prefix_descriptions) {
92   EXPECT_GT(prefix_language_code_pairs_size, 0);
93   for (int i = 0; i < prefix_language_code_pairs_size; ++i) {
94     string language_code_pair(prefix_language_code_pairs[i]);
95     EXPECT_GT(language_code_pair.size(), 0);
96     if (i > 0) {
97       EXPECT_LT(string(prefix_language_code_pairs[i - 1]),
98                 language_code_pair);
99     }
100     TestPrefixDescriptions(get_prefix_descriptions(i));
101   }
102 }
103 
104 }  // namespace
105 
TEST(GeocodingDataTest,TestCountryCallingCodeLanguages)106 TEST(GeocodingDataTest, TestCountryCallingCodeLanguages) {
107   TestCountryCallingCodeLanguages(get_country_calling_codes(),
108                                   get_country_calling_codes_size(),
109                                   get_country_languages);
110 }
111 
TEST(GeocodingDataTest,TestTestCountryCallingCodeLanguages)112 TEST(GeocodingDataTest, TestTestCountryCallingCodeLanguages) {
113   TestCountryCallingCodeLanguages(get_test_country_calling_codes(),
114                                   get_test_country_calling_codes_size(),
115                                   get_test_country_languages);
116 }
117 
TEST(GeocodingDataTest,TestPrefixDescriptions)118 TEST(GeocodingDataTest, TestPrefixDescriptions) {
119   TestAllPrefixDescriptions(get_prefix_language_code_pairs(),
120                             get_prefix_language_code_pairs_size(),
121                             get_prefix_descriptions);
122 }
123 
124 
TEST(GeocodingDataTest,TestTestPrefixDescriptions)125 TEST(GeocodingDataTest, TestTestPrefixDescriptions) {
126   TestAllPrefixDescriptions(get_test_prefix_language_code_pairs(),
127                             get_test_prefix_language_code_pairs_size(),
128                             get_test_prefix_descriptions);
129 }
130 
TEST(GeocodingDataTest,TestTestGeocodingData)131 TEST(GeocodingDataTest, TestTestGeocodingData) {
132   ASSERT_EQ(3, get_test_country_calling_codes_size());
133   const int* country_calling_codes = get_test_country_calling_codes();
134   const int expected_calling_codes[] = {1, 54, 82};
135   for (int i = 0; i < get_test_country_calling_codes_size(); ++i) {
136     EXPECT_EQ(expected_calling_codes[i], country_calling_codes[i]);
137   }
138 
139   const CountryLanguages* langs_1 = get_test_country_languages(0);
140   ASSERT_EQ(2, langs_1->available_languages_size);
141   const char* expected_languages[] = {"de", "en"};
142   for (int i = 0; i < langs_1->available_languages_size; ++i) {
143     EXPECT_STREQ(expected_languages[i], langs_1->available_languages[i]);
144   }
145 
146   ASSERT_EQ(5, get_test_prefix_language_code_pairs_size());
147   const char** language_code_pairs = get_test_prefix_language_code_pairs();
148   const char* expected_language_code_pairs[] = {
149     "1_de", "1_en", "54_en", "82_en", "82_ko",
150   };
151   for (int i = 0; i < get_test_prefix_language_code_pairs_size(); ++i) {
152     EXPECT_STREQ(expected_language_code_pairs[i], language_code_pairs[i]);
153   }
154 
155   const PrefixDescriptions* desc_1_de = get_test_prefix_descriptions(0);
156   ASSERT_EQ(2, desc_1_de->prefixes_size);
157   const int32 expected_prefixes[] = {1201, 1650};
158   const char* expected_descriptions[] = {
159     "New Jersey",
160     "Kalifornien",
161   };
162   for (int i = 0; i < desc_1_de->prefixes_size; ++i) {
163     EXPECT_EQ(expected_prefixes[i], desc_1_de->prefixes[i]);
164     EXPECT_STREQ(expected_descriptions[i], desc_1_de->descriptions[i]);
165   }
166 
167   ASSERT_EQ(1, desc_1_de->possible_lengths_size);
168   const int expected_lengths[] = {4};
169   for (int i = 0; i < desc_1_de->possible_lengths_size; ++i) {
170     EXPECT_EQ(expected_lengths[i], desc_1_de->possible_lengths[i]);
171   }
172 }
173 
174 }  // namespace phonenumbers
175 }  // namespace i18n
176