From ff76b21bb838b283221d79dfcebdbbd975e5098f Mon Sep 17 00:00:00 2001 From: Richard Kettlewell Date: Sun, 9 Dec 2007 22:18:24 +0000 Subject: [PATCH] Abolish some trivial tests and fold them into dbversion.py; the setup and teardown is slow enough that small tests are a bit of a waste of time. Break out nasty macro in search_league() into a function and fix overrun. --- server/trackdb.c | 55 +++++++++++++++++++++++++++++++++++++----------------- tests/Makefile.am | 2 +- tests/dbversion.py | 8 +++++++- tests/nothing.py | 28 --------------------------- tests/version.py | 31 ------------------------------ 5 files changed, 46 insertions(+), 78 deletions(-) delete mode 100755 tests/nothing.py delete mode 100755 tests/version.py diff --git a/server/trackdb.c b/server/trackdb.c index c275f3c..a46223a 100644 --- a/server/trackdb.c +++ b/server/trackdb.c @@ -1065,11 +1065,43 @@ static int get_stats(struct vector *v, return 0; } +/** @brief One entry in the search league */ struct search_entry { char *word; int n; }; +/** @brief Add a word to the search league + * @param se Pointer to search league + * @param count Maximum size for search league + * @param nse Current size of search league + * @param word New word, or NULL + * @param n How often @p word appears + * @return New size of search league + */ +static int register_search_entry(struct search_entry *se, + int count, + int nse, + char *word, + int n) { + int i; + + if(word && (nse < count || n > se[nse - 1].n)) { + /* Find the starting point */ + if(nse == count) + i = nse - 1; + else + i = nse++; + /* Find the insertion point */ + while(i > 0 && n > se[i - 1].n) + --i; + memmove(&se[i + 1], &se[i], (nse - i - 1) * sizeof *se); + se[i].word = word; + se[i].n = n; + } + return nse; +} + /* find the top COUNT words in the search database */ static int search_league(struct vector *v, int count, DB_TXN *tid) { struct search_entry *se; @@ -1082,25 +1114,14 @@ static int search_league(struct vector *v, int count, DB_TXN *tid) { cursor = trackdb_opencursor(trackdb_searchdb, tid); se = xmalloc(count * sizeof *se); + /* Walk across the whole database counting up the number of times each + * word appears. */ while(!(err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT))) { if(word && wl == k.size && !strncmp(word, k.data, wl)) - ++n; + ++n; /* same word again */ else { -#define FINALIZE() do { \ - if(word && (nse < count || n > se[nse - 1].n)) { \ - if(nse == count) \ - i = nse - 1; \ - else \ - i = nse++; \ - while(i > 0 && n > se[i - 1].n) \ - --i; \ - memmove(&se[i + 1], &se[i], (nse - i) * sizeof *se); \ - se[i].word = word; \ - se[i].n = n; \ - } \ -} while(0) - FINALIZE(); + nse = register_search_entry(se, count, nse, word, n); word = xstrndup(k.data, wl = k.size); n = 1; } @@ -1117,7 +1138,7 @@ static int search_league(struct vector *v, int count, DB_TXN *tid) { } if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK; if(err) return err; - FINALIZE(); + nse = register_search_entry(se, count, nse, word, n); byte_xasprintf(&str, "Top %d search words:", nse); vector_append(v, str); for(i = 0; i < nse; ++i) { @@ -1170,7 +1191,7 @@ struct stats_details { static void stats_complete(struct stats_details *d) { char *s; - + if(!(d->exited && d->closed)) return; byte_xasprintf(&s, "\n" diff --git a/tests/Makefile.am b/tests/Makefile.am index 4e9f008..2cd5e63 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -29,5 +29,5 @@ disorder_udplog_DEPENDENCIES=../lib/libdisorder.a check: ${PYTHON} ${srcdir}/alltests -EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py search.py \ +EXTRA_DIST=alltests dtest.py dbversion.py search.py \ queue.py dump.py play.py diff --git a/tests/dbversion.py b/tests/dbversion.py index 4fcdbd2..35f71ad 100755 --- a/tests/dbversion.py +++ b/tests/dbversion.py @@ -31,9 +31,15 @@ def test(): dtest.stop_daemon() # Revert to default configuration dtest.copyfile(configsave, config) - print "Testing daemon manages to upgrade..." + print " testing daemon manages to upgrade..." dtest.start_daemon() assert dtest.check_files() == 0, "dtest.check_files" + print " getting server version" + c = disorder.client() + v = c.version() + print "Server version: %s" % v + print " getting server stats" + s = c.stats() if __name__ == '__main__': dtest.run() diff --git a/tests/nothing.py b/tests/nothing.py deleted file mode 100755 index c12443e..0000000 --- a/tests/nothing.py +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/env python -# -# This file is part of DisOrder. -# Copyright (C) 2007 Richard Kettlewell -# -# 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 2 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -# USA -# -import dtest,time - -def test(): - """Just start the server and then stop it""" - dtest.start_daemon() - -if __name__ == '__main__': - dtest.run() diff --git a/tests/version.py b/tests/version.py deleted file mode 100755 index 9ec922c..0000000 --- a/tests/version.py +++ /dev/null @@ -1,31 +0,0 @@ -#! /usr/bin/env python -# -# This file is part of DisOrder. -# Copyright (C) 2007 Richard Kettlewell -# -# 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 2 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -# USA -# -import dtest,time,disorder - -def test(): - """Ask the server its version number""" - dtest.start_daemon() - c = disorder.client() - v = c.version() - print "Server version: %s" % v - -if __name__ == '__main__': - dtest.run() -- 2.11.0