Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/vczip/tests/tvcrdb.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
#include "vctest.h"
22
#include <vcrdb.h>
23
24
/* Test the suite of functions for dealing with relational data
25
**
26
** Written by Kiem-Phong Vo
27
*/
28
29
#define N_RECORDS 50
30
31
#define FSEP ':'
32
#define RSEP '\n'
33
34
/* dependency between two strings, ie, str1 and str2 always correspond */
35
typedef struct _depend_s
36
{ Vcchar_t* str1;
37
ssize_t len1;
38
Vcchar_t* str2;
39
ssize_t len2;
40
} Depend_t;
41
42
Depend_t Field01[] =
43
{ { (Vcchar_t*)"aaaaa", 5, (Vcchar_t*)"AAAAAA", 6 },
44
{ (Vcchar_t*)"bbbbb", 5, (Vcchar_t*)"BBBBBB", 6 },
45
{ (Vcchar_t*)"ccccc", 5, (Vcchar_t*)"CCCCCC", 6 },
46
{ (Vcchar_t*)"ddddd", 5, (Vcchar_t*)"DDDDDD", 6 },
47
{ (Vcchar_t*)"eeeee", 5, (Vcchar_t*)"EEEEEE", 6 }
48
};
49
50
Depend_t Field23[] =
51
{ { (Vcchar_t*)"iiiii", 5, (Vcchar_t*)"IIIIII", 6 },
52
{ (Vcchar_t*)"jjjjj", 5, (Vcchar_t*)"JJJJJJ", 6 },
53
{ (Vcchar_t*)"kkkkk", 5, (Vcchar_t*)"KKKKKK", 6 },
54
{ (Vcchar_t*)"lllll", 5, (Vcchar_t*)"LLLLLL", 6 },
55
{ (Vcchar_t*)"mmmmm", 5, (Vcchar_t*)"MMMMMM", 6 }
56
};
57
58
Depend_t Field45[] =
59
{ { (Vcchar_t*)"rrrrr", 5, (Vcchar_t*)"RRRRRR", 6 },
60
{ (Vcchar_t*)"sssss", 5, (Vcchar_t*)"SSSSSS", 6 },
61
{ (Vcchar_t*)"ttttt", 5, (Vcchar_t*)"TTTTTT", 6 },
62
{ (Vcchar_t*)"uuuuu", 5, (Vcchar_t*)"UUUUUU", 6 },
63
{ (Vcchar_t*)"vvvvv", 3, (Vcchar_t*)"VVVVVV", 6 }
64
};
65
66
Vcchar_t Data[N_RECORDS*60 + 1]; /* (#fields=6) * (maxLength=9) + (Separators=6) = 60 */
67
68
#ifdef DEBUG
69
#define LENGTH(maxn) (maxn)
70
#else
71
#define LENGTH(maxn) ((trandom() % (maxn)) + 1)
72
#endif
73
74
int main()
75
{
76
ssize_t e, k, r, n, dtsz;
77
Vcrdsepar_t sep[3];
78
Vcrdinfo_t info;
79
Vcrdtable_t *tbl, *tst;
80
Vcrdplan_t *plan;
81
Vcchar_t buff[6][sizeof(Data)]; /* for testing */
82
Vcchar_t test[sizeof(Data)];
83
Vcchar_t *endt, *dedt;
84
Vcodex_t *envc, *devc, *encd, *decd, *entbl, *detbl, *enbwt, *debwt;
85
Vcmethod_t *Vcrdb, *Vctable;
86
87
if (!(Vcrdb = vcgetmeth("rdb", 0)) || !(Vctable = vcgetmeth("table", 0)))
88
terror("rdb plugin not found");
89
/* generate a random set of records */
90
for(k = 0, r = 0; r < N_RECORDS; ++r)
91
{ /* fill in fields 0 and 1 */
92
e = trandom() % (sizeof(Field01)/sizeof(Field01[0]));
93
n = LENGTH(Field01[e].len1);
94
memcpy(Data+k, Field01[e].str1, n); k += n; Data[k] = FSEP; k += 1;
95
n += 1;
96
memcpy(Data+k, Field01[e].str2, n); k += n; Data[k] = FSEP; k += 1;
97
98
/* fill in fields 2 and 3 */
99
e = trandom() % (sizeof(Field23)/sizeof(Field23[0]));
100
n = LENGTH(Field23[e].len1);
101
memcpy(Data+k, Field23[e].str1, n); k += n; Data[k] = FSEP; k += 1;
102
n += 1;
103
memcpy(Data+k, Field23[e].str2, n); k += n; Data[k] = FSEP; k += 1;
104
105
/* fill in fields 4 and 5 */
106
e = trandom() % (sizeof(Field45)/sizeof(Field45[0]));
107
n = LENGTH(Field45[e].len1);
108
memcpy(Data+k, Field45[e].str1, n); k += n; Data[k] = FSEP; k += 1;
109
n += 1;
110
memcpy(Data+k, Field45[e].str2, n); k += n; Data[k] = RSEP; k += 1;
111
}
112
Data[dtsz = k] = 0;
113
114
/* see if vcrdsepar() computes the correct separators fsep and rsep */
115
if((r = vcrdsepar(sep, sizeof(sep)/sizeof(sep[0]), Data, dtsz, -1)) <= 0)
116
terror("vcrdsepar(): Failed to compute separators rv=%d", r);
117
if(sep[0].fsep != FSEP || sep[0].rsep != RSEP)
118
twarn("Unexpected separators found");
119
120
/* construct table */
121
memset(&info, 0, sizeof(info));
122
info.fsep = sep[0].fsep;
123
info.rsep = sep[0].rsep;
124
if(!(tbl = vcrdparse(&info, Data, dtsz, 0)) )
125
terror("vcrdparse(): Can't construct table");
126
if(tbl->fldn != 6 || tbl->recn != N_RECORDS)
127
terror("vcrdparse(): fldn=%d, recn=%d, something is not right", tbl->fldn, tbl->recn);
128
129
/* check different modes of extraction of field data */
130
if((k = vcrdextract(tbl, 3, buff[0], sizeof(buff[0]), VCRD_FIELD)) <= 0)
131
terror("vcrdextract(): failed to get a field data1");
132
if(vcrdattrs(tbl, -1, VCRD_PAD, 1) < 0)
133
terror("vcrdattrs(): can't make fields full length");
134
if((n = vcrdextract(tbl, 3, buff[0], sizeof(buff[0]), VCRD_FIELD)) <= 0)
135
terror("vcrdextract(): failed to get a field data2");
136
if(n == k)
137
twarn("vcrdextract(): unfilled=%d filled=%d", k, n);
138
if(n != tbl->fld[3].maxz*N_RECORDS)
139
terror("vcrdextract(): retrieved data size is wrong");
140
141
/* test table construction with filled data */
142
info.fldn = tbl->fldn;
143
info.recn = tbl->recn;
144
if(!(tst = vcrdparse(&info, 0, 0, 0)) )
145
terror("vcrdparse(): Can't open test table");
146
for(k = 0; k < tbl->fldn; ++k)
147
{ if((r = vcrdextract(tbl, k, buff[k], sizeof(buff[0]), VCRD_FIELD)) <= 0 )
148
terror("vcrdextract(): failed on field %d", k);
149
if((n = vcrdfield(tst, k, tbl->fld[k].maxz, buff[k], r)) != r)
150
terror("vcrdfield(): failed on field %d rv=%d", k, n);
151
vcrdattrs(tst, k, VCRD_PAD, 1);
152
}
153
154
/* extract padded data to see if the right size */
155
for(n = 0, k = 0; k < tst->fldn; ++k)
156
n += tst->fld[k].maxz*N_RECORDS;
157
r = vcrdextract(tst, -1, test, sizeof(test), VCRD_RECORD);
158
if(r != n)
159
terror("vcrdextract(): failed to extract data dtsz=%d rv=%d", n, r);
160
161
/* extract variable-sized data, should be the same as Data[] */
162
vcrdattrs(tst, -1, VCRD_PAD, 0); /* turn off full mode */
163
r = vcrdextract(tst, -1, test, sizeof(test), VCRD_RECORD);
164
if(r != dtsz )
165
terror("vcrdextract(): failed to extract data dtsz=%d rv=%d", dtsz, r);
166
if(memcmp(Data, test, dtsz) != 0)
167
terror("vcrdextract(): extracted data were bad");
168
169
/* test transformation plan making */
170
if(!(plan = vcrdmakeplan(tbl, 0)) )
171
terror("vcrdmakeplan() failed");
172
if(plan->fldn != tbl->fldn)
173
terror("vcrdmakeplan(): wrong number of field %d", plan->fldn);
174
if(plan->pred[1] != 0)
175
terror("vcrdmakeplan(): fields 0 did not predict field 1.");
176
if(plan->pred[3] != 2)
177
terror("vcrdmakeplan(): fields 2 did not predict field 3.");
178
if(plan->pred[5] != 4)
179
terror("vcrdmakeplan(): fields 4 did not predict field 5.");
180
181
/* test transformation plan execution */
182
if(vcrdexecplan(tbl, plan, VC_ENCODE) < 0)
183
terror("vcrdexecplan() encoding failed");
184
vcrdattrs(tbl, -1, VCRD_PAD, 0);
185
r = vcrdextract(tbl, -1, test, sizeof(test), VCRD_RECORD);
186
if(r != dtsz)
187
terror("vcrdextract(): failed after plan transformation");
188
if(vcrdexecplan(tbl, plan, VC_DECODE) < 0)
189
terror("vcrdexecplan() decoding failed");
190
r = vcrdextract(tbl, -1, test, sizeof(test), VCRD_RECORD);
191
if(r != dtsz)
192
terror("vcrdextract(): failed after plan transformation");
193
if(memcmp(Data, test, dtsz) != 0)
194
terror("vcrdextract(): extracted data were bad after transformation");
195
196
/* test internal field transformation */
197
for(k = 0; k < tbl->fldn; ++k)
198
{ if((r = vcrdextract(tbl, k, buff[0], sizeof(buff[0]), VCRD_RECORD)) <= 0)
199
terror("Cannot extract field 1");
200
if(vcrdvector(tbl, k, buff[1], sizeof(buff[1]), VC_ENCODE) < 0)
201
terror("Couldn't get tranformed data for field 1");
202
if(vcrdsize(tbl) != r)
203
terror("Got the wrong size of transformed data");
204
if(vcrdvector(tbl, k, buff[1], sizeof(buff[1]), VC_DECODE) < 0)
205
terror("Couldn't decode tranformed data for field 1");
206
if(vcrdsize(tbl) != r)
207
terror("Got the wrong size of untransformed data");
208
if(vcrdextract(tbl, k, buff[1], sizeof(buff[1]), VCRD_RECORD) != r)
209
terror("Cannot extract field 1 after decoding");
210
if(memcmp(buff[0], buff[1], r) != 0)
211
terror("Wrong data");
212
}
213
214
/* open secondary coders for the transform Vcrdb */
215
if(!(encd = vcopen(0, Vchuffgroup, 0, 0, VC_ENCODE)) ||
216
!(decd = vcopen(0, Vchuffgroup, 0, 0, VC_DECODE)) )
217
terror("vcopen(): can't open Vchuffgroup handles");
218
if(!(encd = vcopen(0, Vcrle, "0", encd, VC_ENCODE)) ||
219
!(decd = vcopen(0, Vcrle, "0", decd, VC_DECODE)) )
220
terror("vcopen(): can't open Vcrle handles");
221
if(!(encd = vcopen(0, Vcmtf, 0, encd, VC_ENCODE)) ||
222
!(decd = vcopen(0, Vcmtf, 0, decd, VC_DECODE)) )
223
terror("vcopen(): can't open Vcmtf handles");
224
225
if(!(entbl = vcopen(0, Vctable, 0, encd, VC_ENCODE)) ||
226
!(detbl = vcopen(0, Vctable, 0, decd, VC_DECODE)) )
227
terror("vcopen(): can't open Vctable handles");
228
229
if(!(enbwt = vcopen(0, Vcbwt, 0, encd, VC_ENCODE)) ||
230
!(debwt = vcopen(0, Vcbwt, 0, decd, VC_DECODE)) )
231
terror("vcopen(): can't open Vcbwt handles");
232
233
/* test compression by field only - no transformation */
234
if(!(envc = vcopen(0, Vcrdb, "sort", encd, VC_ENCODE)) ||
235
!(devc = vcopen(0, Vcrdb, "sort", decd, VC_DECODE)) )
236
terror("vcopen(): can't open Vcrdb handles");
237
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
238
terror("vcapply(): fail to encode");
239
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
240
terror("vcapply(): fail to decode");
241
if(memcmp(Data, dedt, n) != 0)
242
terror("Data do not match");
243
twarn("\tSorted field: Raw size=%d Compressed size=%d", n, e);
244
245
/* test compression by field only - no transformation */
246
if(!(envc = vcopen(0, Vcrdb, "plain", enbwt, VC_ENCODE)) ||
247
!(devc = vcopen(0, Vcrdb, "plain", debwt, VC_DECODE)) )
248
terror("vcopen(): can't open Vcrdb handles");
249
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
250
terror("vcapply(): fail to encode");
251
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
252
terror("vcapply(): fail to decode");
253
if(memcmp(Data, dedt, n) != 0)
254
terror("Data do not match");
255
twarn("\tPlain+bwt: Raw size=%d Compressed size=%d", n, e);
256
257
/* test compression by field with transformation */
258
if(!(envc = vcopen(0, Vcrdb, 0, enbwt, VC_ENCODE)) ||
259
!(devc = vcopen(0, Vcrdb, 0, debwt, VC_DECODE)) )
260
terror("vcopen(): can't open Vcrdb handles");
261
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
262
terror("vcapply(): fail to encode");
263
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
264
terror("vcapply(): fail to decode");
265
if(memcmp(Data, dedt, n) != 0)
266
terror("Data do not match");
267
twarn("\tTransform+bwt: Raw size=%d Compressed size=%d", n, e);
268
269
/* test compression by padded field */
270
if(!(envc = vcopen(0, Vcrdb, "pad", enbwt, VC_ENCODE)) ||
271
!(devc = vcopen(0, Vcrdb, "pad", debwt, VC_DECODE)) )
272
terror("vcopen(): can't open Vcrdb handles");
273
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
274
terror("vcapply(): fail to encode");
275
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
276
terror("vcapply(): fail to decode");
277
if(memcmp(Data, dedt, n) != 0)
278
terror("Data do not match");
279
twarn("\tTransform+Padded fields+bwt: Raw size=%d Compressed size=%d", n, e);
280
281
/* test compression by padded field plus the table compressor */
282
if(!(envc = vcopen(0, Vcrdb, "pad", entbl, VC_ENCODE)) ||
283
!(devc = vcopen(0, Vcrdb, "pad", detbl, VC_DECODE)) )
284
terror("vcopen(): can't open Vcrdb handles");
285
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
286
terror("vcapply(): fail to encode");
287
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
288
terror("vcapply(): fail to decode");
289
if(memcmp(Data, dedt, n) != 0)
290
terror("Data do not match");
291
twarn("\tTransform+Padded fields+Vctable: Raw size=%d Compressed size=%d", n, e);
292
293
/* test compression by padded field plus the table compressor for whole table */
294
if(!(envc = vcopen(0, Vcrdb, "pad.whole", entbl, VC_ENCODE)) ||
295
!(devc = vcopen(0, Vcrdb, "pad.whole", detbl, VC_DECODE)) )
296
terror("vcopen(): can't open Vcrdb handles");
297
if((e = vcapply(envc, Data, dtsz, &endt)) <= 0 )
298
terror("vcapply(): fail to encode");
299
if((n = vcapply(devc, endt, e, &dedt)) != dtsz)
300
terror("vcapply(): fail to decode");
301
if(memcmp(Data, dedt, n) != 0)
302
terror("Data do not match");
303
twarn("\tTransform+Padded fields+Vctable on whole table: Raw size=%d Compressed size=%d", n, e);
304
305
/* test transformation of records with missing fields */
306
strcpy((char*)test, "1.2.3.4\na.b\n5.6.7.8\nc.d.\n"); k = strlen((char*)test);
307
308
if(!(envc = vcopen(0, Vcrdb, "fsep=[.]", entbl, VC_ENCODE)) ||
309
!(devc = vcopen(0, Vcrdb, "fsep=[.]", detbl, VC_DECODE)) )
310
terror("vcopen(): can't open Vcrdb handles");
311
if((e = vcapply(envc, test, k, &endt)) <= 0 )
312
terror("vcapply(): fail to encode");
313
if((n = vcapply(devc, endt, e, &dedt)) != k )
314
terror("vcapply(): fail to decode");
315
if(memcmp(test, dedt, k) != 0)
316
terror("Data do not match");
317
318
if(!(envc = vcopen(0, Vcrdb, "pad.fsep=[.]", entbl, VC_ENCODE)) ||
319
!(devc = vcopen(0, Vcrdb, "pad.fsep=[.]", detbl, VC_DECODE)) )
320
terror("vcopen(): can't open Vcrdb handles");
321
if((e = vcapply(envc, test, k, &endt)) <= 0 )
322
terror("vcapply(): fail to encode");
323
if((n = vcapply(devc, endt, e, &dedt)) != k )
324
terror("vcapply(): fail to decode");
325
if(memcmp(test, dedt, k) != 0)
326
terror("Data do not match");
327
328
return 0;
329
}
330
331