Review at  https://gerrit.osmocom.org/4336

Add tests for bitvec_write_field()

This function is actively used by OsmoPCU but have not been covered by
tests so far. The test code is based on
Minh-Quang Nguyen <minh-quang.ngu...@nutaq.com> submission with some
modifications.

Change-Id: I2ee544256b8675bc62a42493aab66a8eeee54f90
Related: OS#1526
---
M tests/Makefile.am
A tests/bits/bitfield_test.c
A tests/bits/bitfield_test.ok
M tests/testsuite.at
4 files changed, 242 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/36/4336/1

diff --git a/tests/Makefile.am b/tests/Makefile.am
index dbe349f..b5049f2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -11,6 +11,7 @@
                 loggingrb/loggingrb_test strrb/strrb_test              \
                 comp128/comp128_test smscb/gsm0341_test                \
                 bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test    \
+                bits/bitfield_test     \
                 tlv/tlv_test gsup/gsup_test oap/oap_test fsm/fsm_test  \
                 write_queue/wqueue_test socket/socket_test             \
                 coding/coding_test conv/conv_gsm0503_test              \
@@ -76,6 +77,9 @@
 
 bits_bitcomp_test_SOURCES = bits/bitcomp_test.c
 bits_bitcomp_test_LDADD = $(top_builddir)/src/libosmocore.la
+
+bits_bitfield_test_SOURCES = bits/bitfield_test.c
+bits_bitfield_test_LDADD = $(top_builddir)/src/libosmocore.la
 
 conv_conv_test_SOURCES = conv/conv_test.c conv/conv.c
 conv_conv_test_LDADD = $(top_builddir)/src/libosmocore.la 
$(top_builddir)/src/gsm/libgsmint.la
@@ -232,7 +236,7 @@
             vty/ok_more_spaces.cfg \
             vty/ok_tabs_and_spaces.cfg \
             vty/ok_tabs.cfg \
-            comp128/comp128_test.ok                                    \
+            comp128/comp128_test.ok bits/bitfield_test.ok              \
             utils/utils_test.ok stats/stats_test.ok                    \
             bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \
             sim/sim_test.ok tlv/tlv_test.ok abis/abis_test.ok          \
