2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 #include <sys/param.h>
34 #include <string.h> /* memset */
35 #include <stdlib.h> /* rand */
42 static char Errmsg[80];
45 databingen (mode, buffer, bsize, offset)
46 int mode; /* either a, c, r, o, z or C */
47 unsigned char *buffer; /* buffer pointer */
48 int bsize; /* size of buffer */
49 int offset; /* offset into the file where buffer starts */
56 case 'a': /* alternating bit pattern */
57 memset(buffer,0x55,bsize);
60 case 'c': /* checkerboard pattern */
61 memset(buffer,0xf0,bsize);
65 for (ind=0;ind< bsize;ind++) {
66 buffer[ind] = ((offset+ind)%8 & 0177);
71 memset(buffer,0xff,bsize);
75 memset(buffer,0x0,bsize);
78 case 'r': /* random */
79 for (ind=0;ind< bsize;ind++) {
80 buffer[ind] = (rand () & 0177) | 0100;
85 /***********************************************************************
88 * >= 0 : error at byte offset into the file, offset+buffer[0-(bsize-1)]
90 ***********************************************************************/
92 databinchk(mode, buffer, bsize, offset, errmsg)
93 int mode; /* either a, c, r, z, o, or C */
94 unsigned char *buffer; /* buffer pointer */
95 int bsize; /* size of buffer */
96 int offset; /* offset into the file where buffer starts */
106 if ( errmsg != NULL ) {
113 case 'a': /* alternating bit pattern */
117 case 'c': /* checkerboard pattern */
121 case 'C': /* counting pattern */
122 for (cnt=0;cnt< bsize;cnt++) {
123 expbits = ((offset+cnt)%8 & 0177);
125 if ( buffer[cnt] != expbits ) {
127 "data mismatch at offset %d, exp:%#lo, act:%#o",
128 offset+cnt, expbits, buffer[cnt]);
132 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
144 return -1; /* no check can be done for random */
147 for (cnt=0; cnt<bsize; chr++, cnt++) {
148 actbits = (long)*chr;
150 if ( actbits != expbits ) {
151 sprintf(Errmsg, "data mismatch at offset %d, exp:%#lo, act:%#lo",
152 offset+cnt, expbits, actbits);
157 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
158 return -1; /* all ok */
163 /***********************************************************************
164 * main for doing unit testing
165 ***********************************************************************/
175 unsigned char *buffer;
179 if ((buffer=(unsigned char *)malloc(size)) == NULL ) {
185 printf("***** for a ****************************\n");
186 databingen('a', buffer, size, 0);
187 printf("databingen('a', buffer, %d, 0)\n", size);
189 ret=databinchk('a', buffer, size, 0, &errmsg);
190 printf("databinchk('a', buffer, %d, 0, &errmsg) returned %d: %s\n",
193 printf("\tPASS return value of -1 as expected\n");
195 printf("\tFAIL return value %d, expected -1\n", ret);
198 ret=databinchk('a', &buffer[1], size-1, offset, &errmsg);
199 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
200 size, offset, ret, errmsg);
202 printf("\tPASS return value of -1 as expected\n");
204 printf("\tFAIL return value %d, expected -1\n", ret);
207 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset, offset+15);
210 ret=databinchk('a', &buffer[1], size-1, offset+1, &errmsg);
211 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
212 size-1, offset+1, ret, errmsg);
214 printf("\tPASS return value of %d as expected\n", number);
216 printf("\tFAIL return value %d, expected %d\n", ret, number);
220 printf("***** for c ****************************\n");
221 databingen('c', buffer, size, 0);
222 printf("databingen('c', buffer, %d, 0)\n", size);
224 ret=databinchk('c', buffer, size, 0, &errmsg);
225 printf("databinchk('c', buffer, %d, 0, &errmsg) returned %d: %s\n",
228 printf("\tPASS return value of -1 as expected\n");
230 printf("\tFAIL return value %d, expected -1\n", ret);
233 ret=databinchk('c', &buffer[1], size-1, offset, &errmsg);
234 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
235 size, offset, ret, errmsg);
237 printf("\tPASS return value of -1 as expected\n");
239 printf("\tFAIL return value %d, expected -1\n", ret);
242 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset, offset+15);
245 ret=databinchk('c', &buffer[1], size-1, offset+1, &errmsg);
246 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
247 size-1, offset+1, ret, errmsg);
249 printf("\tPASS return value of %d as expected\n", number);
251 printf("\tFAIL return value %d, expected %d\n", ret, number);
253 printf("***** for C ****************************\n");
255 databingen('C', buffer, size, 0);
256 printf("databingen('C', buffer, %d, 0)\n", size);
258 ret=databinchk('C', buffer, size, 0, &errmsg);
259 printf("databinchk('C', buffer, %d, 0, &errmsg) returned %d: %s\n",
262 printf("\tPASS return value of -1 as expected\n");
264 printf("\tFAIL return value %d, expected -1\n", ret);
267 ret=databinchk('C', &buffer[18], size-18, 18, &errmsg);
268 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
269 size-18, ret, errmsg);
271 printf("\tPASS return value of -1 as expected\n");
273 printf("\tFAIL return value %d, expected -1\n", ret);
277 printf("changing char 20 and 21 to 0x0 (offset %d and %d)\n", 20,
280 ret=databinchk('C', &buffer[18], size-18, 18, &errmsg);
281 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
282 size-18, ret, errmsg);
284 if ( ret == 20 || ret == 21 )
285 printf("\tPASS return value of %d or %d as expected\n",
288 printf("\tFAIL return value %d, expected %d or %d\n", ret,