#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;
};
}
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);
}