<commit_before>#include <string.h>

#include <unit.h>

#include <util.h>
#include <txt.c>

static void make_links(struct piece **);

static void test_delete();
static void test_delete2();
static void test_delete3();
static void test_empty();
static void test_insert();
static void test_insert2();
static void test_link();
static void test_traverse();

struct unit_test tests[] = {
	{.msg = "should start with an empty piece list",
	 .fun = unit_list(test_empty),},
	{.msg = "should be able to link pieces",
	 .fun = unit_list(test_link),},
	{.msg = "should be able to traverse piece lists",
	 .fun = unit_list(test_traverse),},
	{.msg = "should be able to insert text within piece",
	 .fun = unit_list(test_insert),},
	{.msg = "should be able to insert text around pieces",
	 .fun = unit_list(test_insert2),},
	{.msg = "should be able to delete text within pieces",
	 .fun = unit_list(test_delete),},
	{.msg = "should be able to delete across pieces",
	 .fun = unit_list(test_delete2),},
	{.msg = "should be able to delete across many pieces",
	 .fun = unit_list(test_delete3),},
};

void
make_links(struct piece **pies)
{
	size_t i;

	for (i=0; pies[i]; ++i) {
		text_link(pies[i], pies[i+1]);
		if (pies[i]->buffer) {
			pies[i]->length = strlen(pies[i]->buffer);
		}
	}
}
#define make_links(...) make_links((struct piece *[]){__VA_ARGS__, 0})

void
test_delete()
{
	struct piece beg[1] = {{0}};
	struct piece pie[1] = {{.buffer="tyypo"}};
	struct piece end[1] = {{0}};
	struct piece *new;
	struct piece *links[2];

	make_links(beg, pie, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 3);

	expect(0, text_delete(links, 2, 1));

	expect(2, pie->length);
	ok(text_next(pie, beg) != end);
	new = text_next(pie, beg);
	ok(text_next(new, pie) == end);

	ok(links[0] == 0);

	free(new);
}

void
test_delete2()
{
	struct piece beg[1] = {{0}};
	struct piece end[1] = {{0}};
	struct piece one[1] = {{.buffer="abc__"}};
	struct piece two[1] = {{.buffer="__def"}};
	struct piece *links[2];

	make_links(beg, one, two, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 3);

	expect(0, text_delete(links, 3, 4));

	expect(3, one->length);
	expect(3, two->length);
	ok(*two->buffer == 'd');
}

void
test_delete3()
{
	struct piece beg[1] = {{0}};
	struct piece end[1] = {{0}};
	struct piece one[1] = {{.buffer="hello, "}};
	struct piece two[1] = {{.buffer="(not you)"}};
	struct piece thr[1] = {{.buffer="friends."}};
	struct piece *links[2];

	make_links(beg, one, two, thr, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 7);

	expect(0, text_delete(links, 0, 9));

	expect(7, one->length);
	expect(8, thr->length);

	ok(*links == two);
	ok(text_next(one, beg) == thr);
	ok(text_next(thr, end) == one);
}

void
test_empty()
{
	struct piece *one;
	struct piece *two;

	ok(one = text_ctor());
	ok(one->link);
	ok(!one->buffer);
	ok(!one->length);
	ok(two = text_next(one, 0));
	ok(two->link == (uintptr_t)one);
	ok(one->link == (uintptr_t)two);
	ok(!two->buffer);
	ok(!two->length);

	try(text_dtor(one));
}

void
test_insert()
{
	struct piece beg[1] = {{0}};
	struct piece hi[1] = {{.buffer="hello!", .length=6}};
	struct piece end[1] = {{0}};
	struct piece new[1] = {{.buffer=", friend", .length=8}};
	struct piece *pie;
	struct piece *links[2];

	make_links(beg, hi, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 5);
	ok(!text_insert(links, new, 5));

	ok(hi->length == 5);

	ok(new == text_next(hi, beg));

	pie = text_next(new, hi);
	ok(pie != end);

	ok(pie->length == 1);
	ok(*pie->buffer == '!');

	links[0] = beg, links[1] = 0;
	text_walk(links, 13);

	ok(links[0] == pie);
	ok(links[1] == new);

	free(pie);
}

