Blame view

RIOT/tests/unittests/tests-priority_pktqueue/tests-priority_pktqueue.c 9.62 KB
fb11e647   vrobic   reseau statique a...
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  /*
   * Copyright (C) 2016, 2016 Shuguo Zhuo <shuguo.zhuo@inria.fr>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   */
  #include <string.h>
  
  #include "embUnit.h"
  
  #include "net/gnrc/pkt.h"
  #include "net/gnrc/priority_pktqueue.h"
  
  #include "unittests-constants.h"
  #include "tests-priority_pktqueue.h"
  
  #define PKT_INIT_ELEM(len, data, next) \
      { 1, (next), (data), (len), GNRC_NETTYPE_UNDEF }
  #define PKT_INIT_ELEM_STATIC_DATA(data, next) PKT_INIT_ELEM(sizeof(data), data, next)
  #define PKTQUEUE_INIT_ELEM(pkt) { NULL, pkt }
  
  static gnrc_priority_pktqueue_t pkt_queue;
  
  static void set_up(void)
  {
      pkt_queue.first = NULL;
  }
  
  static void test_gnrc_priority_pktqueue_init(void)
  {
      gnrc_priority_pktqueue_node_t elem;
  
      pkt_queue.first = (priority_queue_node_t *)(&elem);
      gnrc_priority_pktqueue_init(&pkt_queue);
  
      TEST_ASSERT_NULL(pkt_queue.first);
  }
  
  static void test_gnrc_priority_pktqueue_node_init(void)
  {
      gnrc_pktsnip_t pkt = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_priority_pktqueue_node_t elem;
  
      gnrc_priority_pktqueue_node_init(&elem,TEST_UINT32,&pkt);
  
      TEST_ASSERT_NULL(elem.next);
      TEST_ASSERT(elem.pkt == &pkt);
      TEST_ASSERT_EQUAL_INT(TEST_UINT32, elem.priority);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, elem.pkt->data);
  }
  
  static void test_gnrc_priority_pktqueue_push_one(void)
  {
      gnrc_pktsnip_t pkt = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_priority_pktqueue_node_t elem = PRIORITY_PKTQUEUE_NODE_INIT(TEST_UINT32,&pkt);
  
      gnrc_priority_pktqueue_push(&pkt_queue,&elem);
  
      TEST_ASSERT((gnrc_priority_pktqueue_node_t *)(pkt_queue.first) == &elem);
      TEST_ASSERT_NULL(((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next);
      TEST_ASSERT_EQUAL_INT(TEST_UINT32, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->priority);
      TEST_ASSERT_EQUAL_INT(1, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->users);
      TEST_ASSERT_NULL(((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->next);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->data);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->size);
      TEST_ASSERT_EQUAL_INT(GNRC_NETTYPE_UNDEF, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->type);
  }
  
  static void test_gnrc_priority_pktqueue_push_two(void)
  {
      gnrc_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
      gnrc_priority_pktqueue_node_t elem1 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt1);
      gnrc_priority_pktqueue_node_t elem2 = PRIORITY_PKTQUEUE_NODE_INIT(0,&pkt2);
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem1);
      gnrc_priority_pktqueue_push(&pkt_queue, &elem2);
  
      TEST_ASSERT((gnrc_priority_pktqueue_node_t *)(pkt_queue.first) == &elem2);
      TEST_ASSERT((gnrc_priority_pktqueue_node_t *)(pkt_queue.first->next) == &elem1);
      TEST_ASSERT_NULL(((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->next);
      TEST_ASSERT_EQUAL_INT(0, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->priority);
      TEST_ASSERT_EQUAL_INT(1, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->priority);
      TEST_ASSERT_EQUAL_INT(1, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->users);
      TEST_ASSERT_NULL(((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->next);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->data);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16), ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->size);
      TEST_ASSERT_EQUAL_INT(GNRC_NETTYPE_UNDEF, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->pkt->type);
      TEST_ASSERT_EQUAL_INT(1, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->pkt->users);
      TEST_ASSERT_NULL(((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->pkt->next);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->pkt->data);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->pkt->size);
      TEST_ASSERT_EQUAL_INT(GNRC_NETTYPE_UNDEF, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->next->pkt->type);
  }
  
  static void test_gnrc_priority_pktqueue_length(void)
  {
      gnrc_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
      gnrc_priority_pktqueue_node_t elem1 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt1);
      gnrc_priority_pktqueue_node_t elem2 = PRIORITY_PKTQUEUE_NODE_INIT(0,&pkt2);
  
      TEST_ASSERT_EQUAL_INT(0, gnrc_priority_pktqueue_length(&pkt_queue));
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem1);
      TEST_ASSERT_EQUAL_INT(1, gnrc_priority_pktqueue_length(&pkt_queue));
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem2);
      TEST_ASSERT_EQUAL_INT(2, gnrc_priority_pktqueue_length(&pkt_queue));
  }
  
  static void test_gnrc_priority_pktqueue_flush(void)
  {
      gnrc_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
      gnrc_priority_pktqueue_node_t elem1 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt1);
      gnrc_priority_pktqueue_node_t elem2 = PRIORITY_PKTQUEUE_NODE_INIT(0,&pkt2);
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem1);
      gnrc_priority_pktqueue_push(&pkt_queue, &elem2);
  
      gnrc_priority_pktqueue_flush(&pkt_queue);
  
      TEST_ASSERT_NULL(pkt_queue.first);
      TEST_ASSERT_EQUAL_INT(0, gnrc_priority_pktqueue_length(&pkt_queue));
  }
  
  static void test_gnrc_priority_pktqueue_head(void)
  {
      gnrc_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING12, NULL);
      gnrc_pktsnip_t pkt3 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
      gnrc_priority_pktqueue_node_t elem1 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt1);
      gnrc_priority_pktqueue_node_t elem2 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt2);
      gnrc_priority_pktqueue_node_t elem3 = PRIORITY_PKTQUEUE_NODE_INIT(0,&pkt3);
  
      gnrc_pktsnip_t *head = gnrc_priority_pktqueue_head(&pkt_queue);
      TEST_ASSERT_NULL(head);
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem1);
      head = gnrc_priority_pktqueue_head(&pkt_queue);
      TEST_ASSERT(head == &pkt1);
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem2);
      head = gnrc_priority_pktqueue_head(&pkt_queue);
  
      TEST_ASSERT(head == &pkt1);
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem3);
      head = gnrc_priority_pktqueue_head(&pkt_queue);
  
      TEST_ASSERT(head == &pkt3);
  
      gnrc_priority_pktqueue_flush(&pkt_queue);
  
      head = gnrc_priority_pktqueue_head(&pkt_queue);
      TEST_ASSERT_NULL(head);
  
  }
  
  static void test_gnrc_priority_pktqueue_pop_empty(void)
  {
      gnrc_pktsnip_t *res;
  
      res = gnrc_priority_pktqueue_pop(&pkt_queue);
  
      TEST_ASSERT_NULL(pkt_queue.first);
      TEST_ASSERT_NULL(res);
      TEST_ASSERT_EQUAL_INT(0, gnrc_priority_pktqueue_length(&pkt_queue));
  }
  
  
  static void test_gnrc_priority_pktqueue_pop(void)
  {
      gnrc_pktsnip_t pkt1 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING8, NULL);
      gnrc_pktsnip_t pkt2 = PKT_INIT_ELEM_STATIC_DATA(TEST_STRING16, NULL);
      gnrc_priority_pktqueue_node_t elem1 = PRIORITY_PKTQUEUE_NODE_INIT(1,&pkt1);
      gnrc_priority_pktqueue_node_t elem2 = PRIORITY_PKTQUEUE_NODE_INIT(0,&pkt2);
      gnrc_pktsnip_t *res;
      gnrc_pktsnip_t *head;
  
      gnrc_priority_pktqueue_push(&pkt_queue, &elem1);
      gnrc_priority_pktqueue_push(&pkt_queue, &elem2);
  
      res = gnrc_priority_pktqueue_pop(&pkt_queue);
  
      TEST_ASSERT(res == &pkt2);
      TEST_ASSERT_NULL(elem2.pkt);
      TEST_ASSERT_NULL(elem2.next);
      TEST_ASSERT_EQUAL_INT(0,elem2.priority);
      TEST_ASSERT((gnrc_priority_pktqueue_node_t *)(pkt_queue.first) == &elem1);
      TEST_ASSERT_EQUAL_INT(1, ((gnrc_priority_pktqueue_node_t *)(pkt_queue.first))->priority);
      TEST_ASSERT_EQUAL_INT(1, res->users);
      TEST_ASSERT_NULL(res->next);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING16, res->data);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING16), res->size);
      TEST_ASSERT_EQUAL_INT(GNRC_NETTYPE_UNDEF, res->type);
  
      res = gnrc_priority_pktqueue_pop(&pkt_queue);
  
      TEST_ASSERT_NULL(pkt_queue.first);
      TEST_ASSERT_NULL(elem1.pkt);
      TEST_ASSERT_NULL(elem1.next);
      TEST_ASSERT_EQUAL_INT(0,elem1.priority);
      TEST_ASSERT(res == &pkt1);
      TEST_ASSERT_EQUAL_INT(1, res->users);
      TEST_ASSERT_NULL(res->next);
      TEST_ASSERT_EQUAL_STRING(TEST_STRING8, res->data);
      TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), res->size);
      TEST_ASSERT_EQUAL_INT(GNRC_NETTYPE_UNDEF, res->type);
  
      head = gnrc_priority_pktqueue_head(&pkt_queue);
  
      TEST_ASSERT_NULL(pkt_queue.first);
      TEST_ASSERT_NULL(head);
  
  }
  
  Test *tests_priority_pktqueue_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_gnrc_priority_pktqueue_init),
          new_TestFixture(test_gnrc_priority_pktqueue_node_init),
          new_TestFixture(test_gnrc_priority_pktqueue_push_one),
          new_TestFixture(test_gnrc_priority_pktqueue_push_two),
          new_TestFixture(test_gnrc_priority_pktqueue_length),
          new_TestFixture(test_gnrc_priority_pktqueue_flush),
          new_TestFixture(test_gnrc_priority_pktqueue_head),
          new_TestFixture(test_gnrc_priority_pktqueue_pop_empty),
          new_TestFixture(test_gnrc_priority_pktqueue_pop),
      };
  
      EMB_UNIT_TESTCALLER(priority_pktqueue_tests, set_up, NULL, fixtures);
  
      return (Test *)&priority_pktqueue_tests;
  }
  
  void tests_priority_pktqueue(void)
  {
      TESTS_RUN(tests_priority_pktqueue_tests());
  }
  /** @} */