arith_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. /**
  31. * @fileoverview Test cases for Int64-manipulation functions.
  32. *
  33. * Test suite is written using Jasmine -- see http://jasmine.github.io/
  34. *
  35. * @author cfallin@google.com (Chris Fallin)
  36. */
  37. goog.require('goog.testing.asserts');
  38. goog.require('jspb.arith.Int64');
  39. goog.require('jspb.arith.UInt64');
  40. describe('binaryArithTest', function() {
  41. /**
  42. * Tests comparison operations.
  43. */
  44. it('testCompare', function() {
  45. var a = new jspb.arith.UInt64(1234, 5678);
  46. var b = new jspb.arith.UInt64(1234, 5678);
  47. assertEquals(a.cmp(b), 0);
  48. assertEquals(b.cmp(a), 0);
  49. b.lo -= 1;
  50. assertEquals(a.cmp(b), 1);
  51. assertEquals(b.cmp(a), -1);
  52. b.lo += 2;
  53. assertEquals(a.cmp(b), -1);
  54. assertEquals(b.cmp(a), 1);
  55. b.lo = a.lo;
  56. b.hi = a.hi - 1;
  57. assertEquals(a.cmp(b), 1);
  58. assertEquals(b.cmp(a), -1);
  59. assertEquals(a.zero(), false);
  60. assertEquals(a.msb(), false);
  61. assertEquals(a.lsb(), false);
  62. a.hi = 0;
  63. a.lo = 0;
  64. assertEquals(a.zero(), true);
  65. a.hi = 0x80000000;
  66. assertEquals(a.zero(), false);
  67. assertEquals(a.msb(), true);
  68. a.lo = 0x00000001;
  69. assertEquals(a.lsb(), true);
  70. });
  71. /**
  72. * Tests shifts.
  73. */
  74. it('testShifts', function() {
  75. var a = new jspb.arith.UInt64(1, 0);
  76. assertEquals(a.lo, 1);
  77. assertEquals(a.hi, 0);
  78. var orig = a;
  79. a = a.leftShift();
  80. assertEquals(orig.lo, 1); // original unmodified.
  81. assertEquals(orig.hi, 0);
  82. assertEquals(a.lo, 2);
  83. assertEquals(a.hi, 0);
  84. a = a.leftShift();
  85. assertEquals(a.lo, 4);
  86. assertEquals(a.hi, 0);
  87. for (var i = 0; i < 29; i++) {
  88. a = a.leftShift();
  89. }
  90. assertEquals(a.lo, 0x80000000);
  91. assertEquals(a.hi, 0);
  92. a = a.leftShift();
  93. assertEquals(a.lo, 0);
  94. assertEquals(a.hi, 1);
  95. a = a.leftShift();
  96. assertEquals(a.lo, 0);
  97. assertEquals(a.hi, 2);
  98. a = a.rightShift();
  99. a = a.rightShift();
  100. assertEquals(a.lo, 0x80000000);
  101. assertEquals(a.hi, 0);
  102. a = a.rightShift();
  103. assertEquals(a.lo, 0x40000000);
  104. assertEquals(a.hi, 0);
  105. });
  106. /**
  107. * Tests additions.
  108. */
  109. it('testAdd', function() {
  110. var a = new jspb.arith.UInt64(/* lo = */ 0x89abcdef,
  111. /* hi = */ 0x01234567);
  112. var b = new jspb.arith.UInt64(/* lo = */ 0xff52ab91,
  113. /* hi = */ 0x92fa2123);
  114. // Addition with carry.
  115. var c = a.add(b);
  116. assertEquals(a.lo, 0x89abcdef); // originals unmodified.
  117. assertEquals(a.hi, 0x01234567);
  118. assertEquals(b.lo, 0xff52ab91);
  119. assertEquals(b.hi, 0x92fa2123);
  120. assertEquals(c.lo, 0x88fe7980);
  121. assertEquals(c.hi, 0x941d668b);
  122. // Simple addition without carry.
  123. a.lo = 2;
  124. a.hi = 0;
  125. b.lo = 3;
  126. b.hi = 0;
  127. c = a.add(b);
  128. assertEquals(c.lo, 5);
  129. assertEquals(c.hi, 0);
  130. });
  131. /**
  132. * Test subtractions.
  133. */
  134. it('testSub', function() {
  135. var kLength = 10;
  136. var hiValues = [0x1682ef32,
  137. 0x583902f7,
  138. 0xb62f5955,
  139. 0x6ea99bbf,
  140. 0x25a39c20,
  141. 0x0700a08b,
  142. 0x00f7304d,
  143. 0x91a5b5af,
  144. 0x89077fd2,
  145. 0xe09e347c];
  146. var loValues = [0xe1538b18,
  147. 0xbeacd556,
  148. 0x74100758,
  149. 0x96e3cb26,
  150. 0x56c37c3f,
  151. 0xe00b3f7d,
  152. 0x859f25d7,
  153. 0xc2ee614a,
  154. 0xe1d21cd7,
  155. 0x30aae6a4];
  156. for (var i = 0; i < kLength; i++) {
  157. for (var j = 0; j < kLength; j++) {
  158. var a = new jspb.arith.UInt64(loValues[i], hiValues[j]);
  159. var b = new jspb.arith.UInt64(loValues[j], hiValues[i]);
  160. var c = a.add(b).sub(b);
  161. assertEquals(c.hi, a.hi);
  162. assertEquals(c.lo, a.lo);
  163. }
  164. }
  165. });
  166. /**
  167. * Tests 32-by-32 multiplication.
  168. */
  169. it('testMul32x32', function() {
  170. var testData = [
  171. // a b low(a*b) high(a*b)
  172. [0xc0abe2f8, 0x1607898a, 0x5de711b0, 0x109471b8],
  173. [0x915eb3cb, 0x4fb66d0e, 0xbd0d441a, 0x2d43d0bc],
  174. [0xfe4efe70, 0x80b48c37, 0xbcddea10, 0x7fdada0c],
  175. [0xe222fd4a, 0xe43d524a, 0xd5e0eb64, 0xc99d549c],
  176. [0xd171f469, 0xb94ebd01, 0x4be17969, 0x979bc4fa],
  177. [0x829cc1df, 0xe2598b38, 0xf4157dc8, 0x737c12ad],
  178. [0xf10c3767, 0x8382881e, 0x942b3612, 0x7bd428b8],
  179. [0xb0f6dd24, 0x232597e1, 0x079c98a4, 0x184bbce7],
  180. [0xfcdb05a7, 0x902f55bc, 0x636199a4, 0x8e69f412],
  181. [0x0dd0bfa9, 0x916e27b1, 0x6e2542d9, 0x07d92e65]
  182. ];
  183. for (var i = 0; i < testData.length; i++) {
  184. var a = testData[i][0] >>> 0;
  185. var b = testData[i][1] >>> 0;
  186. var cLow = testData[i][2] >>> 0;
  187. var cHigh = testData[i][3] >>> 0;
  188. var c = jspb.arith.UInt64.mul32x32(a, b);
  189. assertEquals(c.lo, cLow);
  190. assertEquals(c.hi, cHigh);
  191. }
  192. });
  193. /**
  194. * Tests 64-by-32 multiplication.
  195. */
  196. it('testMul', function() {
  197. // 64x32 bits produces 96 bits of product. The multiplication function under
  198. // test truncates the top 32 bits, so we compare against a 64-bit expected
  199. // product.
  200. var testData = [
  201. // low(a) high(a) low(a*b) high(a*b)
  202. [0xec10955b, 0x360eb168, 0x4b7f3f5b, 0xbfcb7c59, 0x9517da5f],
  203. [0x42b000fc, 0x9d101642, 0x6fa1ab72, 0x2584c438, 0x6a9e6d2b],
  204. [0xf42d4fb4, 0xae366403, 0xa65a1000, 0x92434000, 0x1ff978df],
  205. [0x17e2f56b, 0x25487693, 0xf13f98c7, 0x73794e2d, 0xa96b0c6a],
  206. [0x492f241f, 0x76c0eb67, 0x7377ac44, 0xd4336c3c, 0xfc4b1ebe],
  207. [0xd6b92321, 0xe184fa48, 0xd6e76904, 0x93141584, 0xcbf44da1],
  208. [0x4bf007ea, 0x968c0a9e, 0xf5e4026a, 0x4fdb1ae4, 0x61b9fb7d],
  209. [0x10a83be7, 0x2d685ba6, 0xc9e5fb7f, 0x2ad43499, 0x3742473d],
  210. [0x2f261829, 0x1aca681a, 0x3d3494e3, 0x8213205b, 0x283719f8],
  211. [0xe4f2ce21, 0x2e74b7bd, 0xd801b38b, 0xbc17feeb, 0xc6c44e0f]
  212. ];
  213. for (var i = 0; i < testData.length; i++) {
  214. var a = new jspb.arith.UInt64(testData[i][0], testData[i][1]);
  215. var prod = a.mul(testData[i][2]);
  216. assertEquals(prod.lo, testData[i][3]);
  217. assertEquals(prod.hi, testData[i][4]);
  218. }
  219. });
  220. /**
  221. * Tests 64-div-by-32 division.
  222. */
  223. it('testDiv', function() {
  224. // Compute a/b, yielding quot = a/b and rem = a%b.
  225. var testData = [
  226. // --- divisors in (0, 2^32-1) to test full divisor range
  227. // low(a) high(a) b low(quot) high(quot) rem
  228. [0x712443f1, 0xe85cefcc, 0xc1a7050b, 0x332c79ad, 0x00000001, 0x92ffa882],
  229. [0x11912915, 0xb2699eb5, 0x30467cbe, 0xb21b4be4, 0x00000003, 0x283465dd],
  230. [0x0d917982, 0x201f2a6e, 0x3f35bf03, 0x8217c8e4, 0x00000000, 0x153402d6],
  231. [0xa072c108, 0x74020c96, 0xc60568fd, 0x95f9613e, 0x00000000, 0x3f4676c2],
  232. [0xd845d5d8, 0xcdd235c4, 0x20426475, 0x6154e78b, 0x00000006, 0x202fb751],
  233. [0xa4dbf71f, 0x9e90465e, 0xf08e022f, 0xa8be947f, 0x00000000, 0xbe43b5ce],
  234. [0x3dbe627f, 0xa791f4b9, 0x28a5bd89, 0x1f5dfe93, 0x00000004, 0x02bf9ed4],
  235. [0x5c1c53ee, 0xccf5102e, 0x198576e7, 0x07e3ae31, 0x00000008, 0x02ea8fb7],
  236. [0xfef1e581, 0x04714067, 0xca6540c1, 0x059e73ec, 0x00000000, 0x31658095],
  237. [0x1e2dd90c, 0x13dd6667, 0x8b2184c3, 0x248d1a42, 0x00000000, 0x4ca6d0c6],
  238. // --- divisors in (0, 2^16-1) to test larger quotient high-words
  239. // low(a) high(a) b low(quot) high(quot) rem
  240. [0x86722b47, 0x2cd57c9a, 0x00003123, 0x2ae41b7a, 0x0000e995, 0x00000f99],
  241. [0x1dd7884c, 0xf5e839bc, 0x00009eeb, 0x5c886242, 0x00018c21, 0x000099b6],
  242. [0x5c53d625, 0x899fc7e5, 0x000087d7, 0xd625007a, 0x0001035c, 0x000019af],
  243. [0x6932d932, 0x9d0a5488, 0x000051fb, 0x9d976143, 0x0001ea63, 0x00004981],
  244. [0x4d18bb85, 0x0c92fb31, 0x00001d9f, 0x03265ab4, 0x00006cac, 0x000001b9],
  245. [0xbe756768, 0xdea67ccb, 0x00008a03, 0x58add442, 0x00019cff, 0x000056a2],
  246. [0xe2466f9a, 0x2521f114, 0x0000c350, 0xa0c0860d, 0x000030ab, 0x0000a48a],
  247. [0xf00ddad1, 0xe2f5446a, 0x00002cfc, 0x762697a6, 0x00050b96, 0x00000b69],
  248. [0xa879152a, 0x0a70e0a5, 0x00007cdf, 0xb44151b3, 0x00001567, 0x0000363d],
  249. [0x7179a74c, 0x46083fff, 0x0000253c, 0x4d39ba6e, 0x0001e17f, 0x00000f84]
  250. ];
  251. for (var i = 0; i < testData.length; i++) {
  252. var a = new jspb.arith.UInt64(testData[i][0], testData[i][1]);
  253. var result = a.div(testData[i][2]);
  254. var quotient = result[0];
  255. var remainder = result[1];
  256. assertEquals(quotient.lo, testData[i][3]);
  257. assertEquals(quotient.hi, testData[i][4]);
  258. assertEquals(remainder.lo, testData[i][5]);
  259. }
  260. });
  261. /**
  262. * Tests .toString() and .fromString().
  263. */
  264. it('testStrings', function() {
  265. var testData = [
  266. [0x5e84c935, 0xcae33d0e, '14619595947299359029'],
  267. [0x62b3b8b8, 0x93480544, '10612738313170434232'],
  268. [0x319bfb13, 0xc01c4172, '13843011313344445203'],
  269. [0x5b8a65fb, 0xa5885b31, '11927883880638080507'],
  270. [0x6bdb80f1, 0xb0d1b16b, '12741159895737008369'],
  271. [0x4b82b442, 0x2e0d8c97, '3318463081876730946'],
  272. [0x780d5208, 0x7d76752c, '9040542135845999112'],
  273. [0x2e46800f, 0x0993778d, '690026616168284175'],
  274. [0xf00a7e32, 0xcd8e3931, '14811839111111540274'],
  275. [0x1baeccd6, 0x923048c4, '10533999535534820566'],
  276. [0x03669d29, 0xbff3ab72, '13831587386756603177'],
  277. [0x2526073e, 0x01affc81, '121593346566522686'],
  278. [0xc24244e0, 0xd7f40d0e, '15561076969511732448'],
  279. [0xc56a341e, 0xa68b66a7, '12000798502816461854'],
  280. [0x8738d64d, 0xbfe78604, '13828168534871037517'],
  281. [0x5baff03b, 0xd7572aea, '15516918227177304123'],
  282. [0x4a843d8a, 0x864e132b, '9677693725920476554'],
  283. [0x25b4e94d, 0x22b54dc6, '2500990681505655117'],
  284. [0x6bbe664b, 0x55a5cc0e, '6171563226690381387'],
  285. [0xee916c81, 0xb00aabb3, '12685140089732426881']
  286. ];
  287. for (var i = 0; i < testData.length; i++) {
  288. var a = new jspb.arith.UInt64(testData[i][0], testData[i][1]);
  289. var roundtrip = jspb.arith.UInt64.fromString(a.toString());
  290. assertEquals(roundtrip.lo, a.lo);
  291. assertEquals(roundtrip.hi, a.hi);
  292. assertEquals(a.toString(), testData[i][2]);
  293. }
  294. });
  295. /**
  296. * Tests signed Int64s. These are built on UInt64s, so we only need to test
  297. * the explicit overrides: .toString() and .fromString().
  298. */
  299. it('testSignedInt64', function() {
  300. var testStrings = [
  301. '-7847499644178593666',
  302. '3771946501229139523',
  303. '2872856549054995060',
  304. '-5780049594274350904',
  305. '3383785956695105201',
  306. '2973055184857072610',
  307. '-3879428459215627206',
  308. '4589812431064156631',
  309. '8484075557333689940',
  310. '1075325817098092407',
  311. '-4346697501012292314',
  312. '2488620459718316637',
  313. '6112655187423520672',
  314. '-3655278273928612104',
  315. '3439154019435803196',
  316. '1004112478843763757',
  317. '-6587790776614368413',
  318. '664320065099714586',
  319. '4760412909973292912',
  320. '-7911903989602274672'
  321. ];
  322. for (var i = 0; i < testStrings.length; i++) {
  323. var roundtrip =
  324. jspb.arith.Int64.fromString(testStrings[i]).toString();
  325. assertEquals(roundtrip, testStrings[i]);
  326. }
  327. });
  328. });