2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/param.h>
20 #include <string.h> /* memset */
21 #include <stdlib.h> /* rand */
28 static char Errmsg[80];
31 databingen (mode, buffer, bsize, offset)
32 int mode; /* either a, c, r, o, z or C */
33 unsigned char *buffer; /* buffer pointer */
34 int bsize; /* size of buffer */
35 int offset; /* offset into the file where buffer starts */
42 case 'a': /* alternating bit pattern */
43 memset(buffer,0x55,bsize);
46 case 'c': /* checkerboard pattern */
47 memset(buffer,0xf0,bsize);
51 for (ind=0;ind< bsize;ind++) {
52 buffer[ind] = ((offset+ind)%8 & 0177);
57 memset(buffer,0xff,bsize);
61 memset(buffer,0x0,bsize);
64 case 'r': /* random */
65 for (ind=0;ind< bsize;ind++) {
66 buffer[ind] = (rand () & 0177) | 0100;
71 /***********************************************************************
74 * >= 0 : error at byte offset into the file, offset+buffer[0-(bsize-1)]
76 ***********************************************************************/
78 databinchk(mode, buffer, bsize, offset, errmsg)
79 int mode; /* either a, c, r, z, o, or C */
80 unsigned char *buffer; /* buffer pointer */
81 int bsize; /* size of buffer */
82 int offset; /* offset into the file where buffer starts */
92 if ( errmsg != NULL ) {
99 case 'a': /* alternating bit pattern */
103 case 'c': /* checkerboard pattern */
107 case 'C': /* counting pattern */
108 for (cnt=0;cnt< bsize;cnt++) {
109 expbits = ((offset+cnt)%8 & 0177);
111 if ( buffer[cnt] != expbits ) {
113 "data mismatch at offset %d, exp:%#lo, act:%#o",
114 offset+cnt, expbits, buffer[cnt]);
118 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
130 return -1; /* no check can be done for random */
133 for (cnt=0; cnt<bsize; chr++, cnt++) {
134 actbits = (long)*chr;
136 if ( actbits != expbits ) {
137 sprintf(Errmsg, "data mismatch at offset %d, exp:%#lo, act:%#lo",
138 offset+cnt, expbits, actbits);
143 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
144 return -1; /* all ok */
149 /***********************************************************************
150 * main for doing unit testing
151 ***********************************************************************/
161 unsigned char *buffer;
165 if ((buffer=(unsigned char *)malloc(size)) == NULL ) {
171 printf("***** for a ****************************\n");
172 databingen('a', buffer, size, 0);
173 printf("databingen('a', buffer, %d, 0)\n", size);
175 ret=databinchk('a', buffer, size, 0, &errmsg);
176 printf("databinchk('a', buffer, %d, 0, &errmsg) returned %d: %s\n",
179 printf("\tPASS return value of -1 as expected\n");
181 printf("\tFAIL return value %d, expected -1\n", ret);
184 ret=databinchk('a', &buffer[1], size-1, offset, &errmsg);
185 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
186 size, offset, ret, errmsg);
188 printf("\tPASS return value of -1 as expected\n");
190 printf("\tFAIL return value %d, expected -1\n", ret);
193 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset, offset+15);
196 ret=databinchk('a', &buffer[1], size-1, offset+1, &errmsg);
197 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
198 size-1, offset+1, ret, errmsg);
200 printf("\tPASS return value of %d as expected\n", number);
202 printf("\tFAIL return value %d, expected %d\n", ret, number);
206 printf("***** for c ****************************\n");
207 databingen('c', buffer, size, 0);
208 printf("databingen('c', buffer, %d, 0)\n", size);
210 ret=databinchk('c', buffer, size, 0, &errmsg);
211 printf("databinchk('c', buffer, %d, 0, &errmsg) returned %d: %s\n",
214 printf("\tPASS return value of -1 as expected\n");
216 printf("\tFAIL return value %d, expected -1\n", ret);
219 ret=databinchk('c', &buffer[1], size-1, offset, &errmsg);
220 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
221 size, offset, ret, errmsg);
223 printf("\tPASS return value of -1 as expected\n");
225 printf("\tFAIL return value %d, expected -1\n", ret);
228 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset, offset+15);
231 ret=databinchk('c', &buffer[1], size-1, offset+1, &errmsg);
232 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
233 size-1, offset+1, ret, errmsg);
235 printf("\tPASS return value of %d as expected\n", number);
237 printf("\tFAIL return value %d, expected %d\n", ret, number);
239 printf("***** for C ****************************\n");
241 databingen('C', buffer, size, 0);
242 printf("databingen('C', buffer, %d, 0)\n", size);
244 ret=databinchk('C', buffer, size, 0, &errmsg);
245 printf("databinchk('C', buffer, %d, 0, &errmsg) returned %d: %s\n",
248 printf("\tPASS return value of -1 as expected\n");
250 printf("\tFAIL return value %d, expected -1\n", ret);
253 ret=databinchk('C', &buffer[18], size-18, 18, &errmsg);
254 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
255 size-18, ret, errmsg);
257 printf("\tPASS return value of -1 as expected\n");
259 printf("\tFAIL return value %d, expected -1\n", ret);
263 printf("changing char 20 and 21 to 0x0 (offset %d and %d)\n", 20,
266 ret=databinchk('C', &buffer[18], size-18, 18, &errmsg);
267 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
268 size-18, ret, errmsg);
270 if ( ret == 20 || ret == 21 )
271 printf("\tPASS return value of %d or %d as expected\n",
274 printf("\tFAIL return value %d, expected %d or %d\n", ret,