Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/vczip/tests/tvchuff.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
static int Freq[256] =
24
{
25
155, 18, 15, 4, 2, 2, 0, 3, 14, 5, 3, 4, 17, 1, 0, 3,
26
136, 2, 5, 5, 5, 1, 2, 3, 11, 2, 4, 2, 14, 13, 12, 13,
27
12, 9, 7, 1, 17, 4, 0, 4, 16, 1, 2, 4, 9, 2, 2, 2,
28
20, 1, 0, 1, 4, 2, 2, 3, 7, 5, 0, 0, 4, 4, 2, 1,
29
7, 1, 7, 2, 9, 7, 11, 8, 23, 3, 3, 1, 5, 2, 2, 4,
30
13, 0, 0, 1, 21, 1, 1, 2, 9, 1, 0, 0, 3, 2, 1, 3,
31
9, 0, 1, 6, 7, 0, 3, 0, 3, 1, 0, 2, 13, 0, 2, 0,
32
6, 1, 1, 5, 1, 0, 0, 0, 3, 0, 2, 0, 1, 2, 1, 2,
33
10, 4, 0, 1, 2, 0, 1, 2, 7, 2, 0, 0, 3, 1, 3, 1,
34
10, 2, 0, 2, 3, 2, 3, 4, 3, 0, 2, 3, 18, 3, 3, 1,
35
5, 4, 3, 5, 7, 8, 6, 2, 3, 4, 0, 3, 16, 0, 6, 2,
36
16, 4, 2, 1, 4, 2, 0, 4, 14, 3, 0, 0, 1, 0, 1, 0,
37
5, 1, 1, 4, 5, 1, 2, 2, 9, 1, 1, 1, 9, 1, 1, 0,
38
7, 1, 2, 0, 12, 1, 1, 1, 14, 0, 1, 1, 2, 3, 2, 5,
39
7, 1, 10, 1, 4, 0, 1, 3, 3, 0, 2, 2, 4, 2, 1, 2,
40
10, 0, 0, 1, 4, 0, 2, 0, 6, 1, 0, 1, 2, 0, 0, 10
41
};
42
43
int main()
44
{
45
char *tar, *cmp, *t, *b, *endb;
46
Vcodex_t *vch, *vcu;
47
ssize_t k, n, ntar;
48
static char buf[1024*1024];
49
50
if(!(vch = vcopen(0, Vchuffman, 0, 0, VC_ENCODE)) )
51
terror("Cannot open Vchuff handle");
52
if(!(vcu = vcopen(0, Vchuffman, 0, 0, VC_DECODE)) )
53
terror("Cannot open Vchuff handle to decode");
54
55
/* test a few boundary cases */
56
tar = "000000000000000011111122222abc012000000111111222222012abc";
57
ntar = strlen(tar);
58
if((n = vcapply(vch, tar, 0, &cmp)) < 0)
59
terror("Vchuff failed0");
60
if((n = vcapply(vcu, cmp, n, &t)) != 0)
61
terror("Vchuff failed0 decoding");
62
63
if((n = vcapply(vch, tar, 1, &cmp)) < 0)
64
terror("Vchuff failed1");
65
if((n = vcapply(vcu, cmp, n, &t)) != 1)
66
terror("Vchuff failed1 decoding");
67
if(t[0] != tar[0])
68
terror("Bad byte");
69
70
if((n = vcapply(vch, tar, 16, &cmp)) < 0) /* the string of all zeros */
71
terror("Vchuff failed2");
72
if((n = vcapply(vcu, cmp, n, &t)) != 16)
73
terror("Vchuff failed2 decoding");
74
for(k = 0; k < 16; ++k)
75
if(t[k] != tar[k])
76
terror("Bad byte");
77
78
/* test the original learning algorithm */
79
if((n = vcapply(vch, tar, ntar, &cmp)) <= 0)
80
terror("Vchuff failed");
81
if((n = vcapply(vcu, cmp, n, &t)) != ntar )
82
terror("Vchuff returns wrong size");
83
for(k = 0; k < n; ++k)
84
if(t[k] != tar[k])
85
terror("Vchuff computed bad byte");
86
87
/* generate a large array of data using the frequency profile */
88
b = buf;
89
for(k = 0; k < 256; ++k)
90
{ for(n = 0; n < Freq[k]; ++n)
91
*b++ = k;
92
}
93
if((n = vcapply(vch, buf, b-buf, &cmp)) < 0)
94
terror("Vchuff failed8");
95
if((n = vcapply(vcu, cmp, n, &t)) != b-buf)
96
terror("Vchuff failed8 decoding");
97
for(k = 0; k < b-buf; ++k)
98
if(t[k] != buf[k])
99
terror("Bad byte8");
100
101
/* test grouping method */
102
b = buf; endb = buf + sizeof(buf);
103
while(b < endb)
104
{ for(k = 0; k < 256 && b < endb; ++k)
105
{ for(n = 0; n < Freq[k] && b < endb; ++n)
106
*b++ = k;
107
}
108
}
109
if(!(vch = vcopen(0, Vchuffgroup, 0, 0, VC_ENCODE)) )
110
terror("Cannot open Vchuffgroup handle");
111
if(!(vcu = vcopen(0, Vchuffgroup, 0, 0, VC_DECODE)) )
112
terror("Cannot open Vchuffgroup handle decoding ");
113
if((n = vcapply(vch, buf, b-buf, &cmp)) < 0)
114
terror("Vchuffgroup failed");
115
if((n = vcapply(vcu, cmp, n, &t)) != b-buf)
116
terror("Vchuffgroup failed decoding");
117
for(k = 0; k < b-buf; ++k)
118
if(t[k] != buf[k])
119
terror("Bad byte");
120
121
/* test partitioning method */
122
if(!(vch = vcopen(0, Vchuffpart, 0, 0, VC_ENCODE)) )
123
terror("Cannot open Vchuffpart handle");
124
if(!(vcu = vcopen(0, Vchuffpart, 0, 0, VC_DECODE)) )
125
terror("Cannot open Vchuffpart handle decoding");
126
if((n = vcapply(vch, buf, b-buf, &cmp)) < 0)
127
terror("Vchuffpart failed");
128
if((n = vcapply(vcu, cmp, n, &t)) != b-buf)
129
terror("Vchuffpart failed decoding");
130
for(k = 0; k < b-buf; ++k)
131
if(t[k] != buf[k])
132
terror("Bad byte");
133
134
exit(0);
135
}
136
137