Motr  M0
list.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 "lib/ub.h"
24 #include "ut/ut.h"
25 #include "lib/list.h"
26 
27 struct test1 {
29  int c;
30 };
31 
32 void test_list(void)
33 {
34  struct test1 t1, t2, t3;
35  struct m0_list_link *pos;
36  struct m0_list test_head;
37  struct test1 *p;
38  int t_sum;
39  int count;
40 
41  m0_list_init(&test_head);
42 
43  m0_list_link_init(&t1.t_link);
44  t1.c = 5;
45  m0_list_link_init(&t2.t_link);
46  t2.c = 10;
47  m0_list_link_init(&t3.t_link);
48  t3.c = 15;
49 
50  M0_UT_ASSERT(!m0_list_contains(&test_head, &t1.t_link));
51  M0_UT_ASSERT(!m0_list_contains(&test_head, &t2.t_link));
52  M0_UT_ASSERT(!m0_list_contains(&test_head, &t3.t_link));
53 
54  m0_list_add(&test_head, &t1.t_link);
55  m0_list_add_tail(&test_head, &t2.t_link);
56  m0_list_add(&test_head, &t3.t_link);
57 
58  M0_UT_ASSERT(m0_list_contains(&test_head, &t1.t_link));
59  M0_UT_ASSERT(m0_list_contains(&test_head, &t2.t_link));
60  M0_UT_ASSERT(m0_list_contains(&test_head, &t3.t_link));
61 
62  count = 0;
63  M0_UT_ASSERT(m0_list_forall(e, &test_head,
64  count++;
65  m0_list_entry(e, struct test1,
66  t_link)->c > 4));
67  M0_UT_ASSERT(count == 3);
68 
69  count = 0;
70  M0_UT_ASSERT(m0_list_entry_forall(e, &test_head, struct test1, t_link,
71  count++; e->c % 5 == 0));
72  M0_UT_ASSERT(count == 3);
73 
74  t_sum = 0;
75  count = 0;
76  M0_UT_ASSERT(m0_list_entry_forall(e, &test_head, struct test1, t_link,
77  count++; t_sum += e->c; e->c < 16));
78  M0_UT_ASSERT(t_sum == 30);
79  M0_UT_ASSERT(count == 3);
80 
81  count = 0;
82  m0_list_for_each(&test_head, pos) {
83  p = m0_list_entry(pos,struct test1, t_link);
84  M0_UT_ASSERT(p->c % 5 == 0);
85  count++;
86  }
87  M0_UT_ASSERT(count == 3);
88 
89  m0_list_del(&t2.t_link);
90 
91  M0_UT_ASSERT( m0_list_contains(&test_head, &t1.t_link));
92  M0_UT_ASSERT(!m0_list_contains(&test_head, &t2.t_link));
93  M0_UT_ASSERT( m0_list_contains(&test_head, &t3.t_link));
94 
95  count = 0;
96  t_sum = 0;
97  m0_list_for_each(&test_head, pos) {
98  p = m0_list_entry(pos,struct test1, t_link);
99  t_sum += p->c;
100  count++;
101  }
102  M0_UT_ASSERT(t_sum == 20);
103  M0_UT_ASSERT(count == 2);
104 
105  m0_list_del(&t1.t_link);
106 
107  M0_UT_ASSERT(!m0_list_contains(&test_head, &t1.t_link));
108  M0_UT_ASSERT(!m0_list_contains(&test_head, &t2.t_link));
109  M0_UT_ASSERT( m0_list_contains(&test_head, &t3.t_link));
110 
111  t_sum = 0;
112  count = 0;
113  m0_list_for_each_entry(&test_head, p, struct test1, t_link) {
114  t_sum += p->c;
115  count++;
116  }
117  M0_UT_ASSERT(t_sum == 15);
118  M0_UT_ASSERT(count == 1);
119 
120  m0_list_del(&t3.t_link);
121 
122  M0_UT_ASSERT(!m0_list_contains(&test_head, &t1.t_link));
123  M0_UT_ASSERT(!m0_list_contains(&test_head, &t2.t_link));
124  M0_UT_ASSERT(!m0_list_contains(&test_head, &t3.t_link));
125 
126  t_sum = 0;
127  count = 0;
128  m0_list_for_each_entry(&test_head, p, struct test1, t_link) {
129  t_sum += p->c;
130  count++;
131  }
132  M0_UT_ASSERT(t_sum == 0);
133  M0_UT_ASSERT(count == 0);
134 
135  M0_UT_ASSERT(m0_list_is_empty(&test_head));
136  m0_list_fini(&test_head);
137 }
138 
139 enum {
140  UB_ITER = 100000
141 };
142 
143 static struct test1 t[UB_ITER];
144 static struct m0_list list;
145 
146 static int ub_init(const char *opts M0_UNUSED)
147 {
148  int i;
149 
150  for (i = 0; i < ARRAY_SIZE(t); ++i)
152  m0_list_init(&list);
153  return 0;
154 }
155 
156 static void ub_fini(void)
157 {
158  int i;
159 
160  m0_list_fini(&list);
161  for (i = 0; i < ARRAY_SIZE(t); ++i)
163 }
164 
165 static void ub_insert(int i)
166 {
167  m0_list_add(&list, &t[i].t_link);
168 }
169 
170 static void ub_delete(int i)
171 {
172  m0_list_del(&t[i].t_link);
173 }
174 
176  .us_name = "list-ub",
177  .us_init = ub_init,
178  .us_fini = ub_fini,
179  .us_run = {
180  { .ub_name = "insert",
181  .ub_iter = UB_ITER,
182  .ub_round = ub_insert },
183 
184  { .ub_name = "delete",
185  .ub_iter = UB_ITER,
186  .ub_round = ub_delete },
187 
188  { .ub_name = NULL }
189  }
190 };
191 
192 /*
193  * Local variables:
194  * c-indentation-style: "K&R"
195  * c-basic-offset: 8
196  * tab-width: 8
197  * fill-column: 80
198  * scroll-step: 1
199  * End:
200  */
M0_INTERNAL void m0_list_link_fini(struct m0_list_link *link)
Definition: list.c:176
static struct m0_addb2_philter p
Definition: consumer.c:40
M0_INTERNAL void m0_list_add(struct m0_list *head, struct m0_list_link *new)
Definition: list.c:113
static struct m0_list list
Definition: list.c:144
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_list_init(struct m0_list *head)
Definition: list.c:29
void test_list(void)
Definition: list.c:32
static void ub_delete(int i)
Definition: list.c:170
M0_INTERNAL void m0_list_fini(struct m0_list *head)
Definition: list.c:36
M0_INTERNAL void m0_list_del(struct m0_list_link *old)
Definition: list.c:147
Definition: list.c:27
static int ub_init(const char *opts M0_UNUSED)
Definition: list.c:146
static m0_bcount_t count
Definition: xcode.c:167
static void t2(int n)
Definition: thread.c:48
int i
Definition: dir.c:1033
static void ub_fini(void)
Definition: list.c:156
#define m0_list_for_each(head, pos)
Definition: list.h:226
#define m0_list_for_each_entry(head, pos, type, member)
Definition: list.h:235
const char * us_name
Definition: ub.h:76
struct m0_ub_set m0_list_ub
Definition: list.c:175
M0_INTERNAL bool m0_list_contains(const struct m0_list *list, const struct m0_list_link *link)
Definition: list.c:87
#define m0_list_entry_forall(var, head, type, member,...)
Definition: list.h:313
M0_INTERNAL bool m0_list_is_empty(const struct m0_list *head)
Definition: list.c:42
static void ub_insert(int i)
Definition: list.c:165
Definition: list.h:72
int c
Definition: list.c:29
struct m0_list_link t_link
Definition: tlist.h:293
struct m0_list_link t_link
Definition: list.c:28
static void t1(int n)
Definition: mutex.c:48
static struct test1 t[UB_ITER]
Definition: list.c:143
M0_INTERNAL void m0_list_link_init(struct m0_list_link *link)
Definition: list.c:169
M0_INTERNAL void m0_list_add_tail(struct m0_list *head, struct m0_list_link *new)
Definition: list.c:119
static void t3(int n)
Definition: thread.c:59
#define m0_list_forall(var, head,...)
Definition: list.h:279
#define m0_list_entry(link, type, member)
Definition: list.h:217
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: ub.h:74
#define M0_UT_ASSERT(a)
Definition: ut.h:46
Definition: list.c:140
#define M0_UNUSED
Definition: misc.h:380