chiark / gitweb /
Provide test cases for maxlen functions
[base91.git] / lentest.c
1 /*
2  * basE91 length calculation test
3  *
4  * Copyright (c) 2019 Ian Jackson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  - Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *  - Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *  - Neither the name of Joachim Henke nor the names of his contributors may
15  *    be used to endorse or promote products derived from this software without
16  *    specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "base91.h"
36
37 static size_t upto = (14*16 + 14 + 16)*2;
38
39 static int do_test(int do_do, int fill, const char *what,
40         size_t f(struct basE91 *, const void *, size_t, void *),
41         size_t f_end(struct basE91 *, void *),
42         size_t f_maxlen(size_t)
43                                                                                 )
44 {
45         struct basE91 b;
46         size_t i, o, exp;
47         int bad = 0;
48         char ibuf[upto];
49         char obuf[upto*2+100]; /* in case we have bugs */
50
51         memset(ibuf,fill,upto);
52
53         if (!do_do) {
54                 printf("%s: skipping\n",what);
55                 return 0;
56         }
57
58         for (i=0; i<upto; i++) {
59                 basE91_init(&b);
60                 o = f(&b, ibuf, i, obuf);
61                 o += f_end(&b, obuf+o);
62
63                 exp = f_maxlen(i);
64                 if (o == exp) continue;
65
66                 bad = 1;
67                 fprintf(stderr,"%s: i=%lu o=%lu expected=%lu\n",
68                                                 what, (unsigned long)i, (unsigned long)o, (unsigned long)exp);
69         }
70         return bad;
71 }
72
73 int main(int argc, const char **argv) {
74         int do_encode=1, do_decode=1, bad=0;
75
76         if (argc>=2) {
77                 do_encode = !!strchr(argv[1],'e');
78                 do_decode = !!strchr(argv[1],'d');
79         }
80         if (argc>=3) {
81                 upto = atoi(argv[2]);
82         }
83
84 #define MAYBE_DO_TEST(ed, fill) \
85         (bad |= do_test(do_##ed, (fill), #ed, \
86                                                                         basE91_##ed, basE91_##ed##_end, basE91_##ed##_maxlen))
87         MAYBE_DO_TEST(encode, 0xff);
88         MAYBE_DO_TEST(decode, 'A');
89
90         if (bad) exit(8);
91         printf("ok\n");
92         exit(0);
93 }