Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #define HALFBYTE_SIZE (4)
- #define HALFBYTE_MASK (0xF)
- #define HALFBYTE_R (HALFBYTE_MASK << HALFBYTE_SIZE)
- #define HALFBYTE_L (HALFBYTE_MASK)
- static int test_counter;
- /*
- * if position is st to 0, then change only first halfbyte (range from 0 to 3),
- * otherwise if position is se to 1, change only second halfbyte (range from 4 to 7)
- */
- uint8_t halfByteStorage(uint8_t byte, uint8_t position, uint8_t value)
- {
- if(position)
- {
- return ((byte & HALFBYTE_L) | value << HALFBYTE_SIZE);
- }
- else
- {
- return ((byte & HALFBYTE_R) | value);
- }
- }
- /*
- * assertP:
- * function is made for easy module tests of halfByteStorage function.
- * INPUT:
- * byte: storage for two halfbytes
- * position: position of halfbyte to change
- * value: new value to store
- * value: expected result of halfByteStorage() function
- * OUTPUT:
- * printed result of the test
- */
- void assertP(uint8_t byte, uint8_t position, uint8_t value, uint8_t expected)
- {
- ++test_counter;
- uint8_t ret = 0;
- ret = halfByteStorage(byte, position, value);
- if(ret == expected)
- printf("test %d passed\n", test_counter);
- else
- printf("test %d fails\n", test_counter);
- }
- int main()
- {
- //test first halfbyte
- assertP(0xAA, 0, 5, 165); // halfbyte, position, value, expected
- assertP(0xAA, 0, 7, 167);
- assertP(0xFF, 0, 5, 245);
- assertP(0xFF, 0, 0, 240);
- //test second halfbyte
- assertP(0xAA, 1, 5, 90); // halfbyte, position, value, expected
- assertP(0xAA, 1, 7, 122);
- assertP(0xFF, 1, 5, 95);
- assertP(0xFF, 1, 0, 15);
- //test random values (should not pass tests)
- assertP(0xAA, 1, 4, 10); // halfbyte, position, value, expected
- assertP(0xAA, 0, 7, 99);
- assertP(0xFF, 1, 6, 12);
- assertP(0xFF, 0, 8, 55);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement