fsx: Add fallocate collapse range operation
[xfstests-dev.git] / lib / dataascii.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
18 #include <stdio.h>
19 #include <string.h>
20 #include "dataascii.h"
21
22 #define CHARS           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz\n"
23 #define CHARS_SIZE      sizeof(CHARS)
24
25 #ifdef UNIT_TEST
26 #include <stdlib.h> /* malloc */
27 #endif
28
29 static char Errmsg[80];
30
31 int
32 dataasciigen(listofchars, buffer, bsize, offset)
33 char *listofchars;      /* a null terminated list of characters */
34 char *buffer;
35 int bsize;
36 int offset;
37 {
38    int cnt;
39    int total;
40    int ind;     /* index into CHARS array */
41    char *chr;
42    int chars_size;
43    char *charlist;
44
45         chr=buffer;
46         total=offset+bsize;
47
48         if ( listofchars == NULL ) {
49             charlist=CHARS;
50             chars_size=CHARS_SIZE;
51         }
52         else {
53             charlist=listofchars;
54             chars_size=strlen(listofchars);
55         }
56
57         for(cnt=offset; cnt<total;  cnt++) {
58                 ind=cnt%chars_size;
59                 *chr++=charlist[ind];
60         }
61
62         return bsize;
63
64 }       /* end of dataasciigen */
65
66 int
67 dataasciichk(listofchars, buffer, bsize, offset, errmsg)
68 char *listofchars;      /* a null terminated list of characters */
69 char *buffer;
70 int bsize;
71 int offset;
72 char **errmsg;
73 {
74    int cnt;
75    int total;
76    int ind;     /* index into CHARS array */
77    char *chr;
78    int chars_size;
79    char *charlist;
80
81         chr=buffer;
82         total=offset+bsize;
83
84         if ( listofchars == NULL ) {
85             charlist=CHARS;
86             chars_size=CHARS_SIZE;
87         }
88         else {
89             charlist=listofchars;
90             chars_size=strlen(listofchars);
91         }
92
93         if ( errmsg != NULL ) {
94             *errmsg = Errmsg;
95         }
96
97         for(cnt=offset; cnt<total;  chr++, cnt++) {
98             ind=cnt%chars_size;
99             if ( *chr != charlist[ind] ) {
100                 sprintf(Errmsg,
101                     "data mismatch at offset %d, exp:%#o, act:%#o", cnt,
102                     charlist[ind], *chr);
103                 return cnt;
104             }
105         }
106
107         sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
108         return -1;      /* buffer is ok */
109
110 }       /* end of dataasciichk */
111
112
113 #if UNIT_TEST
114
115 /***********************************************************************
116  * main for doing unit testing
117  ***********************************************************************/
118 int
119 main(ac, ag)
120 int ac;
121 char **ag;
122 {
123
124 int size=1023;
125 char *buffer;
126 int ret;
127 char *errmsg;
128
129     if ((buffer=(char *)malloc(size)) == NULL ) {
130         perror("malloc");
131         exit(2);
132     }
133
134     dataasciigen(NULL, buffer, size, 0);
135     printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
136
137     ret=dataasciichk(NULL, buffer, size, 0, &errmsg);
138     printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
139         size, ret, errmsg);
140
141     if ( ret == -1 )
142         printf("\tPASS return value is -1 as expected\n");
143     else
144         printf("\tFAIL return value is %d, expected -1\n", ret);
145
146     ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
147     printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
148         size-1, ret, errmsg);
149
150     if ( ret == -1 )
151         printf("\tPASS return value is -1 as expected\n");
152     else
153         printf("\tFAIL return value is %d, expected -1\n", ret);
154
155     buffer[25]= 0x0;
156     printf("changing char 25\n");
157
158     ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
159     printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
160         size-1, ret, errmsg);
161
162     if ( ret == 25 )
163         printf("\tPASS return value is 25 as expected\n");
164     else
165         printf("\tFAIL return value is %d, expected 25\n", ret);
166
167     dataasciigen("this is a test of the my string" , buffer, size, 0);
168     printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n", size);
169
170     ret=dataasciichk("this is a test of the my string", buffer, size, 0, &errmsg);
171     printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
172         size, ret, errmsg);
173
174     if ( ret == -1 )
175         printf("\tPASS return value is -1 as expected\n");
176     else
177         printf("\tFAIL return value is %d, expected -1\n", ret);
178
179     ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
180     printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
181         size-1, ret, errmsg);
182
183     if ( ret == -1 )
184         printf("\tPASS return value is -1 as expected\n");
185     else
186         printf("\tFAIL return value is %d, expected -1\n", ret);
187
188     buffer[25]= 0x0;
189     printf("changing char 25\n");
190
191     ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
192     printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
193         size-1, ret, errmsg);
194
195     if ( ret == 25 )
196         printf("\tPASS return value is 25 as expected\n");
197     else
198         printf("\tFAIL return value is %d, expected 25\n", ret);
199
200     exit(0);
201 }
202
203 #endif
204