blob: cdeaa3466f724e09664a3164fec576f8de96ba63 [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 *
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000016 */
17
18#include <stdio.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000019#include <string.h>
20
21#include <osmocom/core/strrb.h>
22#include <osmocom/core/talloc.h>
23#include <osmocom/core/logging.h>
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000024#include <osmocom/core/utils.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000025
26struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5;
27
28#define STR0 "hello"
29#define STR1 "a"
30#define STR2 "world"
31#define STR3 "sky"
32#define STR4 "moon"
33
34#define TESTSIZE 3
35
36void init_rbs(void)
37{
38 rb0 = osmo_strrb_create(NULL, TESTSIZE);
39
40 rb1 = osmo_strrb_create(NULL, TESTSIZE);
41 osmo_strrb_add(rb1, STR0);
42
43 rb2 = osmo_strrb_create(NULL, TESTSIZE);
44 osmo_strrb_add(rb2, STR0);
45 osmo_strrb_add(rb2, STR1);
46
47 rb3 = osmo_strrb_create(NULL, TESTSIZE);
48 osmo_strrb_add(rb3, STR0);
49 osmo_strrb_add(rb3, STR1);
50 osmo_strrb_add(rb3, STR2);
51
52 rb4 = osmo_strrb_create(NULL, TESTSIZE);
53 osmo_strrb_add(rb4, STR0);
54 osmo_strrb_add(rb4, STR1);
55 osmo_strrb_add(rb4, STR2);
56 osmo_strrb_add(rb4, STR3);
57
58 rb5 = osmo_strrb_create(NULL, TESTSIZE);
59 osmo_strrb_add(rb5, STR0);
60 osmo_strrb_add(rb5, STR1);
61 osmo_strrb_add(rb5, STR2);
62 osmo_strrb_add(rb5, STR3);
63 osmo_strrb_add(rb5, STR4);
64}
65
66void free_rbs(void)
67{
68 talloc_free(rb0);
69 talloc_free(rb1);
70 talloc_free(rb2);
71 talloc_free(rb3);
72 talloc_free(rb4);
73 talloc_free(rb5);
74}
75
76void test_offset_valid(void)
77{
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000078 OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb1, 0));
79 OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 1));
80 OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 2));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000081
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000082 OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb3, 0));
83 OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 1));
84 OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 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(rb4, 0));
87 OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb4, 1));
88 OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 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(rb5, 0));
91 OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 1));
92 OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb5, 2));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000093}
94
95void test_elems(void)
96{
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000097 OSMO_ASSERT(osmo_strrb_elements(rb0) == 0);
98 OSMO_ASSERT(osmo_strrb_elements(rb1) == 1);
99 OSMO_ASSERT(osmo_strrb_elements(rb2) == 2);
100 OSMO_ASSERT(osmo_strrb_elements(rb3) == 2);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000101}
102
103void test_getn(void)
104{
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000105 OSMO_ASSERT(!osmo_strrb_get_nth(rb0, 0));
106 OSMO_ASSERT(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0)));
107 OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1)));
108 OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0)));
109 OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1)));
110 OSMO_ASSERT(!osmo_strrb_get_nth(rb3, 2));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000111}
112
113void test_getn_wrap(void)
114{
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000115 OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0)));
116 OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000117
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000118 OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0)));
119 OSMO_ASSERT(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000120}
121
122void test_add(void)
123{
124 struct osmo_strrb *rb = osmo_strrb_create(NULL, 4);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000125 OSMO_ASSERT(rb->start == 0);
126 OSMO_ASSERT(rb->end == 0);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000127
128 osmo_strrb_add(rb, "a");
129 osmo_strrb_add(rb, "b");
130 osmo_strrb_add(rb, "c");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000131 OSMO_ASSERT(rb->start == 0);
132 OSMO_ASSERT(rb->end == 3);
133 OSMO_ASSERT(osmo_strrb_elements(rb) == 3);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000134
135 osmo_strrb_add(rb, "d");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000136 OSMO_ASSERT(rb->start == 1);
137 OSMO_ASSERT(rb->end == 0);
138 OSMO_ASSERT(osmo_strrb_elements(rb) == 3);
139 OSMO_ASSERT(!strcmp("b", osmo_strrb_get_nth(rb, 0)));
140 OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 1)));
141 OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 2)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000142
143 osmo_strrb_add(rb, "e");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000144 OSMO_ASSERT(rb->start == 2);
145 OSMO_ASSERT(rb->end == 1);
146 OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 0)));
147 OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 1)));
148 OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 2)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000149
150 osmo_strrb_add(rb, "f");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000151 OSMO_ASSERT(rb->start == 3);
152 OSMO_ASSERT(rb->end == 2);
153 OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 0)));
154 OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 1)));
155 OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 2)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000156
157 osmo_strrb_add(rb, "g");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000158 OSMO_ASSERT(rb->start == 0);
159 OSMO_ASSERT(rb->end == 3);
160 OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 0)));
161 OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 1)));
162 OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 2)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000163
164 osmo_strrb_add(rb, "h");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000165 OSMO_ASSERT(rb->start == 1);
166 OSMO_ASSERT(rb->end == 0);
167 OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 0)));
168 OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 1)));
169 OSMO_ASSERT(!strcmp("h", osmo_strrb_get_nth(rb, 2)));
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000170
171 talloc_free(rb);
172}
173
174void test_long_msg(void)
175{
176 struct osmo_strrb *rb = osmo_strrb_create(NULL, 2);
177 int test_size = RB_MAX_MESSAGE_SIZE + 7;
178 char *tests1, *tests2;
179 const char *rb_content;
180 int i;
181
182 tests1 = malloc(test_size);
183 tests2 = malloc(test_size);
184 /* Be certain allocating memory worked before continuing */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000185 OSMO_ASSERT(tests1);
186 OSMO_ASSERT(tests2);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000187
188 for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) {
189 tests1[i] = 'a';
190 tests1[i + 1] = 'b';
191 }
192 tests1[i] = '\0';
193
194 osmo_strrb_add(rb, tests1);
195 strcpy(tests2, tests1);
196
197 /* Verify that no stale data from test1 is lingering... */
198 bzero(tests1, test_size);
199 free(tests1);
200
201 rb_content = osmo_strrb_get_nth(rb, 0);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000202 OSMO_ASSERT(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1));
203 OSMO_ASSERT(!rb_content[RB_MAX_MESSAGE_SIZE - 1]);
204 OSMO_ASSERT(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000205
206 free(tests2);
207 talloc_free(rb);
208}
209
210int main(int argc, char **argv)
211{
212 init_rbs();
213 test_offset_valid();
214 test_elems();
215 test_getn();
216 test_getn_wrap();
217 test_add();
218 test_long_msg();
219 printf("All tests passed\n");
220
221 free_rbs();
222 return 0;
223}