]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/test/pcep_utils_queue_test.c
2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
29 #include <CUnit/CUnit.h>
31 #include "pcep_utils_queue.h"
32 #include "pcep_utils_queue_test.h"
34 typedef struct node_data_
{
40 void test_empty_queue()
42 queue_handle
*handle
= queue_initialize();
44 CU_ASSERT_PTR_NOT_NULL(handle
);
45 assert(handle
!= NULL
);
46 CU_ASSERT_PTR_NULL(handle
->head
);
47 CU_ASSERT_EQUAL(handle
->num_entries
, 0);
49 queue_destroy(handle
);
53 void test_null_queue_handle()
55 /* test each method handles a NULL handle without crashing */
58 void *ptr
= queue_enqueue(NULL
, &data
);
59 CU_ASSERT_PTR_NULL(ptr
);
61 ptr
= queue_dequeue(NULL
);
62 CU_ASSERT_PTR_NULL(ptr
);
68 node_data data1
, data2
, data3
;
73 queue_handle
*handle
= queue_initialize();
75 queue_enqueue(handle
, &data1
);
76 queue_enqueue(handle
, &data2
);
77 queue_enqueue(handle
, &data3
);
79 CU_ASSERT_EQUAL(handle
->num_entries
, 3);
81 queue_node
*node
= handle
->head
;
82 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
84 node
= node
->next_node
;
85 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
87 node
= node
->next_node
;
88 CU_ASSERT_PTR_EQUAL(node
->data
, &data3
);
90 node
= node
->next_node
;
91 CU_ASSERT_PTR_NULL(node
);
93 queue_destroy(handle
);
97 void test_enqueue_with_limit()
99 node_data data1
, data2
, data3
;
104 queue_handle
*handle
= queue_initialize_with_size(2);
106 queue_node
*node
= queue_enqueue(handle
, &data1
);
107 CU_ASSERT_PTR_NOT_NULL(node
);
109 node
= queue_enqueue(handle
, &data2
);
110 CU_ASSERT_PTR_NOT_NULL(node
);
112 node
= queue_enqueue(handle
, &data3
);
113 CU_ASSERT_PTR_NULL(node
);
115 CU_ASSERT_EQUAL(handle
->num_entries
, 2);
118 CU_ASSERT_PTR_EQUAL(node
->data
, &data1
);
120 node
= node
->next_node
;
121 CU_ASSERT_PTR_EQUAL(node
->data
, &data2
);
123 node
= node
->next_node
;
124 CU_ASSERT_PTR_NULL(node
);
126 queue_destroy(handle
);
132 node_data data1
, data2
, data3
;
137 queue_handle
*handle
= queue_initialize();
139 /* first test dequeue handles an empty queue */
140 void *node_data
= queue_dequeue(handle
);
141 CU_ASSERT_PTR_NULL(node_data
);
143 queue_enqueue(handle
, &data1
);
144 queue_enqueue(handle
, &data2
);
145 queue_enqueue(handle
, &data3
);
147 node_data
= queue_dequeue(handle
);
148 CU_ASSERT_PTR_EQUAL(node_data
, &data1
);
149 CU_ASSERT_EQUAL(handle
->num_entries
, 2);
151 node_data
= queue_dequeue(handle
);
152 CU_ASSERT_PTR_EQUAL(node_data
, &data2
);
153 CU_ASSERT_EQUAL(handle
->num_entries
, 1);
155 node_data
= queue_dequeue(handle
);
156 CU_ASSERT_PTR_EQUAL(node_data
, &data3
);
157 CU_ASSERT_EQUAL(handle
->num_entries
, 0);
159 node_data
= queue_dequeue(handle
);
160 CU_ASSERT_PTR_NULL(node_data
);
162 queue_destroy(handle
);