summaryrefslogtreecommitdiffstats
path: root/test/test-ring-buffer.c
blob: e868cd4e1576a98334adb6aa43aac00265fa440f (plain)
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
#include <glib.h>
#include "uca-ring-buffer.h"


static void
test_new_constructor (void)
{
    UcaRingBuffer *buffer;

    buffer = g_object_new (UCA_TYPE_RING_BUFFER,
                           "block-size", (guint64) 512,
                           "num-blocks", 2,
                           NULL);

    g_assert (uca_ring_buffer_get_block_size (buffer) == 512);
    g_assert (uca_ring_buffer_get_num_blocks (buffer) == 0);
    g_assert (uca_ring_buffer_get_write_pointer (buffer) != NULL);

    g_object_unref (buffer);
}

static void
test_new_func (void)
{
    UcaRingBuffer *buffer;

    buffer = uca_ring_buffer_new (512, 2);

    g_assert (uca_ring_buffer_get_block_size (buffer) == 512);
    g_assert (uca_ring_buffer_get_num_blocks (buffer) == 0);
    g_assert (uca_ring_buffer_get_write_pointer (buffer) != NULL);

    g_object_unref (buffer);
}

static void
test_ring (void)
{
    UcaRingBuffer *buffer;
    guint32 *data;

    buffer = uca_ring_buffer_new (512, 2);

    data = uca_ring_buffer_get_write_pointer (buffer);
    data[0] = 0xBADF00D;
    uca_ring_buffer_write_advance (buffer);

    g_assert (uca_ring_buffer_available (buffer));
    g_assert (uca_ring_buffer_get_num_blocks (buffer) == 1);

    data = uca_ring_buffer_get_write_pointer (buffer);
    data[0] = 0xDEADBEEF;
    uca_ring_buffer_write_advance (buffer);

    g_assert (uca_ring_buffer_get_num_blocks (buffer) == 2);

    data = uca_ring_buffer_get_read_pointer (buffer);
    g_assert (data[0] == 0xBADF00D);

    data = uca_ring_buffer_get_read_pointer (buffer);
    g_assert (data[0] == 0xDEADBEEF);

    g_assert (!uca_ring_buffer_available (buffer));

    uca_ring_buffer_reset (buffer);
    g_assert (uca_ring_buffer_get_num_blocks (buffer) == 0);
}

static void
test_overwrite (void)
{
    UcaRingBuffer *buffer;
    guint32 *data;

    buffer = uca_ring_buffer_new (512, 1);

    data = uca_ring_buffer_get_write_pointer (buffer);
    data[0] = 0xBADF00D;
    uca_ring_buffer_write_advance (buffer);

    data = uca_ring_buffer_get_write_pointer (buffer);
    data[0] = 0xDEADBEEF;
    uca_ring_buffer_write_advance (buffer);

    data = uca_ring_buffer_get_read_pointer (buffer);
    g_assert (data[0] == 0xDEADBEEF);
}

int
main (int argc, char *argv[])
{
#if !(GLIB_CHECK_VERSION (2, 36, 0))
    g_type_init ();
#endif

    g_test_init (&argc, &argv, NULL);

    g_test_add_func ("/ringbuffer/new/constructor", test_new_constructor);
    g_test_add_func ("/ringbuffer/new/func", test_new_func);
    g_test_add_func ("/ringbuffer/functionality ", test_ring);
    g_test_add_func ("/ringbuffer/overwrite ", test_overwrite);

    return g_test_run ();
}