aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/osmocom/core/bitvec.h9
-rw-r--r--tests/bitvec/bitvec_test.c26
-rw-r--r--tests/bitvec/bitvec_test.ok2
3 files changed, 37 insertions, 0 deletions
diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index c9bab398..da2d4e45 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -84,4 +84,13 @@ unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
unsigned int array_len, bool dry_run,
unsigned int num_bits);
+/*! Return the number of bytes used within the bit vector */
+static inline unsigned int bitvec_used_bytes(const struct bitvec *bv)
+{
+ unsigned int bytes = bv->cur_bit/8;
+ if (bv->cur_bit%8)
+ bytes++;
+ return bytes;
+}
+
/*! @} */
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index d0bc30c4..c8795dbd 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -181,6 +181,29 @@ static void test_array()
test_array_item(17, &b, n, array, n * 3);
}
+static void test_used_bytes()
+{
+ struct bitvec b;
+ uint8_t d[32];
+ unsigned int i;
+
+ b.data = d;
+ b.data_len = sizeof(d);
+ bitvec_zero(&b);
+
+ OSMO_ASSERT(bitvec_used_bytes(&b) == 0);
+
+ for (i = 0; i < 8; i++) {
+ bitvec_set_bit(&b, 1);
+ OSMO_ASSERT(bitvec_used_bytes(&b) == 1);
+ }
+
+ for (i = 8; i < 16; i++) {
+ bitvec_set_bit(&b, 1);
+ OSMO_ASSERT(bitvec_used_bytes(&b) == 2);
+ }
+}
+
int main(int argc, char **argv)
{
struct bitvec bv;
@@ -286,6 +309,9 @@ int main(int argc, char **argv)
bitvec_zero(&bv);
test_bitvec_rl_curbit(&bv, 1, 64, 0);
+ printf("\nbitvec bytes used.\n");
+ test_used_bytes();
+
printf("\nbitvec ok.\n");
return 0;
}
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index 62819736..a48912d5 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -168,4 +168,6 @@ bits: 17, est: 1153, real: 1153, x: 0, y: 0
........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........
bitvec_runlength....
+bitvec bytes used.
+
bitvec ok.