Motr  M0
bob.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/tlist.h"
26 #include "lib/bob.h"
27 
28 enum {
29  N = 256
30 };
31 
32 struct foo {
33  void *f_payload;
35  char f_x[7];
36  uint64_t f_magix;
37 };
38 
39 enum {
40  magix = 0xbeda551edcaca0ffULL
41 };
42 
43 M0_TL_DESCR_DEFINE(foo, "foo-s", static, struct foo, f_linkage,
44  f_magix, magix, 0);
45 M0_TL_DEFINE(foo, static, struct foo);
46 
47 static struct m0_bob_type foo_bob;
48 static struct foo F;
49 static struct foo rank[N];
50 
51 M0_BOB_DEFINE(static, &foo_bob, foo);
52 
53 static void test_tlist_init(void)
54 {
55  M0_SET0(&foo_bob);
57  M0_UT_ASSERT(!strcmp(foo_bob.bt_name, foo_tl.td_name));
59  M0_UT_ASSERT(foo_bob.bt_magix_offset == foo_tl.td_link_magic_offset);
60 }
61 
62 static void test_bob_init(void)
63 {
64  foo_bob_init(&F);
66  M0_UT_ASSERT(foo_bob_check(&F));
67 }
68 
69 static void test_bob_fini(void)
70 {
71  foo_bob_fini(&F);
72  M0_UT_ASSERT(F.f_magix == 0);
73  M0_UT_ASSERT(!foo_bob_check(&F));
74 }
75 
76 static void test_tlink_init(void)
77 {
78  foo_tlink_init(&F);
79  M0_UT_ASSERT(foo_bob_check(&F));
80 }
81 
82 static void test_tlink_fini(void)
83 {
84  foo_tlink_fini(&F);
85  M0_UT_ASSERT(foo_bob_check(&F));
86  F.f_magix = 0;
87  M0_UT_ASSERT(!foo_bob_check(&F));
88 }
89 
90 static bool foo_check(const void *bob)
91 {
92  const struct foo *f = bob;
93 
94  return f->f_payload == f + 1;
95 }
96 
97 static void test_check(void)
98 {
99  int i;
100 
102 
103  for (i = 0; i < N; ++i) {
104  foo_bob_init(&rank[i]);
105  rank[i].f_payload = rank + i + 1;
106  }
107 
108  for (i = 0; i < N; ++i)
109  M0_UT_ASSERT(foo_bob_check(&rank[i]));
110 
111  for (i = 0; i < N; ++i)
112  foo_bob_fini(&rank[i]);
113 
114  for (i = 0; i < N; ++i)
115  M0_UT_ASSERT(!foo_bob_check(&rank[i]));
116 
117 }
118 
119 static void test_bob_of(void)
120 {
121  void *p;
122  int i;
123 
124  foo_bob_init(&F);
125  for (i = -1; i < ARRAY_SIZE(F.f_x) + 3; ++i) {
126  p = &F.f_x[i];
127  M0_UT_ASSERT(bob_of(p, struct foo, f_x[i], &foo_bob) == &F);
128  }
129 }
130 
131 void test_bob(void)
132 {
133  test_tlist_init();
134  test_bob_init();
135  test_bob_fini();
136  test_tlink_init();
137  test_tlink_fini();
138  test_check();
139  test_bob_of();
140  /*
141  * Some of the above tests make an unsuccessful m0_bob_check(),
142  * setting m0_failed_condition. Don't let m0_panic() use it.
143  */
145 }
146 M0_EXPORTED(test_bob);
147 
148 /*
149  * Local variables:
150  * c-indentation-style: "K&R"
151  * c-basic-offset: 8
152  * tab-width: 8
153  * fill-column: 80
154  * scroll-step: 1
155  * End:
156  */
char f_x[7]
Definition: bob.c:35
static struct m0_addb2_philter p
Definition: consumer.c:40
#define NULL
Definition: misc.h:38
M0_TL_DEFINE(foo, static, struct foo)
static FILE * f
Definition: adieu.c:79
Definition: bob.c:32
void * f_payload
Definition: bob.c:33
M0_INTERNAL const char * m0_failed_condition
Definition: misc.c:224
uint64_t f_magix
Definition: bob.c:36
#define M0_SET0(obj)
Definition: misc.h:64
uint64_t bt_magix
Definition: bob.h:77
const char * bt_name
Definition: bob.h:73
static struct m0_bob_type foo_bob
Definition: bob.c:47
int bt_magix_offset
Definition: bob.h:75
static void test_tlink_fini(void)
Definition: bob.c:82
int i
Definition: dir.c:1033
void test_bob(void)
Definition: bob.c:131
static struct foo F
Definition: bob.c:48
static void test_tlink_init(void)
Definition: bob.c:76
static void test_check(void)
Definition: bob.c:97
struct m0_tlink f_linkage
Definition: bob.c:34
#define bob_of(ptr, type, field, bt)
Definition: bob.h:140
Definition: bob.c:40
static void test_bob_fini(void)
Definition: bob.c:69
static void test_tlist_init(void)
Definition: bob.c:53
M0_TL_DESCR_DEFINE(foo, "foo-s", static, struct foo, f_linkage, f_magix, magix, 0)
static bool foo_check(const void *bob)
Definition: bob.c:90
static void test_bob_of(void)
Definition: bob.c:119
static struct foo rank[N]
Definition: bob.c:49
bool(* bt_check)(const void *bob)
Definition: bob.h:82
M0_INTERNAL void m0_bob_type_tlist_init(struct m0_bob_type *bt, const struct m0_tl_descr *td)
Definition: bob.c:41
#define ARRAY_SIZE(a)
Definition: misc.h:45
Definition: bob.c:29
uint64_t f_x
Definition: xcode.c:71
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void test_bob_init(void)
Definition: bob.c:62
M0_BOB_DEFINE(static, &foo_bob, foo)