1
0

test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. sjcl.test = { vector: {}, all: {} };
  2. /* A bit of a hack. Because sjcl.test will be reloaded several times
  3. * for different variants of sjcl, but browserUtils will not, this
  4. * variable keeps a permanent record of whether anything has failed.
  5. */
  6. if (typeof browserUtil.allPassed === 'undefined') {
  7. browserUtil.allPassed = true;
  8. }
  9. sjcl.test.TestCase = function(name, doRun) {
  10. this.doRun = doRun;
  11. this.name = name;
  12. this.passes = 0;
  13. this.failures = 0;
  14. this.isUnimplemented = false;
  15. sjcl.test.all[name] = this;
  16. };
  17. sjcl.test.TestCase.prototype = {
  18. /** Pass some subtest of this test */
  19. pass: function () { this.passes ++; },
  20. /** Fail some subtest of this test */
  21. fail: function (message) {
  22. if (message !== undefined) {
  23. this.log("fail", "*** FAIL *** " + this.name + ": " + message);
  24. } else {
  25. this.log("fail", "*** FAIL *** " + this.name);
  26. }
  27. this.failures ++;
  28. browserUtil.allPassed = false;
  29. },
  30. unimplemented: function() {
  31. this.isUnimplemented = true;
  32. },
  33. /** Log a message to the console */
  34. log: browserUtil.write,
  35. /** Require that the first argument is true; otherwise fail with the given message */
  36. require: function (bool, message) {
  37. if (bool) {
  38. this.pass();
  39. } else if (message !== undefined) {
  40. this.fail(message);
  41. } else {
  42. this.fail("requirement failed");
  43. }
  44. },
  45. /** Pause and then take the specified action. */
  46. pauseAndThen: browserUtil.pauseAndThen,
  47. /** Continuation-passing-style iteration */
  48. cpsIterate: browserUtil.cpsIterate,
  49. /** Continuation-passing-style iteration */
  50. cpsMap: browserUtil.cpsMap,
  51. /** Report the results of this test. */
  52. report: function (repo) {
  53. var t = (new Date()).valueOf() - this.startTime;
  54. if (this.failures !== 0) {
  55. repo.update("fail", "failed " + this.failures + " / " +
  56. (this.passes + this.failures) + " tests. (" + t + " ms)");
  57. } else if (this.passes === 1) {
  58. repo.update("pass", "passed. (" + t + " ms)");
  59. } else if (this.isUnimplemented) {
  60. repo.update("unimplemented", "unimplemented");
  61. } else {
  62. repo.update("pass", "passed all " + this.passes + " tests. (" + t + " ms)");
  63. }
  64. browserUtil.writeNewline();
  65. },
  66. /** Run the test. */
  67. run: function (ntests, i, cb) {
  68. var thiz = this, repo = this.log("info", "Running " + this.name + "...");
  69. this.startTime = (new Date()).valueOf();
  70. this.pauseAndThen(function () {
  71. thiz.doRun(function () {
  72. thiz.report(repo);
  73. cb && cb();
  74. })
  75. });
  76. }
  77. };
  78. // pass a list of tests to run, or pass nothing and it will run them all
  79. sjcl.test.run = function (tests, callback) {
  80. var t;
  81. if (tests === undefined || tests.length == 0) {
  82. tests = [];
  83. for (t in sjcl.test.all) {
  84. if (sjcl.test.all.hasOwnProperty(t)) {
  85. tests.push(t);
  86. }
  87. }
  88. }
  89. browserUtil.cpsMap(function (t, i, n, cb) {
  90. sjcl.test.all[tests[i]].run(n, i+1, cb);
  91. }, tests, true, callback);
  92. };
  93. /* Several test scripts rely on sjcl.codec.hex to parse their test
  94. * vectors, but we are not guaranteed that sjcl.codec.hex is
  95. * implemented.
  96. */
  97. sjcl.codec = sjcl.codec || {};
  98. sjcl.codec.hex = sjcl.codec.hex ||
  99. {
  100. fromBits: function (arr) {
  101. var out = "", i, x;
  102. for (i=0; i<arr.length; i++) {
  103. out += ((arr[i]|0)+0xF00000000000).toString(16).substr(4);
  104. }
  105. return out.substr(0, sjcl.bitArray.bitLength(arr)/4);//.replace(/(.{8})/g, "$1 ");
  106. },
  107. toBits: function (str) {
  108. var i, out=[], len;
  109. str = str.replace(/\s|0x/g, "");
  110. len = str.length;
  111. str = str + "00000000";
  112. for (i=0; i<str.length; i+=8) {
  113. out.push(parseInt(str.substr(i,8),16)^0);
  114. }
  115. return sjcl.bitArray.clamp(out, len*4);
  116. }
  117. };