1 #include <ccan/membuf/membuf.h>
5 static int num_realloc, num_memmove;
7 void *memmove_test(void *dest, const void *src, size_t n);
8 void *realloc_test(void *ptr, size_t size);
10 void *memmove_test(void *dest, const void *src, size_t n)
13 return memmove(dest, src, n);
16 void *realloc_test(void *ptr, size_t size)
19 return realloc(ptr, size);
23 #define memmove memmove_test
26 #define realloc realloc_test
28 /* Include the C files directly. */
29 #include <ccan/membuf/membuf.c>
30 #include <ccan/tap/tap.h>
37 /* This is how many tests you plan to run */
38 plan_tests(13 + 100 * 4 + 999);
40 membuf_init(&intbuf, malloc(10 * sizeof(int)), 10, membuf_realloc);
41 ok1(membuf_num_elems(&intbuf) == 0);
42 ok1(membuf_num_space(&intbuf) == 10);
43 ok1(membuf_space(&intbuf) != NULL);
46 for (int i = 0; i < 100; i++) {
47 memcpy(membuf_add(&intbuf, 1), &i, sizeof(i));
48 ok1(membuf_num_elems(&intbuf) == i+1);
50 /* Make sure membuf_elems works */
52 ok1(memcmp(membuf_elems(&intbuf), &i, sizeof(i)) == 0);
57 for (int i = 0; i < 100; i++) {
58 ok1(memcmp(membuf_consume(&intbuf, 1), &i, sizeof(i)) == 0);
59 ok1(membuf_num_elems(&intbuf) == 100 - i - 1);
62 /* Should not have continuously realloced or memmoved */
63 ok1(num_realloc < 10);
64 ok1(num_memmove == 0);
66 /* Doing it again should give 0 reallocs. */
67 prev_reallocs = num_realloc;
68 for (int i = 0; i < 100; i++) {
69 memcpy(membuf_add(&intbuf, 1), &i, sizeof(i));
70 ok1(membuf_num_elems(&intbuf) == i+1);
72 ok1(num_realloc == prev_reallocs);
73 ok1(num_memmove == 0);
75 membuf_consume(&intbuf, 100);
77 /* Keep a single element in the queue, make sure we don't realloc! */
78 for (int i = 0; i < 1000; i++) {
79 memcpy(membuf_add(&intbuf, 1), &i, sizeof(i));
82 ok1(memcmp(membuf_consume(&intbuf, 1),
83 &prev, sizeof(prev)) == 0);
87 ok1(num_realloc == prev_reallocs);
88 /* Should have moved occasionally. */
89 ok1(num_memmove < 20);
91 ok1(membuf_consume(&intbuf, 1));
92 ok1(membuf_num_elems(&intbuf) == 0);
94 /* Force it to more-than-double; make sure that works! */
95 memset(membuf_add(&intbuf, 300), 0, 300*sizeof(int));
96 ok1(membuf_num_elems(&intbuf) == 300);
98 /* Free buffer so valgrind is happy. */
99 free(membuf_cleanup(&intbuf));
101 /* This exits depending on whether all tests passed */
102 return exit_status();