Motr  M0
misc.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 
29 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
30 #include "lib/trace.h"
31 
32 #include "ut/misc.h"
33 
34 #include "lib/arith.h" /* M0_SWAP */
35 #include "lib/misc.h" /* m0_reduce */
36 
37 M0_INTERNAL void m0_ut_random_shuffle(uint64_t *arr,
38  uint64_t nr,
39  uint64_t *seed)
40 {
41  uint64_t i;
42 
43  for (i = nr - 1; i > 0; --i)
44  M0_SWAP(arr[i], arr[m0_rnd64(seed) % (i + 1)]);
45 }
46 
47 M0_INTERNAL void m0_ut_random_arr_with_sum(uint64_t *arr,
48  uint64_t nr,
49  uint64_t sum,
50  uint64_t *seed)
51 {
52  uint64_t split;
53  uint64_t sum_split;
54 
55  M0_PRE(nr > 0);
56  if (nr == 1) {
57  arr[0] = sum;
58  } else {
59  split = m0_rnd64(seed) % (nr - 1) + 1;
60  sum_split = sum == 0 ? 0 : m0_rnd64(seed) % sum;
61  m0_ut_random_arr_with_sum(&arr[0], split, sum_split, seed);
63  nr - split, sum - sum_split, seed);
64  }
65  M0_POST_EX(m0_reduce(i, nr, 0, + arr[i]) == sum);
66 }
67 
68 
69 #undef M0_TRACE_SUBSYSTEM
70 
73 /*
74  * Local variables:
75  * c-indentation-style: "K&R"
76  * c-basic-offset: 8
77  * tab-width: 8
78  * fill-column: 80
79  * scroll-step: 1
80  * End:
81  */
82 /*
83  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
84  */
M0_INTERNAL void m0_ut_random_arr_with_sum(uint64_t *arr, uint64_t nr, uint64_t sum, uint64_t *seed)
Definition: misc.c:47
static size_t nr
Definition: dump.c:1505
#define M0_PRE(cond)
static void split(m0_bindex_t offset, int nr, bool commit)
Definition: extmap.c:230
static int sum
Definition: rwlock.c:53
#define M0_SWAP(v0, v1)
Definition: arith.h:207
int i
Definition: dir.c:1033
uint64_t m0_rnd64(uint64_t *prev)
Definition: misc.c:100
M0_INTERNAL void m0_ut_random_shuffle(uint64_t *arr, uint64_t nr, uint64_t *seed)
Definition: misc.c:37
#define M0_POST_EX(cond)