]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-stringpool.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
29 #include <loc/libloc.h>
30 #include <loc/stringpool.h>
32 static const char* characters
= "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
34 static char* random_string(size_t size
) {
35 char* string
= malloc(size
+ 1);
38 for (unsigned int i
= 0; i
< size
; i
++) {
39 *p
++ = characters
[rand() % strlen(characters
)];
46 int main(int argc
, char** argv
) {
48 time_t now
= time(NULL
);
58 // Enable debug logging
59 loc_set_log_priority(ctx
, LOG_DEBUG
);
61 // Create the stringpool
62 struct loc_stringpool
* pool
;
63 err
= loc_stringpool_new(ctx
, &pool
);
67 // Try reading some invalid string
68 const char* s
= loc_stringpool_get(pool
, 100);
70 fprintf(stderr
, "An unexpected string was returned: %s\n", s
);
75 off_t pos
= loc_stringpool_add(pool
, "ABC");
77 fprintf(stderr
, "Could not add string: %s\n", strerror(-pos
));
81 printf("Added string at %jd\n", (intmax_t)pos
);
83 // Must start at first byte
85 fprintf(stderr
, "First string didn't start at the first byte\n");
89 // Append the same string again
90 pos
= loc_stringpool_add(pool
, "ABC");
92 fprintf(stderr
, "Same string was added at a different position again\n");
96 // Append another string
97 pos
= loc_stringpool_add(pool
, "DEF");
99 fprintf(stderr
, "Second string was added at the first address\n");
103 // Add 10000 random strings
104 for (unsigned int i
= 0; i
< 10000; i
++) {
105 char* string
= random_string(3);
107 pos
= loc_stringpool_add(pool
, string
);
111 fprintf(stderr
, "Could not add string %d: %s\n", i
, strerror(-pos
));
117 loc_stringpool_dump(pool
);
119 loc_stringpool_unref(pool
);