Motr  M0
queue.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "ut/ut.h"
24 #include "lib/queue.h"
25 #include "lib/assert.h"
26 
27 struct qt {
29  int t_val;
30 };
31 
32 enum {
33  NR = 255
34 };
35 
36 void test_queue(void)
37 {
38  int i;
39  int sum0;
40  int sum1;
41 
42  struct m0_queue q;
43  static struct qt t[NR]; /* static to reduce kernel stack consumption. */
44 
45  for (sum0 = i = 0; i < ARRAY_SIZE(t); ++i) {
46  t[i].t_val = i;
47  sum0 += i;
48  };
49 
50  m0_queue_init(&q);
54 
55  for (i = 0; i < ARRAY_SIZE(t); ++i) {
56  m0_queue_put(&q, &t[i].t_linkage);
59  M0_UT_ASSERT(m0_queue_length(&q) == i + 1);
60  }
62 
63  for (i = 0; i < ARRAY_SIZE(t); ++i)
65 
66  for (sum1 = i = 0; i < ARRAY_SIZE(t); ++i) {
67  struct m0_queue_link *ql;
68  struct qt *qt;
69 
70  ql = m0_queue_get(&q);
71  M0_UT_ASSERT(ql != NULL);
72  qt = container_of(ql, struct qt, t_linkage);
73  M0_UT_ASSERT(&t[0] <= qt && qt < &t[NR]);
74  M0_UT_ASSERT(qt->t_val == i);
75  sum1 += qt->t_val;
76  }
77  M0_UT_ASSERT(sum0 == sum1);
81  for (i = 0; i < ARRAY_SIZE(t); ++i)
83 
84  m0_queue_fini(&q);
85 }
86 
87 
88 /*
89  * Local variables:
90  * c-indentation-style: "K&R"
91  * c-basic-offset: 8
92  * tab-width: 8
93  * fill-column: 80
94  * scroll-step: 1
95  * End:
96  */
Definition: queue.c:33
static struct m0_semaphore q
Definition: rwlock.c:55
#define NULL
Definition: misc.h:38
Definition: queue.h:43
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_INTERNAL bool m0_queue_is_empty(const struct m0_queue *q)
Definition: queue.c:65
int i
Definition: dir.c:1033
static struct m0_thread t[8]
Definition: service_ut.c:1230
struct m0_queue_link t_linkage
Definition: queue.c:28
M0_INTERNAL bool m0_queue_contains(const struct m0_queue *q, const struct m0_queue_link *ql)
Definition: queue.c:86
M0_INTERNAL struct m0_queue_link * m0_queue_get(struct m0_queue *q)
Definition: queue.c:112
M0_INTERNAL void m0_queue_put(struct m0_queue *q, struct m0_queue_link *ql)
Definition: queue.c:131
Definition: queue.c:27
M0_INTERNAL size_t m0_queue_length(const struct m0_queue *q)
Definition: queue.c:100
M0_INTERNAL void m0_queue_init(struct m0_queue *q)
Definition: queue.c:53
void test_queue(void)
Definition: queue.c:36
int t_val
Definition: queue.c:29
M0_INTERNAL void m0_queue_fini(struct m0_queue *q)
Definition: queue.c:59
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL bool m0_queue_link_is_in(const struct m0_queue_link *ql)
Definition: queue.c:81