chiark / gitweb /
Simple test for compare_path_raw(). It's moved into a separate source
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 27 Apr 2008 12:22:23 +0000 (13:22 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 27 Apr 2008 12:22:23 +0000 (13:22 +0100)
file since the test program doens't have the config pointer.

lib/Makefile.am
lib/t-trackname.c [new file with mode: 0644]
lib/test.c
lib/test.h
lib/trackname.c
lib/trackorder.c [new file with mode: 0644]

index 000ec0baa3495359ba475281618504835ab23e78..91a2bf177e7fac94926e938c49927fc0663fb389 100644 (file)
@@ -75,7 +75,7 @@ libdisorder_a_SOURCES=charset.c charset.h             \
        table.c table.h                                 \
        timeval.h                                       \
        $(TRACKDB) trackdb.h trackdb-int.h              \
-       trackname.c trackname.h                         \
+       trackname.c trackorder.c trackname.h            \
        url.h url.c                                     \
        user.h user.c                                   \
        unicode.h unicode.c                             \
@@ -119,7 +119,8 @@ test_SOURCES=test.c memgc.c test.h t-addr.c t-basen.c t-cache.c             \
        t-casefold.c t-cookies.c t-filepart.c t-hash.c t-heap.c         \
        t-hex.c t-kvp.c t-mime.c t-printf.c t-regsub.c t-selection.c    \
        t-signame.c t-sink.c t-split.c t-unicode.c t-url.c t-utf8.c     \
-       t-words.c t-wstat.c t-bits.c t-vector.c t-syscalls.c
+       t-words.c t-wstat.c t-bits.c t-vector.c t-syscalls.c            \
+       t-trackname.c
 test_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) $(LIBGC)
 test_DEPENDENCIES=libdisorder.a
 
diff --git a/lib/t-trackname.c b/lib/t-trackname.c
new file mode 100644 (file)
index 0000000..23674ed
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file is part of DisOrder.
+ * Copyright (C) 2008 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
+ */
+#include "test.h"
+#include "trackname.h"
+
+#define CHECK_PATH_ORDER(A,B,EXPECTED) do {                    \
+  const unsigned char a[] = A, b[] = B;                                \
+  insist(compare_path_raw(a, (sizeof a) - 1,                   \
+                         b, (sizeof b) - 1) == (EXPECTED));    \
+  insist(compare_path_raw(b, (sizeof b) - 1,                   \
+                         a, (sizeof a) - 1) == -(EXPECTED));   \
+} while(0)
+
+void test_trackname(void) {
+  printf("test_trackname\n");
+  CHECK_PATH_ORDER("/a/b", "/aa/", -1);
+  CHECK_PATH_ORDER("/a/b", "/a", 1);
+  CHECK_PATH_ORDER("/ab", "/a", 1);
+  CHECK_PATH_ORDER("/ab", "/aa", 1);
+  CHECK_PATH_ORDER("/aa", "/aa", 0);
+  CHECK_PATH_ORDER("/", "/", 0);
+}
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/
index d18aa347e1f3f3001384171f9b05777a54dd917f..0682979e3323e01304c3209f45d267a058286422 100644 (file)
@@ -161,6 +161,7 @@ int main(void) {
   test_bits();
   test_vector();
   test_syscalls();
+  test_trackname();
   fprintf(stderr,  "%lld errors out of %lld tests\n", errors, tests);
   return !!errors;
 }
index dafdfb5203b1b3f7f8c8283bb8376c36f7803ec2..4fbde0b69c18a9a526d761250efcb145e6082c7e 100644 (file)
@@ -156,6 +156,7 @@ void test_wstat(void);
 void test_bits(void);
 void test_vector(void);
 void test_syscalls(void);
+void test_trackname(void);
 
 #endif /* TEST_H */
 
index bb07e6468cae3e963ab24fa75ed60efd45b5718c..0aef934824209425e88659fd3ec6f5df7f0e102e 100644 (file)
@@ -109,46 +109,6 @@ const char *trackname_transform(const char *type,
   return subject;
 }
 
-int compare_tracks(const char *sa, const char *sb,
-                  const char *da, const char *db,
-                  const char *ta, const char *tb) {
-  int c;
-
-  if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
-                utf8_casefold_canon(sb, strlen(sb), 0))))
-    return c;
-  if((c = strcmp(sa, sb))) return c;
-  if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
-                utf8_casefold_canon(db, strlen(db), 0))))
-    return c;
-  if((c = strcmp(da, db))) return c;
-  return compare_path(ta, tb);
-}
-
-int compare_path_raw(const unsigned char *ap, size_t an,
-                    const unsigned char *bp, size_t bn) {
-  /* Don't change this function!  The database sort order depends on it */
-  while(an > 0 && bn > 0) {
-    if(*ap == *bp) {
-      ap++;
-      bp++;
-      an--;
-      bn--;
-    } else if(*ap == '/') {
-      return -1;               /* /a/b < /aa/ */
-    } else if(*bp == '/') {
-      return 1;                        /* /aa > /a/b */
-    } else
-      return *ap - *bp;
-  }
-  if(an > 0)
-    return 1;                  /* /a/b > /a and /ab > /a */
-  else if(bn > 0)
-    return -1;                 /* /a < /ab and /a < /a/b */
-  else
-    return 0;
-}
-
 /*
 Local Variables:
 c-basic-offset:2
diff --git a/lib/trackorder.c b/lib/trackorder.c
new file mode 100644 (file)
index 0000000..a61b89c
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file is part of DisOrder
+ * Copyright (C) 2005, 2006, 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
+ */
+
+#include <config.h>
+#include "types.h"
+
+#include <pcre.h>
+#include <fnmatch.h>
+#include <string.h>
+#include <assert.h>
+
+#include "trackname.h"
+#include "log.h"
+#include "unicode.h"
+
+int compare_tracks(const char *sa, const char *sb,
+                  const char *da, const char *db,
+                  const char *ta, const char *tb) {
+  int c;
+
+  if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
+                utf8_casefold_canon(sb, strlen(sb), 0))))
+    return c;
+  if((c = strcmp(sa, sb))) return c;
+  if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
+                utf8_casefold_canon(db, strlen(db), 0))))
+    return c;
+  if((c = strcmp(da, db))) return c;
+  return compare_path(ta, tb);
+}
+
+int compare_path_raw(const unsigned char *ap, size_t an,
+                    const unsigned char *bp, size_t bn) {
+  /* Don't change this function!  The database sort order depends on it */
+  while(an > 0 && bn > 0) {
+    if(*ap == *bp) {
+      ap++;
+      bp++;
+      an--;
+      bn--;
+    } else if(*ap == '/') {
+      return -1;               /* /a/b < /aa/ */
+    } else if(*bp == '/') {
+      return 1;                        /* /aa > /a/b */
+    } else
+      return *ap - *bp;
+  }
+  if(an > 0)
+    return 1;                  /* /a/b > /a and /ab > /a */
+  else if(bn > 0)
+    return -1;                 /* /a < /ab and /a < /a/b */
+  else
+    return 0;
+}
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+End:
+*/