Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADT][NFC] Early bail out for ComputeEditDistance
The minimun bound for number of edits is the size difference between the 2 arrays. If MaxEditDistance is smaller than this, we can bail out early without needing to traverse any of the arrays. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D127070
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===- llvm/unittest/Support/EditDistanceTest.cpp - Edit distance tests ---===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/ADT/edit_distance.h" | ||
#include "gtest/gtest.h" | ||
#include <cstdlib> | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
struct Result { | ||
unsigned NumMaps; | ||
unsigned EditDist; | ||
}; | ||
} // namespace | ||
|
||
static Result editDistanceAndMaps(StringRef A, StringRef B, | ||
unsigned MaxEditDistance = 0) { | ||
unsigned NumMaps = 0; | ||
auto TrackMaps = [&](const char X) { | ||
++NumMaps; | ||
return X; | ||
}; | ||
unsigned EditDist = llvm::ComputeMappedEditDistance( | ||
makeArrayRef(A.data(), A.size()), makeArrayRef(B.data(), B.size()), | ||
TrackMaps, true, MaxEditDistance); | ||
return {NumMaps, EditDist}; | ||
} | ||
|
||
TEST(EditDistance, VerifyShortCircuit) { | ||
StringRef Hello = "Hello"; | ||
StringRef HelloWorld = "HelloWorld"; | ||
Result R = editDistanceAndMaps(Hello, HelloWorld, 5); | ||
EXPECT_EQ(R.EditDist, 5U); | ||
EXPECT_GT(R.NumMaps, 0U); | ||
|
||
R = editDistanceAndMaps(Hello, HelloWorld); | ||
EXPECT_EQ(R.EditDist, 5U); | ||
EXPECT_GT(R.NumMaps, 0U); | ||
|
||
R = editDistanceAndMaps(Hello, HelloWorld, 4); | ||
EXPECT_EQ(R.EditDist, 5U); | ||
EXPECT_EQ(R.NumMaps, 0U); | ||
|
||
R = editDistanceAndMaps(HelloWorld, Hello, 4); | ||
EXPECT_EQ(R.EditDist, 5U); | ||
EXPECT_EQ(R.NumMaps, 0U); | ||
|
||
R = editDistanceAndMaps(Hello, HelloWorld, 1); | ||
EXPECT_EQ(R.EditDist, 2U); | ||
EXPECT_EQ(R.NumMaps, 0U); | ||
|
||
R = editDistanceAndMaps(HelloWorld, Hello, 1); | ||
EXPECT_EQ(R.EditDist, 2U); | ||
EXPECT_EQ(R.NumMaps, 0U); | ||
} |