~kiwec/ke

bd3a6931d798c9171238fa7d52c599f8e480e26e — Wolf Clément 3 years ago 5bc8d68
Test and fix text erasing
5 files changed, 46 insertions(+), 12 deletions(-)

M README.md
A debug.sh
M src/Document.hpp
M src/Prompt.hpp
M test/tests.cpp
M README.md => README.md +1 -0
@@ 28,3 28,4 @@ This is a work in progress, and there are currently no tests. Use at your own ri
- [ ] Multiple buffers
- [ ] Better mouse support
- [ ] Basic C lexer
- [ ] Tmux debugging setup

A debug.sh => debug.sh +4 -0
@@ 0,0 1,4 @@
#!/bin/sh
exec xterm -e ./ke &
sleep 1
sudo gdb -q -p $(pidof -s ke)

M src/Document.hpp => src/Document.hpp +7 -12
@@ 53,16 53,13 @@ public:
	/// erase(0, 1) would remove the first element
	void erase(uint start, uint size) {
		assert(start + size <= m_size);
		auto base = m_data + start;
		auto erased_bytes = size * sizeof(T);
		auto moved_bytes = (m_size - (start + size)) * sizeof(T);
		memmove(base, base + erased_bytes, moved_bytes);
		memmove(&m_data[start],
		        &m_data[start + size],
				(m_size - (start + size)) * sizeof(T));
		m_size -= size;

		// If we erased the end, rewrite null marker
		if(start == m_size) {
			m_data[m_size] = 0;
		}
		// Rewrite null marker
		m_data[m_size] = 0;
	}

	/// Inserts the given data to the left of pos


@@ 83,10 80,8 @@ public:
		// Update size of container
		m_size += data_size;

		// If we overwrote the end, rewrite null marker
		if(pos + data_size == m_size) {
			m_data[m_size] = 0;
		}
		// Rewrite null marker
		m_data[m_size] = 0;
	}

	uint size() {

M src/Prompt.hpp => src/Prompt.hpp +1 -0
@@ 3,6 3,7 @@
#include <memory>
#include <termbox.h>
#include <wchar.h>
#include "Document.hpp"
#include "Input.hpp"

class Prompt : public InputHandler {

M test/tests.cpp => test/tests.cpp +33 -0
@@ 68,10 68,43 @@ void test_inserted_text() {
	}
}

void test_data_erasing() {
	Data<char> text;
	auto inserted_text = "abcdefg\n1234";
	text.insert(inserted_text, strlen(inserted_text), text.size());
	text.erase(0, 1);

	// Test contents
	auto wanted_text = "bcdefg\n1234";
	for(uint i = 0; i < text.size(); i++) {
		TEST_CHECK(wanted_text[i] == text[i]);
	}
}

void test_text_erasing() {
	Text text;
	auto inserted_text = L"abcdef";
	text.insert(inserted_text, wcslen(inserted_text), text.size());

	// Test size
	text.erase(1, 1);
	auto wanted_len = wcslen(inserted_text) - 1;
	TEST_CHECK(text.size() == wanted_len);
	TEST_CHECK(text.get_line(1).length() == wanted_len);

	// Test contents, '\0' included
	auto wanted_text = L"acdef";
	for(uint i = 0; i < text.size() + 1; i++) {
		TEST_CHECK(wanted_text[i] == text[i]);
	}
}

TEST_LIST = {
	{ "line-count", test_line_count },
	{ "prev-next-line", test_prev_next_line },
	{ "inserted-text", test_inserted_text },
	{ "erase-data", test_data_erasing },
	{ "erase-text", test_text_erasing },
	{ NULL, NULL }
};