File: rope.c

package info (click to toggle)
tarantool 1.7.2.385.g952d79e-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,556 kB
  • ctags: 28,405
  • sloc: ansic: 180,313; cpp: 26,044; sh: 15,513; python: 4,893; makefile: 1,412
file content (69 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "salad/rope.h"
#include "unit.h"
#include "rope_common.h"

static void
test_rope_extract(struct rope *rope, rope_size_t pos)
{
	printf("extract pos = %zu: ", (size_t) pos);
	struct rope_node *node = rope_extract_node(rope, pos);
	rope_check(rope);
	str_print(node->data, node->leaf_size);
	printf("\n");
}

static inline void
test_rope_cut(struct rope *rope, rope_size_t offset, rope_size_t size)
{
	printf("erase offset = %zu, size = %zu \n", (size_t) offset, (size_t) size);
	while (size-- > 0)
		rope_erase(rope, offset);
	rope_pretty_print(rope, str_print);
	rope_check(rope);
}


static void
test_rope()
{
	struct rope *rope = test_rope_new();
	test_rope_insert(rope, rope_size(rope), "who's gonna be");

	test_rope_insert(rope, rope_size(rope), "<Mr.X>");
	test_rope_insert(rope, rope_size(rope), ", Mr. <black!?!>Black");
	test_rope_insert(rope, rope_size(rope), ", but they <know-something-");

	test_rope_insert(rope, 0, "guys all ");

	test_rope_insert(rope, 9, "five fighting over ");
	test_rope_insert(rope, 0, "<yes, got got>You got four of ");
	test_rope_insert(rope, rope_size(rope), "special> don't know each other");
	test_rope_insert(rope, -1, ", so nobody wants to back.");
	test_rope_insert(rope, rope_size(rope) - 1, " down");
	test_rope_insert(rope, -1, "<point-point>");

	test_rope_cut(rope, 0,  5);
	test_rope_cut(rope, 0, 9);
	test_rope_cut(rope, 179, 7);
	test_rope_cut(rope, 173, 1);
	test_rope_cut(rope, 58, 7);
	test_rope_cut(rope, 63, 10);
	test_rope_cut(rope, 79, 25);
	test_rope_cut(rope, 25, 5);
	test_rope_cut(rope, 126, 5);

	test_rope_extract(rope, 0);
	test_rope_extract(rope, 5);
	test_rope_extract(rope, 19);
	test_rope_extract(rope, 59);
	test_rope_extract(rope, 124);

	rope_delete(rope);
}

int
main()
{
	test_rope();
	return 0;
}