void
test_insert2()
{
	struct piece beg[1] = {{0}};
	struct piece one[1] = {{.buffer="two, ", .length=5}};
	struct piece end[1] = {{0}};
	struct piece new[1] = {{.buffer="three.", .length=6}};
	struct piece new1[1] = {{.buffer="one, ", .length=5}};
	struct piece *links[2];

	make_links(beg, one, end);

	links[0] = beg, links[1] = 0;<commit_msg>arrange the examples in a way that makes sense<commit_after>#include <string.h>

#include <unit.h>

#include <util.h>
#include <txt.c>

static void make_links(struct piece **);

static void test_delete();
static void test_delete2();
static void test_delete3();
static void test_empty();
static void test_insert();
static void test_insert2();
static void test_link();
static void test_traverse();

struct unit_test tests[] = {
	{.msg = "should start with an empty piece list",
	 .fun = unit_list(test_empty),},
	{.msg = "should be able to link pieces",
	 .fun = unit_list(test_link),},
	{.msg = "should be able to traverse piece lists",
	 .fun = unit_list(test_traverse),},
	{.msg = "should be able to insert text within piece",
	 .fun = unit_list(test_insert),},
	{.msg = "should be able to insert text around pieces",
	 .fun = unit_list(test_insert2),},
	{.msg = "should be able to delete text within pieces",
	 .fun = unit_list(test_delete),},
	{.msg = "should be able to delete across pieces",
	 .fun = unit_list(test_delete2),},
	{.msg = "should be able to delete across many pieces",
	 .fun = unit_list(test_delete3),},
};

void
make_links(struct piece **pies)
{
	size_t i;

	for (i=0; pies[i]; ++i) {
		text_link(pies[i], pies[i+1]);
		if (pies[i]->buffer) {
			pies[i]->length = strlen(pies[i]->buffer);
		}
	}
}
#define make_links(...) make_links((struct piece *[]){__VA_ARGS__, 0})

void
test_delete()
{
	struct piece beg[1] = {{0}};
	struct piece pie[1] = {{.buffer="tyypo"}};
	struct piece end[1] = {{0}};
	struct piece *new;
	struct piece *links[2];

	make_links(beg, pie, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 3);

	expect(0, text_delete(links, 2, 1));

	expect(2, pie->length);
	ok(text_next(pie, beg) != end);
	new = text_next(pie, beg);
	ok(text_next(new, pie) == end);

	ok(links[0] == 0);

	free(new);
}

void
test_delete2()
{
	struct piece beg[1] = {{0}};
	struct piece end[1] = {{0}};
	struct piece one[1] = {{.buffer="abc__"}};
	struct piece two[1] = {{.buffer="__def"}};
	struct piece *links[2];

	make_links(beg, one, two, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 3);

	expect(0, text_delete(links, 3, 4));

	expect(3, one->length);
	expect(3, two->length);
	ok(*two->buffer == 'd');
}

void
test_delete3()
{
	struct piece beg[1] = {{0}};
	struct piece end[1] = {{0}};
	struct piece one[1] = {{.buffer="hello, "}};
	struct piece two[1] = {{.buffer="(not you)"}};
	struct piece thr[1] = {{.buffer="friends."}};
	struct piece *links[2];

	make_links(beg, one, two, thr, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 7);

	expect(0, text_delete(links, 0, 9));

	expect(7, one->length);
	expect(8, thr->length);

	ok(*links == two);
	ok(text_next(one, beg) == thr);
	ok(text_next(thr, end) == one);
}

void
test_empty()
{
	struct piece *one;
	struct piece *two;

	ok(one = text_ctor());
	ok(one->link);
	ok(!one->buffer);
	ok(!one->length);
	ok(two = text_next(one, 0));
	ok(two->link == (uintptr_t)one);
	ok(one->link == (uintptr_t)two);
	ok(!two->buffer);
	ok(!two->length);

	try(text_dtor(one));
}

void
test_insert()
{
	struct piece beg[1] = {{0}};
	struct piece hi[1] = {{.buffer="hello!", .length=6}};
	struct piece end[1] = {{0}};
	struct piece new[1] = {{.buffer=", friend", .length=8}};
	struct piece *pie;
	struct piece *links[2];

	make_links(beg, hi, end);

	links[0] = beg, links[1] = 0;
	text_walk(links, 5);
	ok(!text_insert(links, new, 5));

	ok(hi->length == 5);

	ok(new == text_next(hi, beg));

	pie = text_next(new, hi);
	ok(pie != end);

	ok(pie->length == 1);
	ok(*pie->buffer == '!');

	links[0] = beg, links[1] = 0;
	text_walk(links, 13);

	ok(links[0] == pie);
	ok(links[1] == new);

	free(pie);
}

void
test_insert2()
{
	struct piece beg[1] = {{0}};
	struct piece one[1] = {{.buffer="two, ", .length=5}};
	struct piece end[1] = {{0}};
	struct piece new[1] = {{.buffer="one, ", .length=5}};
	struct piece new1[1] = {{.buffer="three.", .length=6}};
	struct piece *links[2];

	make_links(beg, one, end);

	links[0] = beg, links[1] = 0;
{'fraction_non_alphanumeric': 0.2132600824072918, 'fraction_numerical': 0.024971906605069295, 'mean_word_length': 4.527950310559007, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2034253', 'n_tokens_mistral': 3366, 'n_tokens_neox': 3202, 'n_words': 819}