]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
1f8031f7 DL |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
27 | ||
fadf00aa | 28 | #include <assert.h> |
74971473 JG |
29 | #include <CUnit/CUnit.h> |
30 | ||
31 | #include "pcep_utils_queue.h" | |
32 | #include "pcep_utils_queue_test.h" | |
33 | ||
34 | typedef struct node_data_ { | |
35 | int int_data; | |
36 | ||
37 | } node_data; | |
38 | ||
39 | ||
40 | void test_empty_queue() | |
41 | { | |
42 | queue_handle *handle = queue_initialize(); | |
43 | ||
44 | CU_ASSERT_PTR_NOT_NULL(handle); | |
fadf00aa | 45 | assert(handle != NULL); |
74971473 JG |
46 | CU_ASSERT_PTR_NULL(handle->head); |
47 | CU_ASSERT_EQUAL(handle->num_entries, 0); | |
48 | ||
49 | queue_destroy(handle); | |
50 | } | |
51 | ||
52 | ||
53 | void test_null_queue_handle() | |
54 | { | |
55 | /* test each method handles a NULL handle without crashing */ | |
56 | node_data data; | |
57 | queue_destroy(NULL); | |
58 | void *ptr = queue_enqueue(NULL, &data); | |
59 | CU_ASSERT_PTR_NULL(ptr); | |
60 | ||
61 | ptr = queue_dequeue(NULL); | |
62 | CU_ASSERT_PTR_NULL(ptr); | |
63 | } | |
64 | ||
65 | ||
66 | void test_enqueue() | |
67 | { | |
68 | node_data data1, data2, data3; | |
69 | data1.int_data = 1; | |
70 | data2.int_data = 2; | |
71 | data3.int_data = 3; | |
72 | ||
73 | queue_handle *handle = queue_initialize(); | |
74 | ||
75 | queue_enqueue(handle, &data1); | |
76 | queue_enqueue(handle, &data2); | |
77 | queue_enqueue(handle, &data3); | |
78 | ||
79 | CU_ASSERT_EQUAL(handle->num_entries, 3); | |
80 | ||
81 | queue_node *node = handle->head; | |
82 | CU_ASSERT_PTR_EQUAL(node->data, &data1); | |
83 | ||
84 | node = node->next_node; | |
85 | CU_ASSERT_PTR_EQUAL(node->data, &data2); | |
86 | ||
87 | node = node->next_node; | |
88 | CU_ASSERT_PTR_EQUAL(node->data, &data3); | |
89 | ||
90 | node = node->next_node; | |
91 | CU_ASSERT_PTR_NULL(node); | |
92 | ||
93 | queue_destroy(handle); | |
94 | } | |
95 | ||
96 | ||
97 | void test_enqueue_with_limit() | |
98 | { | |
99 | node_data data1, data2, data3; | |
100 | data1.int_data = 1; | |
101 | data2.int_data = 2; | |
102 | data3.int_data = 3; | |
103 | ||
104 | queue_handle *handle = queue_initialize_with_size(2); | |
105 | ||
106 | queue_node *node = queue_enqueue(handle, &data1); | |
107 | CU_ASSERT_PTR_NOT_NULL(node); | |
108 | ||
109 | node = queue_enqueue(handle, &data2); | |
110 | CU_ASSERT_PTR_NOT_NULL(node); | |
111 | ||
112 | node = queue_enqueue(handle, &data3); | |
113 | CU_ASSERT_PTR_NULL(node); | |
114 | ||
115 | CU_ASSERT_EQUAL(handle->num_entries, 2); | |
116 | ||
117 | node = handle->head; | |
118 | CU_ASSERT_PTR_EQUAL(node->data, &data1); | |
119 | ||
120 | node = node->next_node; | |
121 | CU_ASSERT_PTR_EQUAL(node->data, &data2); | |
122 | ||
123 | node = node->next_node; | |
124 | CU_ASSERT_PTR_NULL(node); | |
125 | ||
126 | queue_destroy(handle); | |
127 | } | |
128 | ||
129 | ||
130 | void test_dequeue() | |
131 | { | |
132 | node_data data1, data2, data3; | |
133 | data1.int_data = 1; | |
134 | data2.int_data = 2; | |
135 | data3.int_data = 3; | |
136 | ||
137 | queue_handle *handle = queue_initialize(); | |
138 | ||
139 | /* first test dequeue handles an empty queue */ | |
140 | void *node_data = queue_dequeue(handle); | |
141 | CU_ASSERT_PTR_NULL(node_data); | |
142 | ||
143 | queue_enqueue(handle, &data1); | |
144 | queue_enqueue(handle, &data2); | |
145 | queue_enqueue(handle, &data3); | |
146 | ||
147 | node_data = queue_dequeue(handle); | |
148 | CU_ASSERT_PTR_EQUAL(node_data, &data1); | |
149 | CU_ASSERT_EQUAL(handle->num_entries, 2); | |
150 | ||
151 | node_data = queue_dequeue(handle); | |
152 | CU_ASSERT_PTR_EQUAL(node_data, &data2); | |
153 | CU_ASSERT_EQUAL(handle->num_entries, 1); | |
154 | ||
155 | node_data = queue_dequeue(handle); | |
156 | CU_ASSERT_PTR_EQUAL(node_data, &data3); | |
157 | CU_ASSERT_EQUAL(handle->num_entries, 0); | |
158 | ||
159 | node_data = queue_dequeue(handle); | |
160 | CU_ASSERT_PTR_NULL(node_data); | |
161 | ||
162 | queue_destroy(handle); | |
163 | } |