-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
83 lines (69 loc) · 1.84 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "bcmap.h"
static int
print(const void *key0, const void *val0, void *unused)
{
assert(unused == NULL);
printf("{%s: %d}\n", (char *) key0, *((int *) val0));
return 1;
}
static char *
strrand(size_t size)
{
static char cs[] = "abcdefghijklmnopqrstuvwxyz";
static size_t cslen = sizeof(cs) - 1;
char *str = malloc((size + 1) * sizeof(*str));
size_t i;
for (i = 0; i < size; i++) {
size_t n = rand() % cslen;
str[i] = cs[n];
}
str[size] = '\0';
return str;
}
int
main(int argc, char *argv[])
{
bcmap *table;
size_t i;
char *endptr;
unsigned long randstrlen;
unsigned long randstrcount;
if (argc < 2) {
fprintf(stderr, "usage: %s RANDSTRLEN RANDSTRCOUNT\n", argv[0]);
return EXIT_FAILURE;
}
randstrlen = strtoul(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "usage: %s RANDSTRLEN RANDSTRCOUNT\n", argv[0]);
return EXIT_FAILURE;
}
if (argc < 3) {
fprintf(stderr, "usage: %s RANDSTRLEN RANDSTRCOUNT\n", argv[0]);
return EXIT_FAILURE;
}
randstrcount = strtoul(argv[2], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "usage: %s RANDSTRLEN RANDSTRCOUNT\n", argv[0]);
return EXIT_FAILURE;
}
if (argc > 3) {
fprintf(stderr, "usage: %s RANDSTRLEN RANDSTRCOUNT\n", argv[0]);
return EXIT_FAILURE;
}
table = bcmap_new(bcmap_strcmp);
srand(time(NULL));
for (i = 0; i < randstrcount; i++) {
char *key = strrand(randstrlen);
int *val = malloc(sizeof(*val));
*val = rand() % 90 + 10;
bcmap_put(table, key, val);
}
bcmap_for(table, print, NULL);
bcmap_del(table, free, free);
return EXIT_SUCCESS;
}