Motr  M0
combinations.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-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/misc.h" /* ARRAY_SIZE */
24 #include "lib/combinations.h"
25 #include "ut/ut.h"
26 
28 {
29  enum { NR_COMBINATIONS = 35 };
30  int A[] = { 0, 1, 2, 3, 4, 5, 6 };
31  int X[] = { 2, 4, 6 };
32  int comb[] = { 0, 0, 0 };
33  int N = ARRAY_SIZE(A);
34  int K = ARRAY_SIZE(X);
35  int cid;
36  int i;
37 
38  M0_UT_ASSERT(m0_fact(K) == 6);
39  M0_UT_ASSERT(m0_fact(N) / (m0_fact(K) * m0_fact(N - K)) ==
40  m0_ncr(N, K));
41 
42  cid = m0_combination_index(N, K, X);
43  M0_UT_ASSERT(cid == 29);
44  m0_combination_inverse(cid, N, K, comb);
45  M0_UT_ASSERT(m0_forall(j, K, X[j] == comb[j]));
46 
47  M0_UT_ASSERT(m0_ncr(N, K) == NR_COMBINATIONS);
48  for (i = 0; i < NR_COMBINATIONS; i++) {
49  m0_combination_inverse(i, N, K, comb);
50  cid = m0_combination_index(N, K, comb);
51  M0_UT_ASSERT(cid == i);
52  }
53 }
54 M0_EXPORTED(test_combinations);
55 
56 /*
57  * Local variables:
58  * c-indentation-style: "K&R"
59  * c-basic-offset: 8
60  * tab-width: 8
61  * fill-column: 80
62  * scroll-step: 1
63  * End:
64  */
65 
M0_INTERNAL void m0_combination_inverse(int cid, int N, int K, int *x)
Definition: combinations.c:79
#define N(i)
int i
Definition: dir.c:1033
M0_INTERNAL int m0_combination_index(int N, int K, int *x)
Definition: combinations.c:52
M0_INTERNAL uint32_t m0_ncr(uint64_t n, uint64_t r)
Definition: combinations.c:38
void test_combinations(void)
Definition: combinations.c:27
#define X(type)
Definition: helpers.c:48
#define m0_forall(var, nr,...)
Definition: misc.h:112
Definition: module.c:67
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL uint64_t m0_fact(uint64_t n)
Definition: combinations.c:31