test_printbuf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. static void test_basic_printbuf_memset()
  12. {
  13. struct printbuf *pb;
  14. printf("%s: starting test\n", __func__);
  15. pb = printbuf_new();
  16. sprintbuf(pb, "blue:%d", 1);
  17. printbuf_memset(pb, -1, 'x', 52);
  18. printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
  19. printbuf_free(pb);
  20. printf("%s: end test\n", __func__);
  21. }
  22. static void test_printbuf_memset_length()
  23. {
  24. struct printbuf *pb;
  25. printf("%s: starting test\n", __func__);
  26. pb = printbuf_new();
  27. printbuf_memset(pb, -1, ' ', 0);
  28. printbuf_memset(pb, -1, ' ', 0);
  29. printbuf_memset(pb, -1, ' ', 0);
  30. printbuf_memset(pb, -1, ' ', 0);
  31. printbuf_memset(pb, -1, ' ', 0);
  32. printf("Buffer length: %d\n", printbuf_length(pb));
  33. printbuf_memset(pb, -1, ' ', 2);
  34. printbuf_memset(pb, -1, ' ', 4);
  35. printbuf_memset(pb, -1, ' ', 6);
  36. printf("Buffer length: %d\n", printbuf_length(pb));
  37. printbuf_memset(pb, -1, ' ', 6);
  38. printf("Buffer length: %d\n", printbuf_length(pb));
  39. printbuf_memset(pb, -1, ' ', 8);
  40. printbuf_memset(pb, -1, ' ', 10);
  41. printbuf_memset(pb, -1, ' ', 10);
  42. printbuf_memset(pb, -1, ' ', 10);
  43. printbuf_memset(pb, -1, ' ', 20);
  44. printf("Buffer length: %d\n", printbuf_length(pb));
  45. // No length change should occur
  46. printbuf_memset(pb, 0, 'x', 30);
  47. printf("Buffer length: %d\n", printbuf_length(pb));
  48. // This should extend it by one.
  49. printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1);
  50. printf("Buffer length: %d\n", printbuf_length(pb));
  51. printbuf_free(pb);
  52. printf("%s: end test\n", __func__);
  53. }
  54. static void test_printbuf_memappend(int *before_resize);
  55. static void test_printbuf_memappend(int *before_resize)
  56. {
  57. struct printbuf *pb;
  58. int initial_size;
  59. printf("%s: starting test\n", __func__);
  60. pb = printbuf_new();
  61. printf("Buffer length: %d\n", printbuf_length(pb));
  62. initial_size = pb->size;
  63. while(pb->size == initial_size)
  64. {
  65. printbuf_memappend_fast(pb, "x", 1);
  66. }
  67. *before_resize = printbuf_length(pb) - 1;
  68. printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
  69. printbuf_reset(pb);
  70. printbuf_memappend_fast(pb, "bluexyz123", 3);
  71. printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
  72. char with_nulls[] = { 'a', 'b', '\0', 'c' };
  73. printbuf_reset(pb);
  74. printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls));
  75. printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
  76. printbuf_free(pb);
  77. pb = printbuf_new();
  78. char *data = malloc(*before_resize);
  79. memset(data, 'X', *before_resize);
  80. printbuf_memappend_fast(pb, data, *before_resize);
  81. printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
  82. free(data);
  83. printbuf_free(pb);
  84. pb = printbuf_new();
  85. data = malloc(*before_resize + 1);
  86. memset(data, 'X', *before_resize + 1);
  87. printbuf_memappend_fast(pb, data, *before_resize + 1);
  88. printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
  89. free(data);
  90. printbuf_free(pb);
  91. #define SA_TEST_STR "XXXXXXXXXXXXXXXX"
  92. pb = printbuf_new();
  93. printbuf_strappend(pb, SA_TEST_STR);
  94. printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf);
  95. printbuf_free(pb);
  96. #undef SA_TEST_STR
  97. printf("%s: end test\n", __func__);
  98. }
  99. static void test_sprintbuf(int before_resize);
  100. static void test_sprintbuf(int before_resize)
  101. {
  102. struct printbuf *pb;
  103. printf("%s: starting test\n", __func__);
  104. pb = printbuf_new();
  105. printf("Buffer length: %d\n", printbuf_length(pb));
  106. char *data = malloc(before_resize + 1 + 1);
  107. memset(data, 'X', before_resize + 1 + 1);
  108. data[before_resize + 1] = '\0';
  109. sprintbuf(pb, "%s", data);
  110. free(data);
  111. 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));
  112. printbuf_reset(pb);
  113. sprintbuf(pb, "plain");
  114. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  115. sprintbuf(pb, "%d", 1);
  116. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  117. sprintbuf(pb, "%d", INT_MAX);
  118. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  119. sprintbuf(pb, "%d", INT_MIN);
  120. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  121. sprintbuf(pb, "%s", "%s");
  122. printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
  123. printbuf_free(pb);
  124. printf("%s: end test\n", __func__);
  125. }
  126. int main(int argc, char **argv)
  127. {
  128. int before_resize = 0;
  129. mc_set_debug(1);
  130. test_basic_printbuf_memset();
  131. printf("========================================\n");
  132. test_printbuf_memset_length();
  133. printf("========================================\n");
  134. test_printbuf_memappend(&before_resize);
  135. printf("========================================\n");
  136. test_sprintbuf(before_resize);
  137. printf("========================================\n");
  138. return 0;
  139. }