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
17package com.android.providers.contacts.aggregation.util;
18
19import android.test.suitebuilder.annotation.SmallTest;
20
21import com.android.providers.contacts.NameNormalizer;
22import com.android.providers.contacts.util.Hex;
23
24import junit.framework.TestCase;
25
26/**
27 * Unit tests for {@link NameDistance}.
28 *
29 * Run the test like this:
30 * <code>
31 * adb shell am instrument -e class com.android.providers.contacts.NameDistanceTest -w \
32 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
33 * </code>
34 */
35@SmallTest
36public class NameDistanceTest extends TestCase {
37
38    private NameDistance mNameDistance;
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43
44        mNameDistance = new NameDistance(30);
45    }
46
47    public void testExactMatch() {
48        assertFloat(1, "Dwayne", "Dwayne");
49    }
50
51    public void testWinklerBonus() {
52        assertFloat(0.961f, "Martha", "Marhta");
53        assertFloat(0.840f, "Dwayne", "Duane");
54        assertFloat(0.813f, "DIXON", "DICKSONX");
55    }
56
57    public void testJaroDistance() {
58        assertFloat(0.600f, "Donny", "Duane");
59    }
60
61    public void testPoorMatch() {
62        assertFloat(0.467f, "Johny", "Duane");
63    }
64
65    public void testNoMatches() {
66        assertFloat(0, "Abcd", "Efgh");
67    }
68
69    private void assertFloat(float expected, String name1, String name2) {
70        byte[] s1 = Hex.decodeHex(NameNormalizer.normalize(name1));
71        byte[] s2 = Hex.decodeHex(NameNormalizer.normalize(name2));
72
73        float actual = mNameDistance.getDistance(s1, s2);
74        assertTrue("Expected Jaro-Winkler distance: " + expected + ", actual: " + actual,
75                Math.abs(actual - expected) < 0.001);
76    }
77}
78