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