Merge "external/sqlite/android cleanup." am: d4c000d7ed am: c5d9748097
am: 7aa8f92192

Change-Id: I864b612991b7365ed5d49345016315f4de8c18af
diff --git a/android/Android.mk b/android/Android.mk
index 98617d6..9ba06c3 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -23,30 +23,11 @@
 LOCAL_MODULE:= libsqlite3_android
 include $(BUILD_HOST_STATIC_LIBRARY)
 
-# Test for PhoneNumberUtils
-#
-# You can also test this in Unix, like this:
-# > g++ -Wall external/sqlite/android/PhoneNumberUtils.cpp \
-#   external/sqlite/android/PhoneNumberUtilsTest.cpp
-# > ./a.out
-#
-# Note: This "test" is not recognized as a formal test. This is just for enabling developers
-#       to easily check what they modified works well or not.
 #       The formal test for phone_number_compare() is in DataBaseGeneralTest.java
 #       (as of 2009-08-02), in which phone_number_compare() is tested via sqlite's custom
 #       function "PHONE_NUMBER_COMPARE".
-#       Please add tests if you modify the implementation of PhoneNumberUtils.cpp and add
-#       test cases in PhoneNumberUtilsTest.cpp.
 include $(CLEAR_VARS)
-
 LOCAL_MODULE:= libsqlite3_phone_number_utils_test
-
 LOCAL_CFLAGS += -Wall -Werror
-
-LOCAL_SRC_FILES := \
-	PhoneNumberUtils.cpp \
-	PhoneNumberUtilsTest.cpp
-
-LOCAL_MODULE_TAGS := optional
-
-include $(BUILD_EXECUTABLE)
+LOCAL_SRC_FILES := PhoneNumberUtils.cpp PhoneNumberUtilsTest.cpp
+include $(BUILD_NATIVE_TEST)
diff --git a/android/PhoneNumberUtilsTest.cpp b/android/PhoneNumberUtilsTest.cpp
index cf67286..beb9c82 100644
--- a/android/PhoneNumberUtilsTest.cpp
+++ b/android/PhoneNumberUtilsTest.cpp
@@ -13,164 +13,131 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Note that similar (or almost same) tests exist in Java side (See
- * DatabaseGeneralTest.java in AndroidTests). The differences are:
- * - this test is quite easy to do (You can do it in your Unix PC)
- * - this test is not automatically executed by build servers
- *
- * You should also execute the test before submitting this.
- */
+
+//
+// Note that similar (or almost same) tests exist in Java side (See
+// DatabaseGeneralTest.java in AndroidTests). The differences are:
+// - this test is quite easy to do (You can do it in your Unix PC)
+// - this test is not automatically executed by build servers
+//
+// You should also execute the test before submitting this.
+//
 
 #include "PhoneNumberUtils.h"
 
 #include <stdio.h>
 #include <string.h>
 
+#include <gtest/gtest.h>
+
 using namespace android;
 
