spa: Update tests and examples

Keep tests in spa/tests
Make spa/examples
Remove some old examples
This commit is contained in:
Wim Taymans 2019-01-17 09:27:41 +01:00
parent fa2d877925
commit 3ee463ed3a
23 changed files with 266 additions and 5271 deletions

View file

@ -5,11 +5,12 @@
#include <spa/utils/ringbuffer.h>
#define ARRAY_SIZE 64
#define DEFAULT_SIZE 0x2000
#define ARRAY_SIZE 63
#define MAX_VALUE 0x10000
struct spa_ringbuffer rb;
uint32_t size;
int32_t size;
uint8_t *data;
static int fill_int_array(int *array, int start, int count)
@ -37,7 +38,6 @@ static int cmp_array(int *array1, int *array2, int count)
static void *reader_start(void *arg)
{
int i = 0, a[ARRAY_SIZE], b[ARRAY_SIZE];
unsigned long j = 0, nfailures = 0;
printf("reader started on cpu: %d\n", sched_getcpu());
@ -45,22 +45,16 @@ static void *reader_start(void *arg)
while (1) {
uint32_t index;
int32_t avail;
if (spa_ringbuffer_get_read_index(&rb, &index) >= (int32_t)(ARRAY_SIZE * sizeof(int))) {
spa_ringbuffer_read_data(&rb, data, size, index & (size - 1), b,
ARRAY_SIZE * sizeof(int));
avail = spa_ringbuffer_get_read_index(&rb, &index);
if (!cmp_array(a, b, ARRAY_SIZE)) {
nfailures++;
printf
("failure in chunk %lu - probability: %lu/%lu = %.3f per million\n",
j, nfailures, j, (float) nfailures / (j + 1) * 1000000);
i = (b[0] + ARRAY_SIZE) % MAX_VALUE;
}
if (avail >= (int32_t)(sizeof(b))) {
spa_ringbuffer_read_data(&rb, data, size, index % size, b, sizeof(b));
spa_ringbuffer_read_update(&rb, index + sizeof(b));
spa_assert(cmp_array(a, b, ARRAY_SIZE));
i = fill_int_array(a, i, ARRAY_SIZE);
j++;
spa_ringbuffer_read_update(&rb, index + ARRAY_SIZE * sizeof(int));
}
}
@ -76,11 +70,13 @@ static void *writer_start(void *arg)
while (1) {
uint32_t index;
int32_t avail;
if (spa_ringbuffer_get_write_index(&rb, &index) >= (int32_t)(ARRAY_SIZE * sizeof(int))) {
spa_ringbuffer_write_data(&rb, data, size, index & (size - 1), a,
ARRAY_SIZE * sizeof(int));
spa_ringbuffer_write_update(&rb, index + ARRAY_SIZE * sizeof(int));
avail = size - spa_ringbuffer_get_write_index(&rb, &index);
if (avail >= (int32_t)(sizeof(a))) {
spa_ringbuffer_write_data(&rb, data, size, index % size, a, sizeof(a));
spa_ringbuffer_write_update(&rb, index + sizeof(a));
i = fill_int_array(a, i, ARRAY_SIZE);
}
@ -93,7 +89,10 @@ int main(int argc, char *argv[])
{
printf("starting ringbuffer stress test\n");
sscanf(argv[1], "%d", &size);
if (argc > 1)
sscanf(argv[1], "%d", &size);
else
size = DEFAULT_SIZE;
printf("buffer size (bytes): %d\n", size);
printf("array size (bytes): %ld\n", sizeof(int) * ARRAY_SIZE);
@ -105,8 +104,9 @@ int main(int argc, char *argv[])
pthread_create(&reader_thread, NULL, reader_start, NULL);
pthread_create(&writer_thread, NULL, writer_start, NULL);
while (1)
sleep(1);
sleep(5);
printf("read %u, written %u\n", rb.readindex, rb.writeindex);
return 0;
}