Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.encoding.tests.lzw"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.encoding.tests.lzw"] = true;
3
dojo.provide("dojox.encoding.tests.lzw");
4
dojo.require("dojox.encoding.lzw");
5
dojo.require("dojox.encoding.bits");
6
 
7
(function(){
8
	var msg1 = "The rain in Spain falls mainly on the plain.";
9
	var msg2 = "The rain in Spain falls mainly on the plain.1";
10
	var msg3 = "The rain in Spain falls mainly on the plain.ab";
11
	var msg4 = "The rain in Spain falls mainly on the plain.!@#";
12
	var dc = dojox.encoding, dcb = dc.bits, dcl = dc.lzw;
13
 
14
	var s2b = function(s){
15
		var b = [];
16
		for(var i = 0; i < s.length; ++i){
17
			b.push(s.charCodeAt(i));
18
		}
19
		return b;
20
	};
21
 
22
	var b2s = function(b){
23
		var s = [];
24
		dojo.forEach(b, function(c){ s.push(String.fromCharCode(c)); });
25
		return s.join("");
26
	};
27
 
28
	var encode = function(msg){
29
		var x = new dcb.OutputStream(), encoder = new dcl.Encoder(128);
30
		dojo.forEach(s2b(msg), function(v){ encoder.encode(v, x); });
31
		encoder.flush(x);
32
		console.debug("bits =", x.getWidth());
33
		return x.getBuffer();
34
	};
35
 
36
	var decode = function(n, buf){
37
		var x = new dcb.InputStream(buf, buf.length * 8), decoder = new dcl.Decoder(128), t = [], w = 0;
38
		while(w < n){
39
			var v = decoder.decode(x);
40
			t.push(v);
41
			w += v.length;
42
		}
43
		return t.join("");
44
	};
45
 
46
	tests.register("dojox.encoding.tests.lzw", [
47
		function testLzwMsg1(t){ t.assertEqual(msg1, decode(msg1.length, encode(msg1))); },
48
		function testLzwMsg2(t){ t.assertEqual(msg2, decode(msg2.length, encode(msg2))); },
49
		function testLzwMsg3(t){ t.assertEqual(msg3, decode(msg3.length, encode(msg3))); },
50
		function testLzwMsg4(t){ t.assertEqual(msg4, decode(msg4.length, encode(msg4))); }
51
	]);
52
})();
53
 
54
}