This commit adds test range encoding tests. They check the property
decoding(encoding(L)) = L and optionally dump the results and
encoded sequences to stdout.

There a 2 test modes:

 - A list of fixed tests
 - A random number based test loop per ARFCN list size (only dumps
   the first failing test)

Sponsored-by: On-Waves ehf
---
 openbsc/tests/gsm0408/Makefile.am     |    1 +
 openbsc/tests/gsm0408/gsm0408_test.c  |  216 +++++++++++++++++++++++++++++++++
 openbsc/tests/gsm0408/gsm0408_test.ok |   48 ++++++++
 3 files changed, 265 insertions(+)

diff --git a/openbsc/tests/gsm0408/Makefile.am 
b/openbsc/tests/gsm0408/Makefile.am
index b000c08..1c29ece 100644
--- a/openbsc/tests/gsm0408/Makefile.am
+++ b/openbsc/tests/gsm0408/Makefile.am
@@ -8,4 +8,5 @@ gsm0408_test_SOURCES = gsm0408_test.c
 gsm0408_test_LDADD =   $(top_builddir)/src/libbsc/libbsc.a \
                        $(top_builddir)/src/libmsc/libmsc.a \
                        $(top_builddir)/src/libbsc/libbsc.a \
+                       $(top_builddir)/src/libcommon/libcommon.a \
                        $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) -ldbi
diff --git a/openbsc/tests/gsm0408/gsm0408_test.c 
b/openbsc/tests/gsm0408/gsm0408_test.c
index 60a151f..721b283 100644
--- a/openbsc/tests/gsm0408/gsm0408_test.c
+++ b/openbsc/tests/gsm0408/gsm0408_test.c
@@ -27,6 +27,8 @@
 #include <openbsc/gsm_04_08.h>
 #include <openbsc/gsm_subscriber.h>
 #include <openbsc/debug.h>
+#include <openbsc/arfcn_range_encode.h>
+#include <osmocom/core/application.h>
 
 #define COMPARE(result, op, value) \
     if (!((result) op (value))) {\
@@ -94,10 +96,224 @@ static void test_mi_functionality(void)
        COMPARE_STR(mi_parsed, imsi_even);
 }
 
