bits_test.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "assertion.h"
  2. #include <stdio.h>
  3. #include "bits.h"
  4. static void test1(void)
  5. {
  6. static bit_array bits[SIZE(40)] = {0};
  7. assert(get_bit(bits, 0) == 0);
  8. set_bit(bits, 1);
  9. assert(get_bit(bits, 1) == 1);
  10. clear_bit(bits, 1);
  11. assert(get_bit(bits, 1) == 0);
  12. set_bit(bits, 2);
  13. assert(get_bit(bits, 2) == 1);
  14. set_bit(bits, 3);
  15. assert(get_bit(bits, 3) == 1);
  16. for (size_t i = 4; i < 40; i++) {
  17. assert(get_bit(bits, i) == 0);
  18. }
  19. printf("Test1 Passed!\n");
  20. }
  21. static void test2(void)
  22. {
  23. static bit_array bits[SIZE(40)] = {0};
  24. hex2bits(bits, 0, 12450);
  25. hex2bits(bits, 16, 11037);
  26. assert(bits2hex(bits, 0) == 12450);
  27. assert(bits2hex(bits, 16) == 11037);
  28. for (size_t i = 32; i < 40; i++) {
  29. assert(get_bit(bits, i) == 0);
  30. }
  31. printf("Test2 Passed!\n");
  32. }
  33. static void test3(void)
  34. {
  35. static bit_array bits[SIZE(20)] = {0};
  36. char2bits(bits, 0, 'B');
  37. char2bits(bits, 8, 'C');
  38. assert(bits2char(bits, 0) == 'B');
  39. assert(bits2char(bits, 8) == 'C');
  40. for (size_t i = 16; i < 20; i++) {
  41. assert(get_bit(bits, i) == 0);
  42. }
  43. printf("Test3 Passed!\n");
  44. }
  45. static void test4(void)
  46. {
  47. static bit_array bits[SIZE(4)] = {0};
  48. set_bit(bits, 0);
  49. set_bit(bits, 1);
  50. clear_bit(bits, 2);
  51. assert(get_bit(bits, 0) == 1);
  52. assert(get_bit(bits, 1) == 1);
  53. assert(get_bit(bits, 2) == 0);
  54. bits_left_shift(bits, 3, 1);
  55. assert(get_bit(bits, 0) == 1);
  56. assert(get_bit(bits, 1) == 0);
  57. assert(get_bit(bits, 2) == 1);
  58. assert(get_bit(bits, 3) == 0);
  59. bits_left_shift(bits, 4, 1);
  60. assert(get_bit(bits, 0) == 0);
  61. assert(get_bit(bits, 1) == 1);
  62. assert(get_bit(bits, 2) == 0);
  63. assert(get_bit(bits, 3) == 1);
  64. printf("Test4 Passed!\n");
  65. }
  66. int main(void)
  67. {
  68. test1();
  69. test2();
  70. test3();
  71. test4();
  72. return 0;
  73. }