]>
git.ipfire.org Git - location/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.
28 #include <loc/libloc.h>
29 #include <loc/stringpool.h>
31 static const char* characters
= "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
33 static char* random_string(size_t size
) {
34 char* string
= malloc(size
+ 1);
37 for (unsigned int i
= 0; i
< size
; i
++) {
38 *p
++ = characters
[rand() % strlen(characters
)];
45 int main(int argc
, char** argv
) {
47 time_t now
= time(NULL
);
57 // Create the stringpool
58 struct loc_stringpool
* pool
;
59 err
= loc_stringpool_new(ctx
, &pool
);
63 // Try reading some invalid string
64 const char* s
= loc_stringpool_get(pool
, 100);
66 fprintf(stderr
, "An unexpected string was returned: %s\n", s
);
71 off_t pos
= loc_stringpool_add(pool
, "ABC");
73 fprintf(stderr
, "Could not add string: %s\n", strerror(-pos
));
77 printf("Added string at %jd\n", (intmax_t)pos
);
79 // Must start at first byte
81 fprintf(stderr
, "First string didn't start at the first byte\n");
85 // Append the same string again
86 pos
= loc_stringpool_add(pool
, "ABC");
88 fprintf(stderr
, "Same string was added at a different position again\n");
92 // Append another string
93 pos
= loc_stringpool_add(pool
, "DEF");
95 fprintf(stderr
, "Second string was added at the first address\n");
99 // Add 10000 random strings
100 for (unsigned int i
= 0; i
< 10000; i
++) {
101 char* string
= random_string(3);
103 pos
= loc_stringpool_add(pool
, string
);
107 fprintf(stderr
, "Could not add string %d: %s\n", i
, strerror(-pos
));
113 loc_stringpool_dump(pool
);
115 loc_stringpool_unref(pool
);