Motr  M0
fold.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 
23 #include "ut/ut.h"
24 #include "lib/arith.h" /* max64, min64 */
25 #include "lib/misc.h"
26 #include "lib/tlist.h"
27 
28 struct foo {
29  uint64_t f_val;
30  struct m0_tlink f_linkage;
31 };
32 
33 M0_TL_DESCR_DEFINE(foo, "fold-foo", static, struct foo, f_linkage, f_val, 0, 0);
34 M0_TL_DEFINE(foo, static, struct foo);
35 
36 void test_fold(void)
37 {
38  const int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
39  struct m0_tl head;
40  struct foo foos[ARRAY_SIZE(a)];
41  int x = 0;
42 
43  /* Test summation. */
44  M0_UT_ASSERT(m0_reduce(i, 4, 0, + a[i]) == a[0] + a[1] + a[2] + a[3]);
45  /* Test empty range. */
46  M0_UT_ASSERT(m0_reduce(i, 0, 8, + 1/x) == 8);
47  /* Gauss' childhood problem, as in popular sources. */
48  M0_UT_ASSERT(m0_reduce(i, 100, 0, + i + 1) == 5050);
49  /* Maximum. */
50  M0_UT_ASSERT(m0_fold(i, s, ARRAY_SIZE(a), -1, max64(s, a[i])) == 9);
51  /* Minimum. */
52  M0_UT_ASSERT(m0_fold(i, s, ARRAY_SIZE(a), 99, min64(s, a[i])) == 0);
53  /* Now, find the *index* of the maximum. */
54  M0_UT_ASSERT(m0_fold(i, s, ARRAY_SIZE(a), 0, a[i] > a[s] ? i : s) == 8);
55  M0_UT_ASSERT(m0_fold(i, s, ARRAY_SIZE(a), 0, a[i] < a[s] ? i : s) == 9);
56  foo_tlist_init(&head);
57  /* Check empty list. */
58  M0_UT_ASSERT(m0_tl_reduce(foo, f, &head, 8, + 1/x) == 8);
59  for (x = 0; x < ARRAY_SIZE(a); ++x) {
60  foo_tlink_init_at(&foos[x], &head);
61  foos[x].f_val = a[x];
62  }
63  /* Sums of squares are the same. */
64  M0_UT_ASSERT(m0_tl_reduce(foo, f, &head, 0, + f->f_val * f->f_val) ==
65  m0_reduce(i, ARRAY_SIZE(a), 0, + a[i] * a[i]));
66  /* Maximal element in the list has maximal value. */
67  M0_UT_ASSERT(m0_tl_fold(foo, f, s, &head, foo_tlist_head(&head),
68  f->f_val > s->f_val ? f : s)->f_val ==
69  m0_fold(i, s, ARRAY_SIZE(a), -1, max64(s, a[i])));
70 }
71 
72 /*
73  * Local variables:
74  * c-indentation-style: "K&R"
75  * c-basic-offset: 8
76  * tab-width: 8
77  * fill-column: 80
78  * scroll-step: 1
79  * End:
80  */
81 /*
82  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
83  */
M0_TL_DEFINE(foo, static, struct foo)
static bool x
Definition: sm.c:168
static FILE * f
Definition: adieu.c:79
Definition: bob.c:32
void test_fold(void)
Definition: fold.c:36
static struct foo foos[FOO_NR]
Definition: hash.c:56
static int head(struct m0_sm *mach)
Definition: sm.c:468
int i
Definition: dir.c:1033
static int64_t max64(int64_t a, int64_t b)
Definition: arith.h:51
Definition: tlist.h:251
struct m0_tlink f_linkage
Definition: bob.c:34
uint64_t f_val
Definition: fold.c:29
M0_TL_DESCR_DEFINE(foo, "fold-foo", static, struct foo, f_linkage, f_val, 0, 0)
static int64_t min64(int64_t a, int64_t b)
Definition: arith.h:46
static struct m0_addb2_source * s
Definition: consumer.c:39
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46