-#define PHONE_NUMBER_BUFFER_SIZE 6
+TEST(PhoneNumberUtils, phone_number_compare_strict) {
+    EXPECT_TRUE(phone_number_compare_strict(NULL, NULL));
+    EXPECT_TRUE(phone_number_compare_strict("", NULL));
+    EXPECT_TRUE(phone_number_compare_strict(NULL, ""));
+    EXPECT_TRUE(phone_number_compare_strict("", ""));
 
-#define EXPECT(function, input1, input2, expected, total, error)        \
-    ({                                                                  \
-        const char *i1_cache = input1;                                  \
-        const char *i2_cache = input2;                                  \
-        (total)++;                                                      \
-        if ((expected) != (function)((i1_cache), (i2_cache))) {         \
-            if (expected) {                                             \
-                printf("%s != %s while we expect %s == %s\n",           \
-                       (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
-            } else {                                                    \
-                printf("%s == %s while we expect %s != %s\n",           \
-                       (i1_cache), (i2_cache), (i1_cache), (i2_cache)); \
-            }                                                           \
-            (error)++;                                                  \
-        }                                                               \
-    })
+    EXPECT_TRUE(phone_number_compare_strict("999", "999"));
+    EXPECT_TRUE(phone_number_compare_strict("119", "119"));
 
-#define EXPECT_EQ(input1, input2)                                       \
-    EXPECT(phone_number_compare_strict, (input1), (input2), true,       \
-           (total), (error))
-
-
-#define EXPECT_NE(input1, input2)                                       \
-    EXPECT(phone_number_compare_strict, (input1), (input2), false,      \
-           (total), (error))
-
-#define ASSERT_STRIPPED_REVERSE(input, expected)                        \
-    ({                                                                  \
-        char out[PHONE_NUMBER_BUFFER_SIZE];                             \
-        int outlen;                                                     \
-        (total)++;                                                      \
-        phone_number_stripped_reversed_inter((input),                   \
-            out,                                                        \
-            PHONE_NUMBER_BUFFER_SIZE,                                   \
-            &outlen);                                                   \
-        out[outlen] = 0;                                                \
-        if (strcmp((expected), (out)) != 0) {                           \
-            printf("Expected: %s actual: %s\n", (expected), (out));     \
-            (error)++;                                                  \
-        }                                                               \
-     })
-
-int main() {
-    int total = 0;
-    int error = 0;
-
-    EXPECT_EQ(NULL, NULL);
-    EXPECT_EQ("", NULL);
-    EXPECT_EQ(NULL, "");
-    EXPECT_EQ("", "");
-
-    EXPECT_EQ("999", "999");
-    EXPECT_EQ("119", "119");
-
-    EXPECT_NE("123456789", "923456789");
-    EXPECT_NE("123456789", "123456781");
-    EXPECT_NE("123456789", "1234567890");
-    EXPECT_NE("123456789", "0123456789");
+    EXPECT_FALSE(phone_number_compare_strict("123456789", "923456789"));
+    EXPECT_FALSE(phone_number_compare_strict("123456789", "123456781"));
+    EXPECT_FALSE(phone_number_compare_strict("123456789", "1234567890"));
+    EXPECT_FALSE(phone_number_compare_strict("123456789", "0123456789"));
 
     // Google, Inc.
-    EXPECT_EQ("650-253-0000", "6502530000");
-    EXPECT_EQ("650-253-0000", "650 253 0000");
-    EXPECT_EQ("650 253 0000", "6502530000");
+    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "6502530000"));
+    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "650 253 0000"));
+    EXPECT_TRUE(phone_number_compare_strict("650 253 0000", "6502530000"));
 
     // trunk (NDD) prefix must be properly handled in US
-    EXPECT_EQ("650-253-0000", "1-650-253-0000");
-    EXPECT_EQ("650-253-0000", "   1-650-253-0000");
-    EXPECT_NE("650-253-0000", "11-650-253-0000");
-    EXPECT_NE("650-253-0000", "0-650-253-0000");
-    EXPECT_NE("555-4141", "+1-700-555-4141");
+    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "1-650-253-0000"));
+    EXPECT_TRUE(phone_number_compare_strict("650-253-0000", "   1-650-253-0000"));
+    EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "11-650-253-0000"));
+    EXPECT_FALSE(phone_number_compare_strict("650-253-0000", "0-650-253-0000"));
+    EXPECT_FALSE(phone_number_compare_strict("555-4141", "+1-700-555-4141"));
 
-    EXPECT_EQ("+1 650-253-0000", "6502530000");
-    EXPECT_EQ("001 650-253-0000", "6502530000");
-    EXPECT_EQ("0111 650-253-0000", "6502530000");
+    EXPECT_TRUE(phone_number_compare_strict("+1 650-253-0000", "6502530000"));
+    EXPECT_TRUE(phone_number_compare_strict("001 650-253-0000", "6502530000"));
+    EXPECT_TRUE(phone_number_compare_strict("0111 650-253-0000", "6502530000"));
 
     // Country code is different.
