]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-stringpool.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / test-stringpool.c
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>
19 #include <stdint.h>
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 #include <syslog.h>
28
29 #include <loc/libloc.h>
30 #include <loc/stringpool.h>
31
32 static const char* characters = "012345789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
33
34 static char* random_string(size_t size) {
35 char* string = malloc(size + 1);
36
37 char* p = string;
38 for (unsigned int i = 0; i < size; i++) {
39 *p++ = characters[rand() % strlen(characters)];
40 }
41 *p = '\0';
42
43 return string;
44 }
45
46 int main(int argc, char** argv) {
47 // Initialize the RNG
48 time_t now = time(NULL);
49 srand(now);
50
51 int err;
52
53 struct loc_ctx* ctx;
54 err = loc_new(&ctx);
55 if (err < 0)
56 exit(EXIT_FAILURE);
57
58 // Enable debug logging
59 loc_set_log_priority(ctx, LOG_DEBUG);
60
61 // Create the stringpool
62 struct loc_stringpool* pool;
63 err = loc_stringpool_new(ctx, &pool);
64 if (err < 0)
65 exit(EXIT_FAILURE);
66
67 // Try reading some invalid string
68 const char* s = loc_stringpool_get(pool, 100);
69 if (s != NULL) {
70 fprintf(stderr, "An unexpected string was returned: %s\n", s);
71 exit(EXIT_FAILURE);
72 }
73
74 // Append a string
75 off_t pos = loc_stringpool_add(pool, "ABC");
76 if (pos < 0) {
77 fprintf(stderr, "Could not add string: %s\n", strerror(-pos));
78 exit(EXIT_FAILURE);
79 }
80
81 printf("Added string at %jd\n", (intmax_t)pos);
82
83 // Must start at first byte
84 if (pos != 0) {
85 fprintf(stderr, "First string didn't start at the first byte\n");
86 exit(EXIT_FAILURE);
87 }
88
89 // Append the same string again
90 pos = loc_stringpool_add(pool, "ABC");
91 if (pos != 0) {
92 fprintf(stderr, "Same string was added at a different position again\n");
93 exit(EXIT_FAILURE);
94 }
95
96 // Append another string
97 pos = loc_stringpool_add(pool, "DEF");
98 if (pos == 0) {
99 fprintf(stderr, "Second string was added at the first address\n");
100 exit(EXIT_FAILURE);
101 }
102
103 // Add 10000 random strings
104 for (unsigned int i = 0; i < 10000; i++) {
105 char* string = random_string(3);
106
107 pos = loc_stringpool_add(pool, string);
108 free(string);
109
110 if (pos < 0) {
111 fprintf(stderr, "Could not add string %d: %s\n", i, strerror(-pos));
112 exit(EXIT_FAILURE);
113 }
114 }
115
116 // Dump pool
117 loc_stringpool_dump(pool);
118
119 loc_stringpool_unref(pool);
120 loc_unref(ctx);
121
122 return EXIT_SUCCESS;
123 }