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