-    EXPECT_NE("+19012345678", "+819012345678");
+    EXPECT_FALSE(phone_number_compare_strict("+19012345678", "+819012345678"));
 
     // Russian trunk digit
-    EXPECT_EQ("+79161234567", "89161234567");
+    EXPECT_TRUE(phone_number_compare_strict("+79161234567", "89161234567"));
 
     // French trunk digit
-    EXPECT_EQ("+33123456789", "0123456789");
+    EXPECT_TRUE(phone_number_compare_strict("+33123456789", "0123456789"));
 
     // Trunk digit for city codes in the Netherlands
-    EXPECT_EQ("+31771234567", "0771234567");
+    EXPECT_TRUE(phone_number_compare_strict("+31771234567", "0771234567"));
 
     // Japanese dial
-    EXPECT_EQ("090-1234-5678", "+819012345678");
-    EXPECT_EQ("090(1234)5678", "+819012345678");
-    EXPECT_EQ("090-1234-5678", "+81-90-1234-5678");
+    EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+819012345678"));
+    EXPECT_TRUE(phone_number_compare_strict("090(1234)5678", "+819012345678"));
+    EXPECT_TRUE(phone_number_compare_strict("090-1234-5678", "+81-90-1234-5678"));
 
     // Trunk prefix must not be ignored in Japan
-    EXPECT_NE("090-1234-5678", "90-1234-5678");
+    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "90-1234-5678"));
 
-    EXPECT_NE("090-1234-5678", "080-1234-5678");
-    EXPECT_NE("090-1234-5678", "190-1234-5678");
-    EXPECT_NE("090-1234-5678", "890-1234-5678");
-    EXPECT_NE("+81-90-1234-5678", "+81-090-1234-5678");
+    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "080-1234-5678"));
+    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "190-1234-5678"));
+    EXPECT_FALSE(phone_number_compare_strict("090-1234-5678", "890-1234-5678"));
+    EXPECT_FALSE(phone_number_compare_strict("+81-90-1234-5678", "+81-090-1234-5678"));
 
-    EXPECT_EQ("+593(800)123-1234", "8001231234");
+    EXPECT_TRUE(phone_number_compare_strict("+593(800)123-1234", "8001231234"));
 
     // Two continuous 0 at the beginieng of the phone string should not be
     // treated as trunk prefix.
-    EXPECT_NE("008001231234", "8001231234");
+    EXPECT_FALSE(phone_number_compare_strict("008001231234", "8001231234"));
 
     // Test broken caller ID seen on call from Thailand to the US
-    EXPECT_EQ("+66811234567", "166811234567");
+    EXPECT_TRUE(phone_number_compare_strict("+66811234567", "166811234567"));
 
     // Confirm that the bug found before does not re-appear.
-    EXPECT_NE("080-1234-5678", "+819012345678");
-    EXPECT_EQ("650-000-3456", "16500003456");
-    EXPECT_EQ("011 1 7005554141", "+17005554141");
-    EXPECT_NE("011 11 7005554141", "+17005554141");
-    EXPECT_NE("+44 207 792 3490", "00 207 792 3490");
+    EXPECT_FALSE(phone_number_compare_strict("080-1234-5678", "+819012345678"));
+    EXPECT_TRUE(phone_number_compare_strict("650-000-3456", "16500003456"));
+    EXPECT_TRUE(phone_number_compare_strict("011 1 7005554141", "+17005554141"));
+    EXPECT_FALSE(phone_number_compare_strict("011 11 7005554141", "+17005554141"));
+    EXPECT_FALSE(phone_number_compare_strict("+44 207 792 3490", "00 207 792 3490"));
     // This is not related to Thailand case. NAMP "1" + region code "661".
-    EXPECT_EQ("16610001234", "6610001234");
+    EXPECT_TRUE(phone_number_compare_strict("16610001234", "6610001234"));
 
     // We also need to compare two alpha addresses to make sure two different strings
     // aren't treated as the same addresses. This is relevant to SMS as SMS sender may
     // contain all alpha chars.
