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
|
/*
* Embedded Linux library
* Copyright (C) 2015 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <ell/ell.h>
#include "ell/useful.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;
assert(size1 == size2);
assert(size2 == size3);
assert(size3 == size1);
}
}
static void test_alloc(const void *data)
{
int i;
for (i = 2; i < 10000; i++) {
struct l_ringbuf *rb;
rb = l_ringbuf_new(i);
assert(rb != NULL);
assert(l_ringbuf_capacity(rb) == l_ringbuf_avail(rb));
l_ringbuf_free(rb);
}
}
static void test_printf(const void *data)
{
static size_t rb_size = 500;
static size_t rb_capa = 512;
struct l_ringbuf *rb;
int i;
rb = l_ringbuf_new(rb_size);
assert(rb != NULL);
assert(l_ringbuf_capacity(rb) == rb_capa);
for (i = 0; i < 10000; i++) {
size_t len, count = i % rb_capa;
char *str, *ptr;
if (!count)
continue;
len = asprintf(&str, "%*c", (int) count, 'x');
assert(len == count);
len = l_ringbuf_printf(rb, "%s", str);
assert(len == count);
assert(l_ringbuf_len(rb) == count);
assert(l_ringbuf_avail(rb) == rb_capa - len);
ptr = l_ringbuf_peek(rb, 0, &len);
assert(ptr != NULL);
assert(len == count);
assert(strncmp(str, ptr, len) == 0);
len = l_ringbuf_drain(rb, count);
assert(len == count);
assert(l_ringbuf_len(rb) == 0);
assert(l_ringbuf_avail(rb) == rb_capa);
free(str);
}
l_ringbuf_free(rb);
}
static void test_append(const void *unused)
{
static const uint8_t data[6] = { 1, 2, 3, 4, 5, 6 };
static const size_t rb_size = 12;
static const size_t rb_capa = 16;
size_t len_no_wrap;
ssize_t appended;
ssize_t space_left;
void *rb_data;
struct l_ringbuf *rb;
rb = l_ringbuf_new(rb_size);
assert(rb != NULL);
assert(l_ringbuf_capacity(rb) == rb_capa);
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == sizeof(data));
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == sizeof(data));
space_left = minsize(l_ringbuf_avail(rb), sizeof(data));
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == space_left);
rb_data = l_ringbuf_peek(rb, 0, &len_no_wrap);
assert(memcmp(rb_data, data, sizeof(data)) == 0);
l_ringbuf_drain(rb, sizeof(data));
rb_data = l_ringbuf_peek(rb, 0, &len_no_wrap);
assert(memcmp(rb_data, data, sizeof(data)) == 0);
l_ringbuf_drain(rb, sizeof(data));
rb_data = l_ringbuf_peek(rb, 0, &len_no_wrap);
assert(memcmp(rb_data, data, len_no_wrap) == 0);
l_ringbuf_drain(rb, len_no_wrap);
l_ringbuf_free(rb);
}
static void test_append2(const void *unused)
{
static const uint8_t expected_data1[14] = {3, 4, 5, 6, 1, 2, 3, 4, 5,
6, 1, 2, 3, 4 };
static const uint8_t expected_data2[2] = { 5, 6 };
static const uint8_t data[6] = { 1, 2, 3, 4, 5, 6 };
static const size_t rb_size = 12;
static const size_t rb_capa = 16;
size_t len_no_wrap;
ssize_t appended;
ssize_t space_left;
void *rb_data;
struct l_ringbuf *rb;
rb = l_ringbuf_new(rb_size);
assert(rb != NULL);
assert(l_ringbuf_capacity(rb) == rb_capa);
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == sizeof(data));
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == sizeof(data));
space_left = minsize(l_ringbuf_avail(rb), sizeof(data));
l_ringbuf_drain(rb, sizeof(data) - space_left);
appended = l_ringbuf_append(rb, data, sizeof(data));
assert(appended == sizeof(data));
rb_data = l_ringbuf_peek(rb, 0, &len_no_wrap);
assert(len_no_wrap == sizeof(expected_data1));
assert(memcmp(expected_data1, rb_data, len_no_wrap) == 0);
l_ringbuf_drain(rb, len_no_wrap);
rb_data = l_ringbuf_peek(rb, 0, &len_no_wrap);
assert(len_no_wrap == sizeof(expected_data2));
assert(memcmp(expected_data2, rb_data, len_no_wrap) == 0);
l_ringbuf_free(rb);
}
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("/ringbuf/power2", test_power2, NULL);
l_test_add("/ringbuf/alloc", test_alloc, NULL);
l_test_add("/ringbuf/printf", test_printf, NULL);
l_test_add("/ringbuf/append", test_append, NULL);
l_test_add("/ringbuf/append2", test_append2, NULL);
return l_test_run();
}
|