forked from antirez/linenoise
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathteststringbuf.c
More file actions
171 lines (144 loc) · 3.93 KB
/
Copy pathteststringbuf.c
File metadata and controls
171 lines (144 loc) · 3.93 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
/*#include <assert.h>*/
#include <stringbuf.h>
#include <utf8.h>
/* We create our own version of assert() that just prints
* the error message and continues, so that we can run all the tests even if some of them fail. */
#undef assert
#define assert_(F, L, X) do { if (!(X)) { fprintf(stderr, "%s:%d: Assertion failed: %s\n", F, L, #X); }; errs++;} while (0)
#define assert(X) assert_(__FILE__, __LINE__, X)
static int errs;
static void show_buf(stringbuf *sb)
{
printf("[%d,%d] = %s\n", sb_len(sb), sb_chars(sb), sb_str(sb));
}
#define validate_buf(SB, EXP) validate_buf_(__FILE__, __LINE__, SB, EXP)
static void validate_buf_(const char *file, int line, stringbuf *sb, const char *expected)
{
const char *pt = sb_str(sb);
if (pt == NULL) {
if (expected != NULL) {
fprintf(stderr, "%s:%d: Error: Expected NULL, got '%s'\n", file, line, pt);
errs++;
}
}
else if (strcmp(pt, expected) != 0) {
show_buf(sb);
fprintf(stderr, "%s:%d: Error: Expected '%s', got '%s'\n", file, line, expected, pt);
errs++;
}
sb_free(sb);
}
int main(void)
{
stringbuf *sb;
char *pt;
sb = sb_alloc();
validate_buf(sb, NULL);
sb = sb_alloc();
sb_append(sb, "hello");
sb_append(sb, "world");
validate_buf(sb, "helloworld");
sb = sb_alloc();
sb_append(sb, "hello");
sb_append(sb, "world");
sb_append(sb, "");
sb_append(sb, "xxx");
assert(sb_len(sb) == 13);
validate_buf(sb, "helloworldxxx");
sb = sb_alloc();
sb_append(sb, "first");
sb_append(sb, "string");
validate_buf(sb, "firststring");
sb = sb_alloc();
sb_append(sb, "");
validate_buf(sb, "");
sb = sb_alloc();
sb_append_len(sb, "one string here", 3);
sb_append_len(sb, "second string here", 6);
validate_buf(sb, "onesecond");
sb = sb_alloc();
sb_append_len(sb, "one string here", 3);
sb_append_len(sb, "second string here", 6);
pt = sb_to_string(sb);
assert(strcmp(pt, "onesecond") == 0);
free(pt);
sb = sb_alloc();
pt = sb_to_string(sb);
assert(strcmp(pt, "") == 0);
free(pt);
sb = sb_alloc();
sb_append(sb, "one");
sb_append(sb, "three");
sb_insert(sb, 3, "two");
validate_buf(sb, "onetwothree");
sb = sb_alloc();
sb_insert(sb, 0, "two");
sb_insert(sb, 0, "one");
sb_insert(sb, 20, "three");
validate_buf(sb, "onetwothree");
sb = sb_alloc();
sb_append(sb, "one");
sb_append(sb, "extra");
sb_append(sb, "two");
sb_append(sb, "three");
sb_delete(sb, 3, 5);
validate_buf(sb, "onetwothree");
sb = sb_alloc();
sb_append(sb, "one");
sb_append(sb, "two");
sb_append(sb, "three");
validate_buf(sb, "onetwothree");
/*sb_delete(sb, 6, -1);*/
/*validate_buf(sb, "onetwo");*/
sb = sb_alloc();
sb_append(sb, "one");
sb_append(sb, "two");
sb_append(sb, "three");
sb_delete(sb, 0, -1);
validate_buf(sb, "");
sb = sb_alloc();
sb_append(sb, "one");
sb_append(sb, "two");
sb_append(sb, "three");
sb_delete(sb, 50, 20);
validate_buf(sb, "onetwothree");
/* OK to sb_free() a NULL pointer */
sb_free(NULL);
#ifdef USE_UTF8
sb = sb_alloc();
sb_append(sb, "oneµtwo");
assert(sb_len(sb) == 8);
assert(sb_chars(sb) == 7);
show_buf(sb);
sb_delete(sb, 3, 2);
show_buf(sb);
assert(sb_len(sb) == 6);
assert(sb_chars(sb) == 6);
validate_buf(sb, "onetwo");
/* Now test with grapheme clusters */
sb = sb_alloc();
sb_append(sb, "onee\u0301two"); /* 'e' + combining acute accent */
assert(sb_len(sb) == 9);
assert(sb_chars(sb) == 7);
/* Delete the 'e' + combining acute accent as a single grapheme cluster */
sb_delete(sb, 3, 3);
assert(sb_len(sb) == 6);
assert(sb_chars(sb) == 6);
validate_buf(sb, "onetwo");
/* Now test with an emoji that is a single grapheme cluster but multiple code points */
sb = sb_alloc();
sb_append(sb, "one" "❤️❤️" "+two");
assert(sb_chars(sb) == 9);
/* Delete the second heart plus the "+" */
sb_delete_chars(sb, 4, 2);
assert(sb_chars(sb) == 7);
/* Now delete the first heart */
sb_delete_chars(sb, 3, 1);
validate_buf(sb, "onetwo");
#endif
return(0);
}