Debian Codegraph @

src/test_util.c

raw symbols SCIP
1 /*
2 the DASM macro assembler (aka small systems cross assembler)
3
4 Copyright (c) 1988-2002 by Matthew Dillon.
5 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
6 Copyright (c) 2003-2008 by Andrew Davie.
7 Copyright (c) 2008 by Peter H. Froehlich.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /**
25 * @file
26 * @brief Unit tests for util module.
27 * @todo how do we keep panic* from aborting while unit test
28 * runs? or maybe it doesn't matter if we switch to Check?
29 */
30
31 #include "util.h"
32
33 #include "asm.h"
34 #include "errors.h"
35
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #if 0
41 #if defined(TEST)
42 /* hack for unit tests where we don't want to exit! */
43 void new_panic(error_t _error, const char *s)
44 {
45 (void) printf("Simulated Panic: %s\n", s);
46 }
47 void debug(error_t _error, const char *s)
48 {
49 (void) printf("Simulated Debug: %s\n", s);
50 }
51 #endif /* defined(TEST) */
52 #endif
53
54 /* fakes for unit test */
55 FILE *FI_listfile = NULL;
56 char *F_listfile = NULL;
57 INCFILE *pIncfile = NULL;
58
59 int main(int argc, char *argv[])
60 {
61 char *one;
62 char *two;
63 union align { long l; void *p; void (*fp)(void); };
64 const size_t BIG = ((size_t) 1) << 31;
65 char buffer[256];
66 char fuffer[256];
67 char poop[5] = {'a', 'b', 'c', 'd', 'X'};
68 size_t res;
69 char* excellent = NULL;
70 assert(argc == 1);
71 setprogname(argv[0]);
72
73 /* test some string functions */
74 excellent = checked_strdup("Peter's own thing!");
75 assert(excellent != NULL);
76 assert(strcmp(excellent, "Peter's own thing!") == 0);
77
78 res = strlcpy(poop, "Hello 47!", sizeof(poop)-1);
79 assert(res == 9);
80 assert(poop[0] == 'H');
81 assert(poop[1] == 'e');
82 assert(poop[2] == 'l');
83 assert(poop[3] == '\0');
84 assert(poop[4] == 'X');
85
86 res = strlcpy(buffer, "Hello 47!", sizeof(buffer));
87 assert(res == 9);
88 assert(strcmp(buffer, "Hello 47!") == 0);
89
90 assert(!match_either_case(buffer, "hello 47!"));
91
92 res = strlower(fuffer, buffer, sizeof(fuffer));
93 assert(res == 9);
94 assert(strcmp(fuffer, "hello 47!") == 0);
95
96 assert(match_either_case(fuffer, "hello 47!"));
97
98 res = strupper(buffer, fuffer, sizeof(buffer));
99 assert(res == 9);
100 assert(strcmp(buffer, "HELLO 47!") == 0);
101
102 assert(match_either_case(buffer, "hello 47!"));
103
104 res = strlcpy(buffer, "\r\t\n ", sizeof(buffer));
105 assert(res == 7);
106
107 res = strip_whitespace(poop, buffer, sizeof(poop)-1);
108 assert(res == 0);
109 assert(poop[0] == '\0');
110 assert(poop[1] == 'e');
111 assert(poop[2] == 'l');
112 assert(poop[3] == '\0');
113 assert(poop[4] == 'X');
114
115 res = strlcpy(buffer, "\rThere are\tso\nmany good things! ", sizeof(buffer));
116 assert(res == 35);
117
118 res = strip_whitespace(poop, buffer, sizeof(poop)-1);
119 assert(res == 25);
120 assert(poop[0] == 'T');
121 assert(poop[1] == 'h');
122 assert(poop[2] == 'e');
123 assert(poop[3] == '\0');
124 assert(poop[4] == 'X');
125
126 res = strip_whitespace(fuffer, buffer, sizeof(fuffer));
127 assert(res == 25);
128 assert(strcmp(fuffer, "Therearesomanygoodthings!") == 0);
129
130 /* fake a current file */
131 pIncfile = malloc(sizeof(INCFILE));
132 pIncfile->next = NULL;
133 pIncfile->name = checked_strdup("someFileName");
134 pIncfile->lineno = 47;
135 /* enable all messages */
136 set_error_level(ERRORLEVEL_DEBUG);
137 /* the tests */
138 puts(getprogname());
139 setprogname(argv[0]);
140 puts(getprogname());
141 printf("sizeof(align)==%zu\n", sizeof(union align));
142 one = checked_malloc(BIG);
143 puts("First malloc()ed!");
144 two = checked_malloc(BIG);
145 puts("Second malloc()ed! Should have caused a panic?");
146 if (two) free(two);
147 puts("Second free()d!");
148 if (one) free(one);
149 puts("First free()d!");
150 one = small_alloc(4096);
151 printf("First small_alloc()ed returned %p!\n", one);
152 two = small_alloc(4096);
153 assert(one < two);
154 printf("Second small_alloc()ed returned %p!\n", two);
155 one = small_alloc(10240);
156 small_free_all();
157 return EXIT_SUCCESS;
158 }
159
160 /* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4 autoindent: */