OSDN Git Service

e28e04ba1aa712bfb4174f44f2fa37bbb0769aa3
[android-x86/external-bluetooth-bluez.git] / unit / test-ringbuf.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdint.h>
31
32 #include <glib.h>
33
34 #include "src/shared/ringbuf.h"
35
36 static unsigned int nlpo2(unsigned int x)
37 {
38         x--;
39         x |= (x >> 1);
40         x |= (x >> 2);
41         x |= (x >> 4);
42         x |= (x >> 8);
43         x |= (x >> 16);
44         return x + 1;
45 }
46
47 static unsigned int fls(unsigned int x)
48 {
49         return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
50 }
51
52 static unsigned int align_power2(unsigned int u)
53 {
54         return 1 << fls(u - 1);
55 }
56
57 static void test_power2(void)
58 {
59         size_t i;
60
61         for (i = 1; i < 1000000; i++) {
62                 size_t size1, size2, size3 = 1;
63
64                 size1 = nlpo2(i);
65                 size2 = align_power2(i);
66
67                 /* Find the next power of two */
68                 while (size3 < i && size3 < SIZE_MAX)
69                         size3 <<= 1;
70
71                 if (g_test_verbose())
72                         g_print("%zu -> size1=%zu size2=%zu size3=%zu\n",
73                                                 i, size1, size2, size3);
74
75                 g_assert(size1 == size2);
76                 g_assert(size2 == size3);
77                 g_assert(size3 == size1);
78         }
79 }
80
81 static void test_alloc(void)
82 {
83         int i;
84
85         for (i = 2; i < 10000; i++) {
86                 struct ringbuf *rb;
87
88                 if (g_test_verbose())
89                         g_print("Iteration %i\n", i);
90
91                 rb = ringbuf_new(i);
92                 g_assert(rb != NULL);
93
94                 g_assert(ringbuf_capacity(rb) == ringbuf_avail(rb));
95
96                 ringbuf_free(rb);
97         }
98 }
99
100 static void test_printf(void)
101 {
102         static size_t rb_size = 500;
103         static size_t rb_capa = 512;
104         struct ringbuf *rb;
105         int i;
106
107         rb = ringbuf_new(rb_size);
108         g_assert(rb != NULL);
109         g_assert(ringbuf_capacity(rb) == rb_capa);
110
111         for (i = 0; i < 10000; i++) {
112                 size_t len, count = i % rb_capa;
113                 char *str, *ptr;
114
115                 if (!count)
116                         continue;
117
118                 if (g_test_verbose())
119                         g_print("Iteration %i\n", i);
120
121                 len = asprintf(&str, "%*c", (int) count, 'x');
122                 g_assert(len == count);
123
124                 len = ringbuf_printf(rb, "%s", str);
125                 g_assert(len == count);
126                 g_assert(ringbuf_len(rb) == count);
127                 g_assert(ringbuf_avail(rb) == rb_capa - len);
128
129                 ptr = ringbuf_peek(rb, 0, &len);
130                 g_assert(ptr != NULL);
131                 g_assert(len == count);
132                 g_assert(strncmp(str, ptr, len) == 0);
133
134                 len = ringbuf_drain(rb, count);
135                 g_assert(len == count);
136                 g_assert(ringbuf_len(rb) == 0);
137                 g_assert(ringbuf_avail(rb) == rb_capa);
138
139                 free(str);
140         }
141
142         ringbuf_free(rb);
143 }
144
145 int main(int argc, char *argv[])
146 {
147         g_test_init(&argc, &argv, NULL);
148
149         g_test_add_func("/ringbuf/power2", test_power2);
150         g_test_add_func("/ringbuf/alloc", test_alloc);
151         g_test_add_func("/ringbuf/printf", test_printf);
152
153         return g_test_run();
154 }