unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 085bda0cecbff57bccd22942e46d32a9ea5e2108 4935 bytes (raw)
name: test/random-corpus.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
/*
 * Generate a random corpus of stub messages.
 *
 * Initial use case is testing dump and restore, so we only have
 * message-ids and tags.
 *
 * Generated message-id's and tags are intentionally nasty.
 *
 * Copyright (c) 2012 David Bremner
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/ .
 *
 * Author: David Bremner <david@tethera.net>
 */

#include <stdlib.h>
#include <assert.h>
#include <talloc.h>
#include <string.h>
#include <glib.h>
#include <math.h>

#include "notmuch-client.h"
#include "command-line-arguments.h"
#include "database-test.h"

/* Current largest Unicode value defined. Note that most of these will
 * be printed as boxes in most fonts.
 */

#define GLYPH_MAX 0x10FFFE

static gunichar
random_unichar ()
{
    int start = 1, stop = GLYPH_MAX;
    int class = random() % 2;

    /*
     *  Choose about half ascii as test characters, as ascii
     *  punctation and whitespace is the main cause of problems for
     *  the (old) restore parser
    */
    switch (class) {
    case 0:
	/* ascii */
	start = 0x01;
	stop = 0x7f;
	break;
    case 1:
	/* the rest of unicode */
	start = 0x80;
	stop = GLYPH_MAX;
    }

    if (start == stop)
	return start;
    else
	return start + (random() % (stop - start + 1));
}

static char *
random_utf8_string (void *ctx, size_t char_count)
{
    size_t offset = 0;
    size_t i;

    gchar *buf = NULL;
    size_t buf_size = 0;

    for (i = 0; i < char_count; i++) {
	gunichar randomchar;
	size_t written;

	/* 6 for one glyph, one for null, one for luck */
	while (buf_size - offset < 8) {
	    buf_size = 2 * buf_size + 8;
	    buf = talloc_realloc (ctx, buf, gchar, buf_size);
	}

	randomchar = random_unichar();

	written = g_unichar_to_utf8 (randomchar, buf + offset);

	if (written <= 0) {
	    fprintf (stderr, "error converting to utf8\n");
	    exit (1);
	}

	offset += written;

    }
    buf[offset] = 0;
    return buf;
}


int
main (int argc, char **argv)
{

    void *ctx = talloc_new (NULL);

    char *config_path  = NULL;
    notmuch_config_t *config;
    notmuch_database_t *notmuch;

    int num_messages = 500;
    int max_tags = 10;
    // leave room for UTF-8 encoding.
    int tag_len = NOTMUCH_TAG_MAX / 6;
    // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
    // conservative guess.
    int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;

    int seed = 734569;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },
	{ NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },
	{ NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },
	{ NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },
	{ NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },
	{ NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },
	{ 0, 0, 0, 0, 0 }
    };

    int opt_index = parse_arguments (argc, argv, options, 1);

    if (opt_index < 0)
	exit (1);

    if (message_id_len < 1) {
	fprintf (stderr, "message id's must be least length 1\n");
	exit (1);
    }

    if (config_path == NULL) {
	fprintf (stderr, "configuration path must be specified");
	exit (1);
    }

    config = notmuch_config_open (ctx, config_path, NULL);
    if (config == NULL)
	return 1;

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return 1;

    srandom (seed);

    int count;
    for (count = 0; count < num_messages; count++) {
	int j;
	/* explicitly allow zero tags */
	int num_tags = random () % (max_tags + 1);
	/* message ids should be non-empty */
	int this_mid_len = (random () % message_id_len) + 1;
	const char **tag_list;
	char *mid;
	notmuch_status_t status;

	do {
	    mid = random_utf8_string (ctx, this_mid_len);

	    tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 2);

	    tag_list[0] = "random-corpus";

	    for (j = 0; j < num_tags; j++) {
		int this_tag_len = random () % tag_len + 1;

		tag_list[j + 1] = random_utf8_string (ctx, this_tag_len);
	    }

	    tag_list[j + 1] = NULL;

	    status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
	} while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);

	if (status != NOTMUCH_STATUS_SUCCESS) {
	    fprintf (stderr, "error %d adding message", status);
	    exit (status);
	}
    }

    notmuch_database_destroy (notmuch);

    talloc_free (ctx);

    return 0;
}

debug log:

solving 085bda0 ...
found 085bda0 in https://yhetil.org/notmuch/1353792017-31459-6-git-send-email-david@tethera.net/

applying [1/1] https://yhetil.org/notmuch/1353792017-31459-6-git-send-email-david@tethera.net/
diff --git a/test/random-corpus.c b/test/random-corpus.c
new file mode 100644
index 0000000..085bda0

Checking patch test/random-corpus.c...
Applied patch test/random-corpus.c cleanly.

index at:
100644 085bda0cecbff57bccd22942e46d32a9ea5e2108	test/random-corpus.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).