blob: b7c5e27fc6ecbce275255e0b4d10dbf47077b246 [file] [log] [blame]
Katerina Barone-Adesi73377222013-02-21 05:16:29 +00001/* (C) 2012-2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
2 * All Rights Reserved
3 *
4 * This program is iree software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20#include <stdio.h>
21#include <assert.h>
22#include <string.h>
23
24#include <osmocom/core/strrb.h>
25#include <osmocom/core/talloc.h>
26#include <osmocom/core/logging.h>
27
28struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
29
30#define STR0 "hello"
31#define STR1 "a"
32#define STR2 "world"
33#define STR3 "sky"
34#define STR4 "moon"
35
36#define TESTSIZE 2
37
38void init_rbs(void)
39{
40 rb0 = osmo_strrb_create(NULL, TESTSIZE);
41
42 rb1 = osmo_strrb_create(NULL, TESTSIZE);
43 osmo_strrb_add(rb1, STR0);
44
45 rb2 = osmo_strrb_create(NULL, TESTSIZE);
46 osmo_strrb_add(rb2, STR0);
47 osmo_strrb_add(rb2, STR1);
48
49 rb3 = osmo_strrb_create(NULL, TESTSIZE);
50 osmo_strrb_add(rb3, STR0);
51 osmo_strrb_add(rb3, STR1);
52 osmo_strrb_add(rb3, STR2);
53
54 rb4 = osmo_strrb_create(NULL, TESTSIZE);
55 osmo_strrb_add(rb4, STR0);
56 osmo_strrb_add(rb4, STR1);
57 osmo_strrb_add(rb4, STR2);
58 osmo_strrb_add(rb4, STR3);
59
60 rb5 = osmo_strrb_create(NULL, TESTSIZE);
61 osmo_strrb_add(rb5, STR0);
62 osmo_strrb_add(rb5, STR1);
63 osmo_strrb_add(rb5, STR2);
64 osmo_strrb_add(rb5, STR3);
65 osmo_strrb_add(rb5, STR4);
66}
67
68void free_rbs(void)
69{
70 talloc_free(rb0);
71 talloc_free(rb1);
72 talloc_free(rb2);
73 talloc_free(rb3);
74 talloc_free(rb4);
75 talloc_free(rb5);
76}
77
78void test_offset_valid(void)
79{
80 assert(_osmo_strrb_is_bufindex_valid(rb1, 0));
81 assert(!_osmo_strrb_is_bufindex_valid(rb1, 1));
82 assert(!_osmo_strrb_is_bufindex_valid(rb1, 2));
83
84 assert(!_osmo_strrb_is_bufindex_valid(rb3, 0));
85 assert(_osmo_strrb_is_bufindex_valid(rb3, 1));
86 assert(_osmo_strrb_is_bufindex_valid(rb3, 2));
87
88 assert(_osmo_strrb_is_bufindex_valid(rb4, 0));
89 assert(!_osmo_strrb_is_bufindex_valid(rb4, 1));
90 assert(_osmo_strrb_is_bufindex_valid(rb4, 2));
91
92 assert(_osmo_strrb_is_bufindex_valid(rb5, 0));
93 assert(_osmo_strrb_is_bufindex_valid(rb5, 1));
94 assert(!_osmo_strrb_is_bufindex_valid(rb5, 2));
95}
96
97void test_elems(void)
98{
99 assert(osmo_strrb_elements(rb0) == 0);
100 assert(osmo_strrb_elements(rb1) == 1);
101 assert(osmo_strrb_elements(rb2) == 2);
102 assert(osmo_strrb_elements(rb3) == 2);
103}
104
105void test_getn(void)
106{
107 assert(!osmo_strrb_get_nth(rb0, 0));
108 assert(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0)));
109 assert(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1)));
110 assert(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0)));
111 assert(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1)));
112 assert(!osmo_strrb_get_nth(rb3, 2));
113}
114
115void test_getn_wrap(void)
116{
117 assert(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0)));
118 assert(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1)));
119
120 assert(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0)));
121 assert(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1)));
122}
123
124void test_add(void)
125{
126 struct osmo_strrb *rb = osmo_strrb_create(NULL, 3);
127 assert(rb->start == 0);
128 assert(rb->end == 0);
129
130 osmo_strrb_add(rb, "a");
131 osmo_strrb_add(rb, "b");
132 osmo_strrb_add(rb, "c");
133 assert(rb->start == 0);
134 assert(rb->end == 3);
135 assert(osmo_strrb_elements(rb) == 3);
136
137 osmo_strrb_add(rb, "d");
138 assert(rb->start == 1);
139 assert(rb->end == 0);
140 assert(osmo_strrb_elements(rb) == 3);
141 assert(!strcmp("b", osmo_strrb_get_nth(rb, 0)));
142 assert(!strcmp("c", osmo_strrb_get_nth(rb, 1)));
143 assert(!strcmp("d", osmo_strrb_get_nth(rb, 2)));
144
145 osmo_strrb_add(rb, "e");
146 assert(rb->start == 2);
147 assert(rb->end == 1);
148 assert(!strcmp("c", osmo_strrb_get_nth(rb, 0)));
149 assert(!strcmp("d", osmo_strrb_get_nth(rb, 1)));
150 assert(!strcmp("e", osmo_strrb_get_nth(rb, 2)));
151
152 osmo_strrb_add(rb, "f");
153 assert(rb->start == 3);
154 assert(rb->end == 2);
155 assert(!strcmp("d", osmo_strrb_get_nth(rb, 0)));
156 assert(!strcmp("e", osmo_strrb_get_nth(rb, 1)));
157 assert(!strcmp("f", osmo_strrb_get_nth(rb, 2)));
158
159 osmo_strrb_add(rb, "g");
160 assert(rb->start == 0);
161 assert(rb->end == 3);
162 assert(!strcmp("e", osmo_strrb_get_nth(rb, 0)));
163 assert(!strcmp("f", osmo_strrb_get_nth(rb, 1)));
164 assert(!strcmp("g", osmo_strrb_get_nth(rb, 2)));
165
166 osmo_strrb_add(rb, "h");
167 assert(rb->start == 1);
168 assert(rb->end == 0);
169 assert(!strcmp("f", osmo_strrb_get_nth(rb, 0)));
170 assert(!strcmp("g", osmo_strrb_get_nth(rb, 1)));
171 assert(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
172
173 talloc_free(rb);
174}
175
176void test_long_msg(void)
177{
178 struct osmo_strrb *rb = osmo_strrb_create(NULL, 2);
179 int test_size = RB_MAX_MESSAGE_SIZE + 7;
180 char *tests1, *tests2;
181 const char *rb_content;
182 int i;
183
184 tests1 = malloc(test_size);
185 tests2 = malloc(test_size);
186 /* Be certain allocating memory worked before continuing */
187 assert(tests1);
188 assert(tests2);
189
190 for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) {
191 tests1[i] = 'a';
192 tests1[i + 1] = 'b';
193 }
194 tests1[i] = '\0';
195
196 osmo_strrb_add(rb, tests1);
197 strcpy(tests2, tests1);
198
199 /* Verify that no stale data from test1 is lingering... */
200 bzero(tests1, test_size);
201 free(tests1);
202
203 rb_content = osmo_strrb_get_nth(rb, 0);
204 assert(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1));
205 assert(!rb_content[RB_MAX_MESSAGE_SIZE - 1]);
206 assert(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
207
208 free(tests2);
209 talloc_free(rb);
210}
211
212int main(int argc, char **argv)
213{
214 init_rbs();
215 test_offset_valid();
216 test_elems();
217 test_getn();
218 test_getn_wrap();
219 test_add();
220 test_long_msg();
221 printf("All tests passed\n");
222
223 free_rbs();
224 return 0;
225}