Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <errno.h> |
| 22 | #include <strings.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <osmocom/gsm/gsm23236.h> |
| 26 | #include <osmocom/core/utils.h> |
| 27 | |
| 28 | void *ctx; |
| 29 | bool ok = true; |
| 30 | |
| 31 | void bitdump(uint8_t count, uint32_t val) |
| 32 | { |
| 33 | uint32_t bit; |
| 34 | if (count < 1) |
| 35 | return; |
| 36 | for (bit = ((uint32_t)1) << (count - 1); bit; bit >>= 1) |
| 37 | printf("%c", (val & bit)? '1' : '0'); |
| 38 | } |
| 39 | |
| 40 | struct nri_v_get_set_test { |
| 41 | uint32_t tmsi; |
| 42 | uint8_t nri_bitlen; |
| 43 | int16_t expect_get_nri; |
| 44 | int expect_get_rc; |
| 45 | int16_t set_nri_v; |
| 46 | uint32_t expect_tmsi; |
| 47 | int expect_set_rc; |
| 48 | }; |
| 49 | |
| 50 | struct nri_v_get_set_test nri_v_get_set_tests[] = { |
| 51 | { |
| 52 | .tmsi = 0, |
| 53 | .nri_bitlen = 10, |
| 54 | .expect_get_nri = 0, |
| 55 | .set_nri_v = 0, |
| 56 | .expect_tmsi = 0, |
| 57 | }, |
| 58 | { |
| 59 | .tmsi = 0, |
| 60 | .nri_bitlen = 10, |
| 61 | .expect_get_nri = 0, |
| 62 | .set_nri_v = 0x7fff, |
| 63 | .expect_tmsi = 0x00ffc000 |
| 64 | }, |
| 65 | { |
| 66 | .tmsi = 0xffffffff, |
| 67 | .nri_bitlen = 10, |
| 68 | .expect_get_nri = 0x3ff, |
| 69 | .set_nri_v = 0, |
| 70 | .expect_tmsi = 0xff003fff |
| 71 | }, |
| 72 | { |
| 73 | .tmsi = 0xffffffff, |
| 74 | .nri_bitlen = 10, |
| 75 | .expect_get_nri = 0x3ff, |
| 76 | .set_nri_v = 0x7fff, |
| 77 | .expect_tmsi = 0xffffffff |
| 78 | }, |
| 79 | { |
| 80 | .tmsi = 0, |
| 81 | .nri_bitlen = 5, |
| 82 | .expect_get_nri = 0, |
| 83 | .set_nri_v = 0, |
| 84 | .expect_tmsi = 0, |
| 85 | }, |
| 86 | { |
| 87 | .tmsi = 0, |
| 88 | .nri_bitlen = 5, |
| 89 | .expect_get_nri = 0, |
| 90 | .set_nri_v = 0x7fff, |
| 91 | .expect_tmsi = 0x00f80000 |
| 92 | }, |
| 93 | { |
| 94 | .tmsi = 0xffffffff, |
| 95 | .nri_bitlen = 5, |
| 96 | .expect_get_nri = 0x1f, |
| 97 | .set_nri_v = 0, |
| 98 | .expect_tmsi = 0xff07ffff |
| 99 | }, |
| 100 | { |
| 101 | .tmsi = 0xffffffff, |
| 102 | .nri_bitlen = 5, |
| 103 | .expect_get_nri = 0x1f, |
| 104 | .set_nri_v = 0x7fff, |
| 105 | .expect_tmsi = 0xffffffff |
| 106 | }, |
| 107 | { |
| 108 | .tmsi = 0x01234567, |
| 109 | .nri_bitlen = 8, |
| 110 | .expect_get_nri = 0x23, |
| 111 | .set_nri_v = 0x42, |
| 112 | .expect_tmsi = 0x01424567 |
| 113 | }, |
| 114 | { |
| 115 | .tmsi = 0x01234567, |
| 116 | .nri_bitlen = 15, |
| 117 | .expect_get_nri = 0x2345 >> 1, |
| 118 | .set_nri_v = 0x7fff, |
| 119 | .expect_tmsi = 0x01ffff67 |
| 120 | }, |
| 121 | { |
| 122 | .tmsi = 0x01234567, |
| 123 | .nri_bitlen = 16, |
| 124 | .expect_get_rc = -1, |
| 125 | .expect_get_nri = -1, |
| 126 | .set_nri_v = 0x7fff, |
| 127 | .expect_set_rc = -1, |
| 128 | .expect_tmsi = 0x01234567, |
| 129 | }, |
| 130 | { |
| 131 | .tmsi = 0x01234567, |
| 132 | .nri_bitlen = 0, |
| 133 | .expect_get_rc = -1, |
| 134 | .expect_get_nri = -1, |
| 135 | .set_nri_v = 0x7fff, |
| 136 | .expect_set_rc = -1, |
| 137 | .expect_tmsi = 0x01234567, |
| 138 | }, |
| 139 | }; |
| 140 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 141 | void test_nri_v_get_set(void) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 142 | { |
| 143 | struct nri_v_get_set_test *t; |
| 144 | |
| 145 | for (t = nri_v_get_set_tests; t < &nri_v_get_set_tests[ARRAY_SIZE(nri_v_get_set_tests)]; t++) { |
| 146 | int16_t nri_v = 0; |
| 147 | uint32_t tmsi2; |
| 148 | int rc; |
| 149 | |
| 150 | rc = osmo_tmsi_nri_v_get(&nri_v, t->tmsi, t->nri_bitlen); |
| 151 | printf("\nosmo_tmsi_nri_v_get(0x%08x, %u) -> nri_v=0x%x rc=%d\n", t->tmsi, t->nri_bitlen, nri_v, rc); |
| 152 | if (!rc) { |
| 153 | printf("........|NRI->..................\n"); |
| 154 | bitdump(32, t->tmsi); |
| 155 | printf(" tmsi nri_bitlen=%u\n", t->nri_bitlen); |
| 156 | printf(" "); |
| 157 | bitdump(t->nri_bitlen, nri_v); |
| 158 | printf(" = 0x%x", nri_v); |
| 159 | } |
| 160 | if (nri_v == t->expect_get_nri && rc == t->expect_get_rc) { |
| 161 | printf(" ok\n"); |
| 162 | } else { |
| 163 | printf(" ERROR: expected nri_v=0x%x rc=%d\n", t->expect_get_nri, t->expect_get_rc); |
| 164 | ok = false; |
| 165 | } |
| 166 | |
| 167 | tmsi2 = t->tmsi; |
| 168 | rc = osmo_tmsi_nri_v_set(&tmsi2, t->set_nri_v, t->nri_bitlen); |
| 169 | printf("osmo_tmsi_nri_v_set(0x%08x, 0x%x, %u) -> tmsi=0x%08x rc=%d\n", t->tmsi, t->set_nri_v, t->nri_bitlen, |
| 170 | tmsi2, rc); |
| 171 | if (!rc) { |
| 172 | printf(" "); |
| 173 | bitdump(t->nri_bitlen, t->set_nri_v); |
| 174 | printf("\n"); |
| 175 | bitdump(32, tmsi2); |
| 176 | } |
| 177 | if (tmsi2 == t->expect_tmsi && rc == t->expect_set_rc) { |
| 178 | printf(" ok\n"); |
| 179 | } else { |
| 180 | printf(" ERROR: expected tmsi=0x%08x rc=%d\n", t->expect_tmsi, t->expect_set_rc); |
| 181 | ok = false; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | struct nri_validate_tc { |
| 187 | int16_t nri; |
| 188 | uint8_t nri_bitlen; |
| 189 | int expect_rc; |
| 190 | }; |
| 191 | |
| 192 | struct nri_validate_tc nri_validate_tests[] = { |
| 193 | { .nri = INT16_MIN, .nri_bitlen = 10, .expect_rc = -1 }, |
| 194 | { .nri = -23, .nri_bitlen = 10, .expect_rc = -1 }, |
| 195 | { .nri = -1, .nri_bitlen = 10, .expect_rc = -1 }, |
| 196 | { .nri = 0, .nri_bitlen = 10, .expect_rc = 0 }, |
| 197 | { .nri = (1 << 10) - 1, .nri_bitlen = 10, .expect_rc = 0 }, |
| 198 | { .nri = (1 << 10), .nri_bitlen = 10, .expect_rc = 1 }, |
| 199 | { .nri = INT16_MAX, .nri_bitlen = 10, .expect_rc = 1 }, |
| 200 | |
| 201 | { .nri = INT16_MIN, .nri_bitlen = 5, .expect_rc = -1 }, |
| 202 | { .nri = -23, .nri_bitlen = 5, .expect_rc = -1 }, |
| 203 | { .nri = -1, .nri_bitlen = 5, .expect_rc = -1 }, |
| 204 | { .nri = 0, .nri_bitlen = 5, .expect_rc = 0 }, |
| 205 | { .nri = (1 << 5) - 1, .nri_bitlen = 5, .expect_rc = 0 }, |
| 206 | { .nri = (1 << 5), .nri_bitlen = 5, .expect_rc = 1 }, |
| 207 | { .nri = INT16_MAX, .nri_bitlen = 5, .expect_rc = 1 }, |
| 208 | |
| 209 | { .nri = INT16_MIN, .nri_bitlen = 1, .expect_rc = -1 }, |
| 210 | { .nri = -23, .nri_bitlen = 1, .expect_rc = -1 }, |
| 211 | { .nri = -1, .nri_bitlen = 1, .expect_rc = -1 }, |
| 212 | { .nri = 0, .nri_bitlen = 1, .expect_rc = 0 }, |
| 213 | { .nri = 1, .nri_bitlen = 1, .expect_rc = 0 }, |
| 214 | { .nri = 2, .nri_bitlen = 1, .expect_rc = 1 }, |
| 215 | { .nri = INT16_MAX, .nri_bitlen = 1, .expect_rc = 1 }, |
| 216 | |
| 217 | { .nri = INT16_MIN, .nri_bitlen = 0, .expect_rc = -1 }, |
| 218 | { .nri = -23, .nri_bitlen = 0, .expect_rc = -1 }, |
| 219 | { .nri = -1, .nri_bitlen = 0, .expect_rc = -1 }, |
| 220 | { .nri = 0, .nri_bitlen = 0, .expect_rc = 1 }, |
| 221 | { .nri = 1, .nri_bitlen = 0, .expect_rc = 1 }, |
| 222 | { .nri = INT16_MAX, .nri_bitlen = 0, .expect_rc = 1 }, |
| 223 | }; |
| 224 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 225 | void test_nri_validate(void) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 226 | { |
| 227 | struct nri_validate_tc *t; |
| 228 | printf("\n%s()\n", __func__); |
| 229 | for (t = nri_validate_tests; (t - nri_validate_tests) < ARRAY_SIZE(nri_validate_tests); t++) { |
| 230 | int rc = osmo_nri_v_validate(t->nri, t->nri_bitlen); |
| 231 | printf("osmo_nri_v_validate(%d, %u) = %d ", t->nri, t->nri_bitlen, rc); |
| 232 | if (rc == t->expect_rc) { |
| 233 | printf("ok\n"); |
| 234 | } else { |
| 235 | printf("ERROR, expected rc = %d\n", t->expect_rc); |
| 236 | ok = false; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | struct nri_range_validate_tc { |
| 242 | struct osmo_nri_range range; |
| 243 | uint8_t nri_bitlen; |
| 244 | int expect_rc; |
| 245 | }; |
| 246 | |
| 247 | struct nri_range_validate_tc nri_range_validate_tests[] = { |
| 248 | { .range = { .first = INT16_MIN, .last = INT16_MIN }, .nri_bitlen = 10, .expect_rc = -1 }, |
| 249 | { .range = { .first = -23, .last = -23 }, .nri_bitlen = 10, .expect_rc = -1 }, |
| 250 | { .range = { .first = -1, .last = -1 }, .nri_bitlen = 10, .expect_rc = -1 }, |
| 251 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 252 | { .range = { .first = (1 << 10) - 1, .last = (1 << 10) - 1 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 253 | { .range = { .first = (1 << 10), .last = (1 << 10) }, .nri_bitlen = 10, .expect_rc = 1 }, |
| 254 | { .range = { .first = INT16_MAX, .last = INT16_MAX }, .nri_bitlen = 10, .expect_rc = 1 }, |
| 255 | |
| 256 | { .range = { .first = INT16_MIN, .last = INT16_MIN }, .nri_bitlen = 5, .expect_rc = -1 }, |
| 257 | { .range = { .first = -23, .last = -23 }, .nri_bitlen = 5, .expect_rc = -1 }, |
| 258 | { .range = { .first = -1, .last = -1 }, .nri_bitlen = 5, .expect_rc = -1 }, |
| 259 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 260 | { .range = { .first = (1 << 5) - 1, .last = (1 << 5) - 1 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 261 | { .range = { .first = (1 << 5), .last = (1 << 5) }, .nri_bitlen = 5, .expect_rc = 1 }, |
| 262 | { .range = { .first = INT16_MAX, .last = INT16_MAX }, .nri_bitlen = 5, .expect_rc = 1 }, |
| 263 | |
| 264 | { .range = { .first = INT16_MIN, .last = INT16_MIN }, .nri_bitlen = 1, .expect_rc = -1 }, |
| 265 | { .range = { .first = -23, .last = -23 }, .nri_bitlen = 1, .expect_rc = -1 }, |
| 266 | { .range = { .first = -1, .last = -1 }, .nri_bitlen = 1, .expect_rc = -1 }, |
| 267 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 268 | { .range = { .first = 1, .last = 1 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 269 | { .range = { .first = 2, .last = 2 }, .nri_bitlen = 1, .expect_rc = 1 }, |
| 270 | { .range = { .first = INT16_MAX, .last = INT16_MAX }, .nri_bitlen = 1, .expect_rc = 1 }, |
| 271 | |
| 272 | { .range = { .first = INT16_MIN, .last = INT16_MIN }, .nri_bitlen = 0, .expect_rc = -1 }, |
| 273 | { .range = { .first = -23, .last = -23 }, .nri_bitlen = 0, .expect_rc = -1 }, |
| 274 | { .range = { .first = -1, .last = -1 }, .nri_bitlen = 0, .expect_rc = -1 }, |
| 275 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 276 | { .range = { .first = 1, .last = 1 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 277 | { .range = { .first = INT16_MAX, .last = INT16_MAX }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 278 | |
| 279 | |
| 280 | { .range = { .first = 0, .last = INT16_MIN }, .nri_bitlen = 10, .expect_rc = -2 }, |
| 281 | { .range = { .first = 0, .last = -23 }, .nri_bitlen = 10, .expect_rc = -2 }, |
| 282 | { .range = { .first = 0, .last = -1 }, .nri_bitlen = 10, .expect_rc = -2 }, |
| 283 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 284 | { .range = { .first = 0, .last = (1 << 10) - 1 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 285 | { .range = { .first = 0, .last = (1 << 10) }, .nri_bitlen = 10, .expect_rc = 2 }, |
| 286 | { .range = { .first = 0, .last = INT16_MAX }, .nri_bitlen = 10, .expect_rc = 2 }, |
| 287 | |
| 288 | { .range = { .first = 0, .last = INT16_MIN }, .nri_bitlen = 5, .expect_rc = -2 }, |
| 289 | { .range = { .first = 0, .last = -23 }, .nri_bitlen = 5, .expect_rc = -2 }, |
| 290 | { .range = { .first = 0, .last = -1 }, .nri_bitlen = 5, .expect_rc = -2 }, |
| 291 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 292 | { .range = { .first = 0, .last = (1 << 5) - 1 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 293 | { .range = { .first = 0, .last = (1 << 5) }, .nri_bitlen = 5, .expect_rc = 2 }, |
| 294 | { .range = { .first = 0, .last = INT16_MAX }, .nri_bitlen = 5, .expect_rc = 2 }, |
| 295 | |
| 296 | { .range = { .first = 0, .last = INT16_MIN }, .nri_bitlen = 1, .expect_rc = -2 }, |
| 297 | { .range = { .first = 0, .last = -23 }, .nri_bitlen = 1, .expect_rc = -2 }, |
| 298 | { .range = { .first = 0, .last = -1 }, .nri_bitlen = 1, .expect_rc = -2 }, |
| 299 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 300 | { .range = { .first = 0, .last = 1 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 301 | { .range = { .first = 0, .last = 2 }, .nri_bitlen = 1, .expect_rc = 2 }, |
| 302 | { .range = { .first = 0, .last = INT16_MAX }, .nri_bitlen = 1, .expect_rc = 2 }, |
| 303 | |
| 304 | { .range = { .first = 0, .last = INT16_MIN }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 305 | { .range = { .first = 0, .last = -23 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 306 | { .range = { .first = 0, .last = -1 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 307 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 308 | { .range = { .first = 0, .last = 1 }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 309 | { .range = { .first = 0, .last = INT16_MAX }, .nri_bitlen = 0, .expect_rc = 1 }, |
| 310 | |
| 311 | |
| 312 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 313 | { .range = { .first = 1, .last = 0 }, .nri_bitlen = 10, .expect_rc = -3 }, |
| 314 | { .range = { .first = (1 << 10) - 1, .last = (1 << 10) - 1 }, .nri_bitlen = 10, .expect_rc = 0 }, |
| 315 | { .range = { .first = (1 << 10) - 1, .last = (1 << 10) - 2 }, .nri_bitlen = 10, .expect_rc = -3 }, |
| 316 | { .range = { .first = (1 << 10) - 1, .last = 0 }, .nri_bitlen = 10, .expect_rc = -3 }, |
| 317 | |
| 318 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 319 | { .range = { .first = 1, .last = 0 }, .nri_bitlen = 5, .expect_rc = -3 }, |
| 320 | { .range = { .first = (1 << 5) - 1, .last = (1 << 5) - 1 }, .nri_bitlen = 5, .expect_rc = 0 }, |
| 321 | { .range = { .first = (1 << 5) - 1, .last = (1 << 5) - 2 }, .nri_bitlen = 5, .expect_rc = -3 }, |
| 322 | { .range = { .first = (1 << 5) - 1, .last = 0 }, .nri_bitlen = 5, .expect_rc = -3 }, |
| 323 | |
| 324 | { .range = { .first = 0, .last = 0 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 325 | { .range = { .first = 1, .last = 1 }, .nri_bitlen = 1, .expect_rc = 0 }, |
| 326 | { .range = { .first = 1, .last = 0 }, .nri_bitlen = 1, .expect_rc = -3 }, |
| 327 | |
| 328 | }; |
| 329 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 330 | void test_nri_range_validate(void) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 331 | { |
| 332 | struct nri_range_validate_tc *t; |
| 333 | printf("\n%s()\n", __func__); |
| 334 | for (t = nri_range_validate_tests; (t - nri_range_validate_tests) < ARRAY_SIZE(nri_range_validate_tests); t++) { |
| 335 | int rc = osmo_nri_range_validate(&t->range, t->nri_bitlen); |
| 336 | printf("osmo_nri_range_validate({%d,%d}, %u) = %d ", t->range.first, t->range.last, t->nri_bitlen, rc); |
| 337 | if (rc == t->expect_rc) { |
| 338 | printf("ok\n"); |
| 339 | } else { |
| 340 | printf("ERROR, expected rc = %d\n", t->expect_rc); |
| 341 | ok = false; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void dump_list(const struct osmo_nri_ranges *nri_ranges) |
| 347 | { |
| 348 | struct osmo_nri_range *r; |
| 349 | printf("nri_ranges = {\n"); |
| 350 | llist_for_each_entry(r, &nri_ranges->entries, entry) { |
| 351 | printf(" { %d, %d },\n", r->first, r->last); |
| 352 | if (osmo_nri_range_validate(r, 255)) { |
| 353 | ok = false; |
| 354 | printf(" ^^^^^ ERROR: invalid range\n"); |
| 355 | } |
| 356 | } |
| 357 | printf("};\n"); |
| 358 | } |
| 359 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 360 | void test_nri_list(void) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 361 | { |
| 362 | struct osmo_nri_ranges *nri_ranges = osmo_nri_ranges_alloc(ctx); |
| 363 | printf("\n%s()\n", __func__); |
| 364 | |
| 365 | #define ADD(FIRST, LAST) do { \ |
| 366 | struct osmo_nri_range r = { .first = FIRST, .last = LAST }; \ |
| 367 | int rc; \ |
| 368 | rc = osmo_nri_ranges_add(nri_ranges, &r); \ |
| 369 | printf("osmo_nri_ranges_add(%d, %d) -> %d\n", r.first, r.last, rc); \ |
| 370 | dump_list(nri_ranges); \ |
| 371 | } while(0) |
| 372 | |
| 373 | #define DEL(FIRST, LAST) do { \ |
| 374 | struct osmo_nri_range r = { .first = FIRST, .last = LAST }; \ |
| 375 | int rc; \ |
| 376 | rc = osmo_nri_ranges_del(nri_ranges, &r); \ |
| 377 | printf("osmo_nri_ranges_del(%d, %d) -> %d\n", r.first, r.last, rc); \ |
| 378 | dump_list(nri_ranges); \ |
| 379 | } while(0) |
| 380 | |
| 381 | #define MATCHES(NRI, EXPECT_MATCH) do { \ |
| 382 | bool matches = osmo_nri_v_matches_ranges(NRI, nri_ranges); \ |
| 383 | printf("osmo_nri_v_matches_ranges(%d) -> %s\n", NRI, matches ? "true" : "false"); \ |
| 384 | if (matches != EXPECT_MATCH) { \ |
| 385 | ok = false; \ |
| 386 | printf(" ^ ERROR: expected " #EXPECT_MATCH "\n"); \ |
| 387 | } \ |
| 388 | } while(0) |
| 389 | |
| 390 | #define OVERLAPS(FIRST, LAST, EXPECT_OVERLAP) do { \ |
| 391 | struct osmo_nri_range r = { .first = FIRST, .last = LAST }; \ |
| 392 | bool overlaps = osmo_nri_range_overlaps_ranges(&r, nri_ranges); \ |
| 393 | printf("osmo_nri_range_overlaps_ranges(%d, %d) -> %s\n", r.first, r.last, overlaps ? "true" : "false"); \ |
| 394 | if (overlaps != EXPECT_OVERLAP) { \ |
| 395 | ok = false; \ |
| 396 | printf(" ^ ERROR: expected " #EXPECT_OVERLAP "\n"); \ |
| 397 | } \ |
| 398 | } while(0) |
| 399 | |
| 400 | dump_list(nri_ranges); |
| 401 | MATCHES(INT16_MIN, false); |
| 402 | MATCHES(-1, false); |
| 403 | MATCHES(0, false); |
| 404 | MATCHES(INT16_MAX, false); |
| 405 | MATCHES(100, false); |
| 406 | OVERLAPS(INT16_MIN, -1, false); |
| 407 | OVERLAPS(-100, 100, false); |
| 408 | OVERLAPS(10, 20, false); |
| 409 | |
| 410 | ADD(100, 200); |
| 411 | MATCHES(INT16_MIN, false); |
| 412 | MATCHES(-1, false); |
| 413 | MATCHES(0, false); |
| 414 | MATCHES(INT16_MAX, false); |
| 415 | MATCHES(99, false); |
| 416 | MATCHES(100, true); |
| 417 | MATCHES(101, true); |
| 418 | MATCHES(199, true); |
| 419 | MATCHES(200, true); |
| 420 | MATCHES(201, false); |
| 421 | OVERLAPS(INT16_MIN, -1, false); |
| 422 | OVERLAPS(-100, 100, true); |
| 423 | OVERLAPS(10, 20, false); |
| 424 | OVERLAPS(10, 99, false); |
| 425 | OVERLAPS(10, 100, true); |
| 426 | OVERLAPS(10, 150, true); |
| 427 | OVERLAPS(99, 99, false); |
| 428 | OVERLAPS(100, 100, true); |
| 429 | OVERLAPS(150, 300, true); |
| 430 | OVERLAPS(200, 300, true); |
| 431 | OVERLAPS(201, 300, false); |
| 432 | |
| 433 | printf("\ndel from start:\n"); |
| 434 | DEL(0, 110); |
| 435 | DEL(111, 111); |
| 436 | DEL(112, 199); |
| 437 | MATCHES(INT16_MIN, false); |
| 438 | MATCHES(-1, false); |
| 439 | MATCHES(0, false); |
| 440 | MATCHES(INT16_MAX, false); |
| 441 | MATCHES(199, false); |
| 442 | MATCHES(200, true); |
| 443 | MATCHES(201, false); |
| 444 | OVERLAPS(INT16_MIN, -1, false); |
| 445 | OVERLAPS(-1000, 1000, true); |
| 446 | OVERLAPS(0, 199, false); |
| 447 | OVERLAPS(0, 200, true); |
| 448 | OVERLAPS(0, 201, true); |
| 449 | OVERLAPS(0, 1000, true); |
| 450 | OVERLAPS(199, 199, false); |
| 451 | OVERLAPS(200, 200, true); |
| 452 | OVERLAPS(201, 201, false); |
| 453 | |
| 454 | printf("\ndel from end:\n"); |
| 455 | ADD(100, 200); |
| 456 | DEL(190, INT16_MAX); |
| 457 | DEL(189, 189); |
| 458 | DEL(101, 188); |
| 459 | MATCHES(INT16_MIN, false); |
| 460 | MATCHES(-1, false); |
| 461 | MATCHES(0, false); |
| 462 | MATCHES(INT16_MAX, false); |
| 463 | MATCHES(99, false); |
| 464 | MATCHES(100, true); |
| 465 | MATCHES(101, false); |
| 466 | |
| 467 | printf("\ndel from middle:\n"); |
| 468 | ADD(100, 200); |
| 469 | DEL(150, 160); |
| 470 | DEL(110, 120); |
| 471 | DEL(130, 130); |
| 472 | DEL(180, 190); |
| 473 | MATCHES(INT16_MIN, false); |
| 474 | MATCHES(-1, false); |
| 475 | MATCHES(0, false); |
| 476 | MATCHES(INT16_MAX, false); |
| 477 | MATCHES(99, false); |
| 478 | MATCHES(100, true); |
| 479 | MATCHES(109, true); |
| 480 | MATCHES(110, false); |
| 481 | MATCHES(120, false); |
| 482 | MATCHES(121, true); |
| 483 | MATCHES(129, true); |
| 484 | MATCHES(130, false); |
| 485 | MATCHES(131, true); |
| 486 | MATCHES(148, true); |
| 487 | MATCHES(149, true); |
| 488 | MATCHES(150, false); |
| 489 | MATCHES(160, false); |
| 490 | MATCHES(161, true); |
| 491 | MATCHES(170, true); |
| 492 | MATCHES(179, true); |
| 493 | MATCHES(180, false); |
| 494 | MATCHES(185, false); |
| 495 | MATCHES(190, false); |
| 496 | MATCHES(191, true); |
| 497 | MATCHES(195, true); |
| 498 | MATCHES(200, true); |
| 499 | MATCHES(201, false); |
| 500 | MATCHES(1000, false); |
| 501 | OVERLAPS(110, 120, false); |
| 502 | OVERLAPS(110, 130, true); |
| 503 | OVERLAPS(100, 200, true); |
| 504 | |
| 505 | printf("\ndel across whole chunks:\n"); |
| 506 | DEL(115, 185); |
| 507 | DEL(105, 195); |
| 508 | DEL(0, 1000); |
| 509 | |
| 510 | printf("\nadd to join chunks:\n"); |
| 511 | ADD(0, 100); |
| 512 | DEL(11, 19); |
| 513 | DEL(23, 23); |
| 514 | DEL(30, 41); |
| 515 | ADD(23, 23); |
| 516 | ADD(11, 41); |
| 517 | MATCHES(0, true); |
| 518 | MATCHES(10, true); |
| 519 | MATCHES(11, true); |
| 520 | MATCHES(24, true); |
| 521 | MATCHES(41, true); |
| 522 | MATCHES(42, true); |
| 523 | MATCHES(100, true); |
| 524 | MATCHES(101, false); |
| 525 | |
| 526 | printf("\nborder cases:\n"); |
| 527 | ADD(0, 0); |
| 528 | ADD(INT16_MAX, INT16_MAX); |
| 529 | ADD(1, INT16_MAX - 1); |
| 530 | MATCHES(INT16_MIN, false); |
| 531 | MATCHES(-1, false); |
| 532 | MATCHES(0, true); |
| 533 | MATCHES(INT16_MAX, true); |
| 534 | DEL(0, 0); |
| 535 | DEL(INT16_MAX, INT16_MAX); |
| 536 | DEL(1, INT16_MAX - 1); |
| 537 | |
| 538 | printf("\nrange errors:\n"); |
| 539 | ADD(-1, -1); |
| 540 | ADD(-20, -10); |
| 541 | ADD(100, 1); |
| 542 | ADD(0, INT16_MAX); |
| 543 | DEL(-1, -1); |
| 544 | DEL(-20, -10); |
| 545 | DEL(100, 1); |
| 546 | } |
| 547 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 548 | void test_nri_limit_by_ranges(void) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 549 | { |
| 550 | const uint8_t nri_bitlen = 8; |
| 551 | const int16_t expect_nri_vals[] = { 10, 20, 21, 30, 31, 32 }; |
| 552 | int i; |
| 553 | struct osmo_nri_ranges *nri_ranges = osmo_nri_ranges_alloc(ctx); |
| 554 | printf("\n%s()\n", __func__); |
| 555 | |
| 556 | ADD(10, 10); |
| 557 | ADD(20, 21); |
| 558 | ADD(30, 32); |
| 559 | |
| 560 | for (i = 0; i < 19; i++) { |
| 561 | int rc; |
| 562 | int16_t nri_v; |
| 563 | int16_t expect_nri_v = expect_nri_vals[i % ARRAY_SIZE(expect_nri_vals)]; |
| 564 | |
| 565 | nri_v = i; |
| 566 | rc = osmo_nri_v_limit_by_ranges(&nri_v, nri_ranges, nri_bitlen); |
| 567 | printf("osmo_nri_v_limit_by_ranges(%d) -> nri_v=%d rc=%d", i, nri_v, rc); |
| 568 | if (!rc && nri_v == expect_nri_v) { |
| 569 | printf(" ok\n"); |
| 570 | } else { |
| 571 | printf(" ERROR: expected nri_v=%d rc=0\n", expect_nri_v); |
| 572 | ok = false; |
| 573 | } |
| 574 | } |
| 575 | for (i = 0; i < 19; i++) { |
| 576 | int rc; |
| 577 | int16_t nri_v; |
| 578 | uint32_t tmsi, tmsi2; |
| 579 | int16_t expect_nri_v = expect_nri_vals[i % ARRAY_SIZE(expect_nri_vals)]; |
| 580 | |
| 581 | tmsi = 0; |
| 582 | osmo_tmsi_nri_v_set(&tmsi, i, nri_bitlen); |
| 583 | tmsi2 = tmsi; |
| 584 | rc = osmo_tmsi_nri_v_limit_by_ranges(&tmsi2, nri_ranges, nri_bitlen); |
| 585 | osmo_tmsi_nri_v_get(&nri_v, tmsi2, nri_bitlen); |
| 586 | printf("osmo_tmsi_nri_v_limit_by_ranges(0x%08x, %u) -> tmsi=0x%08x nri_v=%d rc=%d", |
| 587 | tmsi, nri_bitlen, tmsi2, nri_v, rc); |
| 588 | if (!rc && nri_v == expect_nri_v) { |
| 589 | printf(" ok\n"); |
| 590 | } else { |
| 591 | printf(" ERROR: expected nri_v=%d rc=0\n", expect_nri_v); |
| 592 | ok = false; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 597 | int main(int argc, char **argv) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 598 | { |
| 599 | ctx = talloc_named_const(NULL, 0, "nri_test"); |
| 600 | |
| 601 | test_nri_v_get_set(); |
| 602 | test_nri_validate(); |
| 603 | test_nri_range_validate(); |
| 604 | test_nri_list(); |
| 605 | test_nri_limit_by_ranges(); |
| 606 | |
| 607 | talloc_free(ctx); |
| 608 | if (!ok) { |
| 609 | printf("\nFAIL\n"); |
| 610 | return -1; |
| 611 | } |
| 612 | |
| 613 | printf("\npass\n"); |
| 614 | return 0; |
| 615 | } |
| 616 | |