summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDonovan Baarda <abo@minkirri.apana.org.au>2019-09-04 22:41:49 +1000
committerDonovan Baarda <abo@minkirri.apana.org.au>2019-09-04 22:41:49 +1000
commitd3297464417a9e92bb9b4045037c122fdc1844ba (patch)
tree8531aec620e174def2c3ee74a3ed840d16deb28d /tests
parentcd4045315563521f9f94c164a3729d00a5ee03ff (diff)
downloadlibrsync-d3297464417a9e92bb9b4045037c122fdc1844ba.tar.gz
Add a RabinKarp rollsum and tests.
Diffstat (limited to 'tests')
-rw-r--r--tests/rabinkarp_test.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/rabinkarp_test.c b/tests/rabinkarp_test.c
new file mode 100644
index 0000000..918d944
--- /dev/null
+++ b/tests/rabinkarp_test.c
@@ -0,0 +1,77 @@
+/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
+ *
+ * rabinkarp_test -- tests for the rabinkarp_t rolling checksum.
+ *
+ * Copyright (C) 2003 by Donovan Baarda <abo@minkirri.apana.org.au>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+/* Force DEBUG on so that tests can use assert(). */
+#undef NDEBUG
+#include <stdio.h>
+#include <stdint.h>
+#include <assert.h>
+#include "rabinkarp.h"
+
+int main(int argc, char **argv)
+{
+ rabinkarp_t r;
+ int i;
+ unsigned char buf[256];
+
+ /* Test rabinkarp_init() */
+ rabinkarp_init(&r);
+ assert(r.count == 0);
+ assert(r.hash == 1);
+ assert(rabinkarp_digest(&r) == 0x00000001);
+
+ /* Test rabinkarp_rollin() */
+ rabinkarp_rollin(&r, 0); /* [0] */
+ assert(r.count == 1);
+ assert(rabinkarp_digest(&r) == 0x08104225);
+ rabinkarp_rollin(&r, 1);
+ rabinkarp_rollin(&r, 2);
+ rabinkarp_rollin(&r, 3); /* [0,1,2,3] */
+ assert(r.count == 4);
+ assert(rabinkarp_digest(&r) == 0xaf981e97);
+
+ /* Test rabinkarp_rotate() */
+ rabinkarp_rotate(&r, 0, 4); /* [1,2,3,4] */
+ assert(r.count == 4);
+ assert(rabinkarp_digest(&r) == 0xe2ef15f3);
+ rabinkarp_rotate(&r, 1, 5);
+ rabinkarp_rotate(&r, 2, 6);
+ rabinkarp_rotate(&r, 3, 7); /* [4,5,6,7] */
+ assert(r.count == 4);
+ assert(rabinkarp_digest(&r) == 0x7cf3fc07);
+
+ /* Test rabinkarp_rollout() */
+ rabinkarp_rollout(&r, 4); /* [5,6,7] */
+ assert(r.count == 3);
+ assert(rabinkarp_digest(&r) == 0xf284a77f);
+ rabinkarp_rollout(&r, 5);
+ rabinkarp_rollout(&r, 6);
+ rabinkarp_rollout(&r, 7); /* [] */
+ assert(r.count == 0);
+ assert(rabinkarp_digest(&r) == 0x00000001);
+
+ /* Test rabinkarp_update() */
+ for (i = 0; i < 256; i++)
+ buf[i] = i;
+ rabinkarp_update(&r, buf, 256);
+ assert(rabinkarp_digest(&r) == 0xc1972381);
+ return 0;
+}