1 //===- llvm/unittest/Support/EditDistanceTest.cpp - Edit distance tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/edit_distance.h"
11 #include "gtest/gtest.h"
24 static Result
editDistanceAndMaps(StringRef A
, StringRef B
,
25 unsigned MaxEditDistance
= 0) {
27 auto TrackMaps
= [&](const char X
) {
31 unsigned EditDist
= llvm::ComputeMappedEditDistance(
32 ArrayRef(A
.data(), A
.size()), ArrayRef(B
.data(), B
.size()), TrackMaps
,
33 true, MaxEditDistance
);
34 return {NumMaps
, EditDist
};
37 TEST(EditDistance
, VerifyShortCircuit
) {
38 StringRef Hello
= "Hello";
39 StringRef HelloWorld
= "HelloWorld";
40 Result R
= editDistanceAndMaps(Hello
, HelloWorld
, 5);
41 EXPECT_EQ(R
.EditDist
, 5U);
42 EXPECT_GT(R
.NumMaps
, 0U);
44 R
= editDistanceAndMaps(Hello
, HelloWorld
);
45 EXPECT_EQ(R
.EditDist
, 5U);
46 EXPECT_GT(R
.NumMaps
, 0U);
48 R
= editDistanceAndMaps(Hello
, HelloWorld
, 4);
49 EXPECT_EQ(R
.EditDist
, 5U);
50 EXPECT_EQ(R
.NumMaps
, 0U);
52 R
= editDistanceAndMaps(HelloWorld
, Hello
, 4);
53 EXPECT_EQ(R
.EditDist
, 5U);
54 EXPECT_EQ(R
.NumMaps
, 0U);
56 R
= editDistanceAndMaps(Hello
, HelloWorld
, 1);
57 EXPECT_EQ(R
.EditDist
, 2U);
58 EXPECT_EQ(R
.NumMaps
, 0U);
60 R
= editDistanceAndMaps(HelloWorld
, Hello
, 1);
61 EXPECT_EQ(R
.EditDist
, 2U);
62 EXPECT_EQ(R
.NumMaps
, 0U);