]>
Commit | Line | Data |
---|---|---|
62b83e6d MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <stdio.h> | |
18 | #include <stddef.h> | |
27e72316 | 19 | #include <stdint.h> |
62b83e6d MT |
20 | #include <stdlib.h> |
21 | #include <string.h> | |
22 | #include <fcntl.h> | |
23 | #include <ctype.h> | |
24 | #include <errno.h> | |
25 | #include <unistd.h> | |
26 | #include <time.h> | |
27 | ||
28 | #include <loc/libloc.h> | |
9fc7f001 | 29 | #include <loc/stringpool.h> |
62b83e6d MT |
30 | |
31 | static const char* characters = "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
32 | ||
33 | static char* random_string(size_t size) { | |
34 | char* string = malloc(size + 1); | |
35 | ||
36 | char* p = string; | |
37 | for (unsigned int i = 0; i < size; i++) { | |
38 | *p++ = characters[rand() % strlen(characters)]; | |
39 | } | |
40 | *p = '\0'; | |
41 | ||
42 | return string; | |
43 | } | |
44 | ||
45 | int main(int argc, char** argv) { | |
46 | // Initialize the RNG | |
47 | time_t now = time(NULL); | |
48 | srand(now); | |
49 | ||
50 | int err; | |
51 | ||
52 | struct loc_ctx* ctx; | |
53 | err = loc_new(&ctx); | |
54 | if (err < 0) | |
55 | exit(EXIT_FAILURE); | |
56 | ||
57 | // Create the stringpool | |
58 | struct loc_stringpool* pool; | |
0e974d4b | 59 | err = loc_stringpool_new(ctx, &pool); |
62b83e6d MT |
60 | if (err < 0) |
61 | exit(EXIT_FAILURE); | |
62 | ||
ec684c1a MT |
63 | // Try reading some invalid string |
64 | const char* s = loc_stringpool_get(pool, 100); | |
65 | if (s != NULL) { | |
66 | fprintf(stderr, "An unexpected string was returned: %s\n", s); | |
67 | exit(EXIT_FAILURE); | |
68 | } | |
69 | ||
62b83e6d MT |
70 | // Append a string |
71 | off_t pos = loc_stringpool_add(pool, "ABC"); | |
72 | if (pos < 0) { | |
73 | fprintf(stderr, "Could not add string: %s\n", strerror(-pos)); | |
74 | exit(EXIT_FAILURE); | |
75 | } | |
76 | ||
7158d9d1 | 77 | printf("Added string at %jd\n", (intmax_t)pos); |
62b83e6d MT |
78 | |
79 | // Must start at first byte | |
80 | if (pos != 0) { | |
81 | fprintf(stderr, "First string didn't start at the first byte\n"); | |
82 | exit(EXIT_FAILURE); | |
83 | } | |
84 | ||
85 | // Append the same string again | |
86 | pos = loc_stringpool_add(pool, "ABC"); | |
87 | if (pos != 0) { | |
88 | fprintf(stderr, "Same string was added at a different position again\n"); | |
89 | exit(EXIT_FAILURE); | |
90 | } | |
91 | ||
92 | // Append another string | |
93 | pos = loc_stringpool_add(pool, "DEF"); | |
94 | if (pos == 0) { | |
95 | fprintf(stderr, "Second string was added at the first address\n"); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | ||
99 | // Add 10000 random strings | |
100 | for (unsigned int i = 0; i < 10000; i++) { | |
101 | char* string = random_string(3); | |
102 | ||
103 | pos = loc_stringpool_add(pool, string); | |
104 | free(string); | |
105 | ||
106 | if (pos < 0) { | |
107 | fprintf(stderr, "Could not add string %d: %s\n", i, strerror(-pos)); | |
108 | exit(EXIT_FAILURE); | |
109 | } | |
110 | } | |
111 | ||
112 | // Dump pool | |
113 | loc_stringpool_dump(pool); | |
114 | ||
115 | loc_stringpool_unref(pool); | |
116 | loc_unref(ctx); | |
117 | ||
118 | return EXIT_SUCCESS; | |
119 | } |