2
2
#include <small/ibuf.h>
3
3
#include <small/slab_cache.h>
4
4
#include <stdio.h>
5
+ #include <string.h>
5
6
#include "unit.h"
6
7
7
8
struct slab_cache cache ;
@@ -95,6 +96,47 @@ test_ibuf_shrink(void)
95
96
footer ();
96
97
}
97
98
99
+ static void
100
+ test_ibuf_truncate ()
101
+ {
102
+ header ();
103
+
104
+ char * ptr ;
105
+ const char * hello = "Hello Hello" ;
106
+ const char * goodbye = "Goodbye" ;
107
+ struct ibuf ibuf ;
108
+
109
+ ibuf_create (& ibuf , & cache , 16 * 1024 );
110
+ ibuf_alloc (& ibuf , 10 );
111
+ ibuf .rpos += 10 ;
112
+ ptr = ibuf_alloc (& ibuf , strlen (hello ) + 1 );
113
+ fail_unless (ptr != NULL );
114
+ strcpy (ptr , hello );
115
+ size_t svp = ibuf_used (& ibuf );
116
+
117
+ /*
118
+ * Test when there is NO reallocation in between used/truncate.
119
+ */
120
+ ptr = ibuf_alloc (& ibuf , 100 );
121
+ fail_unless (ptr != NULL );
122
+ strcpy (ptr , goodbye );
123
+ ibuf_truncate (& ibuf , svp );
124
+ fail_unless (ibuf_used (& ibuf ) == svp );
125
+ fail_unless (strcmp (ibuf .rpos , hello ) == 0 );
126
+
127
+ /*
128
+ * Test when there IS reallocation in between used/truncate.
129
+ */
130
+ ptr = ibuf_alloc (& ibuf , 32 * 1024 );
131
+ fail_unless (ptr != NULL );
132
+ strcpy (ptr , goodbye );
133
+ ibuf_truncate (& ibuf , svp );
134
+ fail_unless (ibuf_used (& ibuf ) == svp );
135
+ fail_unless (strcmp (ibuf .rpos , hello ) == 0 );
136
+
137
+ footer ();
138
+ }
139
+
98
140
int main ()
99
141
{
100
142
quota_init (& quota , UINT_MAX );
@@ -104,6 +146,7 @@ int main()
104
146
105
147
test_ibuf_basic ();
106
148
test_ibuf_shrink ();
149
+ test_ibuf_truncate ();
107
150
108
151
slab_cache_destroy (& cache );
109
152
}
0 commit comments