bitbuf.test.c 919 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* This file is dedicated to the public domain. */
  2. {.desc = "the bit buffer implementation"};
  3. #include "../src/bitbuf.h"
  4. #include "../src/intdefs.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. static union {
  8. char buf[512];
  9. bitbuf_cell buf_align[512 / sizeof(bitbuf_cell)];
  10. } bb_buf;
  11. static struct bitbuf bb = {bb_buf.buf, 512, 512 * 8, 0, false, false, "test"};
  12. TEST("The possible UB in bitbuf_appendbuf shouldn't trigger horrible bugs") {
  13. char unalign[3] = {'X', 'X', 'X'};
  14. char _buf[32 + sizeof(bitbuf_cell)];
  15. char *buf = _buf;
  16. if (bitbuf_align <= 1) {
  17. // *shouldn't* happen
  18. fputs("what's going on with the alignment???\n", stderr);
  19. return false;
  20. }
  21. // make sure the pointer is definitely misaligned
  22. while (!((usize)buf % bitbuf_align)) ++buf;
  23. memcpy(buf, "Misaligned test buffer contents!", 32);
  24. bitbuf_appendbuf(&bb, buf, 32);
  25. return !memcmp(bb.buf, buf, 32);
  26. }
  27. // vi: sw=4 ts=4 noet tw=80 cc=80