2 * Generate a random corpus of stub messages.
4 * Initial use case is testing dump and restore, so we only have
5 * message-ids and tags.
7 * Generated message-id's and tags are intentionally nasty.
9 * Copyright (c) 2012 David Bremner
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.
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.
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/ .
24 * Author: David Bremner <david@tethera.net>
34 #include "notmuch-client.h"
35 #include "command-line-arguments.h"
36 #include "database-test.h"
38 /* Current largest Unicode value defined. Note that most of these will
39 * be printed as boxes in most fonts.
42 #define GLYPH_MAX 0x10FFFE
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.
56 * We then favour code points with 2 byte encodings. Note that
57 * code points 0xD800-0xDFFF are forbidden in UTF-8.
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 } };
71 int class = random () % GLYPH_MAX;
74 for (i = 0; char_class[i].weight < class; i++) /* nothing */;
76 size = char_class[i].stop - char_class[i].start + 1;
78 return char_class[i].start + (random () % size);
82 random_utf8_string (void *ctx, size_t char_count)
89 for (i = 0; i < char_count; i++) {
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);
100 randomchar = random_unichar ();
101 } while (randomchar == '\n');
103 written = g_unichar_to_utf8 (randomchar, buf + offset);
106 fprintf (stderr, "error converting to utf8\n");
119 main (int argc, char **argv)
122 void *ctx = talloc_new (NULL);
124 char *config_path = NULL;
125 notmuch_config_t *config;
126 notmuch_database_t *notmuch;
128 int num_messages = 500;
130 // leave room for UTF-8 encoding.
131 int tag_len = NOTMUCH_TAG_MAX / 6;
132 // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
133 // conservative guess.
134 int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;
138 notmuch_opt_desc_t options[] = {
139 { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },
140 { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },
141 { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },
142 { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },
143 { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },
144 { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },
148 int opt_index = parse_arguments (argc, argv, options, 1);
153 if (message_id_len < 1) {
154 fprintf (stderr, "message id's must be least length 1\n");
158 if (config_path == NULL) {
159 fprintf (stderr, "configuration path must be specified");
163 config = notmuch_config_open (ctx, config_path, NULL);
167 if (notmuch_database_open (notmuch_config_get_database_path (config),
168 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
174 for (count = 0; count < num_messages; count++) {
176 /* explicitly allow zero tags */
177 int num_tags = random () % (max_tags + 1);
178 /* message ids should be non-empty */
179 int this_mid_len = (random () % message_id_len) + 1;
180 const char **tag_list;
182 notmuch_status_t status;
185 mid = random_utf8_string (ctx, this_mid_len);
187 tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);
189 for (j = 0; j < num_tags; j++) {
190 int this_tag_len = random () % tag_len + 1;
192 tag_list[j] = random_utf8_string (ctx, this_tag_len);
197 status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
198 } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);
200 if (status != NOTMUCH_STATUS_SUCCESS) {
201 fprintf (stderr, "error %d adding message", status);
206 notmuch_database_destroy (notmuch);