bugfixing, debug. still broken, but better

This commit is contained in:
Green Sky 2022-12-17 01:18:52 +01:00
parent bbb93040d0
commit 5cba374d2b
No known key found for this signature in database
3 changed files with 115 additions and 9 deletions

View File

@ -245,11 +245,11 @@ struct TextDocument {
//std::cout << "text between: " << getText() << "\n"; //std::cout << "text between: " << getText() << "\n";
// 2. add range (add all text_start - text_end) // 2. add range (add all text_start - text_end)
if (text_start < text_end) { if (state.doc_size < text.size()) {
auto tmp_add_ops = addText( auto tmp_add_ops = addText(
list_start == 0 ? std::nullopt : std::make_optional(state.list[list_start-1].id), list_start == 0 ? std::nullopt : std::make_optional(state.list[list_start-1].id),
list_start == state.list.size() ? std::nullopt :std::make_optional(state.list.at(list_start).id), list_start == state.list.size() ? std::nullopt :std::make_optional(state.list.at(list_start).id),
text.substr(text_start, text_end-text_start) text.substr(text_start, text.size() - state.doc_size)
); );
//std::cout << "added: " << tmp_add_ops.size() << "\n"; //std::cout << "added: " << tmp_add_ops.size() << "\n";
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end()); ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());

View File

@ -398,8 +398,21 @@ void testChange1(size_t seed) {
assert(doc.getText() == otherdoc.getText()); assert(doc.getText() == otherdoc.getText());
} }
void testBugSame(void) {
Doc doc;
doc.local_agent = 'A';
std::string_view new_text1{"a"};
doc.merge(new_text1);
assert(doc.getText() == new_text1);
std::string_view new_text2{"aa"};
doc.merge(new_text2);
assert(doc.getText() == new_text2);
}
int main(void) { int main(void) {
const size_t loops = 10'000; const size_t loops = 1'000;
{ {
std::cout << "testEmptyDocAdds:\n"; std::cout << "testEmptyDocAdds:\n";
for (size_t i = 0; i < loops; i++) { for (size_t i = 0; i < loops; i++) {
@ -453,6 +466,13 @@ int main(void) {
} }
} }
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testBugNLNL:\n";
testBugSame();
}
return 0; return 0;
} }

View File

@ -1,18 +1,83 @@
#include <crdt/text_document.hpp> #include <crdt/text_document.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
extern "C" {
#include <zed_net.h>
}
#include <memory> #include <memory>
#include <string_view> #include <string_view>
#include <zed_net.h> #include <variant>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
// single letter agent, for testing only // single letter agent, for testing only
using Agent = char; //using Agent = char;
using Agent = uint16_t; // tmp local port
using Doc = GreenCRDT::TextDocument<Agent>; using Doc = GreenCRDT::TextDocument<Agent>;
using ListType = Doc::ListType; using ListType = Doc::ListType;
std::ostream& operator<<(std::ostream& out, const std::optional<ListType::ListID>& id) {
if (id.has_value()) {
out << id.value().id << "-" << id.value().seq;
} else {
out << "null";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const ListType::OpAdd& op) {
out
<< "Add{ id:" << op.id.id
<< "-" << op.id.seq
<< ", v:" << op.value
<< ", l:" << op.parent_left
<< ", r:" << op.parent_right
<< " }"
;
return out;
}
std::ostream& operator<<(std::ostream& out, const ListType::OpDel& op) {
out
<< "Del{ id:" << op.id.id
<< "-" << op.id.seq
<< " }"
;
return out;
}
std::ostream& operator<<(std::ostream& out, const Doc::Op& op) {
if (std::holds_alternative<ListType::OpAdd>(op)) {
out << std::get<ListType::OpAdd>(op);
} else if (std::holds_alternative<ListType::OpDel>(op)) {
out << std::get<ListType::OpDel>(op);
}
return out;
}
std::ostream& operator<<(std::ostream& out, const std::optional<char>& id) {
if (id.has_value()) {
out << id.value();
} else {
out << "null";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const ListType::Entry& e) {
out
<< "{ id:" << e.id.id
<< "-" << e.id.seq
<< ", v:" << e.value
<< ", l:" << e.parent_left
<< ", r:" << e.parent_right
<< " }"
;
return out;
}
static bool send_command(zed_net_socket_t* remote_socket, const std::string_view mode, const std::string_view command) { static bool send_command(zed_net_socket_t* remote_socket, const std::string_view mode, const std::string_view command) {
auto j = nlohmann::json::array(); auto j = nlohmann::json::array();
@ -106,7 +171,7 @@ int main(void) {
std::cout << "initialized zed_net\n"; std::cout << "initialized zed_net\n";
const unsigned int port {1337}; const uint16_t port {1337};
zed_net_socket_t listen_socket; zed_net_socket_t listen_socket;
if (zed_net_tcp_socket_open( if (zed_net_tcp_socket_open(
&listen_socket, &listen_socket,
@ -142,6 +207,8 @@ int main(void) {
// send doauto text changed for inital buffer // send doauto text changed for inital buffer
Doc doc; Doc doc;
doc.local_agent = remote_address.port; // tmp: use local port as id
while (true) { while (true) {
// 10MiB // 10MiB
auto buffer = std::make_unique<std::array<uint8_t, 1024*1024*10>>(); auto buffer = std::make_unique<std::array<uint8_t, 1024*1024*10>>();
@ -227,15 +294,34 @@ int main(void) {
} }
std::string new_text; std::string new_text;
for (const auto& line : j_lines) { for (size_t i = 0; i < j_lines.size(); i++) {
new_text += line; if (!j_lines.at(i).empty()) {
new_text += '\n'; new_text += static_cast<std::string>(j_lines.at(i));
} }
if (i+1 < j_lines.size()) {
new_text += "\n";
}
}
//std::cout << "new_text:\n" << new_text << "\n";
//std::cout << "old_text:\n" << doc.getText() << "\n";
std::cout << "doc state: ";
for (const auto& e : doc.state.list) {
std::cout << e << " ";
}
std::cout << "\n";
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
if (!ops.empty()) { if (!ops.empty()) {
std::cout << "ops.size: " << ops.size() << "\n"; std::cout << "ops.size: " << ops.size() << "\n";
std::cout << "ops: ";
for (const auto& op : ops) {
std::cout << op << " ";
} }
std::cout << "\n";
}
assert(doc.getText() == new_text);
} else { } else {
std::cout << "unknown command '" << command << "'\n"; std::cout << "unknown command '" << command << "'\n";
} }