1 /*
2 * Copyright (C) 2014 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 #include "suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy.h"
18
19 #include <gtest/gtest.h>
20
21 #include <vector>
22
23 #include "suggest/policyimpl/utils/edit_distance.h"
24 #include "utils/int_array_view.h"
25
26 namespace latinime {
27 namespace {
28
TEST(DamerauLevenshteinEditDistancePolicyTest,TestConstructPolicy)29 TEST(DamerauLevenshteinEditDistancePolicyTest, TestConstructPolicy) {
30 const std::vector<int> codePoints0 = { 0x20, 0x40, 0x60 };
31 const std::vector<int> codePoints1 = { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60 };
32 DamerauLevenshteinEditDistancePolicy policy(codePoints0.data(), codePoints0.size(),
33 codePoints1.data(), codePoints1.size());
34
35 EXPECT_EQ(static_cast<int>(codePoints0.size()), policy.getString0Length());
36 EXPECT_EQ(static_cast<int>(codePoints1.size()), policy.getString1Length());
37 }
38
getEditDistance(const std::vector<int> & codePoints0,const std::vector<int> & codePoints1)39 float getEditDistance(const std::vector<int> &codePoints0, const std::vector<int> &codePoints1) {
40 DamerauLevenshteinEditDistancePolicy policy(codePoints0.data(), codePoints0.size(),
41 codePoints1.data(), codePoints1.size());
42 return EditDistance::getEditDistance(&policy);
43 }
44
TEST(DamerauLevenshteinEditDistancePolicyTest,TestEditDistance)45 TEST(DamerauLevenshteinEditDistancePolicyTest, TestEditDistance) {
46 EXPECT_FLOAT_EQ(0.0f, getEditDistance({}, {}));
47 EXPECT_FLOAT_EQ(0.0f, getEditDistance({ 1 }, { 1 }));
48 EXPECT_FLOAT_EQ(0.0f, getEditDistance({ 1, 2, 3 }, { 1, 2, 3 }));
49
50 EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1 }, { }));
51 EXPECT_FLOAT_EQ(1.0f, getEditDistance({}, { 100 }));
52 EXPECT_FLOAT_EQ(5.0f, getEditDistance({}, { 1, 2, 3, 4, 5 }));
53
54 EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 0 }, { 100 }));
55 EXPECT_FLOAT_EQ(5.0f, getEditDistance({ 1, 2, 3, 4, 5 }, { 11, 12, 13, 14, 15 }));
56
57 EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1 }, { 1, 2 }));
58 EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 1, 2 }, { 0, 1, 2, 3 }));
59 EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 0, 1, 2, 3 }, { 1, 2 }));
60
61 EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1, 2 }, { 2, 1 }));
62 EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 1, 2, 3, 4 }, { 2, 1, 4, 3 }));
63 }
64 } // namespace
65 } // namespace latinime
66