~kiwec/ke

140cf790a86b113b937922bc5d196311735b5d1f — Wolf Clément 3 years ago eb2fc58
Separe Document and Line tests
5 files changed, 131 insertions(+), 100 deletions(-)

A test/Document.cpp
A test/Document.hpp
A test/Line.cpp
A test/Line.hpp
M test/tests.cpp
A test/Document.cpp => test/Document.cpp +43 -0
@@ 0,0 1,43 @@
#include "Document.hpp"

void test_inserted_text() {
  Text text;
  std::wstring inserted_text = L"abcd\nefg";
  text.insert(inserted_text.c_str(), inserted_text.size(), text.size());

  // Strings are the same
  TEST_CHECK(text.size() == inserted_text.size());
  for(uint i = 0; i < text.size(); i++) {
    TEST_CHECK(text[i] == inserted_text[i]);
  }
}

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 contents, '\0' included
  auto wanted_text = L"acdef";
  for(uint i = 0; i < text.size() + 1; i++) {
    TEST_CHECK(wanted_text[i] == text[i]);
  }
}

A test/Document.hpp => test/Document.hpp +9 -0
@@ 0,0 1,9 @@
#pragma once
#define TEST_NO_MAIN
#include "acutest.h"
#include "../src/Document.hpp"

void test_inserted_text();
void test_data_erasing();
void test_text_erasing();


A test/Line.cpp => test/Line.cpp +61 -0
@@ 0,0 1,61 @@
#include "Line.hpp"

#include "../src/Document.hpp"

void test_line_insert() {
  Text text;
  std::wstring inserted_text = L"abcd\nefg";
  text.insert(inserted_text.c_str(), inserted_text.size(), text.size());
  
  // Lines are the same
  auto first_line = text.get_line(1);
  for(uint i = 0; i < 5; i++) {
    TEST_CHECK(first_line[i] == inserted_text[i]);
  }
  auto second_line = text.get_line(2);
  for(uint i = 0; i < 3; i++) {
    TEST_CHECK(second_line[i] == inserted_text[i + 5]);
  }
}

void test_line_count() {
  Text text;
  TEST_CHECK(text.get_nb_lines() == 1);

  text.insert(L"abcd", 4, text.size());
  TEST_CHECK(text.get_nb_lines() == 1);

  text.insert(L"\n\ne\nfg", 6, text.size());
  TEST_CHECK(text.get_nb_lines() == 4);

  text.erase(0, 1);
  text.erase(text.size() - 1, 1);
  TEST_CHECK(text.get_nb_lines() == 4);

  text.erase(0, text.size());
  TEST_CHECK(text.size() == 0);
  TEST_CHECK(text.get_nb_lines() == 1);
}

void test_prev_next_line() {
  Text text;

  text.insert(L"abcd", 4, text.size());
  TEST_CHECK(text.get_nb_lines() == 1);

  text.insert(L"\nefg", 4, text.size());
  TEST_CHECK(text.get_nb_lines() == 2);

  auto line = text.get_line(1);
  TEST_CHECK(line.is_first());
  TEST_CHECK(line.length() == 4);

  line = line.next();
  TEST_CHECK(line.is_last());
  TEST_CHECK(line.length() == 3);

  line = line.prev();
  TEST_CHECK(line.is_first());
  TEST_CHECK(line.length() == 4);
}


A test/Line.hpp => test/Line.hpp +9 -0
@@ 0,0 1,9 @@
#pragma once
#define TEST_NO_MAIN
#include "acutest.h"
#include "../src/Line.hpp"

void test_line_insert();
void test_line_count();
void test_prev_next_line();


M test/tests.cpp => test/tests.cpp +9 -100
@@ 1,110 1,19 @@
#include "acutest.h"

#include <iostream>
#include <wchar.h>
#include "../src/Document.hpp"
#include "../src/Line.hpp"

void test_line_count() {
	Text text;
	TEST_CHECK(text.get_nb_lines() == 1);

	text.insert(L"abcd", 4, text.size());
	TEST_CHECK(text.get_nb_lines() == 1);

	text.insert(L"\n\ne\nfg", 6, text.size());
	TEST_CHECK(text.get_nb_lines() == 4);

	text.erase(0, 1);
	text.erase(text.size() - 1, 1);
	TEST_CHECK(text.get_nb_lines() == 4);

	text.erase(0, text.size());
	TEST_CHECK(text.size() == 0);
	TEST_CHECK(text.get_nb_lines() == 1);
}

void test_prev_next_line() {
	Text text;

	text.insert(L"abcd", 4, text.size());
	TEST_CHECK(text.get_nb_lines() == 1);

	text.insert(L"\nefg", 4, text.size());
	TEST_CHECK(text.get_nb_lines() == 2);

	auto line = text.get_line(1);
	TEST_CHECK(line.is_first());
	TEST_CHECK(line.length() == 4);

	line = line.next();
	TEST_CHECK(line.is_last());
	TEST_CHECK(line.length() == 3);

	line = line.prev();
	TEST_CHECK(line.is_first());
	TEST_CHECK(line.length() == 4);
}

void test_inserted_text() {
	Text text;
	std::wstring inserted_text = L"abcd\nefg";
	text.insert(inserted_text.c_str(), inserted_text.size(), text.size());

	// Strings are the same
	TEST_CHECK(text.size() == inserted_text.size());
	for(uint i = 0; i < text.size(); i++) {
		TEST_CHECK(text[i] == inserted_text[i]);
	}

	// Lines are the same
	auto first_line = text.get_line(1);
	for(uint i = 0; i < 5; i++) {
		TEST_CHECK(first_line[i] == inserted_text[i]);
	}
	auto second_line = text.get_line(2);
	for(uint i = 0; i < 3; i++) {
		TEST_CHECK(second_line[i] == inserted_text[i + 5]);
	}
}

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]);
	}
}
#include "Document.hpp"
#include "Line.hpp"

TEST_LIST = {
	{ "line-count", test_line_count },
	{ "prev-next-line", test_prev_next_line },
	// Document.hpp
	{ "inserted-text", test_inserted_text },
	{ "erase-data", test_data_erasing },
	{ "erase-text", test_text_erasing },

	// Line.hpp
	{ "line-insert", test_line_insert },
	{ "line-count", test_line_count },
	{ "prev-next-line", test_prev_next_line },

	{ NULL, NULL }
};