]> git.draconx.ca Git - cdecl99.git/blob - test/testlib.c
Hand-code the normalized specifier ordering.
[cdecl99.git] / test / testlib.c
1 /*
2  *  Miscellaneous functions used by the cdecl99 test suite.
3  *  Copyright © 2011-2012, 2021 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <cdecl.h>
24 #include "test.h"
25
26 static size_t printbuf_size;
27 static char *printbuf;
28
29 void *realloc_nofail(void *ptr, size_t size)
30 {
31         void *p;
32         
33         p = realloc(ptr, size);
34         if (!p) {
35                 perror("failed to allocate memory");
36                 abort();
37         }
38
39         return p;
40 }
41
42 void *malloc_nofail(size_t size)
43 {
44         return realloc_nofail(NULL, size);
45 }
46
47 void test_print_decl(struct cdecl *decl)
48 {
49         size_t rc;
50
51 retry:
52         rc = cdecl_declare(printbuf, printbuf_size, decl);
53         if (rc >= printbuf_size) {
54                 printbuf_size = rc + 1;
55                 printbuf = realloc_nofail(printbuf, printbuf_size);
56                 goto retry;
57         }
58
59         printf("%s\n", printbuf);
60 }
61
62 void test_explain_decl(struct cdecl *decl)
63 {
64         size_t rc;
65
66 retry:
67         rc = cdecl_explain(printbuf, printbuf_size, decl);
68         if (rc >= printbuf_size) {
69                 printbuf_size = rc + 1;
70                 printbuf = realloc_nofail(printbuf, printbuf_size);
71                 goto retry;
72         }
73
74         printf("%s\n", printbuf);
75 }
76
77 bool strict_strtoul(unsigned long *val, const char *str, int base)
78 {
79         char *end;
80
81         errno = 0;
82         *val = strtoul(str, &end, base);
83         if (errno != 0 || *end != 0)
84                 return false;
85
86         return true;
87 }
88
89 void test_print_version(const char *program)
90 {
91         printf("%s (%s) %s\n", program, PACKAGE_NAME, PACKAGE_VERSION);
92         puts("Copyright (C) 2021 Nick Bowler.");
93         puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
94         puts("This is free software: you are free to change and redistribute it.");
95         puts("There is NO WARRANTY, to the extent permitted by law.");
96 }