+struct {
+       int range;
+       int arfcns_num;
+       int arfcns[RANGE_ENC_MAX_ARFCNS];
+} arfcn_test_ranges[] = {
+       {ARFCN_RANGE_512, 12,
+               { 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
+       {ARFCN_RANGE_512, 17,
+               { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
+       {ARFCN_RANGE_512, 18,
+               { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 
}},
+       {ARFCN_RANGE_512, 18,
+               { 1, 17, 31, 45, 58, 79, 81, 97,
+                 113, 127, 213, 277, 287, 311, 331, 391,
+                 417, 511 }},
+       {ARFCN_RANGE_512, 6,
+               { 1, 17, 31, 45, 58, 79 }},
+       {ARFCN_RANGE_512, 6,
+               { 10, 17, 31, 45, 58, 79 }},
+       {ARFCN_RANGE_1024, 17,
+               { 0, 17, 31, 45, 58, 79, 81, 97,
+                 113, 127, 213, 277, 287, 311, 331, 391,
+                 1023 }},
+       {ARFCN_RANGE_1024, 16,
+               { 17, 31, 45, 58, 79, 81, 97, 113,
+                 127, 213, 277, 287, 311, 331, 391, 1023 }},
+       {-1}
+};
+
+static int test_single_range_encoding(int range, const int *orig_arfcns,
+                                      int arfcns_num, int silent)
+{
+       int arfcns[RANGE_ENC_MAX_ARFCNS];
+       int w[RANGE_ENC_MAX_ARFCNS];
+       int f0_included = 0;
+       int rc, f0;
+       uint8_t chan_list[16] = {0};
+       struct gsm_sysinfo_freq dec_freq[1024] = {{0}};
+       int dec_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
+       int dec_arfcns_count = 0;
+       int arfcns_used = 0;
+       int i;
+
+       arfcns_used = arfcns_num;
+       memmove(arfcns, orig_arfcns, sizeof(arfcns));
+
+       f0 = arfcns[0];
+       /*
+        * Manipulate the ARFCN list according to the rules in J4 depending
+        * on the selected range.
+        */
+       arfcns_used = range_enc_filter_arfcns(range, arfcns, arfcns_used,
+                                             f0, &f0_included);
+
+       memset(w, 0, sizeof(w));
+       rc = range_enc_arfcns(range, arfcns, arfcns_used, w, 0);
+       if (rc != 0) {
+               printf("Cannot compute range W(k), rc = %d\n", rc);
+               return 1;
+       }
+
+       if (!silent)
+               fprintf(stderr, "range=%d, arfcns_used=%d, f0=%d, 
f0_included=%d\n",
+                       range, arfcns_used, f0, f0_included);
+
+       /* Select the range and the amount of bits needed */
+       switch (range) {
+       case ARFCN_RANGE_128:
+               rc = range_enc_range128(chan_list, f0, w);
+               break;
+       case ARFCN_RANGE_256:
+               rc = range_enc_range256(chan_list, f0, w);
+               break;
+       case ARFCN_RANGE_512:
+               rc = range_enc_range512(chan_list, f0, w);
+               break;
+       case ARFCN_RANGE_1024:
+               rc = range_enc_range1024(chan_list, f0, f0_included, w);
+               break;
+       default:
+               return 1;
+       };
+       if (rc != 0) {
+               printf("Cannot encode range, rc = %d\n", rc);
+               return 1;
+       }
+
+       if (!silent)
+               printf("chan_list = %s\n",
+                      osmo_hexdump(chan_list, sizeof(chan_list)));
+
+       rc = gsm48_decode_freq_list(dec_freq, chan_list, sizeof(chan_list),
+                                   0xfe, 1);
+       if (rc != 0) {
+               printf("Cannot decode freq list, rc = %d\n", rc);
+               return 1;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(dec_freq); i++) {
+               if (dec_freq[i].mask &&
+                   dec_arfcns_count < ARRAY_SIZE(dec_arfcns))
+                       dec_arfcns[dec_arfcns_count++] = i;
+       }
+
+       if (!silent) {
+               printf("Decoded freqs %d (expected %d)\n",
+                      dec_arfcns_count, arfcns_num);
+               printf("Decoded: ");
+               for (i = 0; i < dec_arfcns_count; i++) {
+                       printf("%d ", dec_arfcns[i]);
+                       if (dec_arfcns[i] != orig_arfcns[i])
+                               printf("(!= %d) ", orig_arfcns[i]);
+               }
+               printf("\n");
+       }
+
+       if (dec_arfcns_count != arfcns_num) {
+               printf("Wrong number of arfcns\n");
+               return 1;
+       }
+
+       if (memcmp(dec_arfcns, orig_arfcns, sizeof(dec_arfcns)) != 0) {
+               printf("Decoding error, got wrong freqs\n");
+               fprintf(stderr, " w = ");
+               for (i = 0; i < ARRAY_SIZE(w); i++)
+                       fprintf(stderr, "%d ", w[i]);
+               fprintf(stderr, "\n");
+               return 1;
+       }
+
+       return 0;
+}
+
+static void test_random_range_encoding(int range, int max_arfcn_num)
+{
+       int arfcns_num = 0;
+       int test_idx;
+       int rc, max_count;
+       int num_tests = 1024;
+
+       printf("Random range test: range %d, max num ARFCNs %d\n",
+              range, max_arfcn_num);
+
+       srandom(1);
+
+       for (max_count = 1; max_count < max_arfcn_num; max_count++) {
+               for (test_idx = 0; test_idx < num_tests; test_idx++) {
+                       int count;
+                       int i;
+                       int min_freq = 0;
+
+                       int rnd_arfcns[RANGE_ENC_MAX_ARFCNS] = {0};
+                       char rnd_arfcns_set[1024] = {0};
+
+                       if (range < ARFCN_RANGE_1024)
+                               min_freq = random() % (1023 - range);
+
+                       for (count = max_count; count; ) {
+                               int arfcn = min_freq + random() % (range + 1);
+                               OSMO_ASSERT(arfcn < ARRAY_SIZE(rnd_arfcns_set));
+
+                               if (!rnd_arfcns_set[arfcn]) {
+                                       rnd_arfcns_set[arfcn] = 1;
+                                       count -= 1;
+                               }
+                       }
+
+                       arfcns_num = 0;
+                       for (i = 0; i < ARRAY_SIZE(rnd_arfcns_set); i++)
+                               if (rnd_arfcns_set[i])
+                                       rnd_arfcns[arfcns_num++] = i;
+
+                       rc = test_single_range_encoding(range, rnd_arfcns,
+                                                       arfcns_num, 1);
+                       if (rc != 0) {
+                               printf("Failed on test %d, range %d, num ARFCNs 
%d\n",
+                                      test_idx, range, max_count);
+                               test_single_range_encoding(range, rnd_arfcns,
+                                                          arfcns_num, 0);
+                               return;
+                       }
+               }
+       }
+}
+
+static void test_range_encoding()
+{
+       int *arfcns;
+       int arfcns_num = 0;
+       int test_idx;
+       int range;
+
+       for (test_idx = 0; arfcn_test_ranges[test_idx].arfcns_num > 0; 
test_idx++)
+       {
+               arfcns_num = arfcn_test_ranges[test_idx].arfcns_num;
+               arfcns = &arfcn_test_ranges[test_idx].arfcns[0];
+               range = arfcn_test_ranges[test_idx].range;
+
+               printf("Range test %d: range %d, num ARFCNs %d\n",
+                      test_idx, range, arfcns_num);
+
+               test_single_range_encoding(range, arfcns, arfcns_num, 0);
+       }
+
+       test_random_range_encoding(ARFCN_RANGE_128, 29);
+       test_random_range_encoding(ARFCN_RANGE_256, 22);
+       test_random_range_encoding(ARFCN_RANGE_512, 18);
+       test_random_range_encoding(ARFCN_RANGE_1024, 16);
+}
+
 int main(int argc, char **argv)
 {
+       osmo_init_logging(&log_info);
+       log_set_log_level(osmo_stderr_target, LOGL_INFO);
+
        test_location_area_identifier();
        test_mi_functionality();
+       test_range_encoding();
 
        printf("Done.\n");
        return EXIT_SUCCESS;
diff --git a/openbsc/tests/gsm0408/gsm0408_test.ok 
b/openbsc/tests/gsm0408/gsm0408_test.ok
index 52c601e..a92b879 100644
--- a/openbsc/tests/gsm0408/gsm0408_test.ok
+++ b/openbsc/tests/gsm0408/gsm0408_test.ok
@@ -2,4 +2,52 @@ Testing test location area identifier
 Testing parsing and generating TMSI/IMSI
 hex: 17 08 99 78 56 34 12 90 78 36 
 hex: 17 09 91 78 56 34 12 90 78 56 f4 
+Range test 0: range 511, num ARFCNs 12
+chan_list = 88 00 98 34 85 36 7c 50 22 dc 5e ec 00 00 00 00 
+Decoded freqs 12 (expected 12)
+Decoded: 1 12 31 51 57 91 97 98 113 117 120 125 
+Range test 1: range 511, num ARFCNs 17
+chan_list = 88 00 82 7f 01 3f 7e 04 0b ff ff fc 10 41 07 e0 
+Decoded freqs 17 (expected 17)
+Decoded: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
+Range test 2: range 511, num ARFCNs 18
+chan_list = 88 00 82 7f 01 7f 7e 04 0b ff ff fc 10 41 07 ff 
+Decoded freqs 18 (expected 18)
+Decoded: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
+Range test 3: range 511, num ARFCNs 18
+chan_list = 88 00 94 3a 44 32 d7 2a 43 2a 13 94 e5 38 39 76 
+Decoded freqs 18 (expected 18)
+Decoded: 1 17 31 45 58 79 81 97 113 127 213 277 287 311 331 391 417 507 (!= 
511) 
+Decoding error, got wrong freqs
+Range test 4: range 511, num ARFCNs 6
+chan_list = 88 00 8b 3c 88 b9 6b 00 00 00 00 00 00 00 00 00 
+Decoded freqs 6 (expected 6)
+Decoded: 1 17 31 45 58 79 
+Range test 5: range 511, num ARFCNs 6
+chan_list = 88 05 08 fc 88 b9 6b 00 00 00 00 00 00 00 00 00 
+Decoded freqs 6 (expected 6)
+Decoded: 10 17 31 45 58 79 
+Range test 6: range 1023, num ARFCNs 17
+Cannot encode range, rc = -1
+Range test 7: range 1023, num ARFCNs 16
+Cannot encode range, rc = -1
+Random range test: range 127, max num ARFCNs 29
+Cannot encode range, rc = -1
+Failed on test 0, range 127, num ARFCNs 1
+Cannot encode range, rc = -1
+Random range test: range 255, max num ARFCNs 22
+Cannot encode range, rc = -1
+Failed on test 0, range 255, num ARFCNs 1
+Cannot encode range, rc = -1
+Random range test: range 511, max num ARFCNs 18
+Decoding error, got wrong freqs
+Failed on test 0, range 511, num ARFCNs 17
+chan_list = 88 21 55 fc da d7 76 03 31 2f ed 45 dc 93 d6 80 
+Decoded freqs 17 (expected 17)
+Decoded: 66 81 161 250 314 343 383 (!= 380) 395 396 397 409 429 505 506 516 
518 545 
+Decoding error, got wrong freqs
+Random range test: range 1023, max num ARFCNs 16
+Cannot encode range, rc = -1
+Failed on test 0, range 1023, num ARFCNs 1
+Cannot encode range, rc = -1
 Done.
-- 
1.7.9.5


Reply via email to