]> git.notmuchmail.org Git - notmuch/blob - test/random-corpus.c
f354d4b92c9f9e82d5cdccf6aa9360c3e729dc28
[notmuch] / test / random-corpus.c
1 /*
2  * Generate a random corpus of stub messages.
3  *
4  * Initial use case is testing dump and restore, so we only have
5  * message-ids and tags.
6  *
7  * Generated message-id's and tags are intentionally nasty.
8  *
9  * Copyright (c) 2012 David Bremner
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see http://www.gnu.org/licenses/ .
23  *
24  * Author: David Bremner <david@tethera.net>
25  */
26
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <talloc.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <math.h>
33
34 #include "notmuch-client.h"
35 #include "command-line-arguments.h"
36 #include "database-test.h"
37
38 /* Current largest Unicode value defined. Note that most of these will
39  * be printed as boxes in most fonts.
40  */
41
42 #define GLYPH_MAX 0x10FFFE
43
44
45 typedef struct {
46     int weight;
47     int start;
48     int stop;
49 } char_class_t;
50
51 /*
52  *  Choose about half ascii as test characters, as ascii
53  *  punctation and whitespace is the main cause of problems for
54  *  the (old) restore parser.
55  *
56  *  We then favour code points with 2 byte encodings. Note that
57  *  code points 0xD800-0xDFFF are forbidden in UTF-8.
58  */
59
60 static const
61 char_class_t char_class[] = { { 0.50 * GLYPH_MAX, 0x0001, 0x007f },
62                               { 0.75 * GLYPH_MAX, 0x0080, 0x07ff },
63                               { 0.88 * GLYPH_MAX, 0x0800, 0xd7ff },
64                               { 0.90 * GLYPH_MAX, 0xE000, 0xffff },
65                               {        GLYPH_MAX, 0x10000, GLYPH_MAX } };
66
67 static gunichar
68 random_unichar ()
69 {
70     int i;
71     int class = random () % GLYPH_MAX;
72     int size;
73
74     for (i = 0; char_class[i].weight < class; i++) /* nothing */;
75
76     size = char_class[i].stop - char_class[i].start + 1;
77
78     return char_class[i].start + (random () % size);
79 }
80
81 static char *
82 random_utf8_string (void *ctx, size_t char_count)
83 {
84     size_t offset = 0;
85     size_t i;
86     gchar *buf = NULL;
87     size_t buf_size = 0;
88
89     for (i = 0; i < char_count; i++) {
90         gunichar randomchar;
91         size_t written;
92
93         /* 6 for one glyph, one for null, one for luck */
94         while (buf_size <= offset + 8) {
95             buf_size = 2 * buf_size + 8;
96             buf = talloc_realloc (ctx, buf, gchar, buf_size);
97         }
98
99         randomchar = random_unichar ();
100
101         written = g_unichar_to_utf8 (randomchar, buf + offset);
102
103         if (written <= 0) {
104             fprintf (stderr, "error converting to utf8\n");
105             exit (1);
106         }
107
108         offset += written;
109
110     }
111     buf[offset] = 0;
112     return buf;
113 }
114
115
116 int
117 main (int argc, char **argv)
118 {
119
120     void *ctx = talloc_new (NULL);
121
122     char *config_path  = NULL;
123     notmuch_config_t *config;
124     notmuch_database_t *notmuch;
125
126     int num_messages = 500;
127     int max_tags = 10;
128     // leave room for UTF-8 encoding.
129     int tag_len = NOTMUCH_TAG_MAX / 6;
130     // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
131     // conservative guess.
132     int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;
133
134     int seed = 734569;
135
136     notmuch_opt_desc_t options[] = {
137         { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },
138         { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },
139         { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },
140         { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },
141         { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },
142         { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },
143         { 0, 0, 0, 0, 0 }
144     };
145
146     int opt_index = parse_arguments (argc, argv, options, 1);
147
148     if (opt_index < 0)
149         exit (1);
150
151     if (message_id_len < 1) {
152         fprintf (stderr, "message id's must be least length 1\n");
153         exit (1);
154     }
155
156     if (config_path == NULL) {
157         fprintf (stderr, "configuration path must be specified");
158         exit (1);
159     }
160
161     config = notmuch_config_open (ctx, config_path, NULL);
162     if (config == NULL)
163         return 1;
164
165     if (notmuch_database_open (notmuch_config_get_database_path (config),
166                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
167         return 1;
168
169     srandom (seed);
170
171     int count;
172     for (count = 0; count < num_messages; count++) {
173         int j;
174         /* explicitly allow zero tags */
175         int num_tags = random () % (max_tags + 1);
176         /* message ids should be non-empty */
177         int this_mid_len = (random () % message_id_len) + 1;
178         const char **tag_list;
179         char *mid;
180         notmuch_status_t status;
181
182         do {
183             mid = random_utf8_string (ctx, this_mid_len);
184
185             tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);
186
187             for (j = 0; j < num_tags; j++) {
188                 int this_tag_len = random () % tag_len + 1;
189
190                 tag_list[j] = random_utf8_string (ctx, this_tag_len);
191             }
192
193             tag_list[j] = NULL;
194
195             status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
196         } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);
197
198         if (status != NOTMUCH_STATUS_SUCCESS) {
199             fprintf (stderr, "error %d adding message", status);
200             exit (status);
201         }
202     }
203
204     notmuch_database_destroy (notmuch);
205
206     talloc_free (ctx);
207
208     return 0;
209 }