libamxc  1.10.3
C Generic Data Containers
test_amxc_hash_func.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <stdbool.h>
58 #include <string.h>
59 
60 #include <amxc/amxc_hash.h>
61 
62 #include "test_amxc_hash_func.h"
63 
64 #include <amxc/amxc_macros.h>
65 static void generate_string(char* str, size_t length) {
66  const char* base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
67  size_t len = strlen(base);
68  for(unsigned int i = 0; i < length; i++) {
69  str[i] = base[rand() % len];
70  }
71 }
72 
73 void amxc_hash_generation_check(UNUSED void** state) {
74  char* key[] = { "abcdefghijklmnopqrstuvwxyz1234567890",
75  "0987654321zyxwvutsrqponmlkjihgfedcba",
76  "Damien",
77  "Tom",
78  "Peter",
79  "Project Ambiorix",
80  "1234567890",
81  "abcdefghijklmnopqrstuvwxyz",
82  "!@#$%^&*()_",
83  "1+1=2",
84  NULL};
85 
86  for(int i = 0; key[i]; i++) {
87  printf("Key: %s\n", key[i]);
88  printf(" 1. RS-Hash Function Value: %u\n", amxc_RS_hash(key[i], strlen(key[i])));
89  printf(" 2. JS-Hash Function Value: %u\n", amxc_JS_hash(key[i], strlen(key[i])));
90  printf(" 3. PJW-Hash Function Value: %u\n", amxc_PJW_hash(key[i], strlen(key[i])));
91  printf(" 4. ELF-Hash Function Value: %u\n", amxc_ELF_hash(key[i], strlen(key[i])));
92  printf(" 5. BKDR-Hash Function Value: %u\n", amxc_BKDR_hash(key[i], strlen(key[i])));
93  printf(" 6. SDBM-Hash Function Value: %u\n", amxc_SDBM_hash(key[i], strlen(key[i])));
94  printf(" 7. DJB-Hash Function Value: %u\n", amxc_DJB_hash(key[i], strlen(key[i])));
95  printf(" 8. DEK-Hash Function Value: %u\n", amxc_DEK_hash(key[i], strlen(key[i])));
96  printf(" 9. BP-Hash Function Value: %u\n", amxc_BP_hash(key[i], strlen(key[i])));
97  printf("10. FNV-Hash Function Value: %u\n", amxc_FNV_hash(key[i], strlen(key[i])));
98  printf("11. AP-Hash Function Value: %u\n", amxc_AP_hash(key[i], strlen(key[i])));
99  }
100 }
101 
102 static unsigned int array[2006];
103 static bool first_double = false;
104 
106  memset(array, 0, sizeof(array));
107  srand(2013);
108  first_double = false;
109  printf("\n===========================================\n");
110 
111  return 0;
112 }
113 
115  unsigned int empty_buckets = 0;
116  unsigned int unique_buckets = 0;
117  unsigned int max_chain_length = 0;
118  unsigned int number_of_chains = 0;
119  unsigned int total_chain_lengths = 0;
120  for(int i = 0; i < 2006; i++) {
121  if(array[i] == 0) {
122  empty_buckets++;
123  }
124  if(array[i] > max_chain_length) {
125  max_chain_length = array[i];
126  }
127  if(array[i] > 1) {
128  number_of_chains++;
129  total_chain_lengths += array[i];
130  }
131  if(array[i] == 1) {
132  unique_buckets++;
133  }
134  }
135  printf("Nr. of empty buckets = %d\n", empty_buckets);
136  printf("Nr. of chained buckets = %d\n", number_of_chains);
137  printf("Nr. of uinque buckets = %d\n", unique_buckets);
138  printf("Total buckets = %d\n", empty_buckets + number_of_chains + unique_buckets);
139  printf("max chain length = %d\n", max_chain_length);
140  printf("average chain length = %f\n", (double) (total_chain_lengths / number_of_chains));
141  printf("===========================================\n");
142 
143  return 0;
144 }
145 
148  printf("RS hash algorithm\n");
149 
150  for(int i = 0; i < 2006; i++) {
151  char key[15];
152  generate_string(key, 15);
153  unsigned int hash = amxc_RS_hash(key, 15) % 2006;
154  if(array[hash] && !first_double) {
155  first_double = true;
156  printf("First double insertion = %d\n", i);
157  }
158  array[hash]++;
159  }
161 }
162 
165  printf("JS hash algorithm\n");
166 
167  for(int i = 0; i < 2006; i++) {
168  char key[15];
169  generate_string(key, 15);
170  unsigned int hash = amxc_JS_hash(key, 15) % 2006;
171  if(array[hash] && !first_double) {
172  first_double = true;
173  printf("First double insertion = %d\n", i);
174  }
175  array[hash]++;
176  }
178 }
179 
182  printf("PJW hash algorithm\n");
183 
184  for(int i = 0; i < 2006; i++) {
185  char key[15];
186  generate_string(key, 15);
187  unsigned int hash = amxc_PJW_hash(key, 15) % 2006;
188  if(array[hash] && !first_double) {
189  first_double = true;
190  printf("First double insertion = %d\n", i);
191  }
192  array[hash]++;
193  }
195 }
196 
199  printf("ELF hash algorithm\n");
200 
201  for(int i = 0; i < 2006; i++) {
202  char key[15];
203  generate_string(key, 15);
204  unsigned int hash = amxc_ELF_hash(key, 15) % 2006;
205  if(array[hash] && !first_double) {
206  first_double = true;
207  printf("First double insertion = %d\n", i);
208  }
209  array[hash]++;
210  }
212 }
213 
216  printf("BKDR hash algorithm\n");
217 
218  for(int i = 0; i < 2006; i++) {
219  char key[15];
220  generate_string(key, 15);
221  unsigned int hash = amxc_BKDR_hash(key, 15) % 2006;
222  if(array[hash] && !first_double) {
223  first_double = true;
224  printf("First double insertion = %d\n", i);
225  }
226  array[hash]++;
227  }
229 }
230 
233  printf("SBDM hash algorithm\n");
234 
235  for(int i = 0; i < 2006; i++) {
236  char key[15];
237  generate_string(key, 15);
238  unsigned int hash = amxc_SDBM_hash(key, 15) % 2006;
239  if(array[hash] && !first_double) {
240  first_double = true;
241  printf("First double insertion = %d\n", i);
242  }
243  array[hash]++;
244  }
246 }
247 
250  printf("DJB hash algorithm\n");
251 
252  for(int i = 0; i < 2006; i++) {
253  char key[15];
254  generate_string(key, 15);
255  unsigned int hash = amxc_DJB_hash(key, 15) % 2006;
256  if(array[hash] && !first_double) {
257  first_double = true;
258  printf("First double insertion = %d\n", i);
259  }
260  array[hash]++;
261  }
263 }
264 
267  printf("DEK hash algorithm\n");
268 
269  for(int i = 0; i < 2006; i++) {
270  char key[15];
271  generate_string(key, 15);
272  unsigned int hash = amxc_DEK_hash(key, 15) % 2006;
273  if(array[hash] && !first_double) {
274  first_double = true;
275  printf("First double insertion = %d\n", i);
276  }
277  array[hash]++;
278  }
280 }
281 
284  printf("BP hash algorithm\n");
285 
286  for(int i = 0; i < 2006; i++) {
287  char key[15];
288  generate_string(key, 15);
289  unsigned int hash = amxc_BP_hash(key, 15) % 2006;
290  if(array[hash] && !first_double) {
291  first_double = true;
292  printf("First double insertion = %d\n", i);
293  }
294  array[hash]++;
295  }
297 }
298 
301  printf("FNV hash algorithm\n");
302 
303  for(int i = 0; i < 2006; i++) {
304  char key[15];
305  generate_string(key, 15);
306  unsigned int hash = amxc_FNV_hash(key, 15) % 2006;
307  if(array[hash] && !first_double) {
308  first_double = true;
309  printf("First double insertion = %d\n", i);
310  }
311  array[hash]++;
312  }
314 }
315 
318  printf("AP hash algorithm\n");
319 
320  for(int i = 0; i < 2006; i++) {
321  char key[15];
322  generate_string(key, 15);
323  unsigned int hash = amxc_AP_hash(key, 15) % 2006;
324  if(array[hash] && !first_double) {
325  first_double = true;
326  printf("First double insertion = %d\n", i);
327  }
328  array[hash]++;
329  }
331 }
332 
334  printf("[Key1] = %d\n", amxc_BKDR_hash("Key1", 4) % 64);
335  printf("[Key2] = %d\n", amxc_BKDR_hash("Key2", 4) % 64);
336  printf("[Key3] = %d\n", amxc_BKDR_hash("Key3", 4) % 64);
337  printf("[Key4] = %d\n", amxc_BKDR_hash("Key4", 4) % 64);
338  printf("[Key5] = %d\n", amxc_BKDR_hash("Key5", 4) % 64);
339  printf("[Key6] = %d\n", amxc_BKDR_hash("Key6", 4) % 64);
340  printf("[Key7] = %d\n", amxc_BKDR_hash("Key7", 4) % 64);
341  printf("[Key8] = %d\n", amxc_BKDR_hash("Key8", 4) % 64);
342  printf("[Key9] = %d\n", amxc_BKDR_hash("Key9", 4) % 64);
343  printf("[Key11] = %d\n", amxc_BKDR_hash("Key11", 5) % 64);
344  printf("[Key21] = %d\n", amxc_BKDR_hash("Key21", 5) % 64);
345  printf("[Key31] = %d\n", amxc_BKDR_hash("Key31", 5) % 64);
346  printf("[Key41] = %d\n", amxc_BKDR_hash("Key41", 5) % 64);
347  printf("[Key51] = %d\n", amxc_BKDR_hash("Key51", 5) % 64);
348  printf("[Key61] = %d\n", amxc_BKDR_hash("Key61", 5) % 64);
349  printf("[Key71] = %d\n", amxc_BKDR_hash("Key71", 5) % 64);
350  printf("[Key81] = %d\n", amxc_BKDR_hash("Key81", 5) % 64);
351  printf("[Key91] = %d\n", amxc_BKDR_hash("Key91", 5) % 64);
352  printf("[Key110] = %d\n", amxc_BKDR_hash("Key110", 6) % 64);
353  printf("[Key210] = %d\n", amxc_BKDR_hash("Key210", 6) % 64);
354  printf("[Key310] = %d\n", amxc_BKDR_hash("Key310", 6) % 64);
355  printf("[Key410] = %d\n", amxc_BKDR_hash("Key410", 6) % 64);
356  printf("[Key510] = %d\n", amxc_BKDR_hash("Key510", 6) % 64);
357  printf("[Key610] = %d\n", amxc_BKDR_hash("Key610", 6) % 64);
358  printf("[Key710] = %d\n", amxc_BKDR_hash("Key710", 6) % 64);
359  printf("[Key810] = %d\n", amxc_BKDR_hash("Key810", 6) % 64);
360  printf("[Key910] = %d\n", amxc_BKDR_hash("Key910", 6) % 64);
361 }
Ambiorix string hash functions header file.
#define UNUSED
Definition: amxc_macros.h:70
unsigned int amxc_BKDR_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_ELF_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_DEK_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_FNV_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_BP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_RS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_SDBM_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_JS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_AP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_DJB_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_PJW_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
void amxc_hash_BKDR_hashes_check(UNUSED void **state)
static int amxc_hash_distribution_teardown()
void amxc_hash_distibution_PJW_check(UNUSED void **state)
void amxc_hash_distibution_ELF_check(UNUSED void **state)
void amxc_hash_distibution_SBDM_check(UNUSED void **state)
void amxc_hash_distibution_DJB_check(UNUSED void **state)
void amxc_hash_distibution_BP_check(UNUSED void **state)
static bool first_double
void amxc_hash_distibution_DEK_check(UNUSED void **state)
void amxc_hash_distibution_BKDR_check(UNUSED void **state)
static int amxc_hash_distribution_setup()
void amxc_hash_distibution_RS_check(UNUSED void **state)
static unsigned int array[2006]
void amxc_hash_generation_check(UNUSED void **state)
void amxc_hash_distibution_JS_check(UNUSED void **state)
void amxc_hash_distibution_FNV_check(UNUSED void **state)
void amxc_hash_distibution_AP_check(UNUSED void **state)
static void generate_string(char *str, size_t length)