Motr  M0
hash_fnc.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/vec.h" /* m0_bufvec */
24 #include "ut/ut.h" /* M0_UT_ASSERT() */
25 #include "lib/hash_fnc.h"
26 
27 enum {
28  KEY_COUNT = 100,
29  KEY_SIZE = 100
30 };
31 
32 void hash_city(void)
33 {
34  struct m0_bufvec vals;
35  struct m0_bufvec hash1;
36  struct m0_bufvec hash2;
37  int i;
38  int j;
39  int rc;
40 
42  M0_UT_ASSERT(rc == 0);
43  rc = m0_bufvec_alloc(&hash1, KEY_COUNT, sizeof(uint64_t));
44  M0_UT_ASSERT(rc == 0);
45  rc = m0_bufvec_alloc(&hash2, KEY_COUNT, sizeof(uint64_t));
46  M0_UT_ASSERT(rc == 0);
47 
48  for (i = 0; i < vals.ov_vec.v_nr; i++)
49  for (j = 0; j < KEY_SIZE; j++)
50  *(char*)((char*)(vals.ov_buf[i]) + j) = (j + i) & 0xff;
51  for (i = 0; i < vals.ov_vec.v_nr; i++)
52  *(uint64_t*)hash1.ov_buf[i] =
53  m0_hash_fnc_city(vals.ov_buf[i], vals.ov_vec.v_count[i]);
54  for (i = 0; i < vals.ov_vec.v_nr; i++)
55  *(uint64_t*)hash2.ov_buf[i] =
56  m0_hash_fnc_city(vals.ov_buf[i], vals.ov_vec.v_count[i]);
58  memcmp(hash1.ov_buf[i], hash2.ov_buf[i],
59  hash1.ov_vec.v_count[i]) == 0));
60  m0_bufvec_free(&vals);
61  m0_bufvec_free(&hash1);
62  m0_bufvec_free(&hash2);
63 }
64 
65 void hash_fnv1(void)
66 {
67  struct m0_bufvec vals;
68  struct m0_bufvec hash1;
69  struct m0_bufvec hash2;
70  int i;
71  int j;
72  int rc;
73 
75  M0_UT_ASSERT(rc == 0);
76  rc = m0_bufvec_alloc(&hash1, KEY_COUNT, sizeof(uint64_t));
77  M0_UT_ASSERT(rc == 0);
78  rc = m0_bufvec_alloc(&hash2, KEY_COUNT, sizeof(uint64_t));
79  M0_UT_ASSERT(rc == 0);
80 
81  for (i = 0; i < vals.ov_vec.v_nr; i++)
82  for (j = 0; j < KEY_SIZE; j++)
83  *(char*)((char*)(vals.ov_buf[i]) + j) = (j + i) & 0xff;
84  for (i = 0; i < vals.ov_vec.v_nr; i++)
85  *(uint64_t*)hash1.ov_buf[i] =
86  m0_hash_fnc_fnv1(vals.ov_buf[i], vals.ov_vec.v_count[i]);
87  for (i = 0; i < vals.ov_vec.v_nr; i++)
88  *(uint64_t*)hash2.ov_buf[i] =
89  m0_hash_fnc_fnv1(vals.ov_buf[i], vals.ov_vec.v_count[i]);
91  memcmp(hash1.ov_buf[i], hash2.ov_buf[i],
92  hash1.ov_vec.v_count[i]) == 0));
93  m0_bufvec_free(&vals);
94  m0_bufvec_free(&hash1);
95  m0_bufvec_free(&hash2);
96 }
97 
98 void test_hash_fnc(void)
99 {
100  hash_city();
101  hash_fnv1();
102 }
103 
104 /*
105  * Local variables:
106  * c-indentation-style: "K&R"
107  * c-basic-offset: 8
108  * tab-width: 8
109  * fill-column: 80
110  * scroll-step: 1
111  * End:
112  */
113 /*
114  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
115  */
struct m0_vec ov_vec
Definition: vec.h:147
void ** ov_buf
Definition: vec.h:149
M0_INTERNAL int m0_bufvec_alloc(struct m0_bufvec *bufvec, uint32_t num_segs, m0_bcount_t seg_size)
Definition: vec.c:220
M0_INTERNAL void m0_bufvec_free(struct m0_bufvec *bufvec)
Definition: vec.c:395
int i
Definition: dir.c:1033
void hash_city(void)
Definition: hash_fnc.c:32
M0_INTERNAL uint64_t m0_hash_fnc_fnv1(const void *buffer, m0_bcount_t len)
Definition: hash_fnc.c:84
M0_INTERNAL uint64_t m0_hash_fnc_city(const void *buffer, m0_bcount_t len)
Definition: hash_fnc.c:205
uint32_t v_nr
Definition: vec.h:51
m0_bcount_t * v_count
Definition: vec.h:53
#define m0_forall(var, nr,...)
Definition: misc.h:112
void test_hash_fnc(void)
Definition: hash_fnc.c:98
void hash_fnv1(void)
Definition: hash_fnc.c:65
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46
Definition: vec.h:145