grpc
test
core
gpr
murmur_hash_test.cc
Go to the documentation of this file.
1
/*
2
*
3
* Copyright 2015 gRPC authors.
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
*/
18
19
#include "
src/core/lib/gpr/murmur_hash.h
"
20
21
#include <
string.h
>
22
23
#include <
grpc/support/log.h
>
24
#include <
grpc/support/string_util.h
>
25
26
#include "
test/core/util/test_config.h
"
27
28
typedef
uint32_t
(*
hash_func
)(
const
void
*
key
,
size_t
len
,
uint32_t
seed
);
29
30
/* From smhasher:
31
This should hopefully be a thorough and uambiguous test of whether a hash
32
is correctly implemented on a given platform */
33
34
static
void
verification_test
(
hash_func
hash
,
uint32_t
expected) {
35
uint8_t
key
[256];
36
uint32_t
hashes[256];
37
uint32_t
final
= 0;
38
size_t
i
;
39
40
memset
(
key
, 0,
sizeof
(
key
));
41
memset
(hashes, 0,
sizeof
(hashes));
42
43
/* Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
44
the seed */
45
46
for
(
i
= 0;
i
< 256;
i
++) {
47
key
[
i
] =
static_cast<
uint8_t
>
(
i
);
48
hashes[
i
] =
hash
(
key
,
i
,
static_cast<
uint32_t
>
(256
u
-
i
));
49
}
50
51
/* Then hash the result array */
52
53
final
=
hash
(hashes,
sizeof
(hashes), 0);
54
55
/* The first four bytes of that hash, interpreted as a little-endian integer,
56
is our
57
verification value */
58
59
if
(expected !=
final
) {
60
gpr_log
(
GPR_INFO
,
"Verification value 0x%08X : Failed! (Expected 0x%08x)"
,
61
final
, expected);
62
abort();
63
}
else
{
64
gpr_log
(
GPR_INFO
,
"Verification value 0x%08X : Passed!"
,
final
);
65
}
66
}
67
68
int
main
(
int
argc,
char
** argv) {
69
grpc::testing::TestEnvironment
env
(&argc, argv);
70
/* basic tests to verify that things don't crash */
71
gpr_murmur_hash3
(
""
, 0, 0);
72
gpr_murmur_hash3
(
"xyz"
, 3, 0);
73
verification_test
(
gpr_murmur_hash3
, 0xB0F57EE3);
74
return
0;
75
}
GPR_INFO
#define GPR_INFO
Definition:
include/grpc/impl/codegen/log.h:56
log.h
generate.env
env
Definition:
generate.py:37
memset
return memset(p, 0, total)
string.h
seed
static const uint8_t seed[20]
Definition:
dsa_test.cc:79
u
OPENSSL_EXPORT pem_password_cb void * u
Definition:
pem.h:351
verification_test
static void verification_test(hash_func hash, uint32_t expected)
Definition:
murmur_hash_test.cc:34
uint8_t
unsigned char uint8_t
Definition:
stdint-msvc2008.h:78
hash
uint64_t hash
Definition:
ring_hash.cc:284
uint32_t
unsigned int uint32_t
Definition:
stdint-msvc2008.h:80
string_util.h
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
main
int main(int argc, char **argv)
Definition:
murmur_hash_test.cc:68
test_config.h
key
const char * key
Definition:
hpack_parser_table.cc:164
grpc::testing::TestEnvironment
Definition:
test/core/util/test_config.h:54
murmur_hash.h
len
int len
Definition:
abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
gpr_murmur_hash3
uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed)
Definition:
murmur_hash.cc:36
i
uint64_t i
Definition:
abseil-cpp/absl/container/btree_benchmark.cc:230
hash_func
uint32_t(* hash_func)(const void *key, size_t len, uint32_t seed)
Definition:
murmur_hash_test.cc:28
grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:30