• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
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  * Note that similar (or almost same) tests exist in Java side (See
18  * DatabaseGeneralTest.java in AndroidTests). The differences are:
19  * - this test is quite easy to do (You can do it in your Unix PC)
20  * - this test is not automatically executed by build servers
21  *
22  * You should also execute the test before submitting this.
23  */
24 
25 #include "PhoneNumberUtils.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 using namespace android;
31 
32 #define EXPECT(function, input1, input2, expected, total, error)        \
33     ({                                                                  \
34         const char *i1_cache = input1;                                  \
35         const char *i2_cache = input2;                                  \
36         (total)++;                                                      \
37         if ((expected) != (function)((i1_cache), (i2_cache))) {         \
38             if (expected) {                                             \
39                 printf("%s != %s while we expect %s == %s\n",           \
40                        (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
41             } else {                                                    \
42                 printf("%s == %s while we expect %s != %s\n",           \
43                        (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
44             }                                                           \
45             (error)++;                                                  \
46         }                                                               \
47     })
48 
49 #define EXPECT_EQ(input1, input2)                                       \
50     EXPECT(phone_number_compare, (input1), (input2), true,              \
51            (total), (error))
52 
53 
54 #define EXPECT_NE(input1, input2)                                       \
55     EXPECT(phone_number_compare, (input1), (input2), false,             \
56            (total), (error))
57 
main()58 int main() {
59     int total = 0;
60     int error = 0;
61 
62     EXPECT_EQ(NULL, NULL);
63     EXPECT_EQ("", NULL);
64     EXPECT_EQ(NULL, "");
65     EXPECT_EQ("", "");
66 
67     EXPECT_EQ("999", "999");
68     EXPECT_EQ("119", "119");
69 
70     EXPECT_NE("123456789", "923456789");
71     EXPECT_NE("123456789", "123456781");
72     EXPECT_NE("123456789", "1234567890");
73     EXPECT_NE("123456789", "0123456789");
74 
75     // Google, Inc.
76     EXPECT_EQ("650-253-0000", "6502530000");
77     EXPECT_EQ("650-253-0000", "650 253 0000");
78     EXPECT_EQ("650 253 0000", "6502530000");
79 
80     // trunk (NDD) prefix must be properly handled in US
81     EXPECT_EQ("650-253-0000", "1-650-253-0000");
82     EXPECT_EQ("650-253-0000", "   1-650-253-0000");
83     EXPECT_NE("650-253-0000", "11-650-253-0000");
84     EXPECT_NE("650-253-0000", "0-650-253-0000");
85 
86     EXPECT_EQ("+1 650-253-0000", "6502530000");
87     EXPECT_EQ("001 650-253-0000", "6502530000");
88     EXPECT_EQ("0111 650-253-0000", "6502530000");
89 
90     // Country code is different.
91     EXPECT_NE("+19012345678", "+819012345678");
92 
93     // Russian trunk digit
94     EXPECT_EQ("+79161234567", "89161234567");
95 
96     // French trunk digit
97     EXPECT_EQ("+33123456789", "0123456789");
98 
99     // Trunk digit for city codes in the Netherlands
100     EXPECT_EQ("+31771234567", "0771234567");
101 
102     // Japanese dial
103     EXPECT_EQ("090-1234-5678", "+819012345678");
104     EXPECT_EQ("090(1234)5678", "+819012345678");
105     EXPECT_EQ("090-1234-5678", "+81-90-1234-5678");
106 
107     // Trunk prefix must not be ignored in Japan
108     EXPECT_NE("090-1234-5678", "90-1234-5678");
109 
110     EXPECT_NE("090-1234-5678", "080-1234-5678");
111     EXPECT_NE("090-1234-5678", "190-1234-5678");
112     EXPECT_NE("090-1234-5678", "890-1234-5678");
113     EXPECT_NE("+81-90-1234-5678", "+81-090-1234-5678");
114 
115     EXPECT_EQ("+593(800)123-1234", "8001231234");
116 
117     // Two continuous 0 at the beginieng of the phone string should not be
118     // treated as trunk prefix.
119     EXPECT_NE("008001231234", "8001231234");
120 
121     // Test broken caller ID seen on call from Thailand to the US
122     EXPECT_EQ("+66811234567", "166811234567");
123 
124     // Confirm that the bug found before does not re-appear.
125     EXPECT_NE("080-1234-5678", "+819012345678");
126     EXPECT_EQ("650-000-3456", "16500003456");
127     EXPECT_EQ("16610001234", "6610001234");
128 
129     // We also need to compare two alpha addresses to make sure two different strings
130     // aren't treated as the same addresses. This is relevant to SMS as SMS sender may
131     // contain all alpha chars.
132     EXPECT_NE("abcd", "bcde");
133 
134     // in the U.S. people often use alpha in the phone number to easily remember it
135     // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of
136     // phone number in Contacts and others, we should make sure the comparison method
137     // handle them.
138     EXPECT_EQ("1-800-flowers", "800-flowers");
139 
140     // TODO: we currently do not support this comparison. It maybe nice to support this
141     // TODO: in the future.
142     // EXPECT_EQ("1-800-flowers", "1-800-356-9377")
143 
144     EXPECT_NE("1-800-flowers", "1-800-abcdefg");
145 
146     // Currently we cannot get this test through (Japanese trunk prefix is 0,
147     // but there is no sensible way to know it now (as of 2009-6-12)...
148     // EXPECT_NE("290-1234-5678", "+819012345678");
149 
150     printf("total: %d, error: %d\n\n", total, error);
151     if (error == 0) {
152         printf("Success!\n");
153     } else {
154         printf("Failure... :(\n");
155     }
156 }
157