summaryrefslogtreecommitdiff
path: root/tacplus-daemon/test/queueTester.cpp
blob: a24818d4401619c0337236a06066852633cfc085 (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
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
/*
	Copyright (c) 2018 AT&T Intellectual Property.
	Copyright (c) 2015 Brocade Communications Systems, Inc.

	SPDX-License-Identifier: GPL-2.0-only
*/

#include "CppUTest/TestHarness.h"
extern "C" {
    #include "queue.h"
}

struct test_elem {
    int num;
    void *opaque;
};

TEST_GROUP(Queueing)
{
    Queue *q;

    void setup() {
        q = create_queue(NULL);
        CHECK(q);
    }

    void teardown (void) {
        destroy_queue(&q);
        POINTERS_EQUAL(NULL, q);
    }
};

TEST(Queueing, EnqueueSingle)
{
    struct test_elem *pair = (struct test_elem *)malloc(sizeof(struct test_elem));
    int empty;

    POINTERS_EQUAL(NULL, q->front);
    POINTERS_EQUAL(NULL, q->rear);

    enqueue(q, pair);

    CHECK(q->front != NULL);
    CHECK(q->rear != NULL);
    CHECK(q->front == q->rear);

    empty = is_queue_empty(q);
    LONGS_EQUAL(0, empty)

    /* memory pointed to by pair will be freed in teardown */
};

TEST(Queueing, DequeueEmpty)
{
    struct test_elem *pair_consumed;
    int empty;

    POINTERS_EQUAL(NULL, q->front);
    POINTERS_EQUAL(NULL, q->rear);

    empty = is_queue_empty(q);
    LONGS_EQUAL(1, empty);

    pair_consumed = (struct test_elem *)dequeue(q);
    POINTERS_EQUAL(NULL, pair_consumed);
    LONGS_EQUAL(1, empty);
};

TEST(Queueing, DequeueNonEmpty)
{
    struct test_elem *pair = (struct test_elem *)malloc(sizeof(struct test_elem));
    struct test_elem *pair_consumed;
    int empty;

    POINTERS_EQUAL(NULL, q->front);
    POINTERS_EQUAL(NULL, q->rear);

    enqueue(q, pair);

    CHECK(q->front != NULL);
    CHECK(q->rear != NULL);

    pair_consumed = (struct test_elem *)dequeue(q);
    POINTERS_EQUAL(pair, pair_consumed);

    empty = is_queue_empty(q);
    LONGS_EQUAL(1, empty)

    free(pair);
};

TEST(Queueing, DequeueMultiple)
{
    struct test_elem *pair1 = (struct test_elem *)malloc(sizeof(struct test_elem));
    struct test_elem *pair2 = (struct test_elem *)malloc(sizeof(struct test_elem));
    struct test_elem *pair3 = (struct test_elem *)malloc(sizeof(struct test_elem));
    struct test_elem *pair_consumed1;
    struct test_elem *pair_consumed2;
    struct test_elem *pair_consumed3;
    Node *rear;
    int empty;

    POINTERS_EQUAL(NULL, q->front);
    POINTERS_EQUAL(NULL, q->rear);

    rear = q->rear;
    enqueue(q, pair1);
    CHECK(q->rear != rear);

    CHECK(q->front != NULL);
    CHECK(q->rear != NULL);
    POINTERS_EQUAL(q->front, q->rear);

    rear = q->rear;
    enqueue(q, pair2);
    CHECK(q->rear != rear);

    CHECK(q->front != q->rear);

    rear = q->rear;
    enqueue(q, pair3);
    CHECK(q->rear != rear);

    CHECK(q->front != q->rear);

    LONGS_EQUAL(0, empty);

    pair_consumed1 = (struct test_elem *)dequeue(q);
    POINTERS_EQUAL(pair1, pair_consumed1);
    free(pair1);

    pair_consumed2 = (struct test_elem *)dequeue(q);
    POINTERS_EQUAL(pair2, pair_consumed2);
    free(pair2);

    pair_consumed3 = (struct test_elem *)dequeue(q);
    POINTERS_EQUAL(pair3, pair_consumed3);
    free(pair3);

    empty = is_queue_empty(q);
    LONGS_EQUAL(1, empty);
};