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