blobmsg_json: print int16 elements in blobmsg_format_element (patch by Luka Perkov)
[project/libubox.git] / examples / uhtbl-test.c
1 #include "../hash.h"
2 #include "../uhtbl.h"
3 #include <stdio.h>
4 #include <string.h>
5
6 struct mybucket {
7 uhtbl_head_t __head;
8 long double mydata;
9 };
10
11 int main() {
12 printf("%i\n", (int)sizeof(struct mybucket));
13
14 uhtbl_t tbl;
15 uhtbl_init(&tbl, sizeof(struct mybucket), 16, hash_murmur2, NULL);
16 struct mybucket *bucket;
17
18 const char *t[] = {"null", "eins", "zwei", "drei", "vier", "fünf", "sechs",
19 "sieben", "acht", "neun", "zehn", "elf", "zwölf", "dreizehn",
20 "vierzehn", "fünfzehn", "sechzehn", "siebzehn", "achtzehn",
21 "neunzehn", "zwanzig", "einundzwanzig", "zweiundzwanzig",
22 "dreiundzwanzig", "virundzwanzig", "fünfundzwanzig", "sechsundzwanzig",
23 "siebenundzwanzig", "achtundzwanzig", "neunundzwanzig", "dreißig",
24 "einunddreißig", "zweiunddreißig"};
25 for (int i = 0; i < 33; i++) {
26 bucket = (struct mybucket*)uhtbl_set(&tbl, t[i], strlen(t[i]));
27 bucket->mydata = i;
28 }
29
30 uint32_t iter = 0;
31 while ((bucket = (struct mybucket*)uhtbl_next(&tbl, &iter))) {
32 printf("%i\t", (int)bucket->mydata);
33 }
34 printf("\nSize: %i, Used: %i\n\n", tbl.size, tbl.used);
35
36 for (int i = 0; i < 33; i++) {
37 bucket = (struct mybucket*)uhtbl_set(&tbl, 0, i);
38 bucket->mydata = i;
39 }
40
41 iter = 0;
42 while ((bucket = (struct mybucket*)uhtbl_next(&tbl, &iter))) {
43 printf("%i\t", (int)bucket->mydata);
44 }
45 printf("\nSize: %i, Used: %i\n\n", tbl.size, tbl.used);
46
47 for (int i = 0; i < 33; i++) {
48 if (uhtbl_unset(&tbl, 0, i)) {
49 printf("Unset failed %i\n", i);
50 }
51 if (uhtbl_unset(&tbl, t[i], strlen(t[i]))) {
52 printf("Unset failed %s\n", t[i]);
53 }
54 }
55
56 iter = 0;
57 while ((bucket = (struct mybucket*)uhtbl_next(&tbl, &iter))) {
58 printf("%i\t", (int)bucket->mydata);
59 }
60 printf("\nSize: %i, Used: %i\n\n", tbl.size, tbl.used);
61
62 for (int i = 0; i < 33; i++) {
63 bucket = (struct mybucket*)uhtbl_set(&tbl, t[i], strlen(t[i]));
64 bucket->mydata = i;
65 }
66
67 for (int i = 0; i < 33; i++) {
68 bucket =(struct mybucket*) uhtbl_set(&tbl, 0, i);
69 bucket->mydata = i;
70 }
71
72 for (int i = 0; i < 33; i++) {
73 bucket = (struct mybucket*)uhtbl_set(&tbl, t[i], strlen(t[i]));
74 bucket->mydata = i;
75 }
76
77 for (int i = 0; i < 33; i++) {
78 bucket = (struct mybucket*)uhtbl_set(&tbl, 0, i);
79 bucket->mydata = i;
80 }
81
82 iter = 0;
83 while ((bucket = (struct mybucket*)uhtbl_next(&tbl, &iter))) {
84 printf("%i\t", (int)bucket->mydata);
85 }
86 printf("\nSize: %i, Used: %i\n\n", tbl.size, tbl.used);
87
88 for (int i = 0; i < 33; i++) {
89 bucket = (struct mybucket*)uhtbl_get(&tbl, t[i], strlen(t[i]));
90 printf("%i\t", (int)bucket->mydata);
91 bucket = (struct mybucket*)uhtbl_get(&tbl, 0, i);
92 printf("%i\t", (int)bucket->mydata);
93 }
94 printf("\nSize: %i, Used: %i\n\n", tbl.size, tbl.used);
95
96 uhtbl_finalize(&tbl);
97
98 return 0;
99 }