File: test-sbuf.c

package info (click to toggle)
publib 0.40-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: ansic: 6,708; sh: 395; makefile: 369
file content (189 lines) | stat: -rw-r--r-- 5,209 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Part of publib.

   Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
 * test-sbuf.c -- test various parts of the sbuf routines
 *
 * At the moment, only one test is done: if we have the following text:
 *
 *	"abcdefghi"
 *
 * and two marks ("def" and the other variable), and change the text of
 * one mark, is the other updated correctly?
 */

#include <string.h>
#include <publib.h>

static Sbuf *buf;
static Sbufmark *mark1, *mark2;

static void setup(void) {
	if (buf != NULL) {
		sbuf_unmark(mark1);
		sbuf_unmark(mark2);
		sbuf_destroy(buf);
	}

	buf = sbuf_create();
	mark1 = sbuf_mark(buf, 0, 0);
	mark2 = sbuf_mark(buf, 0, 0);

	sbuf_strchange(mark1, "abcd\377fghi(x[123]y)abcdefghi", 27);
	sbuf_remark(mark1, 3, 3);
}

static void dump(void) {
	char tmp[1024];

	printf("buf = `%s'\n", sbuf_lock(buf));
	sbuf_unlock(buf);	

	sbuf_strat(tmp, mark1);
	printf("mark1 = %ld-%ld, len=%ld, text=`%s'\n",
		sbuf_mark_begin(mark1),
		sbuf_mark_end(mark1),
		sbuf_mark_length(mark1),
		tmp);

	sbuf_strat(tmp, mark2);
	printf("mark2 = %ld-%ld, len=%ld, text=`%s'\n",
		sbuf_mark_begin(mark2),
		sbuf_mark_end(mark2),
		sbuf_mark_length(mark2),
		tmp);

	printf("\n");
}


static void delete(long from, long n, long newfrom, long newn) {
	printf("deleting %ld-%ld (-> mark1 should be: %ld-%ld)\n",
		from, from+n, newfrom, newfrom + newn);
	setup();
	sbuf_remark(mark2, from, n);
	sbuf_strchange(mark2, "", 0);
	dump();
}

static void insert(long at, const char *what, long newfrom, long newn) {
	printf("insert `%s' at %ld (-> mark1 should be: %ld-%ld)\n",
		what, at, newfrom, newfrom + newn);
	setup();
	sbuf_remark(mark2, at, 0);
	sbuf_strchange(mark2, what, strlen(what));
	dump();
}

static void search(long pos, const char *str, unsigned long opts) {
	int ret;
	setup();
	sbuf_remark(mark1, pos, sbuf_length(buf) - pos);
	ret = sbuf_search(mark2, mark1, str, strlen(str), opts);
	printf("search '%s' at %ld: ret=%d range=(%ld,%ld)\n", str, pos,
		ret, sbuf_mark_begin(mark2), sbuf_mark_length(mark2));
}

static void find_pair(long pos) {
	setup();
	printf("find_pair at %ld: ret=%ld\n", pos, sbuf_find_pair(buf, pos));
}

int main(void) {
	int i;
	printf("test-sbuf starting...\n");

	__set_liberror(__complain_on_error | __exit_on_error);

	setup();
	printf("character 4 (macro): %d\n", sbuf_charat(buf, 4));
	printf("character 4 (funcn): %d\n", (sbuf_charat)(buf, 4));
	
	delete(0, 2, 1, 3);	/* delete "ab" */
	delete(0, 3, 0, 3);	/* delete "abc" */
	delete(0, 4, 0, 2);	/* delete "abcd" */
	delete(3, 2, 3, 1);	/* delete "de" */
	delete(3, 3, 3, 0);	/* delete "def" */
	delete(3, 4, 3, 0);	/* delete "defg" */
	delete(5, 2, 3, 2);	/* delete "fg" */
	delete(6, 3, 3, 3);	/* delete "ghi" */
	delete(7, 2, 3, 3);	/* delete "hi" */

	for (i = 0; i < 3; ++i) insert(i, ".", 4, 3);
	for (i = 3; i < 6; ++i) insert(i, ".", 3, 4);
	for (i = 6; i < 10; ++i)insert(i, ".", 3, 3);

	search(0, "abc", 0);
	search(0, "def", 0);
	search(1, "...", 0);

	search(0, "abc", SBUF_BACKWARD);
	search(0, "def", SBUF_BACKWARD);
	search(1, "...", SBUF_BACKWARD);

	search(0, "ABC", SBUF_IGNORE_CASE);
	search(0, "DEF", SBUF_IGNORE_CASE);
	search(1, "...", SBUF_IGNORE_CASE);

	search(0, "ABC", SBUF_IGNORE_CASE | SBUF_BACKWARD);
	search(0, "DEF", SBUF_IGNORE_CASE | SBUF_BACKWARD);
	search(1, "...", SBUF_IGNORE_CASE | SBUF_BACKWARD);

	find_pair(0);
	find_pair(9);
	find_pair(11);
	find_pair(15);
	find_pair(17);
	
	{
	char foo[1000];
	int ret;
	setup();
	sbuf_remark(mark1, 0, 10);
	(void) sbuf_strchange(mark1, "abcdef\nqwerty\n", 14);
	                           /* 0123456 789abcd */
	sbuf_remark(mark1, 1, 11);
	sbuf_mark_set_columnar(mark1, 1);
	sbuf_strat(foo, mark1);
	printf("foo=<%s>\n", foo);
	ret = sbuf_strchange(mark1, "", 0);
	printf("ret == %d\n", ret);
	dump();
	}

	printf("undo:\n");	
	setup();
	dump();
	printf("ret=%d\n", sbuf_undo_atomic(buf));
	dump();

	sbuf_destroy(buf);

	printf("test-sbuf done\n");
	return 0;
}