splay.js 1.9 KB

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