]> git.draconx.ca Git - cdecl99.git/blob - t/testlib.c
5c76bb4dbec84bab03c3e2be23232345e109a2ab
[cdecl99.git] / t / testlib.c
1 /*
2  *  Miscellaneous functions used by the cdecl99 test suite.
3  *  Copyright © 2011-2012, 2021-2023 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 <getopt.h>
24 #include <cdecl.h>
25
26 #include "help.h"
27 #include "test.h"
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 bool strict_strtoul(unsigned long *val, const char *str, int base)
48 {
49         char *end;
50
51         errno = 0;
52         *val = strtoul(str, &end, base);
53         if (errno != 0 || *end != 0)
54                 return false;
55
56         return true;
57 }
58
59 void test_print_options(const struct option *lopts)
60 {
61         const struct option *opt;
62
63         puts("Options:");
64         for (opt = lopts; opt->val; opt++) {
65                 if (help_print_optstring(opt, "ARG", 20))
66                         putchar('\n');
67         }
68 }