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
30using namespace android;
31
32#define PHONE_NUMBER_BUFFER_SIZE 6
33
34#define EXPECT(function, input1, input2, expected, total, error)        \
35    ({                                                                  \
36        const char *i1_cache = input1;                                  \
37        const char *i2_cache = input2;                                  \
38        (total)++;                                                      \
39        if ((expected) != (function)((i1_cache), (i2_cache))) {         \
40            if (expected) {                                             \
41                printf("%s != %s while we expect %s == %s\n",           \
42                       (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
43            } else {                                                    \
44                printf("%s == %s while we expect %s != %s\n",           \
45                       (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
46            }                                                           \
47            (error)++;                                                  \
48        }                                                               \
49    })
50
51#define EXPECT_EQ(input1, input2)                                       \
52    EXPECT(phone_number_compare_strict, (input1), (input2), true,       \
53           (total), (error))
54
55
56#define EXPECT_NE(input1, input2)                                       \
57    EXPECT(phone_number_compare_strict, (input1), (input2), false,      \
58           (total), (error))
59
60#define ASSERT_STRIPPED_REVERSE(input, expected)                        \
61    ({                                                                  \
62        char out[PHONE_NUMBER_BUFFER_SIZE];                             \
63        int outlen;                                                     \
64        (total)++;                                                      \
65        phone_number_stripped_reversed_inter((input),                   \
66            out,                                                        \
67            PHONE_NUMBER_BUFFER_SIZE,                                   \
68            &outlen);                                                   \
69        out[outlen] = 0;                                                \
70        if (strcmp((expected), (out)) != 0) {                           \
71            printf("Expected: %s actual: %s\n", (expected), (out));     \
72            (error)++;                                                  \
73        }                                                               \
74     })
75
76int main() {
77    int total = 0;
78    int error = 0;
79
80    EXPECT_EQ(NULL, NULL);
81    EXPECT_EQ("", NULL);
82    EXPECT_EQ(NULL, "");
83    EXPECT_EQ("", "");
84
85    EXPECT_EQ("999", "999");
86    EXPECT_EQ("119", "119");
87
88    EXPECT_NE("123456789", "923456789");
89    EXPECT_NE("123456789", "123456781");
90    EXPECT_NE("123456789", "1234567890");
91    EXPECT_NE("123456789", "0123456789");
92
93    // Google, Inc.
94    EXPECT_EQ("650-253-0000", "6502530000");
95    EXPECT_EQ("650-253-0000", "650 253 0000");
96    EXPECT_EQ("650 253 0000", "6502530000");
97
98    // trunk (NDD) prefix must be properly handled in US
99    EXPECT_EQ("650-253-0000", "1-650-253-0000");
100    EXPECT_EQ("650-253-0000", "   1-650-253-0000");
101    EXPECT_NE("650-253-0000", "11-650-253-0000");
102    EXPECT_NE("650-253-0000", "0-650-253-0000");
103    EXPECT_NE("555-4141", "+1-700-555-4141");
104
105    EXPECT_EQ("+1 650-253-0000", "6502530000");
106    EXPECT_EQ("001 650-253-0000", "6502530000");
107    EXPECT_EQ("0111 650-253-0000", "6502530000");
108
109    // Country code is different.
110    EXPECT_NE("+19012345678", "+819012345678");
111
112    // Russian trunk digit
113    EXPECT_EQ("+79161234567", "89161234567");
114
115    // French trunk digit
116    EXPECT_EQ("+33123456789", "0123456789");
117
118    // Trunk digit for city codes in the Netherlands
119    EXPECT_EQ("+31771234567", "0771234567");
120
121    // Japanese dial
122    EXPECT_EQ("090-1234-5678", "+819012345678");
123    EXPECT_EQ("090(1234)5678", "+819012345678");
124    EXPECT_EQ("090-1234-5678", "+81-90-1234-5678");
125
126    // Trunk prefix must not be ignored in Japan
127    EXPECT_NE("090-1234-5678", "90-1234-5678");
128
129    EXPECT_NE("090-1234-5678", "080-1234-5678");
130    EXPECT_NE("090-1234-5678", "190-1234-5678");
131    EXPECT_NE("090-1234-5678", "890-1234-5678");
132    EXPECT_NE("+81-90-1234-5678", "+81-090-1234-5678");
133
134    EXPECT_EQ("+593(800)123-1234", "8001231234");
135
136    // Two continuous 0 at the beginieng of the phone string should not be
137    // treated as trunk prefix.
138    EXPECT_NE("008001231234", "8001231234");
139
140    // Test broken caller ID seen on call from Thailand to the US
141    EXPECT_EQ("+66811234567", "166811234567");
142
143    // Confirm that the bug found before does not re-appear.
144    EXPECT_NE("080-1234-5678", "+819012345678");
145    EXPECT_EQ("650-000-3456", "16500003456");
146    EXPECT_EQ("011 1 7005554141", "+17005554141");
147    EXPECT_NE("011 11 7005554141", "+17005554141");
148    EXPECT_NE("+44 207 792 3490", "00 207 792 3490");
149    // This is not related to Thailand case. NAMP "1" + region code "661".
150    EXPECT_EQ("16610001234", "6610001234");
151
152    // We also need to compare two alpha addresses to make sure two different strings
153    // aren't treated as the same addresses. This is relevant to SMS as SMS sender may
154    // contain all alpha chars.
155    EXPECT_NE("abcd", "bcde");
156
157    // in the U.S. people often use alpha in the phone number to easily remember it
158    // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of
159    // phone number in Contacts and others, we should make sure the comparison method
160    // handle them.
161    EXPECT_EQ("1-800-flowers", "800-flowers");
162
163    // TODO: we currently do not support this comparison. It maybe nice to support this
164    // TODO: in the future.
165    // EXPECT_EQ("1-800-flowers", "1-800-356-9377")
166
167    EXPECT_NE("1-800-flowers", "1-800-abcdefg");
168
169    // Currently we cannot get this test through (Japanese trunk prefix is 0,
170    // but there is no sensible way to know it now (as of 2009-6-12)...
171    // EXPECT_NE("290-1234-5678", "+819012345678");
172
173    ASSERT_STRIPPED_REVERSE("", "");
174    ASSERT_STRIPPED_REVERSE("123", "321");
175    ASSERT_STRIPPED_REVERSE("123*N#", "#N*321");
176
177    // Buffer overflow
178    ASSERT_STRIPPED_REVERSE("1234567890", "098765");
179
180    // Only one plus is copied
181    ASSERT_STRIPPED_REVERSE("1+2+", "+21");
182
183    // Pause/wait in the phone number
184    ASSERT_STRIPPED_REVERSE("12;34", "21");
185
186    // Ignoring non-dialable
187    ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321");
188
189    printf("total: %d, error: %d\n\n", total, error);
190    if (error == 0) {
191        printf("Success!\n");
192    } else {
193        printf("Failure... :(\n");
194    }
195}
196