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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static void test_sprintbuf(int before_resize);
  104. static void test_sprintbuf(int before_resize)
  105. {
  106. struct printbuf *pb;
  107. printf("%s: starting test\n", __func__);
  108. pb = printbuf_new();
  109. printf("Buffer length: %d\n", printbuf_length(pb));
  110. char *data = malloc(before_resize + 1 + 1);
  111. memset(data, 'X', before_resize + 1 + 1);
  112. data[before_resize + 1] = '\0';
  113. sprintbuf(pb, "%s", data);
  114. free(data);
  115. 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));
  116. printbuf_reset(pb);
  117. sprintbuf(pb, "plain");
  118. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  119. sprintbuf(pb, "%d", 1);
  120. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  121. sprintbuf(pb, "%d", INT_MAX);
  122. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  123. sprintbuf(pb, "%d", INT_MIN);
  124. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  125. sprintbuf(pb, "%s", "%s");
  126. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  127. printbuf_free(pb);
  128. printf("%s: end test\n", __func__);
  129. }
  130. int main(int argc, char **argv)
  131. {
  132. int before_resize = 0;
  133. MC_SET_DEBUG(1);
  134. test_basic_printbuf_memset();
  135. printf("========================================\n");
  136. test_printbuf_memset_length();
  137. printf("========================================\n");
  138. test_printbuf_memappend(&before_resize);
  139. printf("========================================\n");
  140. test_sprintbuf(before_resize);
  141. printf("========================================\n");
  142. return 0;
  143. }