From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 64FA0431FAF for ; Fri, 30 Nov 2012 15:18:48 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id AA+Ibs8Khwqb for ; Fri, 30 Nov 2012 15:18:43 -0800 (PST) Received: from mail-la0-f53.google.com (mail-la0-f53.google.com [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 2441F431FAE for ; Fri, 30 Nov 2012 15:18:42 -0800 (PST) Received: by mail-la0-f53.google.com with SMTP id w12so844899lag.26 for ; Fri, 30 Nov 2012 15:18:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version:content-type:x-gm-message-state; bh=vOg/oRAlXQv5gUTgVuRwW62CCurn0BQjyOl3DFPZsQ8=; b=F6gkjMxgZt0IgETVSkRc8tpCU2QMbsyXMBGsta/NUvtRo+vu1XLj0GnYywSb3MzSaM ZDObYJNAh/0XxnpuUrFnDsu93BfjNI4gKcxoZlPNoUZF/FESnkiDqj3P8lI0pb54BE04 qXK0tP6V4jI1nGyeo0KBojLkuFShXRew5/p+3Kluf5rv7SVjX5ZeY0vB2ibT5bjRTa8Z ynBuXFO9US4eTVl3xsnZRAFMr/07Bs5bpdBYZ7OAux1k0G9sAoqSm/VO/NOTwBYREBni xR8DQMt6LGusbAoiHP4nyJPU4eyy8Fpm2P2R+pdxAdxyQyOevnH8rL7sGbprM744TrE4 sV+A== Received: by 10.112.38.226 with SMTP id j2mr1469259lbk.128.1354317521400; Fri, 30 Nov 2012 15:18:41 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi. [80.223.81.27]) by mx.google.com with ESMTPS id z9sm2576085lby.8.2012.11.30.15.18.39 (version=SSLv3 cipher=OTHER); Fri, 30 Nov 2012 15:18:40 -0800 (PST) From: Jani Nikula To: david@tethera.net, notmuch@notmuchmail.org Subject: Re: [Patch v2 05/17] test: add generator for random "stub" messages In-Reply-To: <1353792017-31459-6-git-send-email-david@tethera.net> References: <1353792017-31459-1-git-send-email-david@tethera.net> <1353792017-31459-6-git-send-email-david@tethera.net> User-Agent: Notmuch/0.14+124~g3b17402 (http://notmuchmail.org) Emacs/23.4.1 (i686-pc-linux-gnu) Date: Sat, 01 Dec 2012 01:18:37 +0200 Message-ID: <87obieadde.fsf@nikula.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gm-Message-State: ALoCoQng7+yRm1TIse5GMilVCiCecZcs+pavU2I+I3CBB1ERl/m3DPvD5QvB0VWa//axoG2zKZ8I Cc: David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Nov 2012 23:18:48 -0000 On Sat, 24 Nov 2012, david@tethera.net wrote: > From: David Bremner > > Initial use case is testing dump and restore, so we only have > message-ids and tags. > > The message ID's are nothing like RFC compliant, but it doesn't seem > any harder to roundtrip random UTF-8 strings than RFC-compliant ones. > > Tags are UTF-8, even though notmuch is in principle more generous than > that. > > updated for id:m2wr04ocro.fsf@guru.guru-group.fi > > - talk about Unicode value rather some specific encoding > - call talloc_realloc less times > --- > test/.gitignore | 1 + > test/Makefile.local | 10 +++ > test/basic | 1 + > test/random-corpus.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 216 insertions(+) > create mode 100644 test/random-corpus.c > > diff --git a/test/.gitignore b/test/.gitignore > index be7ab5e..1eff7ce 100644 > --- a/test/.gitignore > +++ b/test/.gitignore > @@ -4,4 +4,5 @@ smtp-dummy > symbol-test > arg-test > hex-xcode > +random-corpus > tmp.* > diff --git a/test/Makefile.local b/test/Makefile.local > index 8479f91..6a9f15e 100644 > --- a/test/Makefile.local > +++ b/test/Makefile.local > @@ -16,6 +16,14 @@ $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a > $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a > $(call quiet,CC) -I. $^ -o $@ -ltalloc > > +random_corpus_deps = $(dir)/random-corpus.o $(dir)/database-test.o \ > + notmuch-config.o command-line-arguments.o \ > + lib/libnotmuch.a util/libutil.a \ > + parse-time-string/libparse-time-string.a > + > +$(dir)/random-corpus: $(random_corpus_deps) > + $(call quiet,CC) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS) > + > $(dir)/smtp-dummy: $(smtp_dummy_modules) > $(call quiet,CC) $^ -o $@ > > @@ -29,6 +37,7 @@ $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o > > TEST_BINARIES=$(dir)/arg-test \ > $(dir)/hex-xcode \ > + $(dir)/random-corpus \ > $(dir)/parse-time \ > $(dir)/smtp-dummy \ > $(dir)/symbol-test > @@ -46,5 +55,6 @@ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \ > $(dir)/arg-test $(dir)/arg-test.o \ > $(dir)/hex-xcode $(dir)/hex-xcode.o \ > $(dir)/database-test.o \ > + $(dir)/random-corpus $(dir)/random-corpus.o \ > $(dir)/parse-time $(dir)/parse-time.o \ > $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.* > diff --git a/test/basic b/test/basic > index 2a571ac..f93469f 100755 > --- a/test/basic > +++ b/test/basic > @@ -59,6 +59,7 @@ available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 \ > ! -name hex-xcode \ > ! -name notmuch-test \ > ! -name parse-time \ > + ! -name random-corpus \ > ! -name smtp-dummy \ > ! -name symbol-test \ > ! -name test-verbose \ > diff --git a/test/random-corpus.c b/test/random-corpus.c > new file mode 100644 > index 0000000..085bda0 > --- /dev/null > +++ b/test/random-corpus.c > @@ -0,0 +1,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 > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +#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 > + */ I wonder if there should be even more emphasis on ascii and unicode code points up to U+07FF (which maps to two UTF-8 bytes). > + 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; > + Irritating blank line. ;) > + gchar *buf = NULL; > + size_t buf_size = 0; You could do an initial talloc of the buf based on char_count, e.g. twice that. > + > + 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) { I'd probably write that (offset + 8 >= buf_size). > + 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, ¬much)) > + 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"; We'll probably want messages completely without tags in the tests too. How about a parameter to define the random-corpus tag. Which can be left out too. And if it's present, another parameter to tell whether it should be added even if num_tags == 0. Do the tags always come out sorted from the library? I'm wondering if it's a good idea to always put random-corpus there first. If it matters, you could use random () % num_tags to decide the position in the tag list to put the random-corpus tag in. BR, Jani. > + > + 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; > +} > -- > 1.7.10.4 > > _______________________________________________ > notmuch mailing list > notmuch@notmuchmail.org > http://notmuchmail.org/mailman/listinfo/notmuch