~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to unit/test-ringbuf.c

  • Committer: tim
  • Date: 2015-09-04 08:03:03 UTC
  • Revision ID: tim@tim-inspiron-5442-20150904080303-75u7z8bdsl17xz8q
* init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "src/shared/tester.h"
 
36
 
 
37
static unsigned int nlpo2(unsigned int x)
 
38
{
 
39
        x--;
 
40
        x |= (x >> 1);
 
41
        x |= (x >> 2);
 
42
        x |= (x >> 4);
 
43
        x |= (x >> 8);
 
44
        x |= (x >> 16);
 
45
        return x + 1;
 
46
}
 
47
 
 
48
static unsigned int fls(unsigned int x)
 
49
{
 
50
        return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
 
51
}
 
52
 
 
53
static unsigned int align_power2(unsigned int u)
 
54
{
 
55
        return 1 << fls(u - 1);
 
56
}
 
57
 
 
58
static void test_power2(const void *data)
 
59
{
 
60
        size_t i;
 
61
 
 
62
        for (i = 1; i < 1000000; i++) {
 
63
                size_t size1, size2, size3 = 1;
 
64
 
 
65
                size1 = nlpo2(i);
 
66
                size2 = align_power2(i);
 
67
 
 
68
                /* Find the next power of two */
 
69
                while (size3 < i && size3 < SIZE_MAX)
 
70
                        size3 <<= 1;
 
71
 
 
72
                tester_debug("%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
        tester_test_passed();
 
81
}
 
82
 
 
83
static void test_alloc(const void *data)
 
84
{
 
85
        int i;
 
86
 
 
87
        for (i = 2; i < 10000; i++) {
 
88
                struct ringbuf *rb;
 
89
 
 
90
                tester_debug("Iteration %i\n", i);
 
91
 
 
92
                rb = ringbuf_new(i);
 
93
                g_assert(rb != NULL);
 
94
 
 
95
                g_assert(ringbuf_capacity(rb) == ringbuf_avail(rb));
 
96
 
 
97
                ringbuf_free(rb);
 
98
        }
 
99
 
 
100
        tester_test_passed();
 
101
}
 
102
 
 
103
static void test_printf(const void *data)
 
104
{
 
105
        static size_t rb_size = 500;
 
106
        static size_t rb_capa = 512;
 
107
        struct ringbuf *rb;
 
108
        int i;
 
109
 
 
110
        rb = ringbuf_new(rb_size);
 
111
        g_assert(rb != NULL);
 
112
        g_assert(ringbuf_capacity(rb) == rb_capa);
 
113
 
 
114
        for (i = 0; i < 10000; i++) {
 
115
                size_t len, count = i % rb_capa;
 
116
                char *str, *ptr;
 
117
 
 
118
                if (!count)
 
119
                        continue;
 
120
 
 
121
                tester_debug("Iteration %i\n", i);
 
122
 
 
123
                len = asprintf(&str, "%*c", (int) count, 'x');
 
124
                g_assert(len == count);
 
125
 
 
126
                len = ringbuf_printf(rb, "%s", str);
 
127
                g_assert(len == count);
 
128
                g_assert(ringbuf_len(rb) == count);
 
129
                g_assert(ringbuf_avail(rb) == rb_capa - len);
 
130
 
 
131
                ptr = ringbuf_peek(rb, 0, &len);
 
132
                g_assert(ptr != NULL);
 
133
                g_assert(len == count);
 
134
                g_assert(strncmp(str, ptr, len) == 0);
 
135
 
 
136
                len = ringbuf_drain(rb, count);
 
137
                g_assert(len == count);
 
138
                g_assert(ringbuf_len(rb) == 0);
 
139
                g_assert(ringbuf_avail(rb) == rb_capa);
 
140
 
 
141
                free(str);
 
142
        }
 
143
 
 
144
        ringbuf_free(rb);
 
145
        tester_test_passed();
 
146
}
 
147
 
 
148
int main(int argc, char *argv[])
 
149
{
 
150
        tester_init(&argc, &argv);
 
151
 
 
152
        tester_add("/ringbuf/power2", NULL, NULL, test_power2, NULL);
 
153
        tester_add("/ringbuf/alloc", NULL, NULL, test_alloc, NULL);
 
154
        tester_add("/ringbuf/printf", NULL, NULL, test_printf, NULL);
 
155
 
 
156
        return tester_run();
 
157
}