suite.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2013 Daniel Wirtz <dcode@dcode.io>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS-IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var Long = require(__dirname+"/../index.js"),
  14. gmLong = require("./goog.math.long.js");
  15. var suite = {
  16. "basic": function(test) {
  17. var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
  18. test.equal(longVal.toNumber(), 9223372036854775807);
  19. test.equal(longVal.toString(), "9223372036854775807");
  20. var longVal2 = Long.fromValue(longVal);
  21. test.equal(longVal2.toNumber(), 9223372036854775807);
  22. test.equal(longVal2.toString(), "9223372036854775807");
  23. test.equal(longVal2.unsigned, longVal.unsigned);
  24. test.done();
  25. },
  26. "isLong": function(test) {
  27. var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
  28. test.strictEqual(Long.isLong(longVal), true);
  29. longVal = {"__isLong__": true};
  30. test.strictEqual(Long.isLong(longVal), true);
  31. test.done();
  32. },
  33. // Let's assume goog.math.Long has been tested properly and focus on our extensions:
  34. "toString": function(test) {
  35. var longVal = Long.fromBits(0xFFFFFFFF, 0xFFFFFFFF, true);
  36. // #10
  37. test.equal(longVal.toString(16), "ffffffffffffffff");
  38. test.equal(longVal.toString(10), "18446744073709551615");
  39. test.equal(longVal.toString(8), "1777777777777777777777");
  40. // #7, obviously wrong in goog.math.Long
  41. test.equal(Long.fromString("zzzzzz", 36).toString(36), "zzzzzz");
  42. test.equal(Long.fromString("-zzzzzz", 36).toString(36), "-zzzzzz");
  43. test.done();
  44. },
  45. "toBytes": function(test) {
  46. var longVal = Long.fromBits(0x01234567, 0x12345678);
  47. test.deepEqual(longVal.toBytesBE(), [
  48. 0x12, 0x34, 0x56, 0x78,
  49. 0x01, 0x23, 0x45, 0x67
  50. ]);
  51. test.deepEqual(longVal.toBytesLE(), [
  52. 0x67, 0x45, 0x23, 0x01,
  53. 0x78, 0x56, 0x34, 0x12
  54. ]);
  55. test.done();
  56. },
  57. "unsigned": {
  58. "min/max": function(test) {
  59. test.equal(Long.MIN_VALUE.toString(), "-9223372036854775808");
  60. test.equal(Long.MAX_VALUE.toString(), "9223372036854775807");
  61. test.equal(Long.MAX_UNSIGNED_VALUE.toString(), "18446744073709551615");
  62. test.done();
  63. },
  64. "construct_negint": function(test) {
  65. var longVal = Long.fromInt(-1, true);
  66. test.equal(longVal.low, -1);
  67. test.equal(longVal.high, -1);
  68. test.equal(longVal.unsigned, true);
  69. test.equal(longVal.toNumber(), 18446744073709551615);
  70. test.equal(longVal.toString(), "18446744073709551615");
  71. test.done();
  72. },
  73. "construct_highlow": function(test) {
  74. var longVal = new Long(0xFFFFFFFF, 0xFFFFFFFF, true);
  75. test.equal(longVal.low, -1);
  76. test.equal(longVal.high, -1);
  77. test.equal(longVal.unsigned, true);
  78. test.equal(longVal.toNumber(), 18446744073709551615);
  79. test.equal(longVal.toString(), "18446744073709551615");
  80. test.done();
  81. },
  82. "construct_number": function(test) {
  83. var longVal = Long.fromNumber(0xFFFFFFFFFFFFFFFF, true);
  84. test.equal(longVal.low, -1);
  85. test.equal(longVal.high, -1);
  86. test.equal(longVal.unsigned, true);
  87. test.equal(longVal.toNumber(), 18446744073709551615);
  88. test.equal(longVal.toString(), "18446744073709551615");
  89. test.done();
  90. },
  91. "toSigned/Unsigned": function(test) {
  92. var longVal = Long.fromNumber(-1, false);
  93. test.equal(longVal.toNumber(), -1);
  94. longVal = longVal.toUnsigned();
  95. test.equal(longVal.toNumber(), 0xFFFFFFFFFFFFFFFF);
  96. test.equal(longVal.toString(16), 'ffffffffffffffff');
  97. longVal = longVal.toSigned();
  98. test.equal(longVal.toNumber(), -1);
  99. test.done();
  100. },
  101. "max_unsigned_sub_max_signed": function(test) {
  102. var longVal = Long.MAX_UNSIGNED_VALUE.subtract(Long.MAX_VALUE).subtract(Long.ONE);
  103. test.equal(longVal.toNumber(), Long.MAX_VALUE.toNumber());
  104. test.equal(longVal.toString(), Long.MAX_VALUE.toString());
  105. test.done();
  106. },
  107. "max_sub_max": function(test) {
  108. var longVal = Long.MAX_UNSIGNED_VALUE.subtract(Long.MAX_UNSIGNED_VALUE);
  109. test.equal(longVal, 0);
  110. test.equal(longVal.low, 0);
  111. test.equal(longVal.high, 0);
  112. test.equal(longVal.unsigned, true);
  113. test.equal(longVal.toNumber(), 0);
  114. test.equal(longVal.toString(), "0");
  115. test.done();
  116. },
  117. "zero_sub_signed": function(test) {
  118. var longVal = Long.fromInt(0, true).add(Long.fromInt(-1, false));
  119. test.equal(longVal.low, -1);
  120. test.equal(longVal.high, -1);
  121. test.equal(longVal.unsigned, true);
  122. test.equal(longVal.toNumber(), 18446744073709551615);
  123. test.equal(longVal.toString(), "18446744073709551615");
  124. test.done();
  125. },
  126. "max_unsigned_div_max_signed": function(test) {
  127. var longVal = Long.MAX_UNSIGNED_VALUE.div(Long.MAX_VALUE);
  128. test.equal(longVal.toNumber(), 2);
  129. test.equal(longVal.toString(), "2");
  130. test.done();
  131. },
  132. "max_unsigned_div_max_unsigned": function(test) {
  133. var longVal = Long.MAX_UNSIGNED_VALUE;
  134. test.strictEqual(longVal.div(longVal).toString(), '1');
  135. test.done();
  136. },
  137. "max_unsigned_div_neg_signed": function(test) {
  138. var a = Long.MAX_UNSIGNED_VALUE;
  139. var b = Long.fromInt(-2);
  140. test.strictEqual(b.toUnsigned().toString(), Long.MAX_UNSIGNED_VALUE.sub(1).toString());
  141. var longVal = a.div(b);
  142. test.strictEqual(longVal.toString(), '1');
  143. test.done();
  144. },
  145. "min_signed_div_one": function(test) {
  146. var longVal = Long.MIN_VALUE.div(Long.ONE);
  147. test.strictEqual(longVal.toString(), Long.MIN_VALUE.toString());
  148. test.done();
  149. },
  150. "msb_unsigned": function(test) {
  151. var longVal = Long.UONE.shiftLeft(63);
  152. test.ok(longVal.notEquals(Long.MIN_VALUE));
  153. test.equal(longVal.toString(), "9223372036854775808");
  154. test.equal(Long.fromString("9223372036854775808", true).toString(), "9223372036854775808");
  155. test.done();
  156. },
  157. "issue31": function(test) {
  158. var a = new Long(0, 8, true);
  159. var b = Long.fromNumber(2656901066, true);
  160. test.strictEqual(a.unsigned, true);
  161. test.strictEqual(b.unsigned, true);
  162. var x = a.div(b);
  163. test.strictEqual(x.toString(), '12');
  164. test.strictEqual(x.unsigned, true);
  165. test.done();
  166. }
  167. }
  168. };
  169. module.exports = suite;