test_bits.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* test harness for bits.h
  2. *
  3. * This file is Copyright (c) 2010 by the GPSD project
  4. * SPDX-License-Identifier: BSD-2-clause
  5. */
  6. #include <inttypes.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "../bits.h"
  13. static unsigned char buf[80];
  14. static signed char sb1, sb2;
  15. static unsigned char ub1, ub2;
  16. static short sw1, sw2;
  17. static unsigned short uw1, uw2;
  18. static int sl1, sl2;
  19. static unsigned int ul1, ul2;
  20. static int64_t sL1, sL2;
  21. static uint64_t uL1, uL2;
  22. static float f1;
  23. static double d1;
  24. static char *hexdump(const void *binbuf, size_t len)
  25. {
  26. static char hexbuf[BUFSIZ];
  27. size_t i, j = 0;
  28. const char *ibuf = (const char *)binbuf;
  29. const char *hexchar = "0123456789abcdef";
  30. for (i = 0; i < len; i++) {
  31. hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
  32. hexbuf[j++] = hexchar[ibuf[i] & 0x0f];
  33. }
  34. hexbuf[j] = '\0';
  35. return hexbuf;
  36. }
  37. static void bedumpall(void)
  38. {
  39. (void)printf("getsb: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  40. (uint64_t) sb1, (uint64_t) sb2,
  41. (uint64_t) getsb(buf, 0), (uint64_t) getsb(buf, 8));
  42. (void)printf("getub: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  43. (uint64_t) ub1, (uint64_t) ub2,
  44. (uint64_t) getub(buf, 0), (uint64_t) getub(buf, 8));
  45. (void)printf("getbes16: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  46. (uint64_t) sw1, (uint64_t) sw2,
  47. (uint64_t) getbes16(buf, 0), (uint64_t) getbes16(buf, 8));
  48. (void)printf("getbeu16: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  49. (uint64_t) uw1, (uint64_t) uw2,
  50. (uint64_t) getbeu16(buf, 0), (uint64_t) getbeu16(buf, 8));
  51. (void)printf("getbes32: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  52. (uint64_t) sl1, (uint64_t) sl2,
  53. (uint64_t) getbes32(buf, 0), (uint64_t) getbes32(buf, 8));
  54. (void)printf("getbeu32: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  55. (uint64_t) ul1, (uint64_t) ul2,
  56. (uint64_t) getbeu32(buf, 0), (uint64_t) getbeu32(buf, 8));
  57. (void)printf("getbes64: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  58. (uint64_t) sL1, (uint64_t) sL2,
  59. (uint64_t) getbes64(buf, 0), (uint64_t) getbes64(buf, 8));
  60. (void)printf("getbeu64: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  61. (uint64_t) uL1, (uint64_t) uL2,
  62. (uint64_t) getbeu64(buf, 0), (uint64_t) getbeu64(buf, 8));
  63. (void)printf("getbef32: %f %f\n", f1, getbef32((const char *)buf, 24));
  64. (void)printf("getbed64: %.16f %.16f\n", d1, getbed64((const char *)buf, 16));
  65. }
  66. static void ledumpall(void)
  67. {
  68. (void)printf("getsb: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  69. (uint64_t) sb1, (uint64_t) sb2,
  70. (uint64_t) getsb(buf, 0), (uint64_t) getsb(buf, 8));
  71. (void)printf("getub: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  72. (uint64_t) ub1, (uint64_t) ub2,
  73. (uint64_t) getub(buf, 0), (uint64_t) getub(buf, 8));
  74. (void)printf("getles16: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  75. (uint64_t) sw1, (uint64_t) sw2,
  76. (uint64_t) getles16(buf, 0), (uint64_t) getles16(buf, 8));
  77. (void)printf("getleu16: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  78. (uint64_t) uw1, (uint64_t) uw2,
  79. (uint64_t) getleu16(buf, 0), (uint64_t) getleu16(buf, 8));
  80. (void)printf("getles32: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  81. (uint64_t) sl1, (uint64_t) sl2,
  82. (uint64_t) getles32(buf, 0), (uint64_t) getles32(buf, 8));
  83. (void)printf("getleu32: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  84. (uint64_t) ul1, (uint64_t) ul2,
  85. (uint64_t) getleu32(buf, 0), (uint64_t) getleu32(buf, 8));
  86. (void)printf("getles64: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  87. (uint64_t) sL1, (uint64_t) sL2,
  88. (uint64_t) getles64(buf, 0), (uint64_t) getles64(buf, 8));
  89. (void)printf("getleu64: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
  90. (uint64_t) uL1, (uint64_t) uL2,
  91. (uint64_t) getleu64(buf, 0), (uint64_t) getleu64(buf, 8));
  92. (void)printf("getlef32: %f %f\n", f1, getlef32((const char *)buf, 24));
  93. (void)printf("getled64: %.16f %.16f\n", d1, getled64((const char *)buf, 16));
  94. }
  95. struct unsigned_test
  96. {
  97. unsigned char *buf;
  98. unsigned int start, width;
  99. uint64_t expected;
  100. bool le;
  101. char *description;
  102. };
  103. int main(int argc, char *argv[])
  104. {
  105. bool failures = false;
  106. bool quiet = (argc > 1) && (strcmp(argv[1], "--quiet") == 0);
  107. struct unsigned_test *up, unsigned_tests[] = {
  108. /* tests using the big buffer */
  109. {buf, 0, 1, 0, false, "first bit of first byte"},
  110. {buf, 0, 8, 0x01, false, "first 8 bits"},
  111. {buf, 32, 7, 0x02, false, "first seven bits of fifth byte (0x05)"},
  112. {buf, 56, 12, 0x8f, false, "12 bits crossing 7th to 8th bytes (0x08ff)"},
  113. {buf, 78, 4, 0xb, false, "4 bits crossing 8th to 9th byte (0xfefd)"},
  114. {buf, 0, 1, 0, true, "first bit of first byte"},
  115. {buf, 0, 8, 0x80, true, "first 8 bits"},
  116. {buf, 32, 7, 0x20, true, "first seven bits of fifth byte (0x05)"},
  117. {buf, 56, 12, 0xf10,true, "12 bits crossing 7th to 8th bytes (0x08ff)"},
  118. {buf, 78, 4, 0xd, true, "4 bits crossing 8th to 9th byte (0xfefd)"},
  119. /* sporadic tests based on found bugs */
  120. {(unsigned char *)"\x19\x23\f6",
  121. 7, 2, 2, false, "2 bits crossing 1st to 2nd byte (0x1923)"},
  122. };
  123. memcpy(buf, "\x01\x02\x03\x04\x05\x06\x07\x08", 8);
  124. memcpy(buf + 8, "\xff\xfe\xfd\xfc\xfb\xfa\xf9\xf8", 8);
  125. memcpy(buf + 16, "\x40\x09\x21\xfb\x54\x44\x2d\x18", 8);
  126. memcpy(buf + 24, "\x40\x49\x0f\xdb", 4);
  127. if (!quiet)
  128. (void)printf("Testing bitfield extraction\n");
  129. sb1 = getsb(buf, 0);
  130. sb2 = getsb(buf, 8);
  131. ub1 = getub(buf, 0);
  132. ub2 = getub(buf, 8);
  133. if (!quiet) {
  134. unsigned char *sp;
  135. (void)fputs("Test data:", stdout);
  136. for (sp = buf; sp < buf + 28; sp++)
  137. (void)printf(" %02x", *sp);
  138. (void)putc('\n', stdout);
  139. /* big-endian test */
  140. printf("Big-endian:\n");
  141. sw1 = getbes16(buf, 0);
  142. sw2 = getbes16(buf, 8);
  143. uw1 = getbeu16(buf, 0);
  144. uw2 = getbeu16(buf, 8);
  145. sl1 = getbes32(buf, 0);
  146. sl2 = getbes32(buf, 8);
  147. ul1 = getbeu32(buf, 0);
  148. ul2 = getbeu32(buf, 8);
  149. sL1 = getbes64(buf, 0);
  150. sL2 = getbes64(buf, 8);
  151. uL1 = getbeu64(buf, 0);
  152. uL2 = getbeu64(buf, 8);
  153. f1 = getbef32((const char *)buf, 24);
  154. d1 = getbed64((const char *)buf, 16);
  155. bedumpall();
  156. /* little-endian test */
  157. printf("Little-endian:\n");
  158. sw1 = getles16(buf, 0);
  159. sw2 = getles16(buf, 8);
  160. uw1 = getleu16(buf, 0);
  161. uw2 = getleu16(buf, 8);
  162. sl1 = getles32(buf, 0);
  163. sl2 = getles32(buf, 8);
  164. ul1 = getleu32(buf, 0);
  165. ul2 = getleu32(buf, 8);
  166. sL1 = getles64(buf, 0);
  167. sL2 = getles64(buf, 8);
  168. uL1 = getleu64(buf, 0);
  169. uL2 = getleu64(buf, 8);
  170. f1 = getlef32((const char *)buf, 24);
  171. d1 = getled64((const char *)buf, 16);
  172. ledumpall();
  173. }
  174. if (sb1 != 1) printf("getsb(buf, 0) FAILED\n");
  175. if (sb2 != -1) printf("getsb(buf, 8) FAILED\n");
  176. if (ub1 != 1) printf("getub(buf, 0) FAILED\n");
  177. if (ub2 != 0xff) printf("getub(buf, 8) FAILED\n");
  178. for (up = unsigned_tests;
  179. up <
  180. unsigned_tests + sizeof(unsigned_tests) / sizeof(unsigned_tests[0]);
  181. up++) {
  182. uint64_t res = ubits((unsigned char *)buf, up->start, up->width, up->le);
  183. bool success = (res == up->expected);
  184. if (!success)
  185. failures = true;
  186. if (!success || !quiet)
  187. (void)printf("ubits(%s, %d, %d, %s) %s should be %" PRIx64 ", is %" PRIx64 ": %s\n",
  188. hexdump(buf, strlen((char *)buf)),
  189. up->start, up->width, up->le ? "true" : "false",
  190. up->description, up->expected, res,
  191. success ? "succeeded" : "FAILED");
  192. }
  193. shiftleft(buf, 28, 30);
  194. if (!quiet)
  195. printf("Left-shifted 30 bits: %s\n", hexdump(buf, 28));
  196. /*
  197. * After the 24-bit shift, the bit array loses its first three bytes:
  198. * 0x0405060708 = 00000100 00000101 00000110 00000111 00001000
  199. * By inspection, the results of the 6-bit shift are
  200. * 00000001 01000001 10000001 11000010 00
  201. */
  202. #define LASSERT(n, v) if (buf[n] != v) printf("Expected buf[%d] to be %02x, was %02x\n", n, v, buf[n])
  203. LASSERT(0, 0x01);
  204. LASSERT(1, 0x41);
  205. LASSERT(2, 0x81);
  206. LASSERT(3, 0xc1);
  207. #undef LASSERT
  208. exit(failures ? EXIT_FAILURE : EXIT_SUCCESS);
  209. }