-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cstring.c
147 lines (120 loc) · 4.01 KB
/
test_cstring.c
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
#include "cstring.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
struct string *s = NULL;
struct string *sub = NULL;
char *str = NULL;
string_allocator_set(realloc, free);
s = string_new();
assert(string_empty(s));
assert(NULL == string_c_str(s));
assert(0 == string_size(s));
assert('\0' == string_back(s));
string_append_fill(s, 4, 'x');
assert(4 == string_size(s));
assert(0 == strcmp(string_c_str(s), "xxxx"));
string_insert_buffer(s, 2, "abcdef", 3);
assert(7 == string_size(s));
assert(0 == strcmp(string_c_str(s), "xxabcxx"));
string_insert_c_str(s, 7, "z");
assert(8 == string_size(s));
assert(0 == strcmp(string_c_str(s), "xxabcxxz"));
string_insert_fill(s, 0, 3, 'y');
assert(11 == string_size(s));
assert(0 == strcmp(string_c_str(s), "yyyxxabcxxz"));
string_clear(s);
string_append_c_str(s, "one");
assert(!string_empty(s));
assert(NULL != string_c_str(s));
assert(3 == string_size(s));
assert(0 == strcmp(string_c_str(s), "one"));
str = string_c_str_move(s);
assert(0 == strcmp(str, "one"));
free(str);
assert(string_empty(s));
assert(NULL == string_c_str(s));
assert(0 == string_size(s));
string_append_fill(s, 1, 'o');
string_append_fill(s, 3, 'n');
string_append_fill(s, 1, 'e');
assert(!string_empty(s));
assert(NULL != string_c_str(s));
assert(5 == string_size(s));
assert(0 == strcmp(string_c_str(s), "onnne"));
assert('o' == string_at(s, 0));
assert('n' == string_at(s, 1));
assert('e' == string_at(s, 4));
assert(0 == string_at(s, 5));
assert(0 == string_at(s, 9));
string_clear(s);
string_insert_fill(s, 0, 1, 'O');
string_insert_fill(s, 1, 3, 'N');
string_insert_fill(s, 4, 1, 'E');
assert(5 == string_size(s));
assert(0 == strcmp(string_c_str(s), "ONNNE"));
string_erase(s, 5, 99);
assert(5 == string_size(s));
string_erase(s, 4, 99);
assert(4 == string_size(s));
string_erase(s, 0, 0);
assert(4 == string_size(s));
assert(0 == strcmp(string_c_str(s), "ONNN"));
string_erase(s, 0, 1);
assert(3 == string_size(s));
assert(0 == strcmp(string_c_str(s), "NNN"));
string_append_c_str(s, "abc");
assert(6 == string_size(s));
assert(0 == strcmp(string_c_str(s), "NNNabc"));
sub = string_substr(s, 3, 0);
assert(string_empty(sub));
assert(NULL == string_c_str(sub));
string_delete(sub);
sub = string_substr(s, 6, 0);
assert(string_empty(sub));
assert(NULL == string_c_str(sub));
string_delete(sub);
sub = string_substr(s, 6, 1);
assert(string_empty(sub));
assert(NULL == string_c_str(sub));
string_delete(sub);
sub = string_substr(s, 5, 1);
assert(string_size(sub) == 1);
assert(0 == strcmp(string_c_str(sub), "c"));
string_delete(sub);
sub = string_substr(s, 5, 2);
assert(string_size(sub) == 1);
assert(0 == strcmp(string_c_str(sub), "c"));
string_delete(sub);
sub = string_substr(s, 2, 1);
assert(0 == strcmp(string_c_str(sub), "N"));
string_delete(sub);
sub = string_substr(s, 2, 2);
assert(0 == strcmp(string_c_str(sub), "Na"));
string_delete(sub);
string_append_buffer(s, "defghi", 3);
assert('f' == string_back(s));
assert(9 == string_size(s));
assert(0 == strcmp(string_c_str(s), "NNNabcdef"));
string_push_back(s, 'g');
assert('g' == string_back(s));
assert(10 == string_size(s));
assert(0 == strcmp(string_c_str(s), "NNNabcdefg"));
string_pop_back(s);
assert(0 == strcmp(string_c_str(s), "NNNabcdef"));
assert(10 == string_capacity(s));
string_reserve(s, 3);
assert(10 == string_capacity(s));
string_reserve(s, 20);
assert(20 == string_capacity(s));
assert(0 == strcmp(string_c_str(s), "NNNabcdef"));
string_delete(s);
s = string_new();
string_reserve(s, 0);
assert(0 == string_c_str(s));
string_reserve(s, 1);
assert(0 == strcmp(string_c_str(s), ""));
string_delete(s);
}