-    EXPECT_NE("abcd", "bcde");
+    EXPECT_FALSE(phone_number_compare_strict("abcd", "bcde"));
 
     // in the U.S. people often use alpha in the phone number to easily remember it
     // (e.g. 800-flowers would be dialed as 800-356-9377). Since we accept this form of
     // phone number in Contacts and others, we should make sure the comparison method
     // handle them.
-    EXPECT_EQ("1-800-flowers", "800-flowers");
+    EXPECT_TRUE(phone_number_compare_strict("1-800-flowers", "800-flowers"));
 
     // TODO: we currently do not support this comparison. It maybe nice to support this
     // TODO: in the future.
-    // EXPECT_EQ("1-800-flowers", "1-800-356-9377")
+    // EXPECT_TRUE("1-800-flowers", "1-800-356-9377")
 
-    EXPECT_NE("1-800-flowers", "1-800-abcdefg");
+    EXPECT_FALSE(phone_number_compare_strict("1-800-flowers", "1-800-abcdefg"));
 
     // Currently we cannot get this test through (Japanese trunk prefix is 0,
     // but there is no sensible way to know it now (as of 2009-6-12)...
-    // EXPECT_NE("290-1234-5678", "+819012345678");
+    // EXPECT_FALSE("290-1234-5678", "+819012345678");
+}
+
+TEST(PhoneNumberUtils, phone_number_stripped_reversed_inter) {
+    char out[6];
+    int outlen;
+
+#define ASSERT_STRIPPED_REVERSE(input, expected) \
+    phone_number_stripped_reversed_inter((input), out, sizeof(out), &outlen); \
+    out[outlen] = 0; \
+    ASSERT_STREQ((expected), (out)); \
 
     ASSERT_STRIPPED_REVERSE("", "");
+
     ASSERT_STRIPPED_REVERSE("123", "321");
     ASSERT_STRIPPED_REVERSE("123*N#", "#N*321");
 
@@ -185,11 +152,4 @@
 
     // Ignoring non-dialable
     ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321");
-
-    printf("total: %d, error: %d\n\n", total, error);
-    if (error == 0) {
-        printf("Success!\n");
-    } else {
-        printf("Failure... :(\n");
-    }
 }
diff --git a/android/sqlite3_android.cpp b/android/sqlite3_android.cpp
index b836952..7356f9d 100644
--- a/android/sqlite3_android.cpp
+++ b/android/sqlite3_android.cpp
@@ -415,10 +415,7 @@
 
 extern "C" int register_localized_collators(sqlite3* handle, const char* systemLocale, int utf16Storage)
 {
-    int err;
     UErrorCode status = U_ZERO_ERROR;
-    void* icudata;
-
     UCollator* collator = ucol_open(systemLocale, &status);
     if (U_FAILURE(status)) {
         return -1;
@@ -429,10 +426,7 @@
         return -1;
     }
 
-    status = U_ZERO_ERROR;
-    char buf[1024];
-    ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status);
-
+    int err;
     if (utf16Storage) {
         err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
                 collate16, (void(*)(void*))localized_collator_dtor);
@@ -459,7 +453,6 @@
         return err;
     }
 
-
     //// PHONEBOOK_COLLATOR
     status = U_ZERO_ERROR;
     collator = ucol_open(systemLocale, &status);
@@ -473,8 +466,6 @@
         return -1;
     }
 
-    status = U_ZERO_ERROR;
-    // ucol_getShortDefinitionString(collator, NULL, buf, 1024, &status);
     if (utf16Storage) {
         err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator,
                 collate16, (void(*)(void*))localized_collator_dtor);
@@ -552,8 +543,9 @@
 #endif
 
     // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
-    // PhoneNumberUtils.getStrippedReversed.  This function is not public API,
-    // it is only used for compatibility with Android 1.6 and earlier.
+    // PhoneNumberUtils.getStrippedReversed.  This function is used by
+    // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java
+    // to provide compatibility with Android 1.6 and earlier.
     err = sqlite3_create_function(handle,
         "_PHONE_NUMBER_STRIPPED_REVERSED",
         1, SQLITE_UTF8, NULL,