Motr  M0
buf.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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/buf.h"
25 #include "lib/assert.h"
26 #include "lib/memory.h"
27 #include "lib/finject.h"
28 #include "lib/string.h" /* m0_streq, m0_strings_free */
29 #include "lib/errno.h" /* ENOENT */
30 
31 static void bufs_test(void);
32 static void buf_cmp_test(void);
33 
34 static bool bit_is_set(int bits, int index)
35 {
36  return (bool)(bits & (1 << index));
37 }
38 
40 {
41  struct m0_buf copy = M0_BUF_INIT0;
42  static int d0[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
43  static char *d1 = "1234567890";
44  static char *d2 = "123";
45  char *s;
46  bool equal;
47  int k;
48  int j;
49  int rc;
50  struct {
51  int equality_mask; /* equality to self is implied */
52  struct m0_buf buf;
53  } test[] = {
54  [0] = { (1 << 1), M0_BUF_INIT(strlen(d1), d1) },
55  [1] = { (1 << 0), M0_BUF_INITS(d1) },
56  [2] = { (1 << 4), M0_BUF_INITS(d2) },
57  [3] = { (1 << 6) | (1 << 7), M0_BUF_INIT(sizeof(d0), d0) },
58  [4] = { (1 << 2), M0_BUF_INIT(strlen(d2), d1) },
59  [5] = { 0, M0_BUF_INIT(sizeof(d0) - 1, d0) },
60 
61  /* [6] and [7] are placeholders and will be overwriten with
62  * m0_buf_init() */
63  [6] = { (1 << 3) | (1 << 7), M0_BUF_INIT0 },
64  [7] = { (1 << 3) | (1 << 6), M0_BUF_INIT0 },
65  };
66 
67 #ifdef ENABLE_FAULT_INJECTION
68  struct m0_buf inj_copy = M0_BUF_INIT0;
69  m0_fi_enable_once("m0_alloc", "fail_allocation");
70  rc = m0_buf_copy(&inj_copy, &test[0].buf);
71  M0_UT_ASSERT(rc == -ENOMEM);
72 #endif
73 
74  m0_buf_init(&test[6].buf, d0, sizeof(d0));
75  m0_buf_init(&test[7].buf, d0, sizeof(d0));
76 
77  for (k = 0; k < ARRAY_SIZE(test); ++k) {
78  rc = m0_buf_copy(&copy, &test[k].buf);
79  M0_UT_ASSERT(rc == 0);
81  m0_buf_free(&copy);
82  }
83 
84  for (k = 0; k < ARRAY_SIZE(test); ++k) {
85  for (j = 0; j < ARRAY_SIZE(test); ++j) {
86  if (j == k)
87  continue;
88  equal = m0_buf_eq(&test[j].buf, &test[k].buf);
89  M0_UT_ASSERT(equal == bit_is_set(test[j].equality_mask,
90  k));
91  }
92  }
93 
94  copy = M0_BUF_INIT(0, (void *)13);
98  s = m0_buf_strdup(&copy);
99  M0_UT_ASSERT(s != NULL && *s == '\0');
100  m0_free(s);
101 
102  bufs_test();
103  buf_cmp_test();
104 }
105 M0_EXPORTED(m0_ut_lib_buf_test);
106 
107 static void bufs_test(void)
108 {
109  struct m0_bufs bufs;
110  const char *strs[] = { "", "1", "two", NULL };
111  const char **strs_new = NULL;
112  int rc;
113 
114  rc = m0_bufs_from_strings(&bufs, strs);
115  M0_UT_ASSERT(rc == 0);
116  M0_UT_ASSERT(bufs.ab_count == 3);
117  M0_UT_ASSERT(m0_forall(i, ARRAY_SIZE(strs) - 1,
118  m0_buf_streq(&bufs.ab_elems[i], strs[i])));
119 
120  rc = m0_bufs_to_strings(&strs_new, &bufs);
121  M0_UT_ASSERT(m0_forall(i, ARRAY_SIZE(strs) - 1,
122  m0_streq(strs[i], strs_new[i]) &&
123  strs[i] != strs_new[i]));
124  M0_UT_ASSERT(strs_new[ARRAY_SIZE(strs) - 1] == NULL);
125 
126  M0_UT_ASSERT(m0_bufs_streq(&bufs, strs));
127  ++*(char *)bufs.ab_elems[2].b_addr;
128  M0_UT_ASSERT(!m0_bufs_streq(&bufs, strs));
129  --*(char *)bufs.ab_elems[2].b_addr;
130  M0_UT_ASSERT(m0_bufs_streq(&bufs, strs));
131 
132  m0_strings_free(strs_new);
133  m0_bufs_free(&bufs);
134  M0_UT_ASSERT(bufs.ab_count == 0 && bufs.ab_elems == NULL);
135 }
136 
137 static void buf_cmp_test(void)
138 {
139  struct m0_buf buf;
140 
143  &M0_BUF_INITS("equal")) == 0);
145  &M0_BUF_INITS("2")) < 0);
147  &M0_BUF_INITS("prefixsuffix")) < 0);
148  M0_UT_ASSERT(m0_buf_cmp(&M0_BUF_INITS("prefixsuffix"),
149  &M0_BUF_INITS("prefix")) > 0);
150 
151  buf = M0_BUF_INITS("single");
152  M0_UT_ASSERT(m0_buf_cmp(&buf, &buf) == 0);
153 }
154 
155 /*
156  * Local variables:
157  * c-indentation-style: "K&R"
158  * c-basic-offset: 8
159  * tab-width: 8
160  * fill-column: 80
161  * scroll-step: 1
162  * End:
163  */
struct m0_buf * ab_elems
Definition: buf.h:45
#define NULL
Definition: misc.h:38
void * b_addr
Definition: buf.h:39
static void buf_cmp_test(void)
Definition: buf.c:137
M0_INTERNAL bool m0_buf_eq(const struct m0_buf *x, const struct m0_buf *y)
Definition: buf.c:90
M0_INTERNAL bool m0_buf_streq(const struct m0_buf *buf, const char *str)
Definition: buf.c:132
static void bufs_test(void)
Definition: buf.c:107
void m0_ut_lib_buf_test(void)
Definition: buf.c:39
static struct m0t1fs_fsync_interactions copy
Definition: fsync.c:75
uint32_t ab_count
Definition: buf.h:44
M0_INTERNAL void m0_buf_init(struct m0_buf *buf, void *data, uint32_t nob)
Definition: buf.c:37
M0_INTERNAL int m0_buf_cmp(const struct m0_buf *x, const struct m0_buf *y)
Definition: buf.c:73
Definition: sock.c:887
Definition: buf.h:37
int i
Definition: dir.c:1033
M0_INTERNAL bool m0_bufs_streq(const struct m0_bufs *bufs, const char **strs)
Definition: buf.c:217
M0_INTERNAL void m0_bufs_free(struct m0_bufs *bufs)
Definition: buf.c:229
#define m0_streq(a, b)
Definition: string.h:34
#define M0_BUF_INIT0
Definition: buf.h:71
#define M0_BUF_INITS(str)
Definition: buf.h:70
M0_INTERNAL void m0_buf_free(struct m0_buf *buf)
Definition: buf.c:55
M0_INTERNAL int m0_buf_copy(struct m0_buf *dest, const struct m0_buf *src)
Definition: buf.c:104
#define m0_forall(var, nr,...)
Definition: misc.h:112
M0_INTERNAL char * m0_buf_strdup(const struct m0_buf *buf)
Definition: buf.c:140
M0_INTERNAL void m0_strings_free(const char **arr)
Definition: string.c:45
Definition: list.c:42
M0_INTERNAL int m0_bufs_to_strings(const char ***dest, const struct m0_bufs *src)
Definition: buf.c:188
static bool bit_is_set(int bits, int index)
Definition: buf.c:34
static void m0_fi_enable_once(const char *func, const char *tag)
Definition: finject.h:301
Definition: buf.h:43
void m0_free(void *data)
Definition: memory.c:146
static struct m0_addb2_source * s
Definition: consumer.c:39
#define M0_BUF_INIT(size, data)
Definition: buf.h:64
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL int m0_bufs_from_strings(struct m0_bufs *dest, const char **src)
Definition: buf.c:157
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46