chiark / gitweb /
Provide test cases for maxlen functions master
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 Nov 2019 11:45:57 +0000 (11:45 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 Nov 2019 11:45:57 +0000 (11:45 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
.gitignore
Makefile
lentest.c [new file with mode: 0644]
test/Makefile
test/test.sh

index 9fa91e1..4863b4e 100644 (file)
@@ -1,6 +1,8 @@
 /base91
+/lentest
 *.o
 /test/b91dec
 /test/b91enc
+/test/lentest
 /test/*.dat
 /test/*.b91
index 246aede..feb0e45 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ mandir = $(prefix)/share/man
 man1dir = $(mandir)/man1
 manext = .1
 
-BIN = base91
+BIN = base91 lentest
 
 .PHONY: all install check clean
 
@@ -25,6 +25,8 @@ all: $(BIN)
 base91: cli.o base91.o
        $(CC) $(LDFLAGS) -o $@ $^
 
+lentest: lentest.o base91.o
+
 install: all
        mkdir -p $(DESTDIR)$(bindir)
        $(INSTALL_PROGRAM) base91 $(DESTDIR)$(bindir)/base91
diff --git a/lentest.c b/lentest.c
new file mode 100644 (file)
index 0000000..3e43d70
--- /dev/null
+++ b/lentest.c
@@ -0,0 +1,93 @@
+/*
+ * basE91 length calculation test
+ *
+ * Copyright (c) 2019 Ian Jackson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  - Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *  - Neither the name of Joachim Henke nor the names of his contributors may
+ *    be used to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "base91.h"
+
+static size_t upto = (14*16 + 14 + 16)*2;
+
+static int do_test(int do_do, int fill, const char *what,
+       size_t f(struct basE91 *, const void *, size_t, void *),
+       size_t f_end(struct basE91 *, void *),
+       size_t f_maxlen(size_t)
+                                                                               )
+{
+       struct basE91 b;
+       size_t i, o, exp;
+       int bad = 0;
+       char ibuf[upto];
+       char obuf[upto*2+100]; /* in case we have bugs */
+
+       memset(ibuf,fill,upto);
+
+       if (!do_do) {
+               printf("%s: skipping\n",what);
+               return 0;
+       }
+
+       for (i=0; i<upto; i++) {
+               basE91_init(&b);
+               o = f(&b, ibuf, i, obuf);
+               o += f_end(&b, obuf+o);
+
+               exp = f_maxlen(i);
+               if (o == exp) continue;
+
+               bad = 1;
+               fprintf(stderr,"%s: i=%lu o=%lu expected=%lu\n",
+                                               what, (unsigned long)i, (unsigned long)o, (unsigned long)exp);
+       }
+       return bad;
+}
+
+int main(int argc, const char **argv) {
+       int do_encode=1, do_decode=1, bad=0;
+
+       if (argc>=2) {
+               do_encode = !!strchr(argv[1],'e');
+               do_decode = !!strchr(argv[1],'d');
+       }
+       if (argc>=3) {
+               upto = atoi(argv[2]);
+       }
+
+#define MAYBE_DO_TEST(ed, fill) \
+       (bad |= do_test(do_##ed, (fill), #ed, \
+                                                                       basE91_##ed, basE91_##ed##_end, basE91_##ed##_maxlen))
+       MAYBE_DO_TEST(encode, 0xff);
+       MAYBE_DO_TEST(decode, 'A');
+
+       if (bad) exit(8);
+       printf("ok\n");
+       exit(0);
+}
index 48e3906..2ce31df 100644 (file)
@@ -5,6 +5,7 @@ SHELL = /bin/sh
 all:
        ln -sf ../base91 b91enc
        ln -sf ../base91 b91dec
+       ln -sf ../lentest .
        $(SHELL) test.sh
 
 clean:
index cb79d40..09548f9 100644 (file)
@@ -81,6 +81,10 @@ echo "+-- random data $T"
 fold -w 73 rnd0.b91 | ./b91dec -vvm 89 | cmp rnd0.dat && echo PASSED || fail_exit
 fold -w 71 rnd1.b91 | ./b91dec -vvm 73 | cmp rnd1.dat && echo PASSED || fail_exit
 
+echo "
+maxlength tests:"
+./lentest && echo PASSED || fail_exit
+
 echo '
 ================
 all tests passed