PhoneNumberUtilsTest.cpp revision de43094477419f8a190a6f97b47d346827310a02
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 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
58int 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    EXPECT_EQ("+1 650-253-0000", "6502530000");
81    EXPECT_EQ("001 650-253-0000", "6502530000");
82    EXPECT_EQ("0111 650-253-0000", "6502530000");
83
84    // Country code is different.
85    EXPECT_NE("+19012345678", "+819012345678");
86
87    // Russian trunk digit
88    EXPECT_EQ("+79161234567", "89161234567");
89
90    // French trunk digit
91    EXPECT_EQ("+33123456789", "0123456789");
92
93    // Trunk digit for city codes in the Netherlands
94    EXPECT_EQ("+31771234567", "0771234567");
95
96    // Japanese dial
97    EXPECT_EQ("090-1234-5678", "+819012345678");
98    EXPECT_EQ("090(1234)5678", "+819012345678");
99    EXPECT_EQ("090-1234-5678", "+81-90-1234-5678");
100
101    EXPECT_NE("090-1234-5678", "90-1234-5678");
102    EXPECT_NE("090-1234-5678", "080-1234-5678");
103    EXPECT_NE("090-1234-5678", "190-1234-5678");
104    EXPECT_NE("090-1234-5678", "890-1234-5678");
105    EXPECT_NE("+81-90-1234-5678", "+81-090-1234-5678");
106
107    EXPECT_EQ("+593(800)123-1234", "8001231234");
108
109    // Two continuous 0 at the beginieng of the phone string should not be
110    // treated as trunk prefix.
111    EXPECT_NE("008001231234", "8001231234");
112
113    // Test broken caller ID seen on call from Thailand to the US
114    EXPECT_EQ("+66811234567", "166811234567");
115
116    // Confirm that the bug found before does not re-appear.
117    EXPECT_NE("080-1234-5678", "+819012345678");
118
119    // Currently we cannot get this test through (Japanese trunk prefix is 0,
120    // but there is no sensible way to know it now (as of 2009-6-12)...
121    // EXPECT_NE("290-1234-5678", "+819012345678");
122
123    printf("total: %d, error: %d\n\n", total, error);
124    if (error == 0) {
125        printf("Success!\n");
126    } else {
127        printf("Failure... :(\n");
128    }
129}
130