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 5.4 kB

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