diff --git a/tests/bits/bitfield_test.c b/tests/bits/bitfield_test.c
new file mode 100644
index 0000000..0a86e1f
--- /dev/null
+++ b/tests/bits/bitfield_test.c
@@ -0,0 +1,188 @@
+#include <inttypes.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <stdint.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/bits.h>
+#include <osmocom/core/bitvec.h>
+
+#define INTRO(p) do { printf("=== start %s(%u) ===\n", __func__, p); } while(0)
+#define OUTRO(p) do { printf("=== end %s(%u) ===\n", __func__, p); } while(0)
+
+static void test_bitvec_ia_octet_encode_pkt_dl_ass(struct bitvec *dest, 
uint32_t ttli,
+                                                  uint8_t tfi, uint8_t gamma, 
uint8_t ta_valid, uint8_t ws_enc,
+                                                  bool use_lh)
+{
+       unsigned wp = 0;
+
+       INTRO(use_lh);
+
+       /* GSM 04.08 10.5.2.16 IA Rest Octets */
+       if (use_lh) /* FIXME: add function to encode LH properly */
+               bitvec_write_field(dest, &wp, 3, 2);            /* "HH" */
+       else
+               bitvec_write_field(dest, &wp, 3, 2);            /* "HH" */
+       bitvec_write_field(dest, &wp, 1, 2);                    /* "01" Packet 
Downlink Assignment */
+       bitvec_write_field(dest, &wp, ttli, 32);                /* TLLI */
+       bitvec_write_field(dest, &wp, 1, 1);                    /* switch TFI: 
on */
+       bitvec_write_field(dest, &wp, tfi, 5);                  /* TFI */
+       bitvec_write_field(dest, &wp, 0x0, 1);                  /* RLC 
acknowledged mode */
+       bitvec_write_field(dest, &wp, 0x0, 1);                  /* ALPHA = not 
present */
+       bitvec_write_field(dest, &wp, gamma, 5);                /* GAMMA power 
control parameter */
+       bitvec_write_field(dest, &wp, 0,1);                     /* Polling Bit: 
off */
+       bitvec_write_field(dest, &wp, ta_valid, 1);             /* N. B: NOT 
related to TAI! */
+       bitvec_write_field(dest, &wp, 0, 1);                    /* No 
TIMING_ADVANCE_INDEX: */
+       bitvec_write_field(dest, &wp, 0, 1);                    /* TBF Starting 
TIME present */
+       bitvec_write_field(dest, &wp, 0, 1);                    /* P0 not 
present */
+       if (use_lh) { /* FIXME: add function to encode LH properly */
+               bitvec_write_field(dest, &wp, 1, 1);            /* "H" - 
additional for R99 */
+       } else
+               bitvec_write_field(dest, &wp, 1, 1);            /* "H" - 
additional for R99 */
+       bitvec_write_field(dest, &wp, ws_enc, 5);               /* EGPRS Window 
Size */
+       bitvec_write_field(dest, &wp, 0, 2);                    /* 
LINK_QUALITY_MEASUREMENT_MODE */
+       bitvec_write_field(dest, &wp, 0, 1);                    /* BEP_PERIOD2 
not present */
+
+       printf("Encoded PKT DL ASS IA Rest Octets: %s\n", 
osmo_hexdump(dest->data, dest->data_len));
+
+       OUTRO(use_lh);
+}
+
+static void test_bitvec_ia_octet_encode_pkt_ul_ass(struct bitvec *dest, 
uint32_t fn,
+                                                  uint8_t tfi, uint8_t gamma, 
uint8_t usf, bool tbf, bool use_lh)
+{
+       unsigned wp = 0;
+
+       INTRO(use_lh);
+
+       /* GMS 04.08 10.5.2.37b 10.5.2.16 */
+       if (use_lh) /* FIXME: add function to encode LH properly */
+               bitvec_write_field(dest, &wp, 3, 2);                            
/* "HH" */
+       else
+               bitvec_write_field(dest, &wp, 3, 2);                            
/* "HH" */
+       bitvec_write_field(dest, &wp, 0, 2);                                    
/* "0" Packet Uplink Assignment */
+       if (!tbf) {
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* Block Allocation: SBA */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* ALPHA = not present */
+               bitvec_write_field(dest, &wp, gamma, 5);                        
/* GAMMA power control parameter */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* No TIMING_ADVANCE_INDEX: */
+               bitvec_write_field(dest, &wp, 1, 1);                            
/* TBF_STARTING_TIME_FLAG */
+               bitvec_write_field(dest, &wp, (fn / (26 * 51)) % 32, 5);        
/* T1' */
+               bitvec_write_field(dest, &wp, fn % 51, 6);                      
/* T3 */
+               bitvec_write_field(dest, &wp, fn % 26, 5);                      
/* T2 */
+       } else {
+               bitvec_write_field(dest, &wp, 1, 1);                            
/* Block Allocation: Not SBA */
+               bitvec_write_field(dest, &wp, tfi, 5);                          
/* TFI_ASSIGNMENT */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* POLLING = none */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* ALLOCATION_TYPE: dynamic */
+               bitvec_write_field(dest, &wp, usf, 3);                          
/* USF */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* USF_GRANULARITY */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* "0" power control: Not Present */
+               bitvec_write_field(dest, &wp, 0, 2);                            
/* CHANNEL_CODING_COMMAND */
+               bitvec_write_field(dest, &wp, 1, 1);                            
/* TLLI_BLOCK_CHANNEL_CODING */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* ALPHA = not present */
+               bitvec_write_field(dest, &wp, gamma, 5);                        
/* GAMMA power control parameter */
+               /* note: there is no choise for TAI and no starting time */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* switch TIMING_ADVANCE_INDEX = off */
+               bitvec_write_field(dest, &wp, 0, 1);                            
/* TBF_STARTING_TIME_FLAG */
+       }
+
+       printf("Encoded PKT UL ASS IA Rest Octets: %s\n", 
osmo_hexdump(dest->data, dest->data_len));
+
+       OUTRO(use_lh);
+}
+
+static void test_bitdiff(const struct bitvec *src1, const struct bitvec *src2, 
unsigned len)
+{
+       unsigned int bit_err = 0, i, j;
+       uint8_t byte_err = 0;
+
+       INTRO(len);
+
+       for (i = 0; i < len; i++) {
+               /* byte compare */
+               byte_err = src1->data[i] ^ src2->data[i];
+               if (byte_err)
+                       for (j = 0; j < 8; j++)
+                               bit_err += (byte_err >> j) & 0x01; /* count 
error bits */
+       }
+
+
+       printf("=== total %u bits differ ===\n", bit_err);
+
+       OUTRO(len);
+}
+
+int main(int argc, char **argv)
+{
+       void *tall_pcu_ctx;
+       struct bitvec *dest;
+       struct bitvec *dest_lh;
+       uint8_t tfi = 0; /* Temporary Flow Identity */
+       uint32_t ttli = 0xdeadbeef;
+       uint8_t gamma = 0;
+       uint8_t ta_valid = 1;
+       uint8_t ws_enc = 3;
+       uint8_t usf = 1;
+       uint32_t fn = 1234;
+
+       tall_pcu_ctx = talloc_named_const(NULL, 1, "bitvecTest context");
+       if (!tall_pcu_ctx)
+               abort();
+
+       dest = bitvec_alloc(22, tall_pcu_ctx);
+       dest_lh = bitvec_alloc(22, tall_pcu_ctx);
+
+       /* initialize buffer */
+       bitvec_unhex(dest, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+       bitvec_unhex(dest_lh, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+
+       test_bitvec_ia_octet_encode_pkt_dl_ass(dest, ttli, tfi, gamma, 
ta_valid, ws_enc, false);
+
+       printf("\n\n");
+
+       test_bitvec_ia_octet_encode_pkt_dl_ass(dest_lh, ttli, tfi, gamma, 
ta_valid, ws_enc, true);
+
+       printf("\n\n");
+
+       test_bitdiff(dest, dest_lh, 22);
+
+       printf("\n\n");
+
+       /* initialize buffer */
+       bitvec_unhex(dest, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+       bitvec_unhex(dest_lh, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+
+       test_bitvec_ia_octet_encode_pkt_ul_ass(dest, fn, tfi, gamma, usf, 
false, false);
+
+       printf("\n\n");
+
+       test_bitvec_ia_octet_encode_pkt_ul_ass(dest_lh, fn, tfi, gamma, usf, 
false, true);
+
+       printf("\n\n");
+
+       test_bitdiff(dest, dest_lh, 22);
+
+       printf("\n\n");
+
+       /* initialize buffer */
+       bitvec_unhex(dest, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+       bitvec_unhex(dest_lh, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
+
+       test_bitvec_ia_octet_encode_pkt_ul_ass(dest, fn, tfi, gamma, usf, true, 
false);
+
+       printf("\n\n");
+
+       test_bitvec_ia_octet_encode_pkt_ul_ass(dest_lh, fn, tfi, gamma, usf, 
true, true);
+
+       printf("\n\n");
+
+       test_bitdiff(dest, dest_lh, 22);
+
+       bitvec_free(dest);
+       bitvec_free(dest_lh);
+
+       talloc_free(tall_pcu_ctx);
+
+       return EXIT_SUCCESS;
+}
diff --git a/tests/bits/bitfield_test.ok b/tests/bits/bitfield_test.ok
new file mode 100644
index 0000000..f920a11
--- /dev/null
+++ b/tests/bits/bitfield_test.ok
@@ -0,0 +1,43 @@
+=== start test_bitvec_ia_octet_encode_pkt_dl_ass(0) ===
+Encoded PKT DL ASS IA Rest Octets: dd ea db ee f8 00 22 31 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_dl_ass(0) ===
+
+
+=== start test_bitvec_ia_octet_encode_pkt_dl_ass(1) ===
+Encoded PKT DL ASS IA Rest Octets: dd ea db ee f8 00 22 31 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_dl_ass(1) ===
+
+
+=== start test_bitdiff(22) ===
+=== total 0 bits differ ===
+=== end test_bitdiff(22) ===
+
+
+=== start test_bitvec_ia_octet_encode_pkt_ul_ass(0) ===
+Encoded PKT UL ASS IA Rest Octets: c0 08 0a 63 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_ul_ass(0) ===
+
+
+=== start test_bitvec_ia_octet_encode_pkt_ul_ass(1) ===
+Encoded PKT UL ASS IA Rest Octets: c0 08 0a 63 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_ul_ass(1) ===
+
+
+=== start test_bitdiff(22) ===
+=== total 0 bits differ ===
+=== end test_bitdiff(22) ===
+
+
+=== start test_bitvec_ia_octet_encode_pkt_ul_ass(0) ===
+Encoded PKT UL ASS IA Rest Octets: c8 02 10 0b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_ul_ass(0) ===
+
+
+=== start test_bitvec_ia_octet_encode_pkt_ul_ass(1) ===
+Encoded PKT UL ASS IA Rest Octets: c8 02 10 0b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 
2b 2b 2b 2b 2b 2b 2b 2b 
+=== end test_bitvec_ia_octet_encode_pkt_ul_ass(1) ===
+
+
+=== start test_bitdiff(22) ===
+=== total 0 bits differ ===
+=== end test_bitdiff(22) ===
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 483860f..4a59b22 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -45,6 +45,12 @@
 AT_CHECK([$abs_top_builddir/tests/bits/bitcomp_test], [0], [expout])
 AT_CLEANUP
 
+AT_SETUP([bitfield])
+AT_KEYWORDS([bitfield])
+cat $abs_srcdir/bits/bitfield_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/bits/bitfield_test], [0], [expout])
+AT_CLEANUP
+
 AT_SETUP([conv])
 AT_KEYWORDS([conv])
 cat $abs_srcdir/conv/conv_test.ok > expout

-- 
To view, visit https://gerrit.osmocom.org/4336
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ee544256b8675bc62a42493aab66a8eeee54f90
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msur...@sysmocom.de>

Reply via email to