You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_printbuf.c 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <limits.h>
  7. #include "debug.h"
  8. #include "printbuf.h"
  9. static void test_basic_printbuf_memset(void);
  10. static void test_printbuf_memset_length(void);
  11. #ifndef __func__
  12. /* VC++ compat */
  13. #define __func__ __FUNCTION__
  14. #endif
  15. static void test_basic_printbuf_memset()
  16. {
  17. struct printbuf *pb;
  18. printf("%s: starting test\n", __func__);
  19. pb = printbuf_new();
  20. sprintbuf(pb, "blue:%d", 1);
  21. printbuf_memset(pb, -1, 'x', 52);
  22. printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
  23. printbuf_free(pb);
  24. printf("%s: end test\n", __func__);
  25. }
  26. static void test_printbuf_memset_length()
  27. {
  28. struct printbuf *pb;
  29. printf("%s: starting test\n", __func__);
  30. pb = printbuf_new();
  31. printbuf_memset(pb, -1, ' ', 0);
  32. printbuf_memset(pb, -1, ' ', 0);
  33. printbuf_memset(pb, -1, ' ', 0);
  34. printbuf_memset(pb, -1, ' ', 0);
  35. printbuf_memset(pb, -1, ' ', 0);
  36. printf("Buffer length: %d\n", printbuf_length(pb));
  37. printbuf_memset(pb, -1, ' ', 2);
  38. printbuf_memset(pb, -1, ' ', 4);
  39. printbuf_memset(pb, -1, ' ', 6);
  40. printf("Buffer length: %d\n", printbuf_length(pb));
  41. printbuf_memset(pb, -1, ' ', 6);
  42. printf("Buffer length: %d\n", printbuf_length(pb));
  43. printbuf_memset(pb, -1, ' ', 8);
  44. printbuf_memset(pb, -1, ' ', 10);
  45. printbuf_memset(pb, -1, ' ', 10);
  46. printbuf_memset(pb, -1, ' ', 10);
  47. printbuf_memset(pb, -1, ' ', 20);
  48. printf("Buffer length: %d\n", printbuf_length(pb));
  49. // No length change should occur
  50. printbuf_memset(pb, 0, 'x', 30);
  51. printf("Buffer length: %d\n", printbuf_length(pb));
  52. // This should extend it by one.
  53. printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1);
  54. printf("Buffer length: %d\n", printbuf_length(pb));
  55. printbuf_free(pb);
  56. printf("%s: end test\n", __func__);
  57. }
  58. static void test_printbuf_memappend(int *before_resize);
  59. static void test_printbuf_memappend(int *before_resize)
  60. {
  61. struct printbuf *pb;
  62. int initial_size;
  63. printf("%s: starting test\n", __func__);
  64. pb = printbuf_new();
  65. printf("Buffer length: %d\n", printbuf_length(pb));
  66. initial_size = pb->size;
  67. while(pb->size == initial_size)
  68. {
  69. printbuf_memappend_fast(pb, "x", 1);
  70. }
  71. *before_resize = printbuf_length(pb) - 1;
  72. printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
  73. printbuf_reset(pb);
  74. printbuf_memappend_fast(pb, "bluexyz123", 3);
  75. printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
  76. char with_nulls[] = { 'a', 'b', '\0', 'c' };
  77. printbuf_reset(pb);
  78. printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls));
  79. printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
  80. printbuf_free(pb);
  81. pb = printbuf_new();
  82. char *data = malloc(*before_resize);
  83. memset(data, 'X', *before_resize);
  84. printbuf_memappend_fast(pb, data, *before_resize);
  85. printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
  86. free(data);
  87. printbuf_free(pb);
  88. pb = printbuf_new();
  89. data = malloc(*before_resize + 1);
  90. memset(data, 'X', *before_resize + 1);
  91. printbuf_memappend_fast(pb, data, *before_resize + 1);
  92. printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
  93. free(data);
  94. printbuf_free(pb);
  95. #define SA_TEST_STR "XXXXXXXXXXXXXXXX"
  96. pb = printbuf_new();
  97. printbuf_strappend(pb, SA_TEST_STR);
  98. printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf);
  99. printbuf_free(pb);
  100. #undef SA_TEST_STR
  101. printf("%s: end test\n", __func__);
  102. }
  103. #if 0
  104. static void test_sprintbuf(int before_resize);
  105. static void test_sprintbuf(int before_resize)
  106. {
  107. struct printbuf *pb;
  108. printf("%s: starting test\n", __func__);
  109. pb = printbuf_new();
  110. printf("Buffer length: %d\n", printbuf_length(pb));
  111. char *data = malloc(before_resize + 1 + 1);
  112. memset(data, 'X', before_resize + 1 + 1);
  113. data[before_resize + 1] = '\0';
  114. sprintbuf(pb, "%s", data);
  115. free(data);
  116. printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
  117. printbuf_reset(pb);
  118. sprintbuf(pb, "plain");
  119. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  120. sprintbuf(pb, "%d", 1);
  121. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  122. sprintbuf(pb, "%d", INT_MAX);
  123. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  124. sprintbuf(pb, "%d", INT_MIN);
  125. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  126. sprintbuf(pb, "%s", "%s");
  127. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  128. printbuf_free(pb);
  129. printf("%s: end test\n", __func__);
  130. }
  131. #endif
  132. #if 1
  133. static void test_sprintbuf(int before_resize);
  134. static void test_sprintbuf(int before_resize)
  135. {
  136. struct printbuf *pb;
  137. printf("%s: starting test\n", __func__);
  138. pb = printbuf_new();
  139. printf("Buffer length: %d\n", printbuf_length(pb));
  140. char *data = malloc(before_resize + 1 + 1);
  141. memset(data, 'X', before_resize + 1 + 1);
  142. data[before_resize + 1] = '\0';
  143. sprintbuf(pb, "%s", data);
  144. free(data);
  145. printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
  146. printbuf_reset(pb);
  147. sprintbuf(pb, "plain");
  148. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  149. sprintbuf(pb, "%d", 1);
  150. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  151. sprintbuf(pb, "%d", INT_MAX);
  152. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  153. sprintbuf(pb, "%d", INT_MIN);
  154. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  155. sprintbuf(pb, "%s", "%s");
  156. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  157. printbuf_free(pb);
  158. printf("%s: end test\n", __func__);
  159. }
  160. #endif
  161. int main(int argc, char **argv)
  162. {
  163. int before_resize = 0;
  164. MC_SET_DEBUG(1);
  165. test_basic_printbuf_memset();
  166. printf("========================================\n");
  167. test_printbuf_memset_length();
  168. printf("========================================\n");
  169. test_printbuf_memappend(&before_resize);
  170. printf("========================================\n");
  171. test_sprintbuf(before_resize);
  172. printf("========================================\n");
  173. /*
  174. test_sprintbuf2(before_resize);
  175. printf("========================================\n");
  176. */
  177. return 0;
  178. }