handle more packets, fix major flaw in diffing algo (again) and add extra tests

This commit is contained in:
2022-12-20 18:46:44 +01:00
parent cb0c2642f8
commit ed72b27808
3 changed files with 184 additions and 23 deletions

View File

@ -214,7 +214,6 @@ struct TextDocument {
if (!differ && list_start == state.list.size() && text_start == text.size()) {
return {};
}
//std::cout << "list.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n";
//std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n";
@ -224,7 +223,9 @@ struct TextDocument {
//for (; list_end > 0 && text_end > 0 && list_end >= list_start && text_end >= text_start;) {
//while (list_end >= list_start && text_end >= text_start) {
size_t list_end_counted = 0;
while (list_start_counted - list_end_counted > state.doc_size && text_end >= text_start) {
differ = false; // var reuse
//while (list_start_counted - list_end_counted > state.doc_size && text_end >= text_start) {
while (state.doc_size - list_start_counted > list_end_counted && text_end >= text_start) {
// jump over tombstones
if (!state.list[list_end-1].value.has_value()) {
list_end--;
@ -232,6 +233,7 @@ struct TextDocument {
}
if (state.list[list_end-1].value.value() != text[text_end-1]) {
differ = true;
break;
}
@ -240,20 +242,29 @@ struct TextDocument {
list_end_counted++;
}
if (!differ && text_start == text_end+1) {
// we ran into eachother without seeing the different char
// TODO: do we need to increment list_end? text_end?
list_end++;
}
//std::cout << "list_end: " << list_end << " text_end: " << text_end << "\n";
//std::cout << "substring before: " << text.substr(text_start, text.size() - state.doc_size) << "\n";
std::vector<Op> ops;
// 1. clear range (del all list_start - list_end)
if (list_start <= list_end && list_start < state.list.size()) {
//list_end += list_start == list_end;
ops = delRange(
state.list[list_start].id,
(list_start == list_end ? list_end+1 : list_end) < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
list_end < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
);
//std::cout << "deleted: " << ops.size() << "\n";
}
//std::cout << "text between: " << getText() << "\n";
//std::cout << "substring between: " << text.substr(text_start, text.size() - state.doc_size) << "\n";
// 2. add range (add all text_start - text_end)
if (state.doc_size < text.size()) {
@ -266,7 +277,6 @@ struct TextDocument {
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
}
//assert(false && "implement me");
return ops;
}
};

View File

@ -418,8 +418,9 @@ void testBugDoubleDel(void) {
{
std::string_view new_text{"a"};
doc.merge(new_text);
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
@ -445,20 +446,23 @@ void testBugSameDel(void) {
{
std::string_view new_text{"a"};
doc.merge(new_text);
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
}
@ -468,32 +472,122 @@ void testBugSameDel2(void) {
{
std::string_view new_text{"a"};
doc.merge(new_text);
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aaa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
}
void testMulti1(void) {
Doc docA;
docA.local_agent = 'A';
Doc docB;
docB.local_agent = 'B';
// state A
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(docA.getText() == new_text);
assert(docB.apply(ops));
assert(docB.getText() == new_text);
assert(docB.state.doc_size == docA.state.doc_size);
assert(docB.state.list.size() == docA.state.list.size());
}
// now B inserts b
{
std::string_view new_text{"iiibiiii"};
const auto ops = docB.merge(new_text);
assert(docB.getText() == new_text);
assert(ops.size() == 1); // 1 new inserted char, nothing to delete
assert(docA.apply(ops));
assert(docA.getText() == new_text);
}
}
void testPaste1(void) {
Doc docA;
docA.local_agent = 'A';
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\niiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
}
void testPaste2(void) {
Doc docA;
docA.local_agent = 'A';
{
std::string_view new_text{"aiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\naiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
}
@ -580,6 +674,27 @@ int main(void) {
testBugSameDel2();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testMulti1:\n";
testMulti1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste1:\n";
testPaste1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste2:\n";
testPaste2();
}
return 0;
}