File: test_ringbuffer.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (311 lines) | stat: -rw-r--r-- 6,900 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (c) 2016 Balabit
 * Copyright (c) 2014 Laszlo Budai
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include <criterion/criterion.h>

#include "ringbuffer.h"
#include <string.h>

static const guint32 capacity = 47;

typedef struct _TestData
{
  int idx;
  gboolean ack;
} TestData;

static void
_ringbuffer_init(RingBuffer *self)
{
  ring_buffer_alloc(self, sizeof(TestData), capacity);
}

static void
_ringbuffer_fill(RingBuffer *rb, size_t n, int start_idx, gboolean ack)
{
  TestData *td;
  int i;

  for (i = 0; i < n; i++)
    {
      td = ring_buffer_push(rb);
      td->idx = start_idx + i;
      td->ack = ack;
    }
}

static void
_ringbuffer_fill2(RingBuffer *rb, size_t n, int start_idx, gboolean ack)
{
  TestData *td;
  int i;

  for (i = 0; i < n; i++)
    {
      td = ring_buffer_tail(rb);
      td->idx = start_idx + i;
      td->ack = ack;
      cr_assert_eq(ring_buffer_push(rb), td, "Push should return last tail.");
    }
}

static gboolean
_is_continual(void *data)
{
  TestData *td = (TestData *)data;

  return td->ack;
}

static void
assert_continual_range_length_equals(RingBuffer *rb, size_t expected)
{
  size_t range_len = ring_buffer_get_continual_range_length(rb, _is_continual);
  cr_assert_eq(range_len, expected);
}

static void
assert_test_data_idx_range_in(RingBuffer *rb, int start, int end)
{
  TestData *td;
  int i;

  cr_assert_eq(ring_buffer_count(rb), end-start + 1,
               "invalid ringbuffer size; actual:%d, expected: %d",
               ring_buffer_count(rb), end-start+1);

  for (i = start; i <= end; i++)
    {
      td = ring_buffer_element_at(rb, i - start);
      cr_assert_eq(td->idx, i, "wrong order: idx:(%d) <-> td->idx(%d)", i, td->idx);
    }
}


Test(ringbuffer, test_init_buffer_state)
{
  RingBuffer rb;

  _ringbuffer_init(&rb);

  cr_assert_not(ring_buffer_is_full(&rb), "buffer should not be full");
  cr_assert(ring_buffer_is_empty(&rb), "buffer should be empty");
  cr_assert_eq(ring_buffer_count(&rb), 0, "buffer should be empty");
  cr_assert_eq(ring_buffer_capacity(&rb), capacity, "invalid buffer capacity");

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_pop_from_empty_buffer)
{
  RingBuffer rb;

  _ringbuffer_init(&rb);
  cr_assert_null(ring_buffer_pop(&rb), "cannot pop from empty buffer");

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_push_to_full_buffer)
{
  RingBuffer rb;

  _ringbuffer_init(&rb);
  _ringbuffer_fill(&rb, capacity, 1, TRUE);
  cr_assert_null(ring_buffer_push(&rb), "cannot push to a full buffer");

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_ring_buffer_is_full)
{
  RingBuffer rb;
  int i;
  TestData *last = NULL;

  _ringbuffer_init(&rb);

  for (i = 1; !ring_buffer_is_full(&rb); i++)
    {
      TestData *td = ring_buffer_push(&rb);
      cr_assert_not_null(td, "ring_buffer_push failed");
      td->idx = i;
      last = td;
    }

  cr_assert_eq(ring_buffer_count(&rb), capacity, "buffer count(%d) is not equal to capacity(%d)", ring_buffer_count(&rb),
               capacity);
  cr_assert_eq(last->idx, capacity, "buffer is not full, number of inserted items: %d, capacity: %d", last->idx,
               capacity);

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_pop_all_pushed_element_in_correct_order)
{
  RingBuffer rb;
  int cnt = 0;
  const int start_from = 1;
  TestData *td = NULL;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, 1, TRUE);

  while ((td = ring_buffer_pop(&rb)))
    {
      cr_assert_eq((cnt+start_from), td->idx, "wrong order; %d != %d", td->idx, cnt);
      ++cnt;
    }

  cr_assert_eq(cnt, capacity, "cannot read all element, %d < %d", cnt, capacity);

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_drop_elements)
{
  RingBuffer rb;
  const int rb_capacity = 103;
  const int drop = 31;

  ring_buffer_alloc(&rb, sizeof(TestData), rb_capacity);

  _ringbuffer_fill(&rb, rb_capacity, 1, TRUE);

  ring_buffer_drop(&rb, drop);
  cr_assert_eq(ring_buffer_count(&rb), (rb_capacity - drop), "drop failed");

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_elements_ordering)
{
  RingBuffer rb;
  TestData *td;
  int start_from = 10;
  int cnt = 0;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, start_from, TRUE);

  while ( (td = ring_buffer_pop(&rb)) )
    {
      cr_assert_eq((cnt + start_from), td->idx, "wrong order; %d != %d", cnt, td->idx);
      ++cnt;
    }

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_element_at)
{
  RingBuffer rb;
  guint32 i;
  TestData *td;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, 0, TRUE);

  for ( i = 0; i < ring_buffer_count(&rb); i++ )
    {
      td = ring_buffer_element_at(&rb, i);
      cr_assert_not_null(td, "invalid element, i=%d", i);
      cr_assert_eq(td->idx, i, "invalid order, actual=%d, expected=%d", td->idx, i);
    }

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_continual_range)
{
  RingBuffer rb;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, 1, TRUE);
  assert_continual_range_length_equals(&rb, capacity);

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_zero_length_continual_range)
{
  RingBuffer rb;
  TestData *td;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, 1, TRUE);

  td = ring_buffer_element_at(&rb, 0);
  td->ack = FALSE;

  assert_continual_range_length_equals(&rb, 0);

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_broken_continual_range)
{
  RingBuffer rb;

  TestData *td;

  _ringbuffer_init(&rb);

  _ringbuffer_fill(&rb, capacity, 1, TRUE);

  td = ring_buffer_element_at(&rb, 13);
  td->ack = FALSE;

  assert_continual_range_length_equals(&rb, 13);

  ring_buffer_free(&rb);
}

Test(ringbuffer, test_push_after_pop)
{
}

Test(ringbuffer, test_tail)
{
  RingBuffer rb;
  TestData *td_tail;

  ring_buffer_alloc(&rb, sizeof(TestData), 103);
  _ringbuffer_fill2(&rb, 103, 0, TRUE);

  ring_buffer_pop(&rb);

  td_tail = ring_buffer_tail(&rb);
  td_tail->idx = 103;

  cr_assert_eq(ring_buffer_push(&rb), td_tail, "Push should return last tail.");

  assert_test_data_idx_range_in(&rb, 1, 103);

  ring_buffer_free(&rb);
}