Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALHashFuncTest.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Karl Wette
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20#include <stdlib.h>
21#include <stdint.h>
22#include <lal/LALHashFunc.h>
23
24static int test_XLALPearsonHash(size_t hval_len, const int N, const int nc_ref)
25{
26 int n = 0, nc = 0;
27 for (int i = 0; i < N; ++i) {
28 UINT8 hi = 0;
29 XLAL_CHECK(XLALPearsonHash(&hi, hval_len, &i, sizeof(i)) == XLAL_SUCCESS, XLAL_EFUNC);
30 for (int j = 0; j <= i; ++j) {
31 UINT8 hj = 0;
32 XLAL_CHECK(XLALPearsonHash(&hj, hval_len, &j, sizeof(j)) == XLAL_SUCCESS, XLAL_EFUNC);
33 XLAL_CHECK(i != j || hi == hj, XLAL_EFAILED);
34 ++n;
35 if (i != j && hi == hj) {
36 ++nc;
37 }
38 }
39 }
40 for (int i = 0; i < N; ++i) {
41 for (int j = 0; j <= i; ++j) {
42 UINT8 hij = 0, hji = 0;
43 XLAL_CHECK(XLALPearsonHash(&hij, hval_len, &i, sizeof(i)) == XLAL_SUCCESS, XLAL_EFUNC);
44 XLAL_CHECK(XLALPearsonHash(&hij, hval_len, &j, sizeof(j)) == XLAL_SUCCESS, XLAL_EFUNC);
45 XLAL_CHECK(XLALPearsonHash(&hji, hval_len, &j, sizeof(j)) == XLAL_SUCCESS, XLAL_EFUNC);
46 XLAL_CHECK(XLALPearsonHash(&hji, hval_len, &i, sizeof(i)) == XLAL_SUCCESS, XLAL_EFUNC);
47 XLAL_CHECK(i != j || hij == hji, XLAL_EFAILED);
48 ++n;
49 if (i != j && hij == hji) {
50 ++nc;
51 }
52 }
53 }
54 printf("XLALPearsonHash(hval=%zu) collisions in %i integer pairs: %i\n", hval_len, n, nc);
55 XLAL_CHECK(nc == nc_ref, XLAL_EFAILED, "nc = %i != %i = nc_ref", nc, nc_ref);
56 return XLAL_SUCCESS;
57}
58
59static int test_XLALCityHash(const char N, const int nc_ref)
60{
61 int n = 0, nc = 0;
62 char buf1[5] = {0}, buf2[5] = {0};
63 for (buf1[0] = 'a'; buf1[0] < 'a' + N; ++buf1[0]) {
64 for (buf1[1] = 'a'; buf1[1] < 'a' + N; ++buf1[1]) {
65 for (buf1[2] = 'a'; buf1[2] < 'a' + N; ++buf1[2]) {
66 for (buf1[3] = 'a'; buf1[3] < 'a' + N; ++buf1[3]) {
67 const UINT8 h1 = XLALCityHash64(buf1, strlen(buf1));
68 for (buf2[0] = 'a'; buf2[0] < 'a' + N; ++buf2[0]) {
69 for (buf2[1] = 'a'; buf2[1] < 'a' + N; ++buf2[1]) {
70 for (buf2[2] = 'a'; buf2[2] < 'a' + N; ++buf2[2]) {
71 for (buf2[3] = 'a'; buf2[3] < 'a' + N; ++buf2[3]) {
72 const UINT8 h2 = XLALCityHash64(buf2, strlen(buf2));
73 ++n;
74 if (strcmp(buf1, buf2) != 0 && h1 == h2) {
75 ++nc;
76 }
77 }
78 }
79 }
80 }
81 }
82 }
83 }
84 }
85 printf("XLALCityHash() collisions in %i strings with %i letters: %i\n", n, N, nc);
86 XLAL_CHECK(nc == nc_ref, XLAL_EFAILED, "nc = %i != %i = nc_ref", nc, nc_ref);
87 return XLAL_SUCCESS;
88}
89
90int main(void)
91{
92
93 /* Test XLALPearsonHash() */
97
98 /* Test XLALCityHash() */
101
102 /* Check for memory leaks */
104
105 return EXIT_SUCCESS;
106
107}
static int test_XLALPearsonHash(size_t hval_len, const int N, const int nc_ref)
int main(void)
static int test_XLALCityHash(const char N, const int nc_ref)
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
uint16_t UINT2
Two-byte unsigned integer.
uint32_t UINT4
Four-byte unsigned integer.
int XLALPearsonHash(void *hval, const size_t hval_len, const void *data, const size_t data_len)
Compute a arbitrary-sized Pearson hash value for the given arbitrary data.
UINT8 XLALCityHash64(const char *s, size_t len)
Hash function for a byte array.
Definition: LALCityHash.c:389
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
#define XLAL_CHECK_MAIN(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a C main() routine.
Definition: XLALError.h:885
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418