Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/vczip/tests/tio.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
23
/* This test various forms of integer codings
24
**
25
** Written by Kiem-Phong Vo
26
*/
27
28
int main()
29
{
30
#define N_LIST 1024
31
Vcchar_t buf[1024*N_LIST];
32
Vcio_t io;
33
Vcint_t v, g, list[N_LIST], copy[N_LIST];
34
35
/* test gamma coding */
36
vcioinit(&io, buf, sizeof(buf));
37
vciosetb(&io, io.bits, io.nbits, VC_ENCODE);
38
if(vcioputg(&io, 1) < 0)
39
terror("vcioputg 1\n");
40
if(vcioputg(&io, 11) < 0)
41
terror("vcioputg 11\n");
42
if(vcioputg(&io, 101) < 0)
43
terror("vcioputg 101\n");
44
if(vcioputg(&io, 1) < 0)
45
terror("vcioputg 1\n");
46
if(vcioputg(&io, 1001) < 0)
47
terror("vcioputg 1001\n");
48
if(vcioputg(&io, 10001) < 0)
49
terror("vcioputg 10001\n");
50
if(vcioputg(&io, 1) < 0)
51
terror("vcioputg 1\n");
52
if(vcioputg(&io, 100001) < 0)
53
terror("vcioputg 100001\n");
54
vcioendb(&io, io.bits, io.nbits, VC_ENCODE);
55
56
io.next = io.data;
57
vciosetb(&io, io.bits, io.nbits, VC_DECODE);
58
if((v = vciogetg(&io)) != 1)
59
terror("vciogetg 1 != %d\n", (int)v);
60
if((v = vciogetg(&io)) != 11)
61
terror("vciogetg 11 != %d\n", (int)v);
62
if((v = vciogetg(&io)) != 101)
63
terror("vciogetg 101 != %d\n", (int)v);
64
if((v = vciogetg(&io)) != 1)
65
terror("vciogetg 1 != %d\n", (int)v);
66
if((v = vciogetg(&io)) != 1001)
67
terror("vciogetg 1001 != %d\n", (int)v);
68
if((v = vciogetg(&io)) != 10001)
69
terror("vciogetg 10001 != %d\n", (int)v);
70
if((v = vciogetg(&io)) != 1)
71
terror("vciogetg 1 != %d\n", (int)v);
72
if((v = vciogetg(&io)) != 100001)
73
terror("vciogetg 100001 != %d\n", (int)v);
74
vcioendb(&io, io.bits, io.nbits, VC_DECODE);
75
76
/* Generate a list of positive integers.
77
** Positivity is needed to test the gamma code which does not handle zero.
78
*/
79
list[0] = 2; list[1] = 6; list[2] = 9; list[3] = 7; list[4] = 2;
80
for(v = 5; v < N_LIST; )
81
{ int k, inc;
82
inc = random()%2;
83
for(k = random()%7 + 1; k > 0 && v < N_LIST; --k, ++v)
84
{ if(inc)
85
list[v] = list[v-1] + (random()%17);
86
else list[v] = list[v-1] - (random()%53);
87
if(list[v] <= 0)
88
list[v] = random()%37 + 1;
89
}
90
}
91
memcpy(copy,list,N_LIST*sizeof(Vcint_t));
92
93
/* check coding size of original list using base-128 coding */
94
vcioinit(&io, buf, sizeof(buf));
95
for(v = 0; v < N_LIST; ++v)
96
vcioputu(&io, list[v]);
97
twarn("vcioputu %d: output size=%d\n", N_LIST, vciosize(&io));
98
99
vcioinit(&io, buf, sizeof(buf));
100
for(v = 0; v < N_LIST; ++v)
101
if((g = vciogetu(&io)) != list[v])
102
terror("Bad vciogetu %d", g);
103
104
/* check coding size of original list using gamma coding */
105
vcioinit(&io, buf, sizeof(buf));
106
vciosetb(&io, io.bits, io.nbits, VC_ENCODE);
107
for(v = 0; v < N_LIST; ++v)
108
vcioputg(&io, list[v]);
109
vcioendb(&io, io.bits, io.nbits, VC_ENCODE);
110
twarn("vcioputg %d: output size=%d\n", N_LIST, vciosize(&io));
111
112
vcioinit(&io, buf, sizeof(buf));
113
vciosetb(&io, io.bits, io.nbits, VC_DECODE);
114
for(v = 0; v < N_LIST; ++v)
115
if((g = vciogetg(&io)) != list[v])
116
terror("Bad vciogetg %d", g);
117
vcioendb(&io, io.bits, io.nbits, VC_DECODE);
118
119
/* test integer list io */
120
vcioinit(&io, buf, sizeof(buf));
121
if((v = vcioputlist(&io, list, N_LIST)) < 0)
122
terror("vcioputlist %d\n", (int)v);
123
twarn("vcioputlist %d: output size=%d\n", N_LIST, v);
124
125
for(v = 0; v < N_LIST; ++v)
126
list[v] = -1;
127
vcioinit(&io, buf, sizeof(buf));
128
if((v = vciogetlist(&io, list, N_LIST)) != N_LIST)
129
terror("vciogetlist %d\n", (int)v);
130
for(v = 0; v < N_LIST; ++v)
131
if(list[v] != copy[v])
132
terror("Wrong value %d\n", (int)list[v]);
133
134
exit(0);
135
}
136
137