chiark / gitweb /
Abolish some trivial tests and fold them into dbversion.py; the setup
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 9 Dec 2007 22:18:24 +0000 (22:18 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 9 Dec 2007 22:18:24 +0000 (22:18 +0000)
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
tests/Makefile.am
tests/dbversion.py
tests/nothing.py [deleted file]
tests/version.py [deleted file]

index c275f3cfcecd9e3290d69b5624f4e5b89127bd3e..a46223a7703720f84dcb1072d9d90a9cf8561e24 100644 (file)
@@ -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"
index 4e9f0084b5b04c9e5cc5283da23b216c374b77cc..2cd5e633cdfa328a9559c618db25341892e7c109 100644 (file)
@@ -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
index 4fcdbd29eb0958b7fd243ee2c2e5765f7525d453..35f71adae04172bac8194b655c0b837efc07e0d5 100755 (executable)
@@ -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 (executable)
index c12443e..0000000
+++ /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 (executable)
index 9ec922c..0000000
+++ /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()