testLits.h.template 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. static int testdarray_from_lit(void);
  2. static int testdarray_append_lit(void);
  3. static int testdarray_prepend_lit(void);
  4. static void testLits(void) {
  5. testing(darray_from_lit);
  6. ok1(testdarray_from_lit());
  7. testing(testdarray_append_lit);
  8. ok1(testdarray_append_lit());
  9. testing(testdarray_prepend_lit);
  10. ok1(testdarray_prepend_lit());
  11. }
  12. static int testdarray_from_lit(void) {
  13. darray_char a = darray_new();
  14. size_t testsPassed = 0;
  15. size_t len = 0;
  16. @forEachRandomString
  17. /* Test @i */
  18. darray_from_lit(a, @str);
  19. len = strlen(@str);
  20. if (len != sizeof(@str)-1)
  21. goto end;
  22. if (a.size != len)
  23. goto end;
  24. if (a.size > a.alloc)
  25. goto end;
  26. if (strcmp(a.item, @str))
  27. goto end;
  28. darray_free(a);
  29. darray_init(a);
  30. testsPassed++;
  31. @end
  32. end:
  33. darray_free(a);
  34. return testsPassed == @amount;
  35. }
  36. typedef struct {
  37. char *item;
  38. size_t size;
  39. } testLits_string;
  40. static int testdarray_append_lit(void) {
  41. darray_char a = darray_new();
  42. darray(testLits_string) strings = darray_new();
  43. testLits_string *i;
  44. size_t testsPassed = 0;
  45. size_t oldSize;
  46. testLits_string append;
  47. size_t offs = 0;
  48. @forEachRandomString
  49. /* Test @i */
  50. append.size = sizeof(@str)-1;
  51. oldSize = a.size;
  52. darray_append_lit(a, @str);
  53. if (a.size != oldSize+append.size)
  54. goto end;
  55. if (a.size > a.alloc)
  56. goto end;
  57. if (a.item[a.size])
  58. goto end;
  59. if (memcmp(a.item+oldSize, @str, a.size-oldSize))
  60. goto end;
  61. append.item = strdup(@str);
  62. darray_append(strings, append);
  63. testsPassed++;
  64. @end
  65. if (strings.size != @amount)
  66. goto end;
  67. darray_foreach(i, strings) {
  68. if (a.size-offs < i->size)
  69. goto end;
  70. if (memcmp(a.item+offs, i->item, i->size))
  71. goto end;
  72. offs += i->size;
  73. };
  74. if (offs != a.size)
  75. goto end;
  76. if (a.item[offs])
  77. goto end;
  78. testsPassed++;
  79. end:
  80. darray_free(a);
  81. darray_foreach(i, strings)
  82. free(i->item);
  83. darray_free(strings);
  84. return testsPassed == @amount+1;
  85. }
  86. static int testdarray_prepend_lit(void) {
  87. darray_char a = darray_new();
  88. darray(testLits_string) strings = darray_new();
  89. testLits_string *i;
  90. size_t testsPassed = 0;
  91. size_t oldSize;
  92. testLits_string append;
  93. size_t offs;
  94. @forEachRandomString
  95. /* Test @i */
  96. append.size = sizeof(@str)-1;
  97. oldSize = a.size;
  98. darray_prepend_lit(a, @str);
  99. if (a.size != oldSize+append.size)
  100. goto end;
  101. if (a.size > a.alloc)
  102. goto end;
  103. if (a.item[a.size])
  104. goto end;
  105. if (memcmp(a.item, @str, a.size-oldSize))
  106. goto end;
  107. append.item = strdup(@str);
  108. darray_append(strings, append);
  109. testsPassed++;
  110. @end
  111. offs = a.size;
  112. if (a.item[offs])
  113. goto end;
  114. if (strings.size != @amount)
  115. goto end;
  116. darray_foreach(i, strings) {
  117. if (offs < i->size)
  118. goto end;
  119. offs -= i->size;
  120. if (memcmp(a.item+offs, i->item, i->size))
  121. goto end;
  122. };
  123. if (offs)
  124. goto end;
  125. testsPassed++;
  126. end:
  127. darray_free(a);
  128. darray_foreach(i, strings)
  129. free(i->item);
  130. darray_free(strings);
  131. return testsPassed == @amount+1;
  132. }