Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 1 | /* (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> |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
| 23 | #include <osmocom/core/strrb.h> |
| 24 | #include <osmocom/core/talloc.h> |
| 25 | #include <osmocom/core/logging.h> |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 26 | #include <osmocom/core/utils.h> |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 27 | |
| 28 | struct 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 3 |
| 37 | |
| 38 | void 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 | |
| 68 | void 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 | |
| 78 | void test_offset_valid(void) |
| 79 | { |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 80 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb1, 0)); |
| 81 | OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 1)); |
| 82 | OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 2)); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 83 | |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 84 | OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb3, 0)); |
| 85 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 1)); |
| 86 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 2)); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 87 | |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 88 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 0)); |
| 89 | OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb4, 1)); |
| 90 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 2)); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 91 | |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 92 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 0)); |
| 93 | OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 1)); |
| 94 | OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb5, 2)); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void test_elems(void) |
| 98 | { |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 99 | OSMO_ASSERT(osmo_strrb_elements(rb0) == 0); |
| 100 | OSMO_ASSERT(osmo_strrb_elements(rb1) == 1); |
| 101 | OSMO_ASSERT(osmo_strrb_elements(rb2) == 2); |
| 102 | OSMO_ASSERT(osmo_strrb_elements(rb3) == 2); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void test_getn(void) |
| 106 | { |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 107 | OSMO_ASSERT(!osmo_strrb_get_nth(rb0, 0)); |
| 108 | OSMO_ASSERT(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0))); |
| 109 | OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1))); |
| 110 | OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0))); |
| 111 | OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1))); |
| 112 | OSMO_ASSERT(!osmo_strrb_get_nth(rb3, 2)); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void test_getn_wrap(void) |
| 116 | { |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 117 | OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0))); |
| 118 | OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 119 | |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 120 | OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0))); |
| 121 | OSMO_ASSERT(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void test_add(void) |
| 125 | { |
| 126 | struct osmo_strrb *rb = osmo_strrb_create(NULL, 4); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 127 | OSMO_ASSERT(rb->start == 0); |
| 128 | OSMO_ASSERT(rb->end == 0); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 129 | |
| 130 | osmo_strrb_add(rb, "a"); |
| 131 | osmo_strrb_add(rb, "b"); |
| 132 | osmo_strrb_add(rb, "c"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 133 | OSMO_ASSERT(rb->start == 0); |
| 134 | OSMO_ASSERT(rb->end == 3); |
| 135 | OSMO_ASSERT(osmo_strrb_elements(rb) == 3); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 136 | |
| 137 | osmo_strrb_add(rb, "d"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 138 | OSMO_ASSERT(rb->start == 1); |
| 139 | OSMO_ASSERT(rb->end == 0); |
| 140 | OSMO_ASSERT(osmo_strrb_elements(rb) == 3); |
| 141 | OSMO_ASSERT(!strcmp("b", osmo_strrb_get_nth(rb, 0))); |
| 142 | OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 1))); |
| 143 | OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 2))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 144 | |
| 145 | osmo_strrb_add(rb, "e"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 146 | OSMO_ASSERT(rb->start == 2); |
| 147 | OSMO_ASSERT(rb->end == 1); |
| 148 | OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 0))); |
| 149 | OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 1))); |
| 150 | OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 2))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 151 | |
| 152 | osmo_strrb_add(rb, "f"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 153 | OSMO_ASSERT(rb->start == 3); |
| 154 | OSMO_ASSERT(rb->end == 2); |
| 155 | OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 0))); |
| 156 | OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 1))); |
| 157 | OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 2))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 158 | |
| 159 | osmo_strrb_add(rb, "g"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 160 | OSMO_ASSERT(rb->start == 0); |
| 161 | OSMO_ASSERT(rb->end == 3); |
| 162 | OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 0))); |
| 163 | OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 1))); |
| 164 | OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 2))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 165 | |
| 166 | osmo_strrb_add(rb, "h"); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 167 | OSMO_ASSERT(rb->start == 1); |
| 168 | OSMO_ASSERT(rb->end == 0); |
| 169 | OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 0))); |
| 170 | OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 1))); |
| 171 | OSMO_ASSERT(!strcmp("h", osmo_strrb_get_nth(rb, 2))); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 172 | |
| 173 | talloc_free(rb); |
| 174 | } |
| 175 | |
| 176 | void 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 */ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 187 | OSMO_ASSERT(tests1); |
| 188 | OSMO_ASSERT(tests2); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 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); |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 204 | OSMO_ASSERT(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1)); |
| 205 | OSMO_ASSERT(!rb_content[RB_MAX_MESSAGE_SIZE - 1]); |
| 206 | OSMO_ASSERT(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 207 | |
| 208 | free(tests2); |
| 209 | talloc_free(rb); |
| 210 | } |
| 211 | |
| 212 | int 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 | } |