summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/bitfield/bitfield.c6
-rw-r--r--src/bitfield/bitfield.h3
-rw-r--r--tests/bitfield_tests.c15
3 files changed, 24 insertions, 0 deletions
diff --git a/src/bitfield/bitfield.c b/src/bitfield/bitfield.c
index af17d485..ae1be403 100644
--- a/src/bitfield/bitfield.c
+++ b/src/bitfield/bitfield.c
@@ -21,3 +21,9 @@ uint8_t get_byte(const uint8_t source[], const uint8_t source_length,
}
return 0;
}
+
+bool set_nibble(const uint16_t nibble_index, const uint8_t value,
+ uint8_t* destination, const uint16_t destination_length) {
+ return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination,
+ destination_length, nibble_index * NIBBLE_SIZE);
+}
diff --git a/src/bitfield/bitfield.h b/src/bitfield/bitfield.h
index b58e4e59..990482fc 100644
--- a/src/bitfield/bitfield.h
+++ b/src/bitfield/bitfield.h
@@ -110,6 +110,9 @@ bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_lengt
const uint16_t offset, const uint16_t bit_count,
uint8_t* destination, const uint16_t destination_length);
+bool set_nibble(const uint16_t nibble_index, const uint8_t value,
+ uint8_t* destination, const uint16_t destination_length);
+
#ifdef __cplusplus
}
#endif
diff --git a/tests/bitfield_tests.c b/tests/bitfield_tests.c
index 3f54eee1..e0646e6a 100644
--- a/tests/bitfield_tests.c
+++ b/tests/bitfield_tests.c
@@ -12,6 +12,20 @@ START_TEST (test_get_byte)
}
END_TEST
+START_TEST (test_set_nibble)
+{
+ uint8_t data[4] = {0};
+ fail_unless(set_nibble(0, 0x1, data, sizeof(data)));
+ fail_unless(set_nibble(1, 0x2, data, sizeof(data)));
+ fail_unless(set_nibble(2, 0x3, data, sizeof(data)));
+ fail_unless(set_nibble(3, 0x4, data, sizeof(data)));
+ fail_unless(set_nibble(4, 0x5, data, sizeof(data)));
+ ck_assert_int_eq(data[0], 0x12);
+ ck_assert_int_eq(data[1], 0x34);
+ ck_assert_int_eq(data[2], 0x50);
+}
+END_TEST
+
START_TEST (test_get_nibble)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
@@ -57,6 +71,7 @@ Suite* bitfieldSuite(void) {
TCase *tc_core = tcase_create("core");
tcase_add_test(tc_core, test_get_byte);
tcase_add_test(tc_core, test_get_nibble);
+ tcase_add_test(tc_core, test_set_nibble);
tcase_add_test(tc_core, test_get_bits);
tcase_add_test(tc_core, test_get_bits_out_of_range);
tcase_add_test(tc_core, test_get_uneven_bits);