MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_printbuf.c
Go to the documentation of this file.
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 
8 #include "debug.h"
9 #include "printbuf.h"
10 
11 static void test_basic_printbuf_memset(void);
12 static void test_printbuf_memset_length(void);
13 
15 {
16  struct printbuf *pb;
17 
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 
28 {
29  struct printbuf *pb;
30 
31  printf("%s: starting test\n", __func__);
32  pb = printbuf_new();
33  printbuf_memset(pb, -1, ' ', 0);
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  printf("Buffer length: %d\n", printbuf_length(pb));
39  printbuf_memset(pb, -1, ' ', 2);
40  printbuf_memset(pb, -1, ' ', 4);
41  printbuf_memset(pb, -1, ' ', 6);
42  printf("Buffer length: %d\n", printbuf_length(pb));
43  printbuf_memset(pb, -1, ' ', 6);
44  printf("Buffer length: %d\n", printbuf_length(pb));
45  printbuf_memset(pb, -1, ' ', 8);
46  printbuf_memset(pb, -1, ' ', 10);
47  printbuf_memset(pb, -1, ' ', 10);
48  printbuf_memset(pb, -1, ' ', 10);
49  printbuf_memset(pb, -1, ' ', 20);
50  printf("Buffer length: %d\n", printbuf_length(pb));
51 
52  // No length change should occur
53  printbuf_memset(pb, 0, 'x', 30);
54  printf("Buffer length: %d\n", printbuf_length(pb));
55 
56  // This should extend it by one.
57  printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1);
58  printf("Buffer length: %d\n", printbuf_length(pb));
59 
60  printbuf_free(pb);
61  printf("%s: end test\n", __func__);
62 }
63 
64 static void test_printbuf_memappend(int *before_resize);
65 static void test_printbuf_memappend(int *before_resize)
66 {
67  struct printbuf *pb;
68  int initial_size;
69 
70  printf("%s: starting test\n", __func__);
71  pb = printbuf_new();
72  printf("Buffer length: %d\n", printbuf_length(pb));
73 
74  initial_size = pb->size;
75 
76  while(pb->size == initial_size)
77  {
78  printbuf_memappend_fast(pb, "x", 1);
79  }
80  *before_resize = printbuf_length(pb) - 1;
81  printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
82 
83  printbuf_reset(pb);
84  printbuf_memappend_fast(pb, "bluexyz123", 3);
85  printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
86 
87  char with_nulls[] = { 'a', 'b', '\0', 'c' };
88  printbuf_reset(pb);
89  printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls));
90  printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
91 
92  printbuf_free(pb);
93  pb = printbuf_new();
94  char *data = malloc(*before_resize);
95  memset(data, 'X', *before_resize);
96  printbuf_memappend_fast(pb, data, *before_resize);
97  printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
98 
99  free(data);
100  printbuf_free(pb);
101 
102  pb = printbuf_new();
103  data = malloc(*before_resize + 1);
104  memset(data, 'X', *before_resize + 1);
105  printbuf_memappend_fast(pb, data, *before_resize + 1);
106  printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
107 
108  free(data);
109 
110  printbuf_free(pb);
111  printf("%s: end test\n", __func__);
112 }
113 
114 static void test_sprintbuf(int before_resize);
115 static void test_sprintbuf(int before_resize)
116 {
117  struct printbuf *pb;
118 
119  printf("%s: starting test\n", __func__);
120  pb = printbuf_new();
121  printf("Buffer length: %d\n", printbuf_length(pb));
122 
123  char *data = malloc(before_resize + 1 + 1);
124  memset(data, 'X', before_resize + 1 + 1);
125  data[before_resize + 1] = '\0';
126  sprintbuf(pb, "%s", data);
127  free(data);
128  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));
129 
130  printbuf_reset(pb);
131  sprintbuf(pb, "plain");
132  printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
133 
134  sprintbuf(pb, "%d", 1);
135  printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
136 
137  sprintbuf(pb, "%d", INT_MAX);
138  printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
139 
140  sprintbuf(pb, "%d", INT_MIN);
141  printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
142 
143  sprintbuf(pb, "%s", "%s");
144  printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
145 
146  printbuf_free(pb);
147  printf("%s: end test\n", __func__);
148 }
149 
150 int main(int argc, char **argv)
151 {
152  int before_resize = 0;
153 
154  mc_set_debug(1);
155 
157  printf("========================================\n");
159  printf("========================================\n");
160  test_printbuf_memappend(&before_resize);
161  printf("========================================\n");
162  test_sprintbuf(before_resize);
163  printf("========================================\n");
164 
165  return 0;
166 }
void mc_set_debug(int debug)
Definition: debug.c:36
#define printbuf_length(p)
Definition: printbuf.h:51
static void test_printbuf_memappend(int *before_resize)
Definition: test_printbuf.c:65
int sprintbuf(struct printbuf *p, const char *msg,...)
Definition: printbuf.c:152
struct printbuf * printbuf_new(void)
Definition: printbuf.c:34
void printbuf_reset(struct printbuf *p)
Definition: printbuf.c:180
int size
Definition: printbuf.h:26
void printbuf_free(struct printbuf *p)
Definition: printbuf.c:186
int main(int argc, char **argv)
static void test_basic_printbuf_memset(void)
Definition: test_printbuf.c:14
int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
Definition: printbuf.c:91
static void test_sprintbuf(int before_resize)
char * buf
Definition: printbuf.h:24
#define printbuf_memappend_fast(p, bufptr, bufsize)
Definition: printbuf.h:42
static void test_printbuf_memset_length(void)
Definition: test_printbuf.c:27