]>
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.
27 #include <loc/libloc.h>
28 #include <loc/stringpool.h>
30 static const char* characters
= "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
32 static char* random_string(size_t size
) {
33 char* string
= malloc(size
+ 1);
36 for (unsigned int i
= 0; i
< size
; i
++) {
37 *p
++ = characters
[rand() % strlen(characters
)];
44 int main(int argc
, char** argv
) {
46 time_t now
= time(NULL
);
56 // Create the stringpool
57 struct loc_stringpool
* pool
;
58 err
= loc_stringpool_new(ctx
, &pool
);
63 off_t pos
= loc_stringpool_add(pool
, "ABC");
65 fprintf(stderr
, "Could not add string: %s\n", strerror(-pos
));
69 printf("Added string at %jd\n", pos
);
71 // Must start at first byte
73 fprintf(stderr
, "First string didn't start at the first byte\n");
77 // Append the same string again
78 pos
= loc_stringpool_add(pool
, "ABC");
80 fprintf(stderr
, "Same string was added at a different position again\n");
84 // Append another string
85 pos
= loc_stringpool_add(pool
, "DEF");
87 fprintf(stderr
, "Second string was added at the first address\n");
91 // Add 10000 random strings
92 for (unsigned int i
= 0; i
< 10000; i
++) {
93 char* string
= random_string(3);
95 pos
= loc_stringpool_add(pool
, string
);
99 fprintf(stderr
, "Could not add string %d: %s\n", i
, strerror(-pos
));
105 loc_stringpool_dump(pool
);
107 loc_stringpool_unref(pool
);