]> git.draconx.ca Git - cdecl99.git/blob - test/testlib.c
e5143c20cc49a69805aa1d516d1b72151250d6a3
[cdecl99.git] / test / testlib.c
1 /*
2  *  Miscellaneous functions used by the cdecl99 test suite.
3  *  Copyright © 2011 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 void *realloc_nofail(void *ptr, size_t size)
27 {
28         void *p;
29         
30         p = realloc(ptr, size);
31         if (!p) {
32                 perror("failed to allocate memory");
33                 abort();
34         }
35
36         return p;
37 }
38
39 void *malloc_nofail(size_t size)
40 {
41         return realloc_nofail(NULL, size);
42 }
43
44 void test_print_decl(struct cdecl *decl)
45 {
46         static size_t bufsz;
47         static char *buf;
48         size_t rc;
49
50 retry:
51         rc = cdecl_declare(buf, bufsz, decl);
52         if (rc >= bufsz) {
53                 bufsz = rc + 1;
54                 buf = realloc_nofail(buf, bufsz);
55                 goto retry;
56         }
57
58         printf("%s\n", buf);
59 }
60
61 bool strict_strtoul(unsigned long *val, const char *str, int base)
62 {
63         char *end;
64
65         errno = 0;
66         *val = strtoul(str, &end, base);
67         if (errno != 0 || *end != 0)
68                 return false;
69
70         return true;
71 }