f4bbdede33f8c795eb89f85357126eb06462096a.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. if (!dojo._hasResource["dojox.encoding.tests.bits"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.encoding.tests.bits"] = true;
  7. dojo.provide("dojox.encoding.tests.bits");
  8. dojo.require("dojox.encoding.bits");
  9. (function() {
  10. var msg1 = "The rain in Spain falls mainly on the plain.";
  11. var msg2 = "The rain in Spain falls mainly on the plain.1";
  12. var msg3 = "The rain in Spain falls mainly on the plain.ab";
  13. var msg4 = "The rain in Spain falls mainly on the plain.!@#";
  14. var dcb = dojox.encoding.bits;
  15. var s2b = function(s) {
  16. var b = [];
  17. for (var i = 0; i < s.length; ++i) {
  18. b.push(s.charCodeAt(i));
  19. }
  20. return b;
  21. };
  22. var b2s = function(b) {
  23. var s = [];
  24. dojo.forEach(b, function(c) {
  25. s.push(String.fromCharCode(c));
  26. });
  27. return s.join("");
  28. };
  29. var testOut = function(msg) {
  30. var a = new dojox.encoding.bits.OutputStream();
  31. for (var i = 0; i < msg.length; ++i) {
  32. var v = msg.charCodeAt(i);
  33. var j = Math.floor(Math.random() * 7) + 1;
  34. a.putBits(v >>> (8 - j), j);
  35. a.putBits(v, 8 - j);
  36. }
  37. return b2s(a.getBuffer());
  38. };
  39. var testIn = function(msg) {
  40. var a = new dojox.encoding.bits.InputStream(s2b(msg), msg.length
  41. * 8);
  42. var r = [];
  43. for (var i = 0; i < msg.length; ++i) {
  44. var j = Math.floor(Math.random() * 7) + 1;
  45. r.push((a.getBits(j) << (8 - j)) | a.getBits(8 - j));
  46. }
  47. return b2s(r);
  48. };
  49. var test = function(msg) {
  50. var a = new dojox.encoding.bits.InputStream(s2b(msg), msg.length
  51. * 8);
  52. var o = new dojox.encoding.bits.OutputStream();
  53. while (a.getWidth() > 0) {
  54. var w = Math.min(a.getWidth(), 3);
  55. o.putBits(a.getBits(w), w);
  56. }
  57. return b2s(o.getBuffer());
  58. };
  59. tests.register("dojox.encoding.tests.bits", [function testBitsOut1(t) {
  60. t.assertEqual(msg1, testOut(msg1));
  61. }, function testBitsOut2(t) {
  62. t.assertEqual(msg2, testOut(msg2));
  63. }, function testBitsOut3(t) {
  64. t.assertEqual(msg3, testOut(msg3));
  65. }, function testBitsOut4(t) {
  66. t.assertEqual(msg4, testOut(msg4));
  67. }, function testBitsIn1(t) {
  68. t.assertEqual(msg1, testIn(msg1));
  69. }, function testBitsIn2(t) {
  70. t.assertEqual(msg2, testIn(msg2));
  71. }, function testBitsIn3(t) {
  72. t.assertEqual(msg3, testIn(msg3));
  73. }, function testBitsIn4(t) {
  74. t.assertEqual(msg4, testIn(msg4));
  75. }, function testBits1(t) {
  76. t.assertEqual(msg1, test(msg1));
  77. }, function testBits2(t) {
  78. t.assertEqual(msg2, test(msg2));
  79. }, function testBits3(t) {
  80. t.assertEqual(msg3, test(msg3));
  81. }, function testBits4(t) {
  82. t.assertEqual(msg4, test(msg4));
  83. }]);
  84. })();
  85. }