append.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2019 Ariadne Devos
  3. /* sHT -- test the sHT_append function */
  4. /* Adapted from <tests/memeq.c>. */
  5. #include <sHT/index.h>
  6. #include <sHT/string.h>
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. /* Do a brute-force test of @var{sHT_append}, for little numbers.
  12. To avoid copying the implementation, start with the outcome
  13. (@var{todo}) and test all corresponding inputs.
  14. Only some information flow is tested for the moment.
  15. The lengths, offset and outcome are varied.
  16. _: (to) don't modify
  17. !: (from) don't read, at least non-speculatively
  18. X: (to) overwrite me!
  19. Y: (from) copy me to an X! */
  20. static struct testcase {
  21. char to_expect[16];
  22. char to_input[16];
  23. char from[16];
  24. size_t length0;
  25. size_t length1;
  26. size_t i0;
  27. size_t i1;
  28. size_t todo;
  29. } c;
  30. static void
  31. test(void)
  32. {
  33. _Alignas(struct testcase)
  34. char to_copy[16];
  35. size_t result;
  36. memcpy(to_copy, c.to_input, 16);
  37. result = sHT_append(to_copy, c.from, c.length0, c.length1, c.i0, c.i1);
  38. if (sHT_neq(result, c.todo) || memcmp(to_copy, c.to_expect, 16))
  39. goto fail;
  40. return;
  41. fail:
  42. if (printf("FAIL: append\n"))
  43. exit(2);
  44. exit(1);
  45. }
  46. int
  47. main(void)
  48. {
  49. size_t write_offset;
  50. size_t read_offset;
  51. sHT_index_iterate(c.todo, 8u) {
  52. sHT_index_iterate(write_offset, 8u) {
  53. memcpy(c.to_input, "________________", 16);
  54. memcpy(c.to_expect, "________________", 16);
  55. memset(c.to_input + write_offset, 'X', c.todo);
  56. memset(c.to_expect + write_offset, 'Y', c.todo);
  57. sHT_index_iterate(read_offset, 8u) {
  58. memcpy(c.from, "!!!!!!!!!!!!!!!!", 16);
  59. memset(c.from + read_offset, 'Y', c.todo);
  60. c.i0 = write_offset;
  61. c.i1 = read_offset;
  62. c.length0 = write_offset + c.todo;
  63. c.length1 = read_offset + c.todo;
  64. test();
  65. c.length0 = 16;
  66. c.length1 = read_offset + c.todo;
  67. test();
  68. c.length0 = write_offset + c.todo;
  69. c.length1 = 16;
  70. test();
  71. }
  72. }
  73. }
  74. if (printf("PASS: append\n") < 0)
  75. return 2;
  76. return 0;
  77. }