Debian Codegraph @

dojox/encoding/tests/compression/lzw.js

raw symbols SCIP
1 define(['doh', '../../compression/lzw', '../../bits'], function(doh, dcl, dcb){
2 var msg1 = "The rain in Spain falls mainly on the plain.";
3 var msg2 = "The rain in Spain falls mainly on the plain.1";
4 var msg3 = "The rain in Spain falls mainly on the plain.ab";
5 var msg4 = "The rain in Spain falls mainly on the plain.!@#";
6
7 var s2b = function(s){
8 var b = [];
9 for(var i = 0; i < s.length; ++i){
10 b.push(s.charCodeAt(i));
11 }
12 return b;
13 };
14
15 var b2s = function(b){
16 var s = [];
17 dojo.forEach(b, function(c){ s.push(String.fromCharCode(c)); });
18 return s.join("");
19 };
20
21 var encode = function(msg){
22 var x = new dcb.OutputStream(), encoder = new dcl.Encoder(128);
23 dojo.forEach(s2b(msg), function(v){ encoder.encode(v, x); });
24 encoder.flush(x);
25 console.debug("bits =", x.getWidth());
26 return x.getBuffer();
27 };
28
29 var decode = function(n, buf){
30 var x = new dcb.InputStream(buf, buf.length * 8), decoder = new dcl.Decoder(128), t = [], w = 0;
31 while(w < n){
32 var v = decoder.decode(x);
33 t.push(v);
34 w += v.length;
35 }
36 return t.join("");
37 };
38
39 doh.register("dojox.encoding.tests.compression.lzw", [
40 function testLzwMsg1(t){ t.assertEqual(msg1, decode(msg1.length, encode(msg1))); },
41 function testLzwMsg2(t){ t.assertEqual(msg2, decode(msg2.length, encode(msg2))); },
42 function testLzwMsg3(t){ t.assertEqual(msg3, decode(msg3.length, encode(msg3))); },
43 function testLzwMsg4(t){ t.assertEqual(msg4, decode(msg4.length, encode(msg4))); }
44 ]);
45 });