mirror of
https://git.kernel.org/pub/scm/bluetooth/bluez.git
synced 2024-11-16 00:34:39 +08:00
931d804022
This patch adds SPDX License Identifier and removes the license text. ------------------------------------- License COUNT ------------------------------------- GPL-2.0-or-later : 25 LGPL-2.1-or-later : 1 GPL-2.0-only : 2 BSD-2-Clause : 1 License: GPL-2.0-or-later unit/test-hog.c unit/test-eir.c unit/test-gobex-header.c unit/test-crc.c unit/test-avrcp.c unit/test-ringbuf.c unit/test-gobex-apparam.c unit/test-gobex.c unit/test-hfp.c unit/test-textfile.c unit/test-avdtp.c unit/test-sdp.c unit/test-gatt.c unit/test-avctp.c unit/test-gattrib.c unit/test-queue.c unit/test-gobex-packet.c unit/test-uuid.c unit/test-crypto.c unit/test-uhid.c unit/test-gdbus-client.c unit/test-midi.c unit/test-lib.c unit/test-mgmt.c unit/test-gobex-transfer.c License: LGPL-2.1-or-later unit/test-mesh-crypto.c License: GPL-2.0-only unit/util.h unit/util.c License: BSD-2-Clause unit/test-ecc.c
146 lines
2.6 KiB
C
146 lines
2.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
*
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
*
|
|
* Copyright (C) 2012 Intel Corporation. All rights reserved.
|
|
*
|
|
*
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#define _GNU_SOURCE
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include "src/shared/ringbuf.h"
|
|
#include "src/shared/tester.h"
|
|
|
|
static unsigned int nlpo2(unsigned int x)
|
|
{
|
|
x--;
|
|
x |= (x >> 1);
|
|
x |= (x >> 2);
|
|
x |= (x >> 4);
|
|
x |= (x >> 8);
|
|
x |= (x >> 16);
|
|
return x + 1;
|
|
}
|
|
|
|
static unsigned int fls(unsigned int x)
|
|
{
|
|
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
|
|
}
|
|
|
|
static unsigned int align_power2(unsigned int u)
|
|
{
|
|
return 1 << fls(u - 1);
|
|
}
|
|
|
|
static void test_power2(const void *data)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 1; i < 1000000; i++) {
|
|
size_t size1, size2, size3 = 1;
|
|
|
|
size1 = nlpo2(i);
|
|
size2 = align_power2(i);
|
|
|
|
/* Find the next power of two */
|
|
while (size3 < i && size3 < SIZE_MAX)
|
|
size3 <<= 1;
|
|
|
|
tester_debug("%zu -> size1=%zu size2=%zu size3=%zu\n",
|
|
i, size1, size2, size3);
|
|
|
|
g_assert(size1 == size2);
|
|
g_assert(size2 == size3);
|
|
g_assert(size3 == size1);
|
|
}
|
|
|
|
tester_test_passed();
|
|
}
|
|
|
|
static void test_alloc(const void *data)
|
|
{
|
|
int i;
|
|
|
|
for (i = 2; i < 10000; i++) {
|
|
struct ringbuf *rb;
|
|
|
|
tester_debug("Iteration %i\n", i);
|
|
|
|
rb = ringbuf_new(i);
|
|
g_assert(rb != NULL);
|
|
|
|
g_assert(ringbuf_capacity(rb) == ringbuf_avail(rb));
|
|
|
|
ringbuf_free(rb);
|
|
}
|
|
|
|
tester_test_passed();
|
|
}
|
|
|
|
static void test_printf(const void *data)
|
|
{
|
|
static size_t rb_size = 500;
|
|
static size_t rb_capa = 512;
|
|
struct ringbuf *rb;
|
|
int i;
|
|
|
|
rb = ringbuf_new(rb_size);
|
|
g_assert(rb != NULL);
|
|
g_assert(ringbuf_capacity(rb) == rb_capa);
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
size_t len, count = i % rb_capa;
|
|
char *str, *ptr;
|
|
|
|
if (!count)
|
|
continue;
|
|
|
|
tester_debug("Iteration %i\n", i);
|
|
|
|
len = asprintf(&str, "%*c", (int) count, 'x');
|
|
g_assert(len == count);
|
|
|
|
len = ringbuf_printf(rb, "%s", str);
|
|
g_assert(len == count);
|
|
g_assert(ringbuf_len(rb) == count);
|
|
g_assert(ringbuf_avail(rb) == rb_capa - len);
|
|
|
|
ptr = ringbuf_peek(rb, 0, &len);
|
|
g_assert(ptr != NULL);
|
|
g_assert(len == count);
|
|
g_assert(strncmp(str, ptr, len) == 0);
|
|
|
|
len = ringbuf_drain(rb, count);
|
|
g_assert(len == count);
|
|
g_assert(ringbuf_len(rb) == 0);
|
|
g_assert(ringbuf_avail(rb) == rb_capa);
|
|
|
|
free(str);
|
|
}
|
|
|
|
ringbuf_free(rb);
|
|
tester_test_passed();
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
tester_init(&argc, &argv);
|
|
|
|
tester_add("/ringbuf/power2", NULL, NULL, test_power2, NULL);
|
|
tester_add("/ringbuf/alloc", NULL, NULL, test_alloc, NULL);
|
|
tester_add("/ringbuf/printf", NULL, NULL, test_printf, NULL);
|
|
|
|
return tester_run();
|
|
}
|