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 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 */
108 if ( errmsg
!= NULL
) {
115 case 'a': /* alternating bit pattern */
119 case 'c': /* checkerboard pattern */
123 case 'C': /* counting pattern */
124 for (cnt
=0;cnt
< bsize
;cnt
++) {
125 expbits
= ((offset
+cnt
)%8 & 0177);
127 if ( buffer
[cnt
] != expbits
) {
129 "data mismatch at offset %d, exp:%#lo, act:%#o",
130 offset
+cnt
, expbits
, buffer
[cnt
]);
134 sprintf(Errmsg
, "all %d bytes match desired pattern", bsize
);
146 return -1; /* no check can be done for random */
149 for (cnt
=0; cnt
<bsize
; chr
++, cnt
++) {
150 actbits
= (long)*chr
;
152 if ( actbits
!= expbits
) {
153 sprintf(Errmsg
, "data mismatch at offset %d, exp:%#lo, act:%#lo",
154 offset
+cnt
, expbits
, actbits
);
159 sprintf(Errmsg
, "all %d bytes match desired pattern", bsize
);
160 return -1; /* all ok */
165 /***********************************************************************
166 * main for doing unit testing
167 ***********************************************************************/
177 unsigned char *buffer
;
181 if ((buffer
=(unsigned char *)malloc(size
)) == NULL
) {
187 printf("***** for a ****************************\n");
188 databingen('a', buffer
, size
, 0);
189 printf("databingen('a', buffer, %d, 0)\n", size
);
191 ret
=databinchk('a', buffer
, size
, 0, &errmsg
);
192 printf("databinchk('a', buffer, %d, 0, &errmsg) returned %d: %s\n",
195 printf("\tPASS return value of -1 as expected\n");
197 printf("\tFAIL return value %d, expected -1\n", ret
);
200 ret
=databinchk('a', &buffer
[1], size
-1, offset
, &errmsg
);
201 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
202 size
, offset
, ret
, errmsg
);
204 printf("\tPASS return value of -1 as expected\n");
206 printf("\tFAIL return value %d, expected -1\n", ret
);
209 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset
, offset
+15);
212 ret
=databinchk('a', &buffer
[1], size
-1, offset
+1, &errmsg
);
213 printf("databinchk('a', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
214 size
-1, offset
+1, ret
, errmsg
);
216 printf("\tPASS return value of %d as expected\n", number
);
218 printf("\tFAIL return value %d, expected %d\n", ret
, number
);
222 printf("***** for c ****************************\n");
223 databingen('c', buffer
, size
, 0);
224 printf("databingen('c', buffer, %d, 0)\n", size
);
226 ret
=databinchk('c', buffer
, size
, 0, &errmsg
);
227 printf("databinchk('c', buffer, %d, 0, &errmsg) returned %d: %s\n",
230 printf("\tPASS return value of -1 as expected\n");
232 printf("\tFAIL return value %d, expected -1\n", ret
);
235 ret
=databinchk('c', &buffer
[1], size
-1, offset
, &errmsg
);
236 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
237 size
, offset
, ret
, errmsg
);
239 printf("\tPASS return value of -1 as expected\n");
241 printf("\tFAIL return value %d, expected -1\n", ret
);
244 printf("changing char 15 (offset (%d+15) = %d) to 0x0\n", offset
, offset
+15);
247 ret
=databinchk('c', &buffer
[1], size
-1, offset
+1, &errmsg
);
248 printf("databinchk('c', &buffer[1], %d, %d, &errmsg) returned %d: %s\n",
249 size
-1, offset
+1, ret
, errmsg
);
251 printf("\tPASS return value of %d as expected\n", number
);
253 printf("\tFAIL return value %d, expected %d\n", ret
, number
);
255 printf("***** for C ****************************\n");
257 databingen('C', buffer
, size
, 0);
258 printf("databingen('C', buffer, %d, 0)\n", size
);
260 ret
=databinchk('C', buffer
, size
, 0, &errmsg
);
261 printf("databinchk('C', buffer, %d, 0, &errmsg) returned %d: %s\n",
264 printf("\tPASS return value of -1 as expected\n");
266 printf("\tFAIL return value %d, expected -1\n", ret
);
269 ret
=databinchk('C', &buffer
[18], size
-18, 18, &errmsg
);
270 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
271 size
-18, ret
, errmsg
);
273 printf("\tPASS return value of -1 as expected\n");
275 printf("\tFAIL return value %d, expected -1\n", ret
);
279 printf("changing char 20 and 21 to 0x0 (offset %d and %d)\n", 20,
282 ret
=databinchk('C', &buffer
[18], size
-18, 18, &errmsg
);
283 printf("databinchk('C', &buffer[18], %d, 18, &errmsg) returned %d: %s\n",
284 size
-18, ret
, errmsg
);
286 if ( ret
== 20 || ret
== 21 )
287 printf("\tPASS return value of %d or %d as expected\n",
290 printf("\tFAIL return value %d, expected %d or %d\n", ret
,