cc15: simplify is_insert and add test cases.

master
Michael Hope 6 years ago
parent f21cc02684
commit daf36f8d30
  1. 33
      cc15.cc

@ -3,6 +3,7 @@
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <cassert>
#include <experimental/string_view>
#include <range/v3/all.hpp>
#include <string>
@ -12,21 +13,20 @@ using namespace ranges;
using string_view = std::experimental::string_view;
bool is_insert(string_view lhs, string_view rhs) {
// Possible inserts.
auto lit = lhs.cbegin();
auto rit = rhs.cbegin();
int inserts = 0;
for (; rit != rhs.cend();) {
if (*lit == *rit) {
lit++;
rit++;
} else {
inserts++;
rit++;
}
assert(lhs.length() < rhs.length());
auto lh = lhs.cbegin();
auto rh = rhs.cbegin();
// Skip the matching characters.
for (; *lh == *rh && lh != lhs.cend();) {
lh++;
rh++;
}
return inserts == 1;
// Skip the inserted character.
rh++;
// Rest should match.
return string_view(lh, lhs.cend() - lh) == string_view(rh, rhs.cend() - rh);
}
bool one_edit(string_view lhs, string_view rhs) {
@ -49,6 +49,11 @@ bool one_edit(string_view lhs, string_view rhs) {
}
TEST_CASE("cc15", "edits") {
CHECK(is_insert("", "a") == true);
CHECK(is_insert("a", "ab") == true);
CHECK(is_insert("abc", "abdc") == true);
CHECK(is_insert("bc", "abc") == true);
CHECK(one_edit("pale", "ple") == true);
CHECK(one_edit("pales", "pale") == true);
CHECK(one_edit("pale", "pales") == true);

Loading…
Cancel
Save