Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 84 / 26b19ca3a8cc3734cbba1c8779f8fb462979ce
1 Return-Path: <bremner@tesseract.cs.unb.ca>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 3689B431FBF\r
6         for <notmuch@notmuchmail.org>; Mon, 20 Aug 2012 00:18:37 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id HaQFbs7fXlGq for <notmuch@notmuchmail.org>;\r
16         Mon, 20 Aug 2012 00:18:33 -0700 (PDT)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 55BE2431FBD\r
21         for <notmuch@notmuchmail.org>; Mon, 20 Aug 2012 00:18:25 -0700 (PDT)\r
22 Received: from remotemail by tesseract.cs.unb.ca with local (Exim 4.72)\r
23         (envelope-from <bremner@tesseract.cs.unb.ca>)\r
24         id 1T3MFk-0002FM-Fk; Mon, 20 Aug 2012 04:18:24 -0300\r
25 Received: (nullmailer pid 7818 invoked by uid 1000);\r
26         Sun, 19 Aug 2012 13:19:07 -0000\r
27 From: david@tethera.net\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v3 5/6] test: add generator for random "stub" messages\r
30 Date: Sun, 19 Aug 2012 15:18:33 +0200\r
31 Message-Id: <1345382314-5330-6-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 1.7.10.4\r
33 In-Reply-To: <1345382314-5330-1-git-send-email-david@tethera.net>\r
34 References: <1345382314-5330-1-git-send-email-david@tethera.net>\r
35 Cc: David Bremner <bremner@debian.org>\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.13\r
38 Precedence: list\r
39 List-Id: "Use and development of the notmuch mail system."\r
40         <notmuch.notmuchmail.org>\r
41 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
44 List-Post: <mailto:notmuch@notmuchmail.org>\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
46 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
48 X-List-Received-Date: Mon, 20 Aug 2012 07:18:37 -0000\r
49 \r
50 From: David Bremner <bremner@debian.org>\r
51 \r
52 Initial use case is testing dump and restore, so we only have\r
53 message-ids and tags.\r
54 \r
55 The message ID's are nothing like RFC compliant, but it doesn't seem\r
56 any harder to roundtrip random UTF-8 strings than RFC-compliant ones.\r
57 \r
58 Tags are UTF-8, even though notmuch is in principle more generous than\r
59 that.\r
60 ---\r
61  test/.gitignore      |    1 +\r
62  test/Makefile.local  |    9 +++\r
63  test/basic           |    2 +-\r
64  test/random-corpus.c |  202 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
65  4 files changed, 213 insertions(+), 1 deletion(-)\r
66  create mode 100644 test/random-corpus.c\r
67 \r
68 diff --git a/test/.gitignore b/test/.gitignore\r
69 index be7ab5e..1eff7ce 100644\r
70 --- a/test/.gitignore\r
71 +++ b/test/.gitignore\r
72 @@ -4,4 +4,5 @@ smtp-dummy\r
73  symbol-test\r
74  arg-test\r
75  hex-xcode\r
76 +random-corpus\r
77  tmp.*\r
78 diff --git a/test/Makefile.local b/test/Makefile.local\r
79 index bb67ded..ad0fd91 100644\r
80 --- a/test/Makefile.local\r
81 +++ b/test/Makefile.local\r
82 @@ -16,6 +16,13 @@ $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a\r
83  $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a\r
84         $(call quiet,CC) -I. $^ -o $@ -ltalloc\r
85  \r
86 +random_corpus_deps =  $(dir)/random-corpus.o  $(dir)/database-test.o \\r
87 +                       notmuch-config.o command-line-arguments.o \\r
88 +                       lib/libnotmuch.a util/libutil.a\r
89 +\r
90 +$(dir)/random-corpus: $(random_corpus_deps)\r
91 +       $(call quiet,CC) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS)\r
92 +\r
93  $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
94         $(call quiet,CC) $^ -o $@\r
95  \r
96 @@ -26,6 +33,7 @@ $(dir)/symbol-test: $(dir)/symbol-test.o\r
97  \r
98  TEST_BINARIES=$(dir)/arg-test \\r
99               $(dir)/hex-xcode \\r
100 +             $(dir)/random-corpus \\r
101               $(dir)/smtp-dummy \\r
102               $(dir)/symbol-test\r
103  \r
104 @@ -42,4 +50,5 @@ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
105          $(dir)/arg-test $(dir)/arg-test.o \\r
106          $(dir)/hex-xcode $(dir)/hex-xcode.o \\r
107          $(dir)/database-test.o \\r
108 +        $(dir)/random-corpus $(dir)/random-corpus.o \\r
109          $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
110 diff --git a/test/basic b/test/basic\r
111 index af57026..5f8ed7a 100755\r
112 --- a/test/basic\r
113 +++ b/test/basic\r
114 @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
115  eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
116  tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
117  available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -executable -printf '%f\n' | \\r
118 -    sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|hex-xcode)$/d" | \\r
119 +    sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|hex-xcode|random-corpus)$/d" | \\r
120      sort)\r
121  test_expect_equal "$tests_in_suite" "$available"\r
122  \r
123 diff --git a/test/random-corpus.c b/test/random-corpus.c\r
124 new file mode 100644\r
125 index 0000000..8c5b559\r
126 --- /dev/null\r
127 +++ b/test/random-corpus.c\r
128 @@ -0,0 +1,202 @@\r
129 +/*\r
130 + * Generate a random corpus of stub messages.\r
131 + *\r
132 + * Initial use case is testing dump and restore, so we only have\r
133 + * message-ids and tags.\r
134 + *\r
135 + * Generated message-id's and tags are intentionally nasty.\r
136 + *\r
137 + * Copyright (c) 2012 David Bremner\r
138 + *\r
139 + * This program is free software: you can redistribute it and/or modify\r
140 + * it under the terms of the GNU General Public License as published by\r
141 + * the Free Software Foundation, either version 3 of the License, or\r
142 + * (at your option) any later version.\r
143 + *\r
144 + * This program is distributed in the hope that it will be useful,\r
145 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
146 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
147 + * GNU General Public License for more details.\r
148 + *\r
149 + * You should have received a copy of the GNU General Public License\r
150 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
151 + *\r
152 + * Author: David Bremner <david@tethera.net>\r
153 + */\r
154 +\r
155 +#include <stdlib.h>\r
156 +#include <assert.h>\r
157 +#include <talloc.h>\r
158 +#include <string.h>\r
159 +#include <glib.h>\r
160 +#include <math.h>\r
161 +\r
162 +#include "notmuch-client.h"\r
163 +#include "command-line-arguments.h"\r
164 +#include "database-test.h"\r
165 +\r
166 +/* Current largest UTF-32 value defined. Note that most of these will\r
167 + * be printed as boxes in most fonts.\r
168 + */\r
169 +\r
170 +#define GLYPH_MAX 0x10FFFE\r
171 +\r
172 +static gunichar\r
173 +random_unichar ()\r
174 +{\r
175 +    int start = 1, stop = GLYPH_MAX;\r
176 +    int class = random() % 2;\r
177 +\r
178 +    /*\r
179 +     *  Choose about half ascii as test characters, as ascii\r
180 +     *  punctation and whitespace is the main cause of problems for\r
181 +     *  the (old) restore parser\r
182 +    */\r
183 +    switch (class) {\r
184 +    case 0:\r
185 +       /* ascii */\r
186 +       start = 0x01;\r
187 +       stop = 0x7f;\r
188 +       break;\r
189 +    case 1:\r
190 +       /* the rest of unicode */\r
191 +       start = 0x80;\r
192 +       stop = GLYPH_MAX;\r
193 +    }\r
194 +\r
195 +    if (start == stop)\r
196 +       return start;\r
197 +    else\r
198 +       return start + (random() % (stop - start + 1));\r
199 +}\r
200 +\r
201 +static char *\r
202 +random_utf8_string (void *ctx, size_t char_count)\r
203 +{\r
204 +\r
205 +    gchar *buf = NULL;\r
206 +    size_t buf_size = 0;\r
207 +\r
208 +    size_t offset = 0;\r
209 +\r
210 +    size_t i;\r
211 +\r
212 +    buf = talloc_realloc (ctx, NULL, gchar, char_count);\r
213 +    buf_size = char_count;\r
214 +\r
215 +    for (i = 0; i < char_count; i++) {\r
216 +       gunichar randomchar;\r
217 +       size_t written;\r
218 +\r
219 +       /* 6 for one glyph, one for null */\r
220 +       if (buf_size - offset < 8) {\r
221 +           buf_size += 16;\r
222 +           buf = talloc_realloc (ctx, buf, gchar, buf_size);\r
223 +       }\r
224 +\r
225 +       randomchar = random_unichar();\r
226 +\r
227 +       written = g_unichar_to_utf8 (randomchar, buf + offset);\r
228 +\r
229 +       if (written <= 0) {\r
230 +           fprintf (stderr, "error converting to utf8\n");\r
231 +           exit (1);\r
232 +       }\r
233 +\r
234 +       offset += written;\r
235 +\r
236 +    }\r
237 +    buf[offset] = 0;\r
238 +    return buf;\r
239 +}\r
240 +\r
241 +\r
242 +int\r
243 +main (int argc, char **argv)\r
244 +{\r
245 +\r
246 +    void *ctx = talloc_new (NULL);\r
247 +\r
248 +    char *config_path  = NULL;\r
249 +    notmuch_config_t *config;\r
250 +    notmuch_database_t *notmuch;\r
251 +\r
252 +    int num_messages = 500;\r
253 +    int max_tags = 10;\r
254 +    // leave room for UTF-8 encoding.\r
255 +    int tag_len = NOTMUCH_TAG_MAX / 6;\r
256 +    // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a\r
257 +    // conservative guess.\r
258 +    int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;\r
259 +\r
260 +    int seed = 734569;\r
261 +\r
262 +    notmuch_opt_desc_t options[] = {\r
263 +       { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },\r
264 +       { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },\r
265 +       { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },\r
266 +       { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },\r
267 +       { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },\r
268 +       { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },\r
269 +       { 0, 0, 0, 0, 0 }\r
270 +    };\r
271 +\r
272 +    int opt_index = parse_arguments (argc, argv, options, 1);\r
273 +\r
274 +    if (opt_index < 0)\r
275 +       exit (1);\r
276 +\r
277 +    if (config_path == NULL) {\r
278 +       fprintf (stderr, "configuration path must be specified");\r
279 +       exit (1);\r
280 +    }\r
281 +\r
282 +    config = notmuch_config_open (ctx, config_path, NULL);\r
283 +    if (config == NULL)\r
284 +       return 1;\r
285 +\r
286 +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
287 +                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
288 +       return 1;\r
289 +\r
290 +    srandom (seed);\r
291 +\r
292 +    int count;\r
293 +    for (count = 0; count < num_messages; count++) {\r
294 +       int j;\r
295 +       int num_tags = random () % (max_tags + 1);\r
296 +       int this_mid_len = random () % message_id_len + 1;\r
297 +       const char **tag_list;\r
298 +       char *mid;\r
299 +       notmuch_status_t status;\r
300 +\r
301 +       do {\r
302 +           mid = random_utf8_string (ctx, this_mid_len);\r
303 +\r
304 +           tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 2);\r
305 +\r
306 +           tag_list[0] = "random-corpus";\r
307 +\r
308 +           for (j = 0; j < num_tags; j++) {\r
309 +               int this_tag_len = random () % tag_len + 1;\r
310 +\r
311 +               tag_list[j + 1] = random_utf8_string (ctx, this_tag_len);\r
312 +           }\r
313 +\r
314 +           tag_list[j + 1] = NULL;\r
315 +\r
316 +           status = notmuch_database_add_stub_message (notmuch, mid, tag_list);\r
317 +       } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);\r
318 +\r
319 +       if (status != NOTMUCH_STATUS_SUCCESS) {\r
320 +           fprintf (stderr, "error %d adding message", status);\r
321 +           exit (status);\r
322 +       }\r
323 +    }\r
324 +\r
325 +    notmuch_database_destroy (notmuch);\r
326 +\r
327 +    talloc_free (ctx);\r
328 +\r
329 +    return 0;\r
330 +}\r
331 -- \r
332 1.7.10.4\r
333 \r