PhoneNumberUtilsTest.cpp revision f06f5fa351ded0b9e04e15efa7e34f0f085fc9df
1f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa/* 2f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Copyright (C) 2009 The Android Open Source Project 3f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * 4f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Licensed under the Apache License, Version 2.0 (the "License"); 5f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * you may not use this file except in compliance with the License. 6f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * You may obtain a copy of the License at 7f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * 8f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * http://www.apache.org/licenses/LICENSE-2.0 9f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * 10f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Unless required by applicable law or agreed to in writing, software 11f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * distributed under the License is distributed on an "AS IS" BASIS, 12f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * See the License for the specific language governing permissions and 14f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * limitations under the License. 15f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa */ 16f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa/* 17f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * Note that similar (or almost same) tests exist in Java side (See 18f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * DatabaseGeneralTest.java in AndroidTests). The differences are: 19f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * - this test is quite easy to do (You can do it in your Unix PC) 20f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * - this test is not automatically executed by build servers 21f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * 22f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa * You should also execute the test before submitting this. 23f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa */ 24f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 25f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include "PhoneNumberUtils.h" 26f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 27f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include <stdio.h> 28f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#include <string.h> 29f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 30f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawausing namespace android; 31f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 32f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#define EXPECT(function, input1, input2, expected, total, error) \ 33f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa ({ \ 34f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa const char *i1_cache = input1; \ 35f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa const char *i2_cache = input2; \ 36f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (total)++; \ 37f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa if ((expected) != (function)((i1_cache), (i2_cache))) { \ 38f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa if (expected) { \ 39f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa printf("%s != %s while we expect %s == %s\n", \ 40f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \ 41f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa } else { \ 42f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa printf("%s == %s while we expect %s != %s\n", \ 43f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \ 44f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa } \ 45f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (error)++; \ 46f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa } \ 47f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa }) 48f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 49f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#define EXPECT_EQ(input1, input2) \ 50f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT(phone_number_compare, (input1), (input2), true, \ 51f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (total), (error)) 52f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 53f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 54f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa#define EXPECT_NE(input1, input2) \ 55f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT(phone_number_compare, (input1), (input2), false, \ 56f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa (total), (error)) 57f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 58f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawaint main() { 59f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa int total = 0; 60f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa int error = 0; 61f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 62f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ(NULL, NULL); 63f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("", NULL); 64f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ(NULL, ""); 65f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("", ""); 66f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 67f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("999", "999"); 68f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("119", "119"); 69f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 70f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("123456789", "923456789"); 71f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("123456789", "123456781"); 72f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("123456789", "1234567890"); 73f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("123456789", "0123456789"); 74f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 75f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Google, Inc. 76f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("650-253-0000", "6502530000"); 77f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("650-253-0000", "650 253 0000"); 78f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("650 253 0000", "6502530000"); 79f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 80f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // trunk (NDD) prefix must be properly handled in US 81f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("650-253-0000", "1-650-253-0000"); 82f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("650-253-0000", " 1-650-253-0000"); 83f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("650-253-0000", "11-650-253-0000"); 84f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("650-253-0000", "0-650-253-0000"); 85f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 86f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+1 650-253-0000", "6502530000"); 87f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("001 650-253-0000", "6502530000"); 88f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("0111 650-253-0000", "6502530000"); 89f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 90f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Country code is different. 91f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("+19012345678", "+819012345678"); 92f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 93f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Russian trunk digit 94f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+79161234567", "89161234567"); 95f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 96f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // French trunk digit 97f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+33123456789", "0123456789"); 98f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 99f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Trunk digit for city codes in the Netherlands 100f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+31771234567", "0771234567"); 101f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 102f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Japanese dial 103f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("090-1234-5678", "+819012345678"); 104f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("090(1234)5678", "+819012345678"); 105f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("090-1234-5678", "+81-90-1234-5678"); 106f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 107f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Trunk prefix must not be ignored in Japan 108f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("090-1234-5678", "90-1234-5678"); 109f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 110f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("090-1234-5678", "080-1234-5678"); 111f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("090-1234-5678", "190-1234-5678"); 112f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("090-1234-5678", "890-1234-5678"); 113f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("+81-90-1234-5678", "+81-090-1234-5678"); 114f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 115f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+593(800)123-1234", "8001231234"); 116f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 117f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Two continuous 0 at the beginieng of the phone string should not be 118f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // treated as trunk prefix. 119f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("008001231234", "8001231234"); 120f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 121f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Test broken caller ID seen on call from Thailand to the US 122f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_EQ("+66811234567", "166811234567"); 123f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 124f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Confirm that the bug found before does not re-appear. 125f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa EXPECT_NE("080-1234-5678", "+819012345678"); 126f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 127f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // Currently we cannot get this test through (Japanese trunk prefix is 0, 128f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // but there is no sensible way to know it now (as of 2009-6-12)... 129f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa // EXPECT_NE("290-1234-5678", "+819012345678"); 130f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa 131f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa printf("total: %d, error: %d\n\n", total, error); 132f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa if (error == 0) { 133f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa printf("Success!\n"); 134f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa } else { 135f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa printf("Failure... :(\n"); 136f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa } 137f06f5fa351ded0b9e04e15efa7e34f0f085fc9dfDaisuke Miyakawa} 138