Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define HALFBYTE_SIZE (4)
  5. #define HALFBYTE_MASK (0xF)
  6. #define HALFBYTE_R (HALFBYTE_MASK << HALFBYTE_SIZE)
  7. #define HALFBYTE_L (HALFBYTE_MASK)
  8. static int test_counter;
  9. /*
  10. * if position is st to 0, then change only first halfbyte (range from 0 to 3),
  11. * otherwise if position is se to 1, change only second halfbyte (range from 4 to 7)
  12. */
  13.  
  14. uint8_t halfByteStorage(uint8_t byte, uint8_t position, uint8_t value)
  15. {
  16. if(position)
  17. {
  18. return ((byte & HALFBYTE_L) | value << HALFBYTE_SIZE);
  19. }
  20.  
  21. else
  22. {
  23. return ((byte & HALFBYTE_R) | value);
  24. }
  25. }
  26.  
  27. /*
  28. * assertP:
  29. * function is made for easy module tests of halfByteStorage function.
  30. * INPUT:
  31. * byte: storage for two halfbytes
  32. * position: position of halfbyte to change
  33. * value: new value to store
  34. * value: expected result of halfByteStorage() function
  35. * OUTPUT:
  36. * printed result of the test
  37. */
  38. void assertP(uint8_t byte, uint8_t position, uint8_t value, uint8_t expected)
  39. {
  40. ++test_counter;
  41. uint8_t ret = 0;
  42. ret = halfByteStorage(byte, position, value);
  43.  
  44. if(ret == expected)
  45. printf("test %d passed\n", test_counter);
  46.  
  47. else
  48. printf("test %d fails\n", test_counter);
  49. }
  50.  
  51. int main()
  52. {
  53.  
  54. //test first halfbyte
  55. assertP(0xAA, 0, 5, 165); // halfbyte, position, value, expected
  56. assertP(0xAA, 0, 7, 167);
  57. assertP(0xFF, 0, 5, 245);
  58. assertP(0xFF, 0, 0, 240);
  59.  
  60. //test second halfbyte
  61. assertP(0xAA, 1, 5, 90); // halfbyte, position, value, expected
  62. assertP(0xAA, 1, 7, 122);
  63. assertP(0xFF, 1, 5, 95);
  64. assertP(0xFF, 1, 0, 15);
  65.  
  66. //test random values (should not pass tests)
  67. assertP(0xAA, 1, 4, 10); // halfbyte, position, value, expected
  68. assertP(0xAA, 0, 7, 99);
  69. assertP(0xFF, 1, 6, 12);
  70. assertP(0xFF, 0, 8, 55);
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement