libamxc  1.10.3
C Generic Data Containers
test_amxc_hash_func.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <amxc/amxc_hash.h>
#include "test_amxc_hash_func.h"
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

static void generate_string (char *str, size_t length)
 
void amxc_hash_generation_check (UNUSED void **state)
 
static int amxc_hash_distribution_setup ()
 
static int amxc_hash_distribution_teardown ()
 
void amxc_hash_distibution_RS_check (UNUSED void **state)
 
void amxc_hash_distibution_JS_check (UNUSED void **state)
 
void amxc_hash_distibution_PJW_check (UNUSED void **state)
 
void amxc_hash_distibution_ELF_check (UNUSED void **state)
 
void amxc_hash_distibution_BKDR_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_DEK_check (UNUSED void **state)
 
void amxc_hash_distibution_BP_check (UNUSED void **state)
 
void amxc_hash_distibution_FNV_check (UNUSED void **state)
 
void amxc_hash_distibution_AP_check (UNUSED void **state)
 
void amxc_hash_BKDR_hashes_check (UNUSED void **state)
 

Variables

static unsigned int array [2006]
 
static bool first_double = false
 

Function Documentation

◆ amxc_hash_BKDR_hashes_check()

void amxc_hash_BKDR_hashes_check ( UNUSED void **  state)

Definition at line 333 of file test_amxc_hash_func.c.

333  {
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 }
unsigned int amxc_BKDR_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_AP_check()

void amxc_hash_distibution_AP_check ( UNUSED void **  state)

Definition at line 316 of file test_amxc_hash_func.c.

316  {
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 }
unsigned int amxc_AP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
static int amxc_hash_distribution_teardown()
static bool first_double
static int amxc_hash_distribution_setup()
static unsigned int array[2006]
static void generate_string(char *str, size_t length)

◆ amxc_hash_distibution_BKDR_check()

void amxc_hash_distibution_BKDR_check ( UNUSED void **  state)

Definition at line 214 of file test_amxc_hash_func.c.

214  {
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 }

◆ amxc_hash_distibution_BP_check()

void amxc_hash_distibution_BP_check ( UNUSED void **  state)

Definition at line 282 of file test_amxc_hash_func.c.

282  {
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 }
unsigned int amxc_BP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_DEK_check()

void amxc_hash_distibution_DEK_check ( UNUSED void **  state)

Definition at line 265 of file test_amxc_hash_func.c.

265  {
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 }
unsigned int amxc_DEK_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_DJB_check()

void amxc_hash_distibution_DJB_check ( UNUSED void **  state)

Definition at line 248 of file test_amxc_hash_func.c.

248  {
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 }
unsigned int amxc_DJB_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_ELF_check()

void amxc_hash_distibution_ELF_check ( UNUSED void **  state)

Definition at line 197 of file test_amxc_hash_func.c.

197  {
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 }
unsigned int amxc_ELF_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_FNV_check()

void amxc_hash_distibution_FNV_check ( UNUSED void **  state)

Definition at line 299 of file test_amxc_hash_func.c.

299  {
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 }
unsigned int amxc_FNV_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_JS_check()

void amxc_hash_distibution_JS_check ( UNUSED void **  state)

Definition at line 163 of file test_amxc_hash_func.c.

163  {
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 }
unsigned int amxc_JS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_PJW_check()

void amxc_hash_distibution_PJW_check ( UNUSED void **  state)

Definition at line 180 of file test_amxc_hash_func.c.

180  {
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 }
unsigned int amxc_PJW_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_RS_check()

void amxc_hash_distibution_RS_check ( UNUSED void **  state)

Definition at line 146 of file test_amxc_hash_func.c.

146  {
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 }
unsigned int amxc_RS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distibution_SBDM_check()

void amxc_hash_distibution_SBDM_check ( UNUSED void **  state)

Definition at line 231 of file test_amxc_hash_func.c.

231  {
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 }
unsigned int amxc_SDBM_hash(const char *str, const unsigned int len)
Calculate a hash from a string.

◆ amxc_hash_distribution_setup()

static int amxc_hash_distribution_setup ( )
static

Definition at line 105 of file test_amxc_hash_func.c.

105  {
106  memset(array, 0, sizeof(array));
107  srand(2013);
108  first_double = false;
109  printf("\n===========================================\n");
110 
111  return 0;
112 }

◆ amxc_hash_distribution_teardown()

static int amxc_hash_distribution_teardown ( )
static

Definition at line 114 of file test_amxc_hash_func.c.

114  {
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 }

◆ amxc_hash_generation_check()

void amxc_hash_generation_check ( UNUSED void **  state)

Definition at line 73 of file test_amxc_hash_func.c.

73  {
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 }

◆ generate_string()

static void generate_string ( char *  str,
size_t  length 
)
static

Definition at line 65 of file test_amxc_hash_func.c.

65  {
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 }

Variable Documentation

◆ array

unsigned int array[2006]
static

Definition at line 102 of file test_amxc_hash_func.c.

◆ first_double

bool first_double = false
static

Definition at line 103 of file test_amxc_hash_func.c.