summaryrefslogtreecommitdiff
path: root/src/lib/tests/shm_rbuff_test.c
blob: df29d48fab59b4f820c3a2db1b99ee656e6a5b29 (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
105
/*
 * Ouroboros - Copyright (C) 2016 - 2019
 *
 * Test of the shm_rbuff
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200112L

#include "config.h"

#include <ouroboros/shm_rbuff.h>

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int shm_rbuff_test(int     argc,
                   char ** argv)
{
        struct shm_rbuff * rb;
        size_t             i;

        (void) argc;
        (void) argv;

        printf("Test: create rbuff...");

        rb = shm_rbuff_create(getpid(), 1);
        if (rb == NULL)
                goto err;

        printf("success.\n\n");
        printf("Test: write a value...");

        if (shm_rbuff_write(rb, 1) < 0)
                goto error;

        printf("success.\n\n");
        printf("Test: check queue length is 1...");

        if (shm_rbuff_queued(rb) != 1)
                goto error;

        printf("success.\n\n");
        printf("Test: read a value...");

        if (shm_rbuff_read(rb) != 1)
                goto error;

        printf("success.\n\n");
        printf("Test: check queue is empty...");

        if (shm_rbuff_read(rb) != -EAGAIN)
                goto error;

        printf("success.\n\n");
        printf("Test: fill the queue...");

        for (i = 0; i < SHM_RBUFF_SIZE - 1; ++i) {
                if (shm_rbuff_queued(rb) != i)
                        goto error;
                if (shm_rbuff_write(rb, 1) < 0)
                        goto error;
        }

        printf("success.\n\n");
        printf("Test: check queue is full...");

        if (shm_rbuff_queued(rb) != SHM_RBUFF_SIZE - 1)
                goto error;

        printf("success [%zd entries].\n\n", shm_rbuff_queued(rb));

        printf("Test: check queue is full by writing value...");
        if (!(shm_rbuff_write(rb, 1) < 0))
                goto error;

        printf("success [%zd entries].\n\n", shm_rbuff_queued(rb));

        shm_rbuff_destroy(rb);

        return 0;

 error:
        shm_rbuff_destroy(rb);
 err:
        printf("failed.\n\n");
        return -1;
}