Add a function to set a single nibble in a bitarray.
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Sun, 29 Dec 2013 20:15:16 +0000 (15:15 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Sun, 29 Dec 2013 20:15:16 +0000 (15:15 -0500)
src/bitfield/bitfield.c
src/bitfield/bitfield.h
tests/bitfield_tests.c

index af17d48..ae1be40 100644 (file)
@@ -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);
+}
index b58e4e5..990482f 100644 (file)
@@ -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
index 3f54eee..e0646e6 100644 (file)
@@ -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);