-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_plist.c
137 lines (113 loc) · 4.14 KB
/
test_plist.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
#include <assert.h>
#include "vita/container/str.h"
#include "vita/container/plist.h"
#include "vita/util/log.h"
int main(void) {
vt_mallocator_t *alloctr = vt_mallocator_create();
// elements
char *h = "hello";
char *w = "world";
char *t = "temp";
char *a = "Aaaa";
char *b = "Bbbb";
char *c = "Cccc";
char *d = "Dddd";
vt_plist_t *p = vt_plist_create(5, alloctr); {
assert(vt_plist_len(p) == 0);
assert(vt_plist_capacity(p) == 5);
vt_plist_push_back(p, h);
vt_plist_push_back(p, w);
assert(vt_plist_len(p) == 2);
assert(vt_plist_capacity(p) == 5);
assert(vt_plist_has_space(p) == 3);
assert(vt_plist_can_find(p, w) == 1);
assert(vt_plist_can_find(p, t) == -1);
vt_plist_shrink(p);
assert(vt_plist_capacity(p) == 2);
vt_plist_reserve(p, 5);
assert(vt_plist_capacity(p) == 7);
assert(strncmp(vt_plist_pop_get(p), w, strlen(w)) == 0);
assert(vt_plist_len(p) == 1);
assert(vt_plist_capacity(p) == 7);
vt_plist_remove(p, 0, VT_REMOVE_STRATEGY_FAST);
assert(vt_plist_len(p) == 0);
assert(vt_plist_capacity(p) == 7);
vt_plist_push_back(p, h);
vt_plist_push_back(p, w);
vt_plist_push_back(p, h);
vt_plist_push_back(p, w);
vt_plist_push_back(p, h);
vt_plist_push_back(p, w);
void *i = NULL;
while ((i = vt_array_slide_front(p)) != NULL) {
printf("%s\n", *((char**)(i)));
}
char *c = NULL;
while ((c = vt_plist_slide_front(p)) != NULL) {
printf("--- %s\n", c);
}
// copying
vt_plist_t *pcopy = vt_plist_dup(p, alloctr); {
assert(vt_plist_len(pcopy) == vt_plist_len(p));
assert(vt_plist_get(pcopy, 0) == vt_plist_get(p, 0));
assert(vt_str_equals_z(vt_plist_get(pcopy, 0), "hello"));
} vt_plist_destroy(pcopy);
// resize
vt_plist_resize(p, 3);
assert(vt_plist_len(p) == 3);
} vt_plist_destroy(p);
// testing elements removal
vt_plist_t *list = vt_plist_create(1, alloctr);
{
// push items
vt_plist_push_back(list, a);
vt_plist_push_back(list, b);
vt_plist_push_back(list, c);
vt_plist_push_back(list, d);
vt_plist_push_back(list, h);
vt_plist_push_back(list, w);
vt_plist_push_back(list, t);
assert(vt_plist_len(list) == 7);
assert(vt_plist_get(list, 0) == a);
vt_plist_remove(list, 0, VT_REMOVE_STRATEGY_STABLE);
assert(vt_plist_get(list, 0) != NULL);
assert(vt_plist_get(list, 0) == b);
assert(vt_plist_len(list) == 6);
vt_plist_remove(list, 0, VT_REMOVE_STRATEGY_FAST);
assert(vt_plist_get(list, 0) != NULL);
assert(vt_plist_get(list, 0) == t);
assert(vt_plist_len(list) == 5);
vt_plist_remove(list, 0, VT_REMOVE_STRATEGY_STABLE);
assert(vt_plist_get(list, 0) != NULL);
assert(vt_plist_get(list, 0) == c);
assert(vt_plist_len(list) == 4);
vt_plist_remove(list, 0, VT_REMOVE_STRATEGY_FAST);
assert(vt_plist_get(list, 0) != NULL);
assert(vt_plist_get(list, 0) == w);
assert(vt_plist_get(list, 1) == d);
assert(vt_plist_get(list, 2) == h);
assert(vt_plist_len(list) == 3);
vt_plist_shrink(list);
assert(vt_plist_len(list) == 3);
assert(vt_plist_capacity(list) == 3);
vt_plist_remove(list, vt_plist_len(list) - 1, VT_REMOVE_STRATEGY_STABLE);
assert(vt_plist_len(list) == 2);
assert(vt_plist_capacity(list) == 3);
}
vt_plist_destroy(list);
// pop_get
list = vt_plist_create(5, alloctr);
{
vt_plist_push_back(list, h);
vt_plist_push_back(list, w);
vt_plist_push_back(list, t);
assert(vt_plist_pop_get(list) == t);
assert(vt_plist_pop_get(list) == w);
assert(vt_plist_pop_get(list) == h);
assert(vt_plist_pop_get(list) == NULL);
assert(vt_plist_pop_get(list) == NULL);
}
vt_plist_destroy(list);
vt_mallocator_destroy(alloctr);
return 0;
}