]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/fwp/fwp/tests/hashtest/hashtest.c
Add preliminary configuration for RTEMS build
[frescor/frsh-forb.git] / src / fwp / fwp / tests / hashtest / hashtest.c
1 #include <stdio.h>
2 #include "list.h"
3 #include "hashtable.h"
4 #include <string.h>
5
6
7 #define MAX_CHARS 48
8
9 struct item {
10         struct list_head node;
11         int value;
12         char label[MAX_CHARS];
13 };
14
15 unsigned int hash_func(char *key, unsigned int modulus) 
16 {
17         unsigned int hash = 0;
18         int i;
19         
20         // loop while max_chars is not reached and a null character is not found
21         for (i=0; i < MAX_CHARS && key[i]!=0; i++) {
22                hash = (hash*37+ key[i]);
23         }
24         
25         return (hash % modulus);
26 }
27
28
29 int main()
30 {
31         struct hash_table htable;
32         struct item a,c;
33         //struct item a = {{NULL, NULL}, 5, "FRESCOR"};
34         //struct item c = {{NULL, NULL}, 100, "FRSH"};
35         
36         unsigned int hash;
37
38         INIT_LIST_HEAD(&a.node);
39         a.value = 5;
40         strcpy(a.label,"FRESCOR");
41         
42         INIT_LIST_HEAD(&c.node);
43         c.value = 100;
44         strcpy(c.label,"FRSH");
45         
46         htable_init(&htable, 5);
47         
48         hash = hash_func(a.label, htable.size);
49         printf("hash(a) = %d\n", hash); 
50         htable_add(&htable, hash, &a.node);
51
52         hash = hash_func(c.label, htable.size);
53         printf("hash(b) = %d\n", hash); 
54         htable_add(&htable, hash, &c.node);
55         
56         htable_free(&htable);
57
58         return 